Consul introduction

The Consul’s official website: www.consul.io/intro/index…

Chinese Consul: www.springcloud.cc/spring-clou…

1. What is Consul

Consul is an open source distributed service discovery and configuration management system developed by HashiCorp in the Go language.

It provides service governance, configuration center, control bus and other functions in microservice system. Each of these features can be used individually or together as needed to build a full service grid, so Consul provides a complete service grid solution.

It has many advantages. Including: Based on RAFT protocol, relatively simple; Health check and HTTP and DNS support WAN cluster across data centers Cross-platform GUI support Linux, Mac, and Windows.

2, functionality,

  • Service discovery – Provides HTTP and DNS discovery modes.
  • Health monitoring – support a variety of ways, HTTP, TCP, Docker, Shell script customization
  • KV storage – Storage mode of keys and values
  • Multi-data Center – Consul supports multiple data centers
  • Visual Web Interface

3, installation,

Download address: www.consul.io/downloads.h…

Go straight 64-bit and unzip:

Double-click the consul decompression path, CMD, development mode start:

consul agent -dev
Copy the code

Then click localHost :8500 to get to the page.

Consul use

1. Service provider register Consul

Create the module Cloud-ProviderY-Payment8006

Same as before. Import poM files.


      
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2021</artifactId>
        <groupId>com.atguigu</groupId>
        <version>1.0.0 - the SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>cloud-providerconsul-payment8006</artifactId>

    <dependencies>
        <! -- Introduced a generic API package that allows you to pay Entity using Payment -->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <! --SpringCloud consul-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <! -- SpringBoot integrate Web components -->
        <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>
        <! Jar package configuration -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
Copy the code

Yaml configuration:

### Consul service port number
server:
  port: 8006

spring:
  application:
    name: consul-provider-payment
#### Consul registry address
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        # the hostname: 127.0.0.1
        service-name: ${spring.application.name}
Copy the code

Main startup class:

@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
    public static void main(String[] args) { SpringApplication.run(PaymentMain8006.class,args); }}Copy the code

controller:

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

    @RequestMapping(value = "/payment/consul")
    public String paymentConsul(a){
        return "SpringCloud with consul:" + serverPort + "\t"+ UUID.randomUUID().toString(); }}Copy the code

The registration screen has been updated:

Like ZooKeeper, the corresponding node number is generated:

Click in to see more information:

2. Service consumers register Consul

Create the module cloud-ConsumerConsul-order80

Create same, poM file same.


      
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2021</artifactId>
        <groupId>com.atguigu</groupId>
        <version>1.0.0 - the SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>cloud-consumerconsul-order80</artifactId>
    <dependencies>
        <! --SpringCloud consul-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <! -- SpringBoot integrate Web components -->
        <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>
        <! Jar package configuration -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
Copy the code

Yaml is the same:

### Consul service port number
server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  #### Consul registry address
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        # the hostname: 127.0.0.1
        service-name: ${spring.application.name}
Copy the code

Main startup class:

@SpringBootApplication
@EnableDiscoveryClient  // This annotation is used to register services when using Consul or ZooKeeper as a registry
public class OrderConsulMain80 {
    public static void main(String[] args) { SpringApplication.run(OrderConsulMain80.class,args); }}Copy the code

The configuration class:

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(a){
        return newRestTemplate(); }}Copy the code

controller:

@RestController
@Slf4j
public class OrderConsulController {

    public static final String INVOKE_URL = "http://consul-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/payment/consul")
    public String paymentInfo(a){
        String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul",String.class);
        returnresult; }}Copy the code

Test started. Registration succeeded.

Test localhost/consumer/payment/consul was successful.

Third, the similarities and differences of the registry

1. Introduction of the three registries

Component name Language CAP Service health check External exposed interface Spring Cloud integration
Eureka Java AP Can match support HTTP
Consul Go CP support HTTP/DNS
Zookeeper Java CP Client support Has integrated

2. What is CAP

  • C: Consistency
  • A: Availability
  • P: Partition tolerance

It is impossible for a distributed system to meet the requirements of consistency, availability, and fault tolerance of partitions.

Eureka (AP) and Zookeeper, Consul (CP).

CP Architecture (ZooKeeper/Consul)

When the network partition appears, in order to ensure consistency, the request must be rejected, otherwise the consistency cannot be guaranteed.

Conclusion: It violates the requirements of availability A and only satisfies consistency and partition fault tolerance, i.e. CP.

AP Architecture (Eureka)

When a network partition appears, system B can return the old value to ensure availability.

Conclusion: It violates the requirement of consistency C and only satisfies availability and partition fault tolerance, i.e. AP