Bus of spring cloud 2020.0.4 series

Keywords: Java RabbitMQ Spring Boot Distribution Spring Cloud

1. General

As the old saying goes: people who can rest can work better. Body is the capital of revolution. If their body breaks down, they can no longer work.

 

To get back to business, we talked about the distributed configuration center config of SpringCloud before. In this article, we talked about the dynamic refresh of config configuration, but this dynamic refresh can only refresh one Config Client node at a time. If there are fewer service nodes, it's OK. If there are more business, there are hundreds of thousands of service nodes, and one by one, it's not appropriate.

Therefore, today, let's talk about the message Bus component Bus of SpringCloud. The Bus component can send refresh broadcasts to all Config clients with the help of MQ middleware. It only needs to call one interface to refresh the Config configuration of the whole cluster.

We use RabbitMQ as the middleware. For the construction of RabbitMQ cluster, please refer to my other article "construction of RabbitMQ 3.9.7 mirror mode cluster"( https://www.cnblogs.com/w84422/p/15356202.html).

 

Without much gossip, go straight to the code.

 

2. Git preparation  

1) Create a repository to place configuration files, such as my config repo

2) Create my bus client folder

3) Under my bus client folder, create a new my bus client dev.yml configuration file, as follows:

     
info:
  profile: dev
    
name: zhuifengren
desc: hello dev

 

3. Introducing Bus components into config server

3.1 main dependence

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

 

3.2 application.yml configuration

server:
  port: 42000

spring:
  application:
    name: my-bus-server
  rabbitmq:
    addresses: 192.168.1.22:5672,192.168.1.12:5672,192.168.1.11:5672  # Address of rabbitmq cluster
    username: guest
    password: guest
    virtual-host: /
    connection-timeout: 16000
  cloud:
    config:
      server:
        git:
          uri: https://github.com/w84422/my-config-repo.git   # git address
          force-pull: true  # Force pull resource file
          default-label: main   # Default pull branch
          search-paths: '{application}'      # Put the configuration file in the folder corresponding to the service name of the access party. This writing method only takes effect in the config component settings

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Address of Eureka Server

 

3.3 add notes for startup class

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class MyBusServerApplication {

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

 

3.4 start Config Server

Start Config Server

 

4. The config client introduces the Bus component

4.1 main dependence

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>3.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- rabbitmq -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

 

4.2 bootstrap.yml configuration

server:
  port: 43000

spring:
  application:
    name: my-bus-client
  rabbitmq:
    addresses: 192.168.1.22:5672,192.168.1.12:5672,192.168.1.11:5672
    username: guest
    password: guest
    virtual-host: /
    connection-timeout: 16000
  cloud:
    config:
      profile: dev  # Pulled profile
      label: main   # Pulled branch
      name: my-bus-client  # Specify the application for obtaining the configuration file remotely. spring.application.name will be taken by default
      discovery:
        enabled: true
        service-id: my-bus-server    # Service name of config service

myDesc: ${desc}

eureka:
  client:
    service-url:
      defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/    # Address of Eureka Server

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always

 

4.3 add notes for startup class

@SpringBootApplication
@EnableDiscoveryClient
public class MyBusClientApplication {

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

 

4.4 add Controller class for experiment

@RefreshScope
@RestController
public class MyBusClientRefreshController {

    @Value("${info.profile}")
    private String profile;
    @Value("${name}")
    private String name;
    @Value("${myDesc}")
    private String desc;

    @RequestMapping("/refresh-info")
    public String getInfo() {

        String info = "profile: " + profile + "<br>";
        info += "name: " + name + "<br>";
        info += "desc: " + desc + "<br>";

        return info;
    }
}

 

4.5 start the Config Client service and verify it

4.5.1 start Config Client service

Start two or more Config Client services

 

4.5.2 access the Controller interface to obtain configuration information

GET http://localhost:43000/refresh-info

 

 

4.5.3 modify the configuration on Git

Change the name to zhangsan

 

4.5.4 perform bus configuration refresh interface

On the Config Server or any Config Client node, execute the bus configuration refresh interface

POST http://localhost:42000/actuator/busrefresh

 

4.5.5 at all   The Config Client node calls the Controller interface to retrieve the information

GET http://localhost:43000/refresh-info

 

 

5. Overview

Today, I talked about the Bus component of spring cloud. I hope it can be helpful to your work.

Welcome to like, comment, forward and pay attention:)

Pay attention to those who follow the wind to talk about Java and update Java dry goods every day.

 

6. official account

Fans talk about Java. Welcome to pay attention

 

Posted by mbdonner on Thu, 04 Nov 2021 13:54:57 -0700