赵计刚
每天进步一点点

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

posted on 2016-05-25 17:17  赵计刚  阅读(19731)  评论(1编辑  收藏  举报