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 use Spring Cloud Alibaba Nacos Config

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "how to use Spring Cloud Alibaba Nacos Config". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. demand

It mainly implements some methods of using nacos as a configuration center.

Function 1, loading product-provider-dev.yaml configuration file 2, realizing automatic refresh of configuration 3, loading multiple configuration files 4, priority of configuration file 3, matters needing attention

1. The code related to nacos config configuration needs to be defined in the bootstrap.yml configuration file.

Spring.application.namespring.profiles.activespring.cloud.nacos.config.xxx

2. During the test, it is found that if the class @ RefreshScope annotation is added to the class of the scheduled task, the scheduled task will become invalid after the configuration attribute in nacos is dynamically modified. (reference Code: com.huan.study.product.RefreshScopeScheduledTask)

3. If the value in the @ Value annotation is printed at regular intervals in a thread, it will not change after nacos modifies the value (see code com.huan.study.product.PrintPropertiesTask#initPrint)

4. It is best not to have a priority in the configuration file.

4. Function realization 1. Load a single configuration file

1. Definition of data id: ${prefix}-${spring.profile.active}. ${file-extension}

Prefix: the value of spring.application.name by default, or you can configure it through the configuration item spring.cloud.nacos.config.prefix

Spring.profile.active: this value needs to be defined in the bootstrap.yml configuration file, or passed through a command line argument, and can be empty, where the configuration degenerates to ${prefix}. ${file-extension}

File-extension: specifies the suffix of the configuration file. It defaults to properties and can be specified as yaml.

2. Location of configuration related to nacos

It needs to be placed in the bootstrap.yml configuration file.

2. Realize automatic refresh of configuration

1. Based on the properties of the ConfigurationProperties configuration, the configuration can be refreshed automatically.

2. Based on the attributes of @ Value annotation configuration, you need to add @ RefreshScope annotation to the class.

3. Based on annotations such as @ ConditionalOnProperty, if the properties of the configuration are configured in nacos, it will take effect when the program is started for the first time, and later modifications will not take effect in nacos.

Note:

In the course of testing, it was found that

1. If the class @ RefreshScope annotation is added to the class of the scheduled task, the scheduled task will become invalid after the configuration property in nacos is dynamically modified.

2. If the value in the @ Value annotation is printed at regular intervals in a thread, it will not change after nacos modifies the value.

3. Implement loading multiple configuration files

You can realize that there are multiple profiles in the same project, or multiple projects share the same profile

4. Priority of configuration files

The higher the value of n in spring.cloud.nacos.config.extension- configs [n]. Data-id, the higher the priority

This is the end of "how to use Spring Cloud Alibaba Nacos Config". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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