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 realize Multi-Environment Management by Spring Cloud Alibaba Nacos

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

Share

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

This article mainly explains "Spring Cloud Alibaba Nacos how to achieve multi-environment management", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Spring Cloud Alibaba Nacos how to achieve multi-environment management"!

In Nacos, there are several concepts at different levels of management, including Data ID, Group, Namespace. As long as we make good use of the relationship between these hierarchical concepts, we can realize the management of multiple environments according to our own needs.

Here are a few ways you can do it:

Using Data ID and profiles

Data ID in Nacos, we can understand as a Spring Cloud application configuration file name. We know that by default the Data ID name format is ${spring.application.name}.properties, i.e. properties file named after Spring Cloud app.

In fact, the rules of Data ID also contain environmental logic, which is similar to the design of Spring Cloud Config. When we start the application, we can specify the specific environment name through spring.profiles.active. At this time, the client will organize the Data ID to obtain the configuration as: ${spring.application.name}-${spring.profiles.active}.properties.

In fact, the more primitive and most general matching rule is this: ${spring.cloud.nacos.config.prefix}-${spring.profile.active}.$ {spring.cloud.nacos.config.file-extension}。The above result is because ${spring.cloud.nacos.config.prefix} and ${spring.cloud.nacos.config.file-extension} both use default values.

Give it a try

We can use the examples from the previous article as a basis for experiencing this configuration of differentiated environments.

Step 1: In Nacos, create the configuration content of two different environments according to this rule. For example:

As shown above, we define two separate environment configurations for the alibaba-nacos-config-client application, DEV and TEST. We can define different content values in it, so that we can verify whether the correct configuration is actually loaded later.

Step 2: Add environment configuration to alibaba-nacos-config-client application configuration file: spring.profiles.active=DEV

Implementation with Groups

Group in Nacos is an important concept used to manage collections of Data IDs. So, if we think of the configuration of one environment as a collection, then we can also implement configuration management for different environments. There is no fixed rule for the usage of Group, so when we actually use it, we need to manage multiple environments according to our specific requirements, which can be architecture operation and maintenance, or parameter management for different modules in business. To avoid conflicts, we need to do some planning at the beginning of the architecture design. Here, let's first talk about how to use Group to implement the specific implementation of multi-environment configuration management.

Give it a try

Step 1: First, in Nacos, create the configuration content of two different environments by distinguishing between groups. For example:

Step 2: At the top of the configuration list, you can see that in addition to Public, there are several Namepsace just created. Create configuration content for alibaba-nacos-config-client application under DEV and TEST space respectively:

Step 3: In the configuration file of alibaba-nacos-config-client application, add the specified configuration of Namespace, for example: spring.cloud.nacos.config.namespace=83eed625-d166-4619-b923-93df 208883a.

Note here that namespace configuration does not use the name, but uses the namespace ID.

Step 4: Start the application and verify that the returned content is correct by accessing the localhost:8001/test interface. In this way, the current version of the log does not output information related to Namespace, so it cannot be used as a basis for judging the loading content.

think deeply

Above, we use several different dimensions of Nacos configuration management function to realize multi-environment configuration management. As a result, either way will meet the demand, but which is best?

The first one is realized through Data ID and profile.

Advantages: This method is very similar to the implementation of Spring Cloud Config. Users who have used Spring Cloud Config can transition without violating the rules. Because the naming rules are similar, it is very easy to migrate from Spring Cloud Config.

Disadvantages: This approach can be very confusing when there are many projects and environments. Configuration list will see a variety of different applications, different environment configurations intertwined, very detrimental to management.

Suggestion: It can be used when the project is not used much, or it can be combined with the Group to make some split planning for the project according to the business or organizational structure.

The second is achieved through groups.

Advantages: Separate the configuration of each application by environment through Group. It is very convenient to use the search function of Data ID and Group to view the configuration from application latitude and environment latitude respectively.

Disadvantages: Since it will occupy the Group latitude, it is necessary to plan the use of the Group. After all, it conflicts with some configuration groups in the business.

Suggestion: Although this method is better than the previous one in structure, there may still be some confusion, mainly in the planning and control of Group management.

The third type: implemented through Namespace.

Advantages: The officially recommended way to distinguish different environments through Namespace releases the freedom of the Group, which allows the use of the Group to focus on group management at the business level. At the same time, Nacos control page for Namespace also made a group display, do not need to search, you can isolate different environment configuration, very easy to use.

Disadvantages: There is no disadvantage, it may be that more than one concept is introduced, and users need to understand it.

Suggestion: this method will be more convenient in the long run. Although there may not be many projects for small teams, the first and second methods are enough, but what if they become bigger later?

Note: Either way. Do not configure the configuration of the specified environment (spring.profiles.active=DEV, spring.cloud.nacos.config.group=DEV_GROUP, spring.cloud.nacos.config.namespace=83eed625-d166-4619-b923-93df 208883 a) in the bootstrap.properties of the application. Instead, in the launch command of the release script, dynamically specify it in the form of-Dspring.profiles.active=DEV, which will be more flexible!

At this point, I believe that everyone has a deeper understanding of "Spring Cloud Alibaba Nacos how to achieve multi-environment management". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

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