Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

SpringCloud distributed Micro Services Cloud Architecture part 8: message bus (SpringCloud Bus) (Finchley version)

Shulou Source: shulou.com Published: 2022-06-02 22:20:05 09月22日 Update

Spring Cloud Bus connects distributed nodes with lightweight message agents. It can be used to broadcast changes to configuration files or to communicate between services

It can also be used for monitoring. This article is about the use of Spring Cloud Bus to notify changes to the configuration file of the microservice architecture.

I. preparatory work

This paper is based on the previous article. According to the official documentation, we only need to configure spring-cloud-starter-bus-amqp in the configuration file

To understand the springcloud architecture, please add: 3536247259, which means we need to install rabbitMq and click rabbitmq to download it. As for how

Use rabbitmq, under the search engine.

Second, transform config-client

Add the startup dependency spring-cloud-starter-bus-amqp to the pom file, and the complete configuration file is as follows:

Org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud- Starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-actuator

Add the configuration of RabbitMq to the configuration file application.properties, including the address, port, user name and password of RabbitMq. And you need to add three configurations of spring.cloud.bus, as follows:

Spring.rabbitmq.host=localhostspring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guestspring.cloud.bus.enabled=truespring.cloud.bus.trace.enabled=truemanagement.endpoints.web.exposure.include=bus-refresh

The ConfigClientApplication startup class code is as follows:

@ SpringBootApplication@EnableEurekaClient@EnableDiscoveryClient@RestController@RefreshScopepublic class ConfigClientApplication {/ * http://localhost:8881/actuator/bus-refresh * / public static void main (String [] args) {SpringApplication.run (ConfigClientApplication.class, args);} @ Value ("${foo}") String foo @ RequestMapping (value = "/ hi") public String hi () {return foo;}}

Start eureka-server, then confg-cserver, and then start the two config-client, ports 8881 and 8882.

Visit the http://localhost:8881/hi or http://localhost:8882/hi browser to display:

Foo version 3

At this point, we go to the code repository to change the value of foo to "foo version 4", that is, to change the value of the configuration file foo. If it is a traditional practice, you need to restart the service.

To achieve the update of the profile. At this point, we just need to send the post request: http://localhost:8881/actuator/bus-refresh, you will find

Config-client will reread the configuration file

Reread the configuration file:

At this point, we visit the http://localhost:8881/hi or http://localhost:8882/hi browser to show:

Foo version 4

In addition, the / actuator/bus-refresh interface can specify services, even with the "destination" parameter, such as "/ actuator/bus-refresh?destination=customers:**", which refreshes all services with the service name customers.

III. Analysis

The architecture diagram at this time:

When the git file is changed, the request / bus/refresh/; is sent to the config-client with port 8882 using post through the pc side. At this time, port 8882 will send a message to other services through the message bus, so that the entire micro-service cluster reaches the update profile.

Tags: Configuration file service port architecture message code browser update browse bus three two between warehouse traditional practice distributed parameter address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info MySQL Shulou Information Apple