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

Case Analysis of SpringCloud dynamic configuration change Monitoring

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

Share

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

SpringCloud dynamic configuration change monitoring case analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Brief introduction

Configuration centers include native SpringCloud Config, domestic open source Ali Nacos, Ctrip Apollo and so on. Other functions are added on the basis of configuring hot loading.

The configuration center has configuration hot loading, modify configuration-> push to program-> perform configuration updates.

The specific content of the configuration update is obtained, which is not achieved by these components (SpringCloud Config, Nacos, Apollo to be verified). For example, the test of getting the changed configuration is 123before update and 111after update.

The configuration updates to be verified by SpringCloud Config, Nacos and Apollo all push the entire configuration file to the service for configuration comparison and update. Users cannot get updated content from the program at this stage.

In order to achieve this extended function point, the update results can make the program aware. At this stage, users can use code to monitor the configuration, listen for a configuration change and do other operations.

Example: get the change of push configuration, sense the change of test, update and synchronize data, and so on.

Perceived result sets such as:

{`updated configuration key`: {before:` original value, after: `updated value`}, `updated configuration key`: {before:` original value, after: `updated value`} function to add dependencies

Ps: please use the latest version of the actual version version

Com.purgeteam dynamic-config-spring-boot-starter 0.1.0.RELEASE

Of course, the actual situation should be combined with adding dynamic configuration dependency packages SpringCloud Config, Nacos, Apollo.

The dynamic-config-spring-boot-starter module does not contain the above dependencies and needs to be added by yourself.

@ EnableDynamicConfigEvent

Summary: enable this feature annotation and have the ability to configure push and update monitoring.

Add @ EnableDynamicConfigEvent annotation to the startup class to enable the configuration change listening feature.

@ EnableDynamicConfigEvent@SpringBootApplicationpublic class DynamicConfigSpringBootApplication {public static void main (String [] args) {SpringApplication.run (DynamicConfigSpringBootApplication.class, args);}} write event sinks

Create NacosListener (name at will) to implement the ApplicationListener#onApplicationEvent method

@ Slf4j@Componentpublic class NacosListener implements ApplicationListener {@ Override public void onApplicationEvent (ActionConfigEvent event) {log.info ("receive event"); log.info (event.getPropertyMap (). ToString ());}}

Get the target value in the NacosListener#onApplicationEvent method and do the corresponding logic processing.

ActionConfigEvent event:

Public class ActionConfigEvent extends ApplicationEvent {/ / event description private String eventDesc; / / updates the change result set private Map propertyMap;.}

ActionConfigEvent mainly includes Map propertyMap;, where you can get the update result. The structure of propertyMap is as follows:

{`updated configuration key`: {before: `original value`, after: `updated value`}, `updated configuration key`: {before: `original value`, after: `updated configuration demonstration

Ps: the example is Nacos. There is no difference between other configuration centers.

Original configuration:

Test.age=18user.name=purgeyao

Modify the configuration:

# test.age updates 18 to 19test.age=19user.name=purgeyao

Add endpoint debugging to the NacosListener#onApplicationEvent method to observe the parameters of the ActionConfigEvent object.

The updated test.age has been recorded in ActionConfigEvent.propertyMap, from 18 to 19.

Console print:

2019-10-17 10 INFO 4415 09.221 INFO 54054-[- 10.1.1.97 before=18 8848] c.p.dynamic.config.demo.NacosListener: receive event 2019-10-17 10 INFO 45 INFO 19.752 8848-[- 10.1.1.97 July 8848] c.p.dynamic.config.demo.NacosListener: {test.age= {before=18, 8848}}

The above functions are based on Nacos, SpringCloud Config or SpringCloud Config-based configuration centers can be used, other configuration centers need to be verified.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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