consul的具體安裝與操作查看博客的consul系列。 一、啟動consul (1個server+1個client,方便起見,client使用本機):查看:http://www.cnblogs.com/java-zhao/p/5375132.html 1、開啟虛擬機-->切換到vagrantFil ...
consul的具體安裝與操作查看博客的consul系列。
一、啟動consul
(1個server+1個client,方便起見,client使用本機):查看:http://www.cnblogs.com/java-zhao/p/5375132.html
1、開啟虛擬機-->切換到vagrantFile中配置的節點
- vagrant up
- vagrant ssh n110
2、啟動server(n110)
-
consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=server-110 -bind=192.168.21.110 -dc=zjgdc1 -client 0.0.0.0 -ui
說明:-client 0 0 0 0 -ui-->使得客戶端可以直接通過url訪問服務端的consul ui
3、啟動client(local)
- consul agent -data-dir=/tmp/consul -node=client-my -bind=xxx -dc=zjgdc1
說明:xxx代表本機IP
4、client加入server
- consul join 192.168.21.110
二、java部分
1、pom.xml
<!-- consul-client --> <dependency> <groupId>com.orbitz.consul</groupId> <artifactId>consul-client</artifactId> <version>0.10.0</version> </dependency> <!-- consul需要的包 --> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.22.2</version> </dependency>
說明:consul的java客戶端有兩個:consul-client和consul-api。
consul-client的github地址:https://github.com/OrbitzWorldwide/consul-client
2、ConsulService
1 package com.xxx.firstboot.service; 2 3 import java.net.MalformedURLException; 4 import java.net.URI; 5 import java.util.List; 6 7 import org.springframework.stereotype.Service; 8 9 import com.orbitz.consul.AgentClient; 10 import com.orbitz.consul.Consul; 11 import com.orbitz.consul.HealthClient; 12 import com.orbitz.consul.KeyValueClient; 13 //import com.orbitz.consul.NotRegisteredException; 14 import com.orbitz.consul.StatusClient; 15 import com.orbitz.consul.model.health.ServiceHealth; 16 17 @Service 18 public class ConsulService { 19 20 /** 21 * 註冊服務 22 * 並對服務進行健康檢查 23 * servicename唯一 24 * serviceId:沒發現有什麼作用 25 */ 26 public void registerService(String serviceName, String serviceId) { 27 Consul consul = Consul.builder().build(); //建立consul實例 28 AgentClient agentClient = consul.agentClient(); //建立AgentClient 29 30 try { 31 /** 32 * 註意該註冊介面: 33 * 需要提供一個健康檢查的服務URL,以及每隔多長時間訪問一下該服務(這裡是3s) 34 */ 35 agentClient.register(8080, URI.create("http://localhost:8080/health").toURL(), 3, serviceName, serviceId, "dev"); 36 } catch (MalformedURLException e) { 37 e.printStackTrace(); 38 } 39 // try { 40 // agentClient.pass(serviceId);//健康檢查 41 // } catch (NotRegisteredException e) { 42 // e.printStackTrace(); 43 // } 44 } 45 46 /** 47 * 發現可用的服務 48 */ 49 public List<ServiceHealth> findHealthyService(String servicename){ 50 Consul consul = Consul.builder().build(); 51 HealthClient healthClient = consul.healthClient();//獲取所有健康的服務 52 return healthClient.getHealthyServiceInstances(servicename).getResponse();//尋找passing狀態的節點 53 } 54 55 /** 56 * 存儲KV 57 */ 58 public void storeKV(String key, String value){ 59 Consul consul = Consul.builder().build(); 60 KeyValueClient kvClient = consul.keyValueClient(); 61 kvClient.putValue(key, value);//存儲KV 62 } 63 64 /** 65 * 根據key獲取value 66 */ 67 public String getKV(String key){ 68 Consul consul = Consul.builder().build(); 69 KeyValueClient kvClient = consul.keyValueClient(); 70 return kvClient.getValueAsString(key).get(); 71 } 72 73 /** 74 * 找出一致性的節點(應該是同一個DC中的所有server節點) 75 */ 76 public List<String> findRaftPeers(){ 77 StatusClient statusClient = Consul.builder().build().statusClient(); 78 return statusClient.getPeers(); 79 } 80 81 /** 82 * 獲取leader 83 */ 84 public String findRaftLeader(){ 85 StatusClient statusClient = Consul.builder().build().statusClient(); 86 return statusClient.getLeader(); 87 } 88 89 }
列出了常用API。
註意:
- 服務註冊的時候不需要傳遞IP
- 服務註冊的時候需要給出health check的url和時間間隔。該url是一個服務(要提供該服務,需要使用spring boot actuator,具體操作如下:)。
直接在pomx.ml中加入:
1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-actuator</artifactId> 4 </dependency>
此時重啟應用後,訪問http://localhost:8080/health,得到如下結果一個json串:
1 { 2 status: "UP", 3 diskSpace: - { 4 status: "UP", 5 total: 249769230336, 6 free: 182003318784, 7 threshold: 10485760 8 }, 9 rabbit: - { 10 status: "UP", 11 version: "3.6.1" 12 }, 13 mongo: - { 14 status: "UP", 15 version: "3.2.6" 16 }, 17 db: - { 18 status: "UP", 19 myTestDbDataSource: - { 20 status: "UP", 21 database: "MySQL", 22 hello: 1 23 }, 24 myTestDb2DataSource: - { 25 status: "UP", 26 database: "MySQL", 27 hello: 1 28 }, 29 dataSource: - { 30 status: "UP", 31 database: "MySQL", 32 hello: 1 33 } 34 }, 35 _links: - { 36 self: - { 37 href: "http://localhost:8080/health" 38 } 39 } 40 } 41 Format online
說明:status
- UP:伺服器正常(以上只要有一個組件DOWN,伺服器就處於DOWN,所以我需要啟動伺服器上的mongo和rabbitmq,這裡我之前使用了這兩個組件)
- DOWN:伺服器掛了
3、ConsulController
1 package com.xxx.firstboot.web; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.PathVariable; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import com.orbitz.consul.model.health.ServiceHealth; 12 import com.xxx.firstboot.service.ConsulService; 13 14 import io.swagger.annotations.Api; 15 import io.swagger.annotations.ApiOperation; 16 17 @Api("consul相關API") 18 @RestController 19 @RequestMapping("/consul") 20 public class ConsulController { 21 @Autowired 22 private ConsulService consulService; 23 24 /*******************************服務註冊與發現*******************************/ 25 @ApiOperation("註冊服務") 26 @RequestMapping(value="/registerService/{servicename}/{serviceid}",method=RequestMethod.POST) 27 public void registerService(@PathVariable("servicename") String serviceName, 28 @PathVariable("serviceid") String serviceId) { 29 consulService.registerService(serviceName, serviceId); 30 } 31 32 @ApiOperation("發現服務") 33 @RequestMapping(value="/discoverService/{servicename}",method=RequestMethod.GET) 34 public List<ServiceHealth> discoverService(@PathVariable("servicename") String serviceName) { 35 return consulService.findHealthyService(serviceName); 36 } 37 38 /*******************************KV*******************************/ 39 @ApiOperation("store KV") 40 @RequestMapping(value="/kv/{key}/{value}",method=RequestMethod.POST) 41 public void storeKV(@PathVariable("key") String key, 42 @PathVariable("value") String value) { 43 consulService.storeKV(key, value); 44 } 45 46 @ApiOperation("get KV") 47 @RequestMapping(value="/kv/{key}",method=RequestMethod.GET) 48 public String getKV(@PathVariable("key") String key) { 49 return consulService.getKV(key); 50 } 51 52 /*******************************server*******************************/ 53 @ApiOperation("獲取同一個DC中的所有server節點") 54 @RequestMapping(value="/raftpeers",method=RequestMethod.GET) 55 public List<String> findRaftPeers() { 56 return consulService.findRaftPeers(); 57 } 58 59 @ApiOperation("獲取leader") 60 @RequestMapping(value="/leader",method=RequestMethod.GET) 61 public String leader() { 62 return consulService.findRaftLeader(); 63 } 64 }
4、測試(通過swagger測試+通過consul UI查看結果)
- swagger:http://localhost:8080/swagger-ui.html
- consul UI:http://192.168.21.110:8500/ui/
上圖展示了consul UI所展示的所有東西。services、nodes、kv、datacenter