1, Version selection and concept understanding
1. Version selection
Graduation version dependency (recommended)
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version | Nacos Version |
---|---|---|---|
Spring Cloud Hoxton.SR9 | 2.2.6.RELEASE | 2.3.2.RELEASE | 1.4.2 |
2.Namespace
Nacos introduces the concept of namespace to manage and isolate multi environment configuration and services
Namespace is also an officially recommended multi environment support scheme.
3. How to manage and isolate configuration and services (Group)
The group here is the concept of Namespace. Services and configurations are managed in the same Namespace
Services and configurations under different namespaces are isolated.
2, Scheme selection
1. Namespace creation
The NamespaceId value is a configuration item that must be filled in when configuring the configuration file, so we need to create the Namespace and Id first. The steps are as follows:
You can see the function of a namespace in the function bar on the left of the nacos console. Click to see the new namespace button.
After the creation is successful, you can find the Namespace you created and the ID value generated in the Namespace list.
2. Namespace implementation plan
For one tenant
From the perspective of a tenant (user), if there are multiple sets of different environments, you can create different namepces according to the specified environment to realize the isolation of multiple environments.
For example, you may have three different environments: dev, test and prod, so you can build the following three different namespace s using a set of nacos clusters. As shown in the figure below:
By defining different environments, projects in different environments are managed under different namespaces, and different environments are isolated through namespaces.
When multiple projects use the Nacos cluster at the same time, you can also refine the grouping in the Namespace through the Group.
Here, take Namespace: dev as an example. In Namespace, different groups are used to reclassify different projects in the same environment.
3, nacos configuration practice
3.1. dev environment configuration creation
Create a new configuration file under Namespace
Here, it is assumed that there are two projects, namely, Nacos namespace one and Nacos namespace two, which will be created later.
Start the Nacos server, enter the Nacos console, switch to the Namespace: dev interface, and create a new configuration file
label | value |
---|---|
DataId | nacos-namespace-one-dev.yml |
Group | namespace-one |
Configuration format | YAML |
Configuration content:
nacos: config: Project: nacos-namespace-one,Namespace: dev
Continue creating new profile
label | value |
---|---|
DataId | nacos-namespace-two-dev.yml |
Group | namespace-two |
Configuration format | YAML |
Configuration content:
nacos: config: Project: nacos-namespace-two,Namespace: dev
Check whether the DataId is correct, whether the group and configuration content match the environment
3.2. test environment configuration creation
Create a new configuration file under Namespace
Here, it is assumed that there are two projects, namely, Nacos namespace one and Nacos namespace two, which will be created later.
Start the Nacos server, enter the Nacos console, switch to the Namespace: test interface, and create a new configuration file
label | value |
---|---|
DataId | nacos-namespace-one-test.yml |
Group | namespace-one |
Configuration format | YAML |
Configuration content:
nacos: config: Project: nacos-namespace-one,Namespace: test
Continue creating new profile
label | value |
---|---|
DataId | nacos-namespace-two-test.yml |
Group | namespace-two |
Configuration format | YAML |
Configuration content:
nacos: config: Project: nacos-namespace-two,Namespace: test
4, Code coding practice
4.1. Create 2 project
Nacos namespace one and Nacos namespace two
4.2. Introduce dependency
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--Service registration discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--spring-cloud-alibaba version control --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
4.3. Create test class
@RestController @RefreshScope public class NaocsController { @Value("${nacos.config}") private String nacosConfig; @GetMapping("/nacosConfig") public String nacosConfig(){ return nacosConfig; } }
4.4. Create dev environment configuration file
Configuration Description: by default, the bootstrap of the dev environment is configured for the Nacos namespace one project and the Nacos namespace two project YML, after the project is started successfully, the test environment configuration file bootstrap YML content overwrites the configuration of the dev environment, because the dev environment and the test environment only have different environment configurations, but they belong to the same project. Four services will be started later
They are 9911 of the Nacos namespace one project, 9912 of the Nacos namespace one project, 9921 of the Nacos namespace two project and 9922 of the Nacos namespace two project
The Nacos namespace one project creates the dev environment configuration file bootstrap yml
dev
server: port: 9911 spring: application: name: nacos-namespace-one profiles: active: dev cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: bb5689cc-7613-4e61-a05f-a750371fe46f group: namespace-one config: server-addr: 127.0.0.1:8848 prefix: ${spring.application.name} file-extension: yml namespace: bb5689cc-7613-4e61-a05f-a750371fe46f group: namespace-one
The Nacos namespace one project creates the test environment configuration file bootstrap yml
test
server: port: 9912 spring: application: name: nacos-namespace-one profiles: active: test cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: 2b3082ac-ecbe-4ad8-b122-d5f92fa6e1eb group: namespace-one config: server-addr: 127.0.0.1:8848 prefix: ${spring.application.name} file-extension: yml namespace: 2b3082ac-ecbe-4ad8-b122-d5f92fa6e1eb group: namespace-one
The Nacos namespace two project creates the dev environment configuration file bootstrap yml
dev
server: port: 9921 spring: application: name: nacos-namespace-two profiles: active: dev cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: bb5689cc-7613-4e61-a05f-a750371fe46f group: namespace-two config: server-addr: 127.0.0.1:8848 prefix: ${spring.application.name} file-extension: yml namespace: bb5689cc-7613-4e61-a05f-a750371fe46f group: namespace-two
The Nacos namespace two project creates the test environment configuration file bootstrap yml
test
server: port: 9922 spring: application: name: nacos-namespace-two profiles: active: test cloud: nacos: discovery: server-addr: 127.0.0.1:8848 namespace: 2b3082ac-ecbe-4ad8-b122-d5f92fa6e1eb group: namespace-two config: server-addr: 127.0.0.1:8848 prefix: ${spring.application.name} file-extension: yml namespace: 2b3082ac-ecbe-4ad8-b122-d5f92fa6e1eb group: namespace-two
4.5. Start services separately
Start 4 services
They are 9911 of the Nacos namespace one project, 9912 of the Nacos namespace one project, 9921 of the Nacos namespace two project and 9922 of the Nacos namespace two project
Tips for starting services on different ports in the same project
This option needs to be checked
4.6. Test verification
Access the interface to obtain the configuration information and verify whether the corresponding environment configuration can be read
http://localhost:9911/nacosConfig http://localhost:9912/nacosConfig http://localhost:9921/nacosConfig http://localhost:9922/nacosConfig
Summary: isolate different environments through namespaces (dev\test), and manage different projects through groups in specific environment namespaces