In Spring Cloud(04) — Introduction and Deployment of Eureka and Spring Cloud(05) — Spring Cloud integration with Zookeeper instead of Eureka, We used Eureka and ZooKeeper for service registration and discovery respectively, and now we use Consul for service registration and discovery.

1, Consul introduction

Consul is a service software that enables distributed, highly available service discovery and configuration sharing in multiple data centers, developed by HashiCorp In the Go language.

Consul provides service governance, configuration center, control bus and other functions in a 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.

The Consul’s official website

Consul Chinese document

Benefits of Consul:

  • Based on raft protocol, relatively simple
  • Supports health check and HTTP and DNS protocols
  • Support for WAN clustering across data centers
  • Provide graphical interface
  • Cross-platform, support Linux, Mac, Windows

2. Install Consul

1. Download Consul and unzip it

releases.hashicorp.com/consul/

2. Check whether the installation is successful

If the version number is displayed, the installation is successful.

3. Run consul agent-dev to start consul

4. After Consul is enabled, access request localhost:8500 to access the Consul control interface

3. Service providers register with Consul

1. Create the cloud-Provideconsul-Payment8006 module

2. Import PIM dependencies

<dependencies>
    <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>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <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>
    </dependency>
</dependencies>
Copy the code

3. Write the YML configuration file

server:
  port: 8006

spring:
  application:
    name: consul-provide-payment

  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
Copy the code

4. Write the main startup class

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

5. Write controllers

@RestController
@Slf4j
public class PaymentConsulController {

    @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

6, test,

  1. Run consul consul agent-dev

  2. Start the cloud-Provideconsul-Payment8006 module

  3. Access request: lcoalhost:8500

Registration successful!

  1. Access to the local service: localhost: 8006 / payment/consul

Access successful!

4. Service consumers register in Consul

Create the cloud-ConsumerConsul-Order80 module

2. Import POM dependencies

<dependencies>
    <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>

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

    <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>
    </dependency>
</dependencies>
Copy the code

3. Write the YML configuration file

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order

  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
Copy the code

4. Create the main startup class

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

5. Write the restTemplate configuration

@Configuration
public class ConfigBean {

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

6. Write controllers

@RestController
@Slf4j
public class OrderConsulController {

    // Extract the remote address prefix
    public static final String REST1_URL_PREFIX = "http://consul-provide-payment";

    @Resource
    private RestTemplate restTemplate;

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

7, test,

  1. Run consul: consul agent-dev

  2. Start the cloud-Provideconsul-Payment8006 module

  3. Start the cloud-ConsumerConsul-Order80 module

  4. Access request: lcoalhost:8500

Registration successful!

  1. Access to remote services: http://localhost//consumer/payment/consul

Access successful!Copy the code

5. Similarities and differences between Eureka, Zookeeper and Consul registries

Similarities and differences:

There is a famous CAP theorem in distributed systems:

  • C) Consistency D) Consistency
  • A: Availability.
  • P:Partition tolerance. Similar to the deployment of multiple equipment rooms, ensuring service stability.

These three characteristics cannot be met in any distributed system at the same time, at most two.