一,為什么要搭建consul的client?
1,網上的很多資料,訪問consul時用的單機模式,這樣是不可以直接在生產環境中使用的
還有一些資料,搭建了consul的集群后,直接訪問集群中的某一個ip,
這樣不能達到高可用的目的,因為如果當前訪問的ip宕機,則到整個consul集群的訪問會失效.
2,如何訪問consul集群?
第一個方法:一個是集成java代碼,直接在配置文件中的host寫上集群的多個ip,
當訪問的地址有異常則訪問其他的ip,
大家可以參考這個項目:
https://segmentfault.com/a/1190000020155983
第二個方法:較常用的方法:
在項目所在機器上以client模式安裝運行一個consul的agent,
java代碼通過client訪問consul的server集群
因為服務提供者會多個實例,所以如果某個實例上的consul client發生故障,
只會有一個服務提供者失敗,而不會影響到其他的實例繼續提供服務
3,安裝consul集群:參見:
https://www.cnblogs.com/architectforest/p/13735545.html
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,在linux centos8上搭建consul的client
[root@localhost consul]# mkdir /usr/local/soft/consul
[root@localhost consul]# mv ./consul /usr/local/soft/consul/
[root@localhost consul]# nohup /usr/local/soft/consul/consul agent -data-dir=/data/consul/data -node=client-1 -bind=192.168.1.7 -datacenter=dc1 -ui -client=0.0.0.0 -join=172.17.0.2 > /dev/null 2>&1 &
[root@localhost consul]# /usr/local/soft/consul/consul members Node Address Status Type Build Protocol DC Segment server-2 172.17.0.2:8301 alive server 1.8.4 2 dc1 <all> server-3 172.17.0.3:8301 alive server 1.8.4 2 dc1 <all> server-4 172.17.0.4:8301 alive server 1.8.4 2 dc1 <all> client-1 192.168.1.7:8301 alive client 1.8.4 2 dc1 <default>
可以看到client-1是以client類型加入到集群中的
三,演示項目的相關信息
1,項目所在地址
https://github.com/liuhongdi/cloudconsul
2,功能說明:
演示了獲取consul集群中的service和實例信息
3,項目結構:如圖:
四,配置文件說明
1,pom.xml
<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>
2,application.properties
spring.application.name=provider1 server.port=8080 #consul spring.cloud.consul.host=127.0.0.1 spring.cloud.consul.port=8500 #service name spring.cloud.consul.discovery.service-name=lhdprovider provider.name = p1
五, java代碼說明
1,DemoApplication.java
@EnableDiscoveryClient @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
2,HomeController.java
@RestController @RequestMapping("/home") public class HomeController { @Resource private DiscoveryClient discoveryClient; @Value("${provider.name}") private String name; @Value("${server.port}") private String port; // list services @GetMapping("/serviceslist") public Object serviceslist() { return discoveryClient.getServices(); } // list instances in a service id @GetMapping("/instanceslist") public Object instanceslist() { return discoveryClient.getInstances("consul"); } //test api @GetMapping("/hello") public String hello() { //return discoveryClient.getInstances("consul"); String res = "name:"+name+";port:"+port; return res; } }
六,測試效果
1,得到服務列表:訪問:
http://127.0.0.1:8080/home/serviceslist
返回
2,得到consul server集群的實例列表
http://127.0.0.1:8080/home/instanceslist
返回:
3,從web ui查看服務列表
查看lhdprovider中的實例:
七,查看spring boot的版本
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.4.RELEASE)