Spring Cloud configuration center uses bus (message bus) to refresh cluster configuration automatically

spring boot version: 2.1.10.RELEASE
spring cloud version: Greenwich.SR4

1, Refresh from client


All of the following configurations are for clients and are available on the Microservice builds Spring Cloud configuration center [Client] The configuration posted here is the full version, but will not be explained in detail.

Add dependency

<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>

<!--Configure central client dependencies-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

<!--utilize spring boot Of refresh send git Configuration dynamic validation in-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!--Support rabbitmq and kafka-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

Add configuration

Add configuration in bootstrap.properties

spring.application.name=ebook-config-client-bus
server.port=9101

#Configure registry
eureka.client.service-url.defaultZone=http://192.168.xxx.xxx:8761/eureka/,http://192.168.xxx.xxx:8762/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=ebook-config-client-bus

#defaultfalse,Set up heretrue,Indicates that the configuration of read configuration center is enabled
spring.cloud.config.discovery.enabled=true
#Corresponding to the serviceId of the configuration center in eureka, the default is configserver
spring.cloud.config.discovery.serviceId=ebook-config-server
#Specify the application name, that is, the{application}Section, if client's spring.application.name With profile{application}This item may not be configured if it is partially the same
spring.cloud.config.name=config-client
#Designated environment
spring.cloud.config.profile=dev
#git Tags
spring.cloud.config.label=master

#By default, springboot turns on permission blocking, which will result in /actuator/refresh Appear404,access denied
management.endpoints.web.exposure.include=*

#rabbitmq configuration
spring.rabbitmq.host=192.168.xxx.xxx
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/

adding annotations

Add @ RefreshScope annotation on the class.

Send update request

Use POST to request http://127.0.0.1:9101/actor/bus-refresh (the request address is the client address) to refresh. If it is a cluster configuration, refresh one client, and other clients in the cluster will refresh at the same time.

In spring boot 1.x, the request address is http://127.0.0.1:9101/bus/refresh

2, Refresh from server


All of the following configurations are in the Microservices build Spring Cloud configuration center [service end] All the configurations pasted here are the complete version, but will not be explained in detail).

Add dependency

<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>

<!--Configure central server dependency-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

<!--bus Dependency, support rabbitmq and kafka-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

Add configuration

Add configuration in application.properties.

spring.application.name=ebook-config-server
server.port=9100

#Configure registry
eureka.client.service-url.defaultZone=http://192.168.xxx.xxx:8761/eureka/,http://192.168.xxx.xxx:8762/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.hostname=ebook-config-server

spring.cloud.config.server.git.uri=http://192.168.xxx.xxx/git/config
spring.cloud.config.server.git.username=user
spring.cloud.config.server.git.password=123456

#rabbitmq configuration
spring.rabbitmq.host=192.168.xxx.xxx
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/

#By default, springboot turns on permission blocking, which will result in /actuator/refresh Appear404,access denied
management.endpoints.web.exposure.include=*

Send update request

Use POST to request http://127.0.0.1:9100/actor/bus-refresh (the request address is the server address) to refresh. If it is a cluster configuration, refresh the server, and the clients configured in the cluster will refresh at the same time.

Partial refresh

The service refresh can be specified by requesting the address above, but there is a certain problem with this refresh. If it is a cluster deployment, we may need to perform gray-scale publishing, that is, only refresh a certain service or a certain type of service.

Use POST to request http: / / 127.0.0.1:9100 / Actor / bus refresh? Destination = ebook config client bus: 9101 (the request address is the server address)

Among them, ebook config client bus is the spring.application.name of the service, and 9101 is its corresponding port.

If the address is changed to
http://127.0.0.1:9100/actuator/bus-refresh?destination=ebook-config-client-bus:**
Indicates to refresh the cluster of all ebook config client bus services.

272 original articles published, 189 praised, 220000 visitors+
Private letter follow

Tags: Spring RabbitMQ git kafka

Posted on Sun, 02 Feb 2020 03:09:37 -0500 by Mega