Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Spring Cloud Bus message

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of Spring Cloud Bus messages for you. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Concept

When we use the configuration center, when the configuration of the configuration center changes, we have to send a post request to the client to pull the new configuration. When there are many clients and still use the same configuration file, so when the configuration of the configuration center changes, we have to send post request notifications one by one, which is undoubtedly a waste of manpower and material resources.

The Bus message bus component helps us solve this problem. His workflow is that when the configuration of the configuration center changes, we send a post request to one of the clients, then client sends the requested information to the rabbitmq queue, and the message queue sends the message to another queue.

Use preparation work

The project is based on the project transformation of Chapter 7 of Spring Cloud.

Transform config-client and add corresponding coordinates

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 @ RefreshScope annotation to the startup class

The @ RefreshScope annotation only needs to be written where the configuration file needs to be refreshed, not necessarily in the startup class

@ 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;}}

Configure related configuration

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-refreshmanagement.security.enabled=false / / error report plus

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 in order to update the configuration file. At this point, we just need to send the post request: http://localhost:8881/actuator/bus-refresh, and you will find that config-client will reread the configuration file.

Version 1.5 post request http://localhost:8881/bus/refresh

Version 2.0 post request http://localhost:8881/actuator/bus-refresh

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. Schematic diagram

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.

This is the end of this article on "sample Analysis of Spring Cloud Bus messages". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report