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 Config

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

Share

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

This article mainly introduces "the use of Spring Config". In the daily operation, I believe that many people have doubts about the use of Spring Config. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the use of Spring Config. Next, please follow the editor to study!

Configuration is actually divided into structure and content, structure corresponds to the code, for example, 1.0.0 newly developed code has a function switch ${feature.switchA}, but not on master, this is the structure change. On the other hand, the content. The development branch of 1.0.0 has two test environments connected to different databases, so the content of the corresponding ${mysql.url} must be different.

The content can also be divided into three categories: business configuration, function switch, service configuration.

The configuration center of Spring Cloud is Spring Config. After two years of use, we have found many problems, some of which are caused by the use of Spring Config and some caused by the management ability of Spring Config itself.

Spring Config first promotes git-based management, which provides two management dimensions, one is label (that is, branch), and the other is profile. When the service foo installs multiple environments under one set of code, for example, there are two pre-release environments, one in the shanghai server room and the other in the beijing server room. Then the more natural management dimension is the use of profile,foo-shanghai.yaml and foo-beijing.yaml. When the production environment still needs 2 sets, how to deal with it? At this point, there will be two approaches, one is to make a distinction by adding label dimensions, and the other is still using only profile.

Method 1: use label + profile to distinguish that NameBranchProfilefoo-shanghai.yamlstgshanghaifoo-beijing.yamlstgbeijingfoo-shanghai.yamlprdshanghaifoo-beijing.yamlPrdbeijingbranch actually represents structure, that is, corresponding to different code, while profile corresponds to content.

What's wrong with this approach? In general, only profile is used to distinguish the environment. For example, logback is also specified by distinguishing configuration according to the environment. Once two dimensions are used to determine the unique configuration, all projects need to have the variable label.

Imagine that if foo has a bug online that needs fix, it is bound to add a branch of hotfix in the configuration center, and also need to add a corresponding profile. The label variable of the corresponding foo is set to hotfix,profile and set to beijing or shanghai.

In another case, what if foo's code in prd needs to be put into stg for verification? The code version of foo is definitely prd (because the configuration structure of stg may have changed), but profile requires a stg environment. At this point, you can only create a new profile on the prd branch of the configuration center to meet this requirement.

Method 2: use only profile to distinguish between NameBranchProfilefoo-stg-shanghai.yamlmasterstg-shanghaifoo-stg-beijing.yamlmasterstg-beijingfoo-prd-shanghai.yamlmasterprd-shanghaifoo-prd-beijing.yamlmasterprd-beijing

In this way, you can reduce the administrative dimension, that is, abandon the dimension of label, and only the dimension of profile. Similarly, if the foo app has a bug online that needs fix, then you need to add two new profile,hotfix-beijing and hotfix-shanghai. Although the dimension has been reduced, there is some trouble in management. Because this branch of master cannot be protected, if a developer directly modifies the environment of prd-XXX, it will cause online problems.

Similarly, what if foo's code in prd needs to be put into stg for verification? The code version of foo is definitely prd (because the configuration structure of stg may have changed), but profile requires a stg environment. In fact, you can only configure the center to create a new profile, such as stg-oldshanghai, to meet this requirement.

However, we know that adding a new profile is actually quite troublesome, and if there is a logic in the code to directly compare profile, then it is often prone to problems.

Is there any way not to add profile temporarily? In fact, think carefully, what is the real logic of validating prd services in a stg environment? You want to use the configuration content of the stg environment, as well as the configuration structure of a historical version of stg that matches prd. So what we need in the vertical dimension is that version,profile is all stg-shanghai, while version is 1.0.0 and latest.

Method 3: synthesize

OK, now let's combine the two ways, whether we can use the branch of git as version,profile or follow method 2 to distinguish. After all, the possibility of increasing the environment frequently is not high. However, if you want to maintain two branches of a profile at the same time, you still have to switch back and forth, which is troublesome. This is also the weak management function of Spring Config. Fortunately, Spring Cloud also supports mysql, and it is still convenient to use mysql to manage the content of multiple label at the same time, but the "history" function of git is gone. So there are still advantages and disadvantages.

Summary

If you want better configuration management tools, it is recommended to use Apollo. If you want to make good use of Spring Cloud, you must be able to put up with its weak management ability, and make good pre-planning, combined with the characteristics of the project to use the ability of label and profile.

At this point, the study of "how to use Spring Config" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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