Bus Message Bus

  

What is a message bus

  

Message broker middleware builds a common message topic for all microservice instances to subscribe to, and when this message topic generates messages, they are listened to and consumed by all microservice instances.

What is a message broker? Message broker is a message authentication, transmission, routing architecture pattern, mainly used to receive and distribute messages, and forward to the correct application according to the set message processing flow. It plays a communication scheduling role between microservices, reducing the dependency between services.

  

What is Spring Cloud Bus

  

Spring Cloud Bus is a message Bus within the Spring Cloud architecture that connects all nodes of a distributed system.

Spring Cloud Bus connects distributed nodes with lightweight message brokers (RibbitMQ, Kafka). Changes to configuration files, or communication between services, can be broadcast through a message broker, and can also be used for monitoring. Solve the problem of timely synchronization of micro-service data change.

Cloud-spring. IO /spring-clou…

  

When to use Spring Cloud Bus

  

Microservices are generally deployed in clusters. In high concurrency, microservices need to be expanded, reduced, online, or offline. For example, if we need to update the configuration, or if we need to invalidate a cache on all servers at the same time, and we need to send commands to all relevant servers, we can use Spring Cloud Bus.

In summary, Spring Cloud Bus is used when we need to distribute an operation to all back-end related servers.

Next we implement a configuration refresh of the microservices architecture through Spring Cloud Bus.

  

Environment to prepare

  

RibbitMQ v3.8.2 Address: 192.168.10.101

  

Bus-demo aggregate project SpringBoot 2.2.4.RELEASE, Spring Cloud Hoxton.SR1.

  • eureka-server: Registration Centre
  • eureka-server02: Registration Centre
  • config-server: Configures the central server
  • config-server02: Configures the central server
  • order-service: Order service (configuration center client)
  • order-service02: Order service (configuration center client)

  

Configure the order-service-prod.yml file

spring:
  application:
    name: order-service # app name

Configure the Eureka Server registry
eureka:
  instance:
    prefer-ip-address: true       Whether to register with an IP address
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
  client:
    service-url:                  Set service registry address
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

# Custom configuration
name: order-service-prod
password: root
Copy the code

  

Spring Cloud Bus implements configuration refresh

  

The client initiates the notification

  

A typical application scenario for a message Bus is to configure a central client refresh.

When we learned the Spring Cloud Config configuration center, we talked about the configuration refresh based on the Actuator. At that time, there was only one Config Client. We could use Webhook, and it was not too much trouble to set the manual refresh. However, if there are a large number of clients and it is a bit complicated to manually refresh one by one, this scheme is not suitable. Spring Cloud Bus is the perfect solution to this problem.

With the broadcast function of Spring Cloud Bus, the Config Client can subscribe to the configuration update event. When the configuration update is triggered on one end, Spring Cloud Bus will broadcast this event to other subscribing clients, so as to achieve batch update.

  1. When the Webhook listener is triggered, A Bus-refresh request is sent to ConfigClient A to refresh the configuration
  2. ConfigClient A reads the ConfigServer configuration and sends A message to the Bus
  3. After receiving the message, the Bus broadcasts a notification to other ConfigClients
  4. Other ConfigClients read the latest configuration again after receiving the message

  

Add the dependent

  

Config Client Adds the Spring Cloud Starter Bus AMQP dependency.

<! Spring Cloud Starter Bus AMQP -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
Copy the code

  

The configuration file

  

The configuration file requires configuring message queues and bus-refresh to automatically refresh endpoints. /actuator/bus-refresh Endpoints clear the @refreshScope cache and rebind attributes.

Config Bootstrap. yml core configuration of the Client.

spring:
  cloud:
    config:
      name: order-service Git repository configuration file name, corresponding to the first half of the configuration file
      label: master # git branch
      profile: prod # specify environment
      discovery:
        enabled: true # open
        service-id: config-server # specify the service id of the configuration center server
  # message queue
  rabbitmq:
    host: 192.16810.101.
    port: 5672
    username: guest
    password: guest
    virtual-host: /

Metrics monitoring and health check
management:
  endpoints:
    web:
      base-path: /actuator    # access the root path of the endpoints. The default is /actuator
      exposure:
        include: bus-refresh  The endpoint that needs to be opened
        #exclude: # endpoints that do not need to be opened
Copy the code