The boss is talking. Can I say no?

This article looks at how to implement configuration management for different environments (development, test, gray scale, formal).

Just as Maven uses groupId, artifactId, and Version to locate JAR packages in the repository, Nacos also provides Namespace (Namespace), Data ID (configuration set ID), and Group (Group) to determine a configuration file (or set).

Therefore, there are three solutions to implement multi-environment configuration:

1. Use a namespace to distinguish different environments. One namespace corresponds to one environment.

The default namespace is public. Each group corresponds to one environment.

3. Use the Data ID (configuration set ID) name to distinguish different environments. Use the default namespace and group name to distinguish different environments.

So let’s look at them one by one

http://{host}:{port}/nacos

http://{host}:{port}/nacos/index.html

The default username and password are both nacos



For demonstration purposes, a Spring Boot project named Example has been built



pom.xml

1 <? The XML version = "1.0" encoding = "utf-8"? < > 2 project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3 Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId> org.springFramework. boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.3.6.RELEASE</version> 9 <relativePath/> <! -- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>example</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>example</name> 15 16 <properties> 17 <java.version>1.8</java.version> 18 <spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-web</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>com.alibaba.cloud</groupId> 28 <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> 29 </dependency> 30 </dependencies> 31 32 <dependencyManagement> 33 <dependencies> 34 <dependency> 35 <groupId>com.alibaba.cloud</groupId> 36 <artifactId>spring-cloud-alibaba-dependencies</artifactId> 37 <version>${spring-cloud-alibaba.version}</version> 38 <type>pom</type> 39 <scope>import</scope> 40 </dependency> 41 </dependencies> 42 </dependencyManagement> 43 44 <build> 45 <plugins> 46 <plugin> 47 <groupId>org.springframework.boot</groupId> 48 <artifactId>spring-boot-maven-plugin</artifactId> 49 </plugin> 50 </plugins> 51 </build> 52 53 </project>Copy the code

bootstrap.yml

Spring: Application: name: example Cloud: nacos: config: server-addr: 192.168.100.10:8848 File-extension: YAMLCopy the code

HelloController.java

package com.example.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @author ChengJianSheng * @data 2020/11/19 */ @RestController @RequestMapping("/hello") @RefreshScope public class HelloController { @Value("${greet.hello}") private String greet; @GetMapping("/sayHi") public String sayHi() { return greet; }}Copy the code

1. Use the Data ID to identify the environment. Use the Data ID to identify the environment

In Nacos Spring Cloud, the full format of dataId is as follows:

${prefix}-${spring.profiles.active}.${file-extension} 
Copy the code
  • Prefix for spring. The default application. The name of the value, but can be by spring configuration items. The cloud. Nacos. Config. The prefix to configuration

  • Spring.profiles. active is the profile corresponding to the current environment. For details, refer to the Spring Boot documentation. Note: When spring.profiles.active is null, the corresponding connector – will also not exist and the dataId concatenation format will become

    {prefix}.

    prefix.{file-extension}

  • File – exetension as the configuration of content data format, can be configured a spring. Cloud. Nacos. Config. The file – the extension to the configuration. Currently, only properties and YAML types are supported.









    The same goes for starting from the command line



    Such as:

    Java – Dspring. Profiles. The active = test – jar example – 0.0.1 – the SNAPSHOT. The jar



2. Use groups to differentiate the environment

The project stays the same and we change spring.application.name to example2

The default namespace is public





Java - Dspring. Cloud. Nacos. Config. Group = DEV_GROUP - jar example - 0.0.1 - the SNAPSHOT. The jarCopy the code

Java - Dspring. Cloud. Nacos. Config. Group = TEST_GROUP - jar example - 0.0.1 - the SNAPSHOT. The jarCopy the code

Java - Dspring. Profiles. The active = test - Dspring. Cloud. Nacos. Config. The group = TEST_GROUP - jar example - 0.0.1 - the SNAPSHOT. The jarCopy the code

If so, at this point, the Data ID name would be example2-test.yaml

3. Use the Namespace to differentiate the environment



When creating a namespace, if you do not specify an ID, the generated ID will be a uUID string like this, so we will specify a meaningful ID ourselves













Java - Dspring. Cloud. Nacos. Config. The namespace = ns_dev - jar example - 0.0.1 - the SNAPSHOT. The jarCopy the code

Java - Dspring. Profiles. The active = dev - Dspring. Cloud. Nacos. Config. The namespace = ns_dev - jar example - 0.0.1 - the SNAPSHOT. The jarCopy the code

Java - Dspring. Cloud. Nacos. Config. The namespace = ns_test - jar example - 0.0.1 - the SNAPSHOT. The jarCopy the code

java -Dspring.profiles.active=test \ -Dspring.cloud.nacos.config.namespace=ns_test \ - Dspring. Cloud. Nacos. Config. Group = TEST_GROUP \ - jar example - 0.0.1 - the SNAPSHOT of the jarCopy the code



4. Summary

The first is to use Data ID to differentiate the environment. Although it is simple, each project needs to create 4 configuration files. As the number of projects increases, it is very confusing to find in the same namespace, and it is not very convenient, and it is not conducive to do permission control

The second one, Group, is the same problem

To sum up, it is best to distinguish the environment with Namespace, which is clear and conducive to permission control

Source | urlify. Cn/jmY3Ez