Spring Cloud Consul项目是针对Consul的服务治理实现。Consul是一个分布式高可用的系统,它包含多个组件,可是做为一个总体,在微服务架构中为咱们的基础设施提供服务发现和服务配置的工具。它包含了下面几个特性:java
因为Spring Cloud Consul项目的实现,咱们能够轻松的将基于Spring Boot的微服务应用注册到Consul上,并经过此实现微服务架构中的服务治理。web
之因此在本项目中选择Consul而不是Eureka,是考虑到Eureka 2.0 开源工做宣告中止 spring
前篇文章已经介绍了consul服务端的部署安装,接下来介绍基于Spring Cloud Consul客户端的使用apache
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.orrin</groupId> <artifactId>modules-woqu</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>businessa-woqu</module> <module>businessb-woqu</module> </modules> <packaging>pom</packaging> <parent> <groupId>com.orrin</groupId> <artifactId>parent-woqu</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> </project>
包含业务系统客户端client-businessa-woqu和服务端server-businessa-woqujson
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>modules-woqu</artifactId> <groupId>com.orrin</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>businessa-woqu</artifactId> <packaging>pom</packaging> <modules> <module>client-businessa-woqu</module> <module>server-businessa-woqu</module> </modules> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> </project>
下面咱们建立提供服务的客户端,并向服务注册中心注册本身。 假设咱们有一个提供计算功能的微服务模块,咱们实现一个RESTful API,经过传入两个参数x和y,最后返回x + y的结果。架构
建立client-businessa-woquapp
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>businessa-woqu</artifactId> <groupId>com.orrin</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <packaging>jar</packaging> <artifactId>client-businessa-woqu</artifactId> <dependencies> <dependency> <groupId>com.orrin</groupId> <artifactId>model-woqu</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.orrin</groupId> <artifactId>core-woqu</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
在业务系统A服务端,须要集成consul的服务发现,同时加入spring-boot-starter-web的功能dom
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>businessa-woqu</artifactId> <groupId>com.orrin</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>server-businessa-woqu</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.orrin</groupId> <artifactId>client-businessa-woqu</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
编写启动类maven
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan; /** * @author orrin on 2018/7/4 */ @SpringBootApplication @EnableDiscoveryClient @ComponentScan(value = "com.woqu") public class BusinessAApp { public static void main(String[] args) { SpringApplication.run(BusinessAApp.class, args); } }
配置文件application.yml分布式
spring: application: name: business-a-woqu cloud: consul: host: woqu.consul port: 8500 discovery: instance-id: ${spring.application.name} instance-group: ${spring.application.name} register: true service-name: ${spring.application.name} server: port: 9001 logging: level: root: info com.woqu: debug
@RestController @RefreshScope public class AdditionController { @GetMapping("/add") public Integer add(@RequestParam("x") int x, @RequestParam("y") int y) { return x + y; } }
启动BusinessAApp.java类,能够在consul页面看到注册结果。
调用rest接口
GET http://127.0.0.1:9001/add?x=1&y=2 HTTP/1.1 200 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Wed, 14 Nov 2018 07:50:04 GMT 3 Response code: 200; Time: 138ms; Content length: 1 bytes
该问题可能在开发阶段不必定会发现,可是在线上部署多实例的时候,将会发现Consul中只有一个实例。
形成该问题的主要缘由是Spring Cloud Consul在注册的时候实例名(InstanceId)采用了:“服务名-端口号”(即: {spring.application.name}-{server.port} )的值,能够看到这个实例名若是不改变端口号的状况下,实例名都是相同的。若是熟悉Spring Cloud Consul的读者,可能会问老版本也是这个规则,怎么没有这个问题呢?。主要是因为Consul对实例惟一性的判断标准也有改变,在老版本的Consul中,对于实例名相同,可是服务地址不一样,依然会认为是不一样的实例。在Consul 1.2.x中,服务实例名成为了集群中的惟一标识,因此,也就致使了上述问题。
spring.cloud.consul.discovery.instance-id=${spring.application.name}-${random.int[10000,99999]}
因为经过配置属性的方式对于定义实例名的能力有限,因此咱们但愿能够用更灵活的方式来定义。这时候咱们就能够经过重写 ConsulServiceRegistry 的 register 方法来修改。好比下面的实现:
public class CustomConsulServiceRegistry extends ConsulServiceRegistry { private static final Logger LOGGER = LoggerFactory.getLogger(CustomConsulServiceRegistry.class); public CustomConsulServiceRegistry(ConsulClient client, ConsulDiscoveryProperties properties, TtlScheduler ttlScheduler, HeartbeatProperties heartbeatProperties) { super(client, properties, ttlScheduler, heartbeatProperties); } @Override public void register(ConsulRegistration reg) { LOGGER.info("new service id = {}", reg.getService().getName() + "-" + reg.getService().getAddress() + "-" + reg.getService().getPort()); reg.getService().setId(reg.getService().getName() + "-" + reg.getService().getAddress() + "-" + reg.getService().getPort()); super.register(reg); } } @Configuration @ConditionalOnConsulEnabled @ConditionalOnProperty(value = "spring.cloud.service-registry.enabled", matchIfMissing = true) @AutoConfigureBefore(ServiceRegistryAutoConfiguration.class) public class CustomConsulServiceRegistryConfiguration { private static final Logger LOGGER = LoggerFactory.getLogger(CustomConsulServiceRegistryConfiguration.class); @Autowired(required = false) private TtlScheduler ttlScheduler; @Bean public ConsulServiceRegistry consulServiceRegistry(ConsulClient consulClient, ConsulDiscoveryProperties properties, HeartbeatProperties heartbeatProperties) { return new CustomConsulServiceRegistry(consulClient, properties, ttlScheduler, heartbeatProperties); } }