How to "read" the corresponding configuration in Nacos in multiple environments

Nacos: How to "read" the corresponding configuration in Nacos in multiple environments

In actual development, usually a system will prepare a development environment, a test environment, a pre-release environment, and a formal environment

So how to ensure that the service can correctly read the configuration file of the corresponding environment on Nacos when the specified environment starts?

This article mainly discusses how to read multi-environment configuration files when Nacos is used as the configuration center.

# my environment

  • Windows10
  • JDK8
  • SpringCloud:Finchley.RELEASE
  • SpringBoot:2.0.4.RELEASE
  • spring-cloud-alibaba-dependencies:0.2.2.RELEASE
  • Nacos-server:1.0.1

Data ID Scheme

It was introduced in the previous article Data ID, and its naming rules are:${prefix}-${spring.profile.active}.${file-extension}

Through the attributes in it, spring.profile.activeyou can read configuration files in multiple environments

Let's try it together~

new configuration

1. After starting Nacos-Server, create a configuration file whose Data ID is: nacos-config-dev.yml, and its configuration is as follows:

server:
  port: 9980
nacos: 
  config: 这里是dev环境

2. Continue to create the configuration file Data ID is: nacos-config-test.yml, the configuration is as follows:

server:
  port: 9981
nacos: 
  config: 这里是test环境

Multi-environment testing

Start nacos-configthe project through Idea, and specify spring.profiles.active, start through different environments

Through the above configuration, the project is divided into two environments, dev and test. After starting, test

Visit http://127.0.0.1:9980/getValue to return: here is the dev environment

Visit http://127.0.0.1:9981/getValue return: here is the test environment

It can be seen that after starting with dev and test, different configurations are read accordingly. The dev environment reads the startup port as 9980, and the test reads the startup port as 9981.

Group plan

The above describes how to read different configurations in different environments by specifying spring.profile.activeand configuring filesDataID

It can also not be used here DataID, directly by Grouprealizing the environment distinction

Note: This method is not recommended, the switch is not flexible, and the Gruop configuration needs to be changed when the environment needs to be switched

new configuration

1. Create a configuration file. Data ID is: nacos-config.yml, Group is: DEV_GROUP, and the configuration is as follows:


server:
port: 9980
nacos:
config: 这里是dev环境

2. Continue to create the configuration file Data ID is: nacos-config.yml, Group is: TEST_GROUP, and the configuration is as follows:


server:
port: 9981
nacos:
config: 这里是test环境

The two configuration files here have the same DataID but different Groups

Modify the configuration file bootstrap.yml in the project

Add a group configuration under config, and specify the group where the configuration file is located, which can be configured as DEV_GROUPorTEST_GROUP


spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
group: DEV_GROUP

start test

Configure the group to DEV_GROUPstart for testing

Visit http://127.0.0.1:9980/getValue to return: here is the dev environment

Configure the group to TEST_GROUPstart for testing

Visit http://127.0.0.1:9981/getValue return: here is the test environment

Start by specifying the group, DEV_GROUP reads the startup port as 9980, and TEST_GROUP reads the startup port as 9981

illustrate

I don't recommend using Group to distinguish between multiple environments, because it will naturally change when multiple environments are involved spring.profile.active, and once the profile takes effect, the configuration file will be searched according to the rules of DataID. So the method of Group is only for reference.

The reasonable usage of Group should be to cooperate with namespace to isolate and manage the service list and configuration list

Namespace scheme

Environment isolation by Namespace namespace is also an officially recommended way. One of the common scenarios of Namespace is the separation and isolation of configurations in different environments, such as the isolation of resources (such as configurations and services) in development and test environments and production environments.

create namespace

Create a namespace DEVand TESTdifferent namespaces will generate corresponding UUIDs, as shown below

New configuration file

1. Create the DataID in the namespace DEV as: nacos-config.yml, and the Group is the default value configuration. The configuration is as follows:


server:
port: 9980
nacos:
config: 这里是DEV命名空间

2. Create the DataID under the namespace TEST as: nacos-config.yml, and the Group as the default value configuration. The configuration is as follows:

server:
port: 9981
nacos:
config: 这里是TEST命名空间

Modify the configuration file bootstrap.yml in the project

Add a namespace configuration under config, and specify the namespace ID where the current configuration is located. Note that the namespace ID!!! The configuration is as follows


spring:
application:
name: nacos-config
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
prefix: ${spring.application.name}
file-extension: yml
namespace: edbd013b-b178-44f7-8caa-e73071e49c4d

## 启动测试

将namespace配置为`DEV`的ID:`edbd013b-b178-44f7-8caa-e73071e49c4d`,启动进行测试

访问 [http://127.0.0.1:9980/getValue](http://127.0.0.1:9980/getValue) 返回:这里是DEV命名空间

将namespace配置为`TEST`的ID:`0133bd1e-25c3-4985-96ed-a4e34efdea2e`,启动进行测试

访问 [http://127.0.0.1:9981/getValue](http://127.0.0.1:9981/getValue) 返回:这里是TEST命名空间

通过指定namespace的方式启动,均可读取到对应的启动端口和相关配置

## 说明

Namespace是官方推荐的环境隔离方案,确实有他的独到之处,使用namespace这种方案,同时可以与DataID+profile的方式结合

同时释放Group的限制,大大提高多环境配置管理的灵活性。

# 总结

通过上面三种方案的介绍,想必大家对于多环境下的配置读取方式应该有所选择

* DataID: 适用于项目不多,服务量少的情况。
* Group:实现方式简单,但是容易与DataID方案发生冲突,仅适合于本地调试
* Namespace:实现方式简单,配置管理简单灵活,同时可以结合DataID共同使用,推荐这种方案

Guess you like

Origin blog.csdn.net/Wis57/article/details/129754198