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

What is the reason why the configuration of the corresponding Group cannot be obtained when the Spring Cloud Alibaba Nacos configuration center uses ext-config,DataID and Group

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "the reason why the Spring Cloud Alibaba Nacos configuration center can not get the configuration of the corresponding Group when using ext-config,DataID and Group". In the operation of the actual case, many people will encounter such a dilemma, so 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!

When using Spring Cloud with Nacos Config as the configuration center, Nacos supports configuration management of multiple environments, usually through namespaces, such as

Test environment, pre-release environment. If you use the same configuration center to manage multiple project teams or teams or different modules, you usually use Group to distinguish. The configuration details related to Nacos Config have been introduced in the official documents. You can learn about https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Nacos-config by reading. Here, I would like to share with you a problem encountered in use. Or it is not clear in the document that NacosConfig supports the configuration of custom Group and the configuration of Data Id that supports custom extension. Group uses DEFAULT_GROUP by default. We can see from the source code org.springframework.cloud.alibaba.nacos.NacosConfigProperties of nacos that if you want to support the use of multiple configuration files, you need to use ext-config. Spring.cloud.nacos.config.ext- config [n] .data-id, spring.cloud.nacos.config.ext- config.group, spring.cloud.nacos.config.ext- config.refresh, but encountered a problem when using it, that is, when a configuration file with the same name as the application name is configured in ext-config, what is actually obtained is not the group of ext-config, but the DEFAULT_GROUP that is used to obtain it. If you do not have a configuration file in DEFAULT_GROUP, you will get nothing, which means that DEFAULT_GROUP overwrites the Group configured in the custom configuration. For example, the name of the current service is nacos-demo and spring.application.name=nacos-demo.

Spring.cloud.nacos.config.ext-config [0] .data-id=nacos-demo.properties (${spring.application.name} .properties)

Spring.cloud.nacos.config.ext-config [0] .group = DEMO

Spring.cloud.nacos.config.ext-config [0] .refresh = true

So when Client acquires the configuration, the default is to get the configuration file whose data-id is nacos-demo.properties group and DEFAULT_GROUP, but not the configuration of DEMO. Why? From the source code NacosPropertySourceLocator of nacos, you can see

His loading order is to load the SharedConfig shared configuration by default, then load the extConfig custom configuration, and finally load applicationConfig, so the last load will overwrite the previous configuration, so when you read the configuration with the same name, you will not be able to read it, because the configuration center does not configure nacos-demo.properties with Group as DEFAULT_GROUP. The priority of spring.cloud.nacos.config.group will be higher than that of spring.cloud.nacos.config.ext-config [0] .group

Therefore, it should be noted that if it is a configuration file with the same name as the application name, do not configure it in config.ext-config. Configure the configuration file other than the application name in spring.cloud.nacos.config.group=DEMO,ext-config directly. Group can also be configured as other custom groups.

Spring.cloud.nacos.config.ext-config [0] .data-id= (not application name) .properties

Spring.cloud.nacos.config.ext-config [0] .group = default

Spring.cloud.nacos.config.ext-config [0] .refresh = true so that you don't have to get the configuration file.

Configured priority

Spring Cloud Alibaba Nacos Config currently provides three configuration capabilities to pull relevant configurations from Nacos.

A: supports multiple shared Data Id configurations through spring.cloud.nacos.config.shared-dataids

B: support the configuration of multiple extended Data Id through the way of spring.cloud.nacos.config.ext- config.data-id

C: automatically generate relevant Data Id configuration through internal relevant rules (application name, application name + Profile)

When the three methods are used together, one of their priority relationships is: a < B < C

This is the end of the content of "what is the reason why the Spring Cloud Alibaba Nacos configuration center uses ext-config,DataID and Group cannot get the corresponding Group configuration". Thank you for your 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

Internet Technology

Wechat

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

12
Report