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

How to refresh a configuration using spring cloud Bus

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to use spring cloud Bus to refresh the configuration, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

As our business system becomes more and more large and complex, various configurations will increase. As soon as the configuration file is modified, the commonservice-config configuration center will stop the service, then restart, and finally make the configuration effective.

If there are few services, we can start it manually, but it will certainly have an impact on the stability of the business and the system.

In view of the above problems, the commonservice-config server and the business microservice are configured respectively. The server is responsible for configuring the configuration files stored in the git (svn or local file system) (we use the local configuration scheme to facilitate updating the configuration files directly to the linux).

The business micro-service obtains the relevant configuration from the server configuration center through configuration. If the configuration file changes, the latest configuration information is obtained by refreshing the business micro-service.

Spring cloud Bus connects nodes of distributed systems through a lightweight message broker. This can be used to broadcast state changes, such as configuration changes, or other administrative instructions.

Next, we will implement the spring cloud Bus solution to dynamically refresh the server configuration. The specific steps are as follows:

1. For commonservice-config service configuration, please refer to the previous link:

two。 Business micro-service configuration (take honghu-member-servcie member service as an example):

Org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-bus-amqp

Yml file configuration:

Span style= "font-size: 16px "> server: port: 5012 spring: application: name: honghu-member-client profiles: active: dev DiscoveryClient cloud: config: discovery: enabled: true service-id: commonservice-config-server name: honghu-member profile: dev bus: trace: enabled: true # enable message tracking rabbitmq: host: 192.168.1.254 port: 5672 username: honghu password: honghu eureka: client: ServiceUrl: defaultZone: http://honghu:123456@localhost:8761/eureka/ instance: prefer-ip-address: true logging: level: root: INFO org.springframework.security: INFO management: security: enabled: false security: basic: enabled: false

Write a test class (MemberController.java) to get the configuration items

Package com.honghu.cloud.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @ RefreshScope @ RestController public class MemberController {@ Value ("${profile}") private String profile; @ GetMapping ("/ profile") public String getProfile () {return this.profile }}

3. Check the registry, whether the commonservice-config and honghu-member-service services have been registered

4. Visit profile to obtain the configuration information corresponding to profile (original configuration):

Access http://localhost:7071/profile = "access result: 123456

5. Modify the configuration file of config configuration center to change profile=123456 to honghu123456

Visit http://localhost:7071/profile again = "access result: 123456

6. Use the spring cloud bus refresh scheme (test with the post man test tool)

Visit http://localhost:7071/profile again = "access result: honghu123456

At this point, the dynamic refresh scheme of the entire commonservice-config configuration center is sorted out.

After reading the above, have you learned how to refresh the configuration using spring cloud Bus? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Internet Technology

Wechat

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

12
Report