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); } }