Microservice architecture | 3.4 hashicorp consol registry

Posted by lynxus on Wed, 19 Jan 2022 02:07:47 +0100

preface

reference material:
<Spring Microservices in Action>
Principles and practice of Spring Cloud Alibaba microservice
"Spring cloud framework development tutorial in Silicon Valley of station B" Zhou Yang
Consul official website

Consul is an open source distributed service discovery and configuration management system developed by HashiCorp in Go language. It provides the functions of service governance, configuration center, control bus and so on. Each of these functions can be used alone or together to build a comprehensive service grid. In short, consul provides a complete service grid solution;

1. Consul Basics

1.1 what is consult

  • Consul is an open source distributed service discovery and configuration management system developed by HashiCorp in Go language;
  • It provides the functions of service governance, configuration center, control bus and so on. Each of these functions can be used alone or together to build a comprehensive service grid. In short, Consul provides a complete service grid solution;
  • It has many advantages. Including: Based on raft protocol, relatively simple; Support health check, HTTP and DNS protocols, WAN clusters across data centers, and provide graphical interfaces; Cross platform, support Linux, Mac and Windows

1.2 characteristics of consult

  • Service discovery: provide HTTP and DNS discovery methods;
  • Health monitoring: support multiple methods: HTTP, TCP, Docker, Shell script customized monitoring;
  • KV storage: storage method of Key and Value;
  • Multiple data centers: support multiple data centers;
  • Visual Web interface;

2. Install and run the consult server

Consul server installation based on Win10;

2.1 download consult

2.2 running Consul server

  • In consumer Open the cmd command window in the directory where the EXE program is located;
  • Type the command consumer to display the following description: the installation is successful;

  • At this time, type the command consumer agent - Dev to start using the development mode;


3. Use Consul to manage service providers

Using Consul to build service providers is roughly the same as Nacos and Zookeeper; See 3.2 Alibaba Nacos registry and 3.3 Apache Zookeeper registry for details of how Nacos and Zookeeper are built;

3.1 introduction of POM XML dependency

<!--SpringCloud consul-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

3.2 modify bootstrap YML profile

# Consumer service provider port
server:
  port: 8006
spring:
  application:
    name: consul-provider
# Consumer registry address
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        #hostname: 127.0.0.1
        service-name: ${spring.application.name}

3.3 add comments on the main startup class

  • @EnableDiscoveryClient: use other components (Nacos, zookeeper, Consul) as the registry;

3.4 write the business class and open the interface at the controller layer

A simple interface is written here as an example only;

@RestController
public class providerController{
    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/provider/consul")
    public String providerConsul(){
        return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
    }
}

4. Use Consul to manage service consumers

Using Consul to build service consumers is roughly the same as Nacos and Zookeeper; See 3.2 Alibaba Nacos registry and 3.3 Apache Zookeeper registry for details of how Nacos and Zookeeper are built;

4.1 introduction of POM XML dependency

  • The same provider;
<!--SpringCloud consul-server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

4.2 modify bootstrap YML profile

  • Basically the same as the provider;
# Consumer service consumer port number
server:
  port: 80
spring:
  application:
    name: consul-consumer
# Consumer registry address
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        #hostname: 127.0.0.1
        service-name: ${spring.application.name}

4.3 add comments on the main startup class

  • The same provider;
  • @EnableDiscoveryClient: use other components (Nacos, zookeeper, Consul) as the registry;

4.4 write business class

  • Because we use the Ribbon + RestTemplate load balancing strategy, we need to add a RestTemplate JavaBean in the IoC container;
  • For details, see 4.1 detailed explanation of Ribbon based load balancing;
  • The Bean can be added in the main startup class; It can also be added in the config package of the package or sub package where the main startup class is located, as follows:
@Configuration
public class ApplicationContextBean{
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}
  • We open the interface to the client in the controller layer, and call the provider's API in the interface;
@RestController
public class ComsumerConsulController{
    public static final String INVOKE_URL = "http://consul-provider";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/consumer/consul")
    public String paymentInfo(){
        String result = restTemplate.getForObject(INVOKE_URL+"/provider/consul", String.class);
        System.out.println("Consumer invokes provider to get service--->result:" + result);
        return result;
    }
}

last

Newcomer production, if there are mistakes, welcome to point out, thank you very much! Welcome to the official account and share some more everyday things. If you need to reprint, please mark the source!