spring cloud官方耦合度最好的服务注册于服务发现是eureka。但是在实际应用中,consul服务既能结合spring cloud又能结合其他服务进行服务注册与服务发现,同时功能点强于eureka,对于consul与eureka的优缺点,在此不做介绍。下文使用consul结合spring cloud进行服务注册与服务发现。
consul安装包:consul_0.8.3_windows_amd64.zip
系统环境:win7
注:本文其他开发环境见上篇,在此略过
consul既可以单节点使用,也可以进行集群部署,听过算法raft保持集群各节点一致性。在这里只是用consul单节点进行spring cloud的服务注册与服务发现应用。
该工程作为server进行consul的注册server端。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.fiberhomegroupId>
<artifactId>second-fiberhomeartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>second-consul-server-1artifactId>
<packaging>jarpackaging>
<name>second-consul-server-1name>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-consul-dependenciesartifactId>
<version>1.0.1.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
注:对于spring boot服务做健康检查,需要依赖actuator,该依赖在父类工程second-fiberhome中进行了配置,见spring-boot-starter-actuator。
server.port=9900
spring.application.name=consul-server
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.instance-id=consul-server-1
spring.cloud.consul.discovery.hostname=localhost
spring.cloud.consul.discovery.port=${server.port}
spring.cloud.consul.discovery.serviceName=consul-server
spring.cloud.consul.discovery.healthCheckUrl=http://127.0.0.1:${server.port}/health
spring.cloud.consul.discovery.healthCheckInterval=10s
spring.cloud.consul.discovery.tags=dev
注:spring.cloud.consul.discovery.serviceName=consul-server下,可以通过consul服务发现调用consul-server进行访问second-consul-server1(2)服务,需要注意的是类似consul-server使用下划线无效,会转化为“-”,从consul界面可以发现该情况。
package com.fiberhome;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulServerApplication.class, args);
}
}
注:需要在启动类上添加注解@EnableDiscoveryClient
通过second-consul-client结合consul,进行服务发现访问。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.fiberhomegroupId>
<artifactId>second-fiberhomeartifactId>
<version>0.0.1-SNAPSHOTversion>
parent>
<artifactId>second-consul-clientartifactId>
<packaging>jarpackaging>
<name>second-consul-clientname>
<url>http://maven.apache.orgurl>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-consul-discoveryartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-consul-dependenciesartifactId>
<version>1.0.1.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
project>
注:主要使用spring-cloud-starter-consul-discovery依赖。
server.port=9990
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.instance-id=consul-client
spring.cloud.consul.discovery.hostname=localhost
spring.cloud.consul.discovery.serviceName=consul-client
spring.cloud.consul.discovery.healthCheckUrl=http://127.0.0.1:${server.port}/health
spring.cloud.consul.discovery.healthCheckInterval=10s
spring.cloud.consul.discovery.tags=dev
spring.cloud.consul.discovery.enabled=true
spring.cloud.consul.discovery.port=${server.port}
package com.fiberhome.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ClientController {
@Autowired
public DiscoveryClient discoveryClient;
@Autowired
public LoadBalancerClient loadBalancer;
@RequestMapping(value="/services",method=RequestMethod.GET)
public Object services(){
return discoveryClient.getInstances("consul-server");
}
@RequestMapping("/discovery")
public Object discovery(){
return loadBalancer.choose("consul-server").getUri().toString();
}
}
注:spring-cloud-starter-consul-discovery依赖包含了负载均衡和服务发现的类,用以访问consul服务。
package com.fiberhome;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulClientApplication.class, args);
}
}
second-consul-server-1
second-consul-server-2
second-consul-client
然后使用postman访问client服务,localhost:9990/services
访问localhost:9990/discovery
注意,由于采用的是两个server服务,所以每次请求discovery获取的结果是不同的,默认采用的是轮询方式访问两者服务,具有负载均衡的特点。
该工程源码见github:https://github.com/moyu2012/second-fiberhome