The latest version of SpringCloud (H version & alibaba) framework development tutorial of Silicon Valley 2020 learn the use of Consul of three spring clouds

The course of Teacher Zhou Yang in Silicon Valley is still quite good.This is the video address, https://www.bilibili.com/video/BV18E411x7eT, you can follow along.This is a personal study note for later viewing.

Introduction

SpringCloud document address:
https://cloud.spring.io/spring-cloud-static/spring-cloud-consul/2.2.2.RELEASE/reference/html/
Consul is a distributed high availability system, it has the following Features:
Service discovery: Consul customers can register a service, such as api or mysql, and other customers can query a specified service provider on Consul. Consul provides DNS and HTTP service discovery interfaces.
Health check: Consul can flexibly use scripts and so on to check whether the services registered on it are available, and unhealthy services Consul can also be flexibly handled. For example, the memory of the host providing the service exceeds 90%. The service is provided to the service caller.
Key / value storage: This function is similar to etcd, and can be conveniently used through the HTTP API.
Multi-data center support: Consul supports out-of-the-box multi-data center support, which means users do n’t need to build additional abstraction layers to allow businesses to expand to various regions.

1. Install and run Consul

1. Download and install

Official address: https://learn.hashicorp.com/consul/getting-started/install.html
Download address: https://www.consul.io/downloads.html
Download the corresponding version according to your operating system (this machine (The test uses the 64-bit version of windows)

Start command-start in developer mode

Unzip the downloaded compressed package and release it to the specified location, find the decompressed and released file, and execute the corresponding command according to the system situation (the following is the execution command under windows)

consul agent -dev

2. Test: http: // localhost: 8500 /

Insert picture description here

2. Modification content of the engineering module

1. Pom.xml adds new dependencies

<!--导入consul依赖-->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-consul-discovery</artifactId>
       </dependency>

2. yml file configuration

server:
  port: 8006
spring:
  application:
    name: consul-provider-payment
  cloud:
    consul:
      port: 8500
      discovery:
        service-name: ${spring.application.name}

3. Realization of the main startup program

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

4. Business layer code implementation

@RestController
@Slf4j
public class PaymentController {
    @Value("${server.port}")
    private  String serverport;
    @RequestMapping(value="/payment/consul")
    public  String paymentConsul(){
        return "springcloud with consul:"+serverport+"\t"+ UUID.randomUUID().toString();
    }
}

5. Check the registration status on Consul

Insert picture description here
Insert picture description here

3. The similarities and differences of the three registration centers

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

1. AP Architecture-Eureka

Insert picture description here
Insert picture description here

2.CP architecture-Zookeeper / Consul

Insert picture description here
Insert picture description here

Published 11 original articles · praised 7 · 10,000+ views

Guess you like

Origin blog.csdn.net/foundtime/article/details/105560957