SpringBoot e-Commerce project mall (20K + STAR) address: github.com/macrozheng/…

Abstract

Spring Cloud Bus uses a lightweight message broker to connect services in a microservice architecture, which can be used to broadcast state changes (such as configuration center configuration changes) or other administrative instructions, and its usage is described in detail in this article.

Introduction to Spring Cloud Bus

We typically use a message broker to build a topic and then connect all the services in the microservices architecture to this topic, and when we send a message to this topic, all the services that subscribe to this topic receive the message and consume it. This mechanism can be easily built using the Spring Cloud Bus, which is also known as the message Bus. Spring Cloud Bus is used in conjunction with Spring Cloud Config to enable dynamic refreshing of configurations. Spring Cloud Bus supports two types of message brokers: RabbitMQ and Kafka. The following uses RabbitMQ as an example to demonstrate the dynamic configuration refresh function using Spring Cloud Bus.

The installation of the RabbitMQ

  • Install Erlang at erlang.org/download/ot…

  • Install the RabbitMQ, download address: dl.bintray.com/rabbitmq/al…

  • After installation, go to the sbin directory in the RabbitMQ installation directory:

  • Enter CMD in the address bar and press Enter to start the command line. Then enter the following command to start the management function:
rabbitmq-plugins enable rabbitmq_management
Copy the code

  • Visit http://localhost:15672/ to check whether the installation is successful

  • Enter the password and log in to: guest guest

Dynamically Refreshing configurations

Using Spring Cloud Bus to dynamically refresh the configuration needs to be used in conjunction with Spring Cloud Config. We use the config-server and config-client modules in the previous section to demonstrate this function.

Add message bus support to config-server

  • Add dependencies to pom.xml:
<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>
Copy the code
  • Add the application-amqp.yml configuration file, which adds the RabbitMQ configuration and exposes the updated configuration of the Actuator endpoints.
server:
  port: 8904
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/macrozheng/springcloud-config.git
          username: macro
          password: 123456
          clone-on-start: true Get configuration directly from Git at startup
  rabbitmq: Configure rabbitMQ
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
management:
  endpoints: Expose endpoints of bus refresh configuration
    web:
      exposure:
        include: 'bus-refresh'
Copy the code

Add message bus support to config-client

  • Add dependencies to pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
Copy the code
  • Add the bootstrap-amqp1.yml and bootstrap-amqp2.yml configuration files used to start two different config-clients. The two configuration files only have different port numbers.
server:
  port: 9004
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev # Enable the environment name
      label: dev # branch name
      name: config # Config file name
      discovery:
        enabled: true
        service-id: config-server
  rabbitmq: Configure rabbitMQ
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
management:
  endpoints:
    web:
      exposure:
        include: 'refresh'
Copy the code

Dynamically refresh the configuration demo

  • We started relevant services first, started Eureka-server, started config-server with application-AMqp. yml as configuration, and started config-client with bootstrap-amqp1.yml as configuration. Start a config-client with bootstrap-amqp2.yml as the configuration. The following information is displayed in the registry:

  • After starting all the services, we can log on to the RabbitMQ console and see that Spring CloudBus has created a switch named springCloudBus and three queues starting with springCloudbus.anonymous:

  • Git repository: config-dev. Yml
# Modify the previous information
config:
  info: "config info for dev(dev)"
# Modified information
config:
  info: "update config info for dev(dev)"  
Copy the code
  • Call the registry interface refresh all configuration: http://localhost:8904/actuator/bus-refresh

  • Refresh again after call http://localhost:9004/configInfo and http://localhost:9005/configInfo for configuration information, found that have been set;
update config info for dev(dev)
Copy the code
  • If you only need to refresh the configuration of the specified instance, you can use the following format: http://localhost:8904/actuator/bus-refresh/ {destination}, We are here to refresh the config run on port 9004 – client, for example http://localhost:8904/actuator/bus-refresh/config-client:9004.

Use with WebHooks

WebHooks are like a hook function that can be configured to trigger when pushing code to a Git repository. Here’s how to use them: Gitee.

The module used

Springcloud - learning ├ ─ ─ eureka - server-- Eureka Registry├ ─ ─config-server - Configure the center service└ ─ ─config-client -- Get the configured client service
Copy the code

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.