Use consul to achieve service registration and discovery

Reprint address: http://blog.csdn.net/mn960mn/article/details/51768678


Service Registration - The service process registers its position in the registry. It usually registers its own host and port number, and sometimes authentication information, protocol, version number, and details of the operating environment.

Service Discovery - The client application process queries the registry to obtain the location of the service. An important role of service discovery is to provide a list of available

services. The format of the service definition is similar to the following:

[plain] view plain copy
 View code slice on CODE derived from my code slice

    { 
      "service":{ 
        "id": "jetty ", 
        "name": "jetty", 
        "address": "192.168.1.200", 
        "port": 8080, 
        "tags": ["dev"], 
        "checks": [ 
            { 
                "http": "http:/ /192.168.1.200:8080/health", 
                "interval": 






Among them, the check is used for the health check of the service. There can be multiple or none. The check that supports multiple methods

must be of script or TTL type. If it is of script type, script and interval variables must be provided. , if it is TTL type, the ttl variable must be provided.

script is consul to actively check the health status of the service, ttl is the service actively reporting its health status to consul The

following are several configuration methods

script check

[plain] view plain copy
 in CODE Derive to my snippet

    { 
      "check": { 
        "id": "mem-util", 
        "name": "Memory utilization", 
        "script": "/usr/local/bin/check_mem.py ", 
        "interval": "10s", 
        "timeout": "1s" 
      } 
    } 


HTTP check:

[plain] view plain copy
 on CODE to view the code slice derived to my code slice

    { 
      "check": { 
        "id": "api", 
        "name": "HTTP API on port 5000", 
        "http": "http://localhost:5000/health", 
        "interval": "10s", 
        "timeout": "1s" 
      } 
    } 


TCP check:

[plain] view plain copy
 在CODE上查看代码片派生到我的代码片

    { 
      "check": { 
        "id": "ssh", 
        "name": "SSH TCP on port 22", 
        "tcp": "localhost:22", 
        "interval": "10s", 
        "timeout": "1s" 
      } 
    } 


TTL check:        "id": "web-app",        "check": {      {   View code snippet on CODE derived to my code snippet

[plain] view plain copy





        "name": "Web App Status", 
        "notes": "Web app does a curl internally every 10 seconds", 
        "ttl": "30s" 
      } 
    } 


There are three ways to register a service,
1: Static through the configuration file Register
to create a folder /etc/consul.d
.d means there are many configuration files in
vim /etc/consul.d/jetty.json The content is as follows:

[plain] view plain copy
 View code snippets on CODE Derived to my code slice

    { 
      "service":{ 
        "id": "jetty", 
        "name": "jetty", 
        "address": "192.168.1.200", 
        "port": 8080, 
        "tags": ["dev"], 
        " checks":  [ 
            { 
                "http": "http://192.168.1.200:8080/health", 
                "interval": "5s" 
            } 
        ] 
      } 
    } 


Restart consul and give the path of the configuration file to consul (specified parameter: -config-dir /etc/consul.d) 2: Dynamically register and directly call /v1

through the HTTP API interface
The /agent/service/register interface can be registered. It should be noted that the http method is the PUT submission method,

such as:

curl -X PUT -d '{"id": "jetty","name": "jetty","address ": "192.168.1.200","port": 8080,"tags": ["dev"],"checks": [{"http": "http://192.168.1.104:9020/health","interval ": "5s"}]}' http://192.168.1.100:8500/v1/agent/service/register


Note that this method is a little different from the above registration method. The parameter of the body is the value of the above service. Note


3: Use the program to realize the registration and discovery of the service ( Java)

first add the dependency of the consul client

[html] view plain copy
 View the code slice on CODE Derive to my code slice

    <dependency> 
        <groupId>com.orbitz.consul</groupId> 
        <artifactId>consul-client</artifactId> 
        <version>0.12.3</version> 
    </dependency> 


[java] view plain copy
 在CODE上查看代码片派生到我的代码片

    package com.pp.cnosul; 
     
    import com.google.common.net.HostAndPort; 
    import com.orbitz.consul.AgentClient; 
    import com.orbitz.consul.Consul; 
    import com.orbitz.consul.HealthClient; 
    import com.orbitz.consul.model.agent.ImmutableRegCheck; 
    import com.orbitz.consul.model.agent.ImmutableRegistration; 
     
    public class ConsulDemo { 
     
        static Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromString("192.168.1.246:8500")).build(); 
     
        /**
         * 服务注册
         */ 
        public static void serviceRegister() { 
            AgentClient agent = consul.agentClient(); 
             
            //健康检测 
            ImmutableRegCheck check = ImmutableRegCheck.builder().http("http://192.168.1.104:9020/health").interval("5s").build(); 
             
            ImmutableRegistration.Builder builder = ImmutableRegistration.builder(); 
            builder.id("tomcat1").name("tomcat").addTags("v1").address("192.168.1.104").port(8080).addChecks(check); 
             
            agent.register(builder.build()); 
        } 
         
        /**
         * Service Get
         */ 
        public static void serviceGet() { 
            HealthClient client = consul.healthClient(); 
            String name = "tomcat"; 
            //Get all services 
            System.out.println(client.getAllServiceInstances(name).getResponse(). size()); 
             
            //Get all normal services (passed health check) 
            client.getHealthyServiceInstances(name).getResponse().forEach((resp) -> { 
                System.out.println(resp); 
            }); 
        } 
         
        public static void main(String[] args) { 
            serviceRegister(); 
            serviceGet(); 
        } 
    } 


Of ​​course, you can also use the following consul api

[html] view plain copy
 on CODE to view the snippet derived to my snippet

    <dependency> 
        <groupId>com.ecwid.consul</groupId> 
        <artifactId>consul-api</artifactId> 
        <version>1.1.10< /version> 
    </dependency> 


[java] view plain copy
 View snippet on CODE Derive to my snippet

    import com.ecwid.consul.v1.ConsulClient; 
    import com.ecwid.consul.v1.ConsulRawClient; 
    import com. ecwid.consul.v1.agent.model.Service; 
     
    public class App { 
         
        public static void main(String[] args) { 
            ConsulRawClient client = new ConsulRawClient("192.168.1.100", 8500); 
            ConsulClient consul = new ConsulClient(client) ; 
            // get all services 
            Map<String, Service> map = consul.getAgentServices().getValue(); 
        } 
    } 


Among them, spring cloud uses the second consul api

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991573&siteId=291194637