注:尊重原創,本文參考這位大神【純潔的微笑】的,由於他寫得太好了html
Consul是一個分佈式服務註冊與發現,用於跨任何運行時平臺和公共或私有云鏈接、保護和配置服務。它內置了服務註冊與發現框 架、分佈一致性協議實現、健康檢查、Key/Value 存儲、多數據中心方案。
它是GO語言寫。
官方文檔:https://www.consul.io/docs/gu...java
它使用 Raft 算法(https://www.jdon.com/artichec...)來保證一致性, 比複雜的 Paxos 算法更直接. 相比較而言, zookeeper 採用的是 Paxos.web
client: 客戶端, 無狀態, 將 HTTP 和 DNS 接口請求轉發給局域網內的服務端集羣。
server: 服務端, 保存配置信息, 高可用集羣, 在局域網內與本地客戶端通信, 經過廣域網與其它數據中心通信。 每一個數據中心的 server 數量推薦爲 3 個或是 5 個(通常是奇數,不要問我爲啥)。
Consul 客戶端、服務端還支持誇中心的使用,更加提升了它的高可用性。算法
Consul 不一樣於 Eureka 須要單獨安裝,訪問Consul 官網下載 Consul 的最新版本,我這裏是 consul_1.4.4。spring
1.pom.xml,添加相關依賴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> <parent> <groupId>cn.xbmchina</groupId> <artifactId>cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.xbmchina</groupId> <artifactId>cloud-consul-producer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-consul-producer</name> <description>consul-producer</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
啓動類中須要設置@EnableDiscoveryClientapi
@SpringBootApplication @EnableDiscoveryClient public class CloudConsulProducerApplication { public static void main(String[] args) { SpringApplication.run(CloudConsulProducerApplication.class, args); } @RequestMapping("/hello") public String hello() { return "helle consul"; } }
application.yml配置相關端口和服務名稱app
spring: application: name: spring-cloud-consul-producer2 cloud: consul: host: localhost port: 8500 discovery: ##註冊到consul的服務名稱 service-name: service-producer server: port: 8505
同理也須要添加相應的pom.xml負載均衡
<?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> <parent> <groupId>cn.xbmchina</groupId> <artifactId>cloud-parent</artifactId> <version>1.0-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.xbmchina</groupId> <artifactId>cloud-consul-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>cloud-consul-consumer</name> <description>consul消費者</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
調用生產者Producer中的服務maven
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class CallHelloController { @Autowired private LoadBalancerClient loadBalancer;//用於負載均衡 @RequestMapping("/call") public String call() { ServiceInstance serviceInstance = loadBalancer.choose("service-producer"); System.out.println("服務地址:" + serviceInstance.getUri()); System.out.println("服務名稱:" + serviceInstance.getServiceId()); String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class); System.out.println(callServiceResult); return callServiceResult; } }
application.yml配置鏈接註冊到consul
spring: application: name: cloud-consul-consumer cloud: consul: host: 127.0.0.1 port: 8500 discovery: ##設置不須要註冊到 consul 中 register: false server: port: 8503
效果:
訪問地址http://localhost:8503/call,若是報錯的話,你能夠看一下日誌,是不是你本機名沒在hosts中配置,致使的。
Consul 真厲害!!
若是對 Java、大數據感興趣請長按二維碼關注一波,我會努力帶給大家價值。以爲對你哪怕有一丁點幫助的請幫忙點個贊或者轉發哦。