当前位置:网站首页>7.consul service registration and discovery

7.consul service registration and discovery

2022-06-26 13:44:00 Bitter candy

Study B Standing still, Mr. Zhou Yang of Silicon Valley SpringCloud The lecture notes

1. Consul brief introduction

1.1 What is it?

 Insert picture description here

1.2 What can I do?

Service discovery : Provide HTTP and DNS Two ways of discovery
Health monitoring : Support multiple protocols ,HTTP、TCP、Docker、Shell Script customization
KV Storage :key , Value Storage mode
Multi-data center :Consul Support multiple data centers
visualization Web Interface

Official website :https://www.consul.io/intro/index.html
Download address :https://www.consul.io/downloads.html
Chinese document :https://www.springcloud.cc/spring-cloud-consul.html

1.3 Install and run Consul

After downloading, there is only one consul.exe file , Double click on the hard disk path to run , View version information
 Insert picture description here  Insert picture description here
Use development mode to start :consul agent -dev
You can access... Through the following address Consul Home page :http://localhost:8500

 Insert picture description here

2. newly build Module Payment services provider8006

2.1 newly build module cloud-providerconsul-payment8006

2.2 Change pom

<?xml version="1.0" encoding="UTF-8"?>
<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>springcloud2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-providerconsul-payment8006</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2.3 Change yml

server:
  port: 8006

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

2.4 Startup class and business class

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

    @Value("${server.port}")
    private String serverPort;

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

2.5 Verification test

http://localhost:8006/payment/consul

3. newly build Module Consumer service order8006

3.1 newly build Modulecloud-consumerconsul-order80

3.2 Change pom

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3.3 Change yml

server:
  port: 80

spring:
  application:
    name: consul-consumer-order
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${
    spring.application.name}

3.4 Startup class and business class

@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
    
    public static void main(String[] args) {
    
        SpringApplication.run(OrderConsulMain80.class,args);
    }
}
@Configuration
public class ApplicationContextConfig {
    
    @LoadBalanced
    @Bean
    public RestTemplate getRestTemplate(){
    
        return new RestTemplate();
    }
}
@RestController
@Slf4j
public class OrderConsulController {
    

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

    @Resource 
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/consul")
    public String payment (){
    
      String result = restTemplate.getForObject(INVOME_URL+"/payment/consul",String.class);
      return result;
    }
}

3.5 Verification test

http://localhost/consumer/payment/consul

4. The similarities and differences between the three registries

4.1CAP theory

C:Consistency( Strong consistency )
A:Availability( Usability )
P:Partition tolerance( Partition fault tolerance )
CAP Theory focuses on granularity is data , Rather than the overall system design strategy
 Insert picture description here  Insert picture description here

4.2 The similarities and differences between the three registries

 Insert picture description here

4.2.1AP(Eureka)

 Insert picture description here  Insert picture description here

4.2.2 CP(Zookeeper/Consul)

 Insert picture description here  Insert picture description here

原网站

版权声明
本文为[Bitter candy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261253449828.html