Nacos Server configures YAML files based on Data IDS
Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Configure YAML files based on Data IDS
Spring Cloud Alibaba Nacos Config matches Data ID when loading configuration from Nacos Config Server. In the implementation of Spring Cloud Nacos, The default Data ID rule is prefix−{prefix}-prefix−{spring.profile.active}.${file-extension}.
- The default is to load the Data ID on the Nacos server to Spring. The application, the name {spring. The application, the name}. Spring. The application of the name. {file – the extension: the properties} for prefix based configuration, In the bootstrap.yml file, the property spring.application.name=spring-cloud-nacos-config, Without specifying spring. Cloud. Nacos. Config. The prefix for the specified Data ID prefix, The configuration file whose Data ID is Spring-cloud-nacos-config. properties in Nacos Config Server is read by default.
- If specified spring. Cloud. Nacos. Config. The prefix = server1, will load the Data ID for server1 configuration file
- Spring.profile. active: Supports multiple environments
To use yamL files, the following steps are required:
-
In the bootstrap. Yml specified in the spring. Cloud. Nacos. Config. The file – the extension = yaml
-
Add configuration to the Nacos console
Data ID: server1. Yaml
Group: DEFAULT_GROUP
Configuration format: YAML
Info: yML config content
Switch configurations in different environments
In Spring Boot, different environment configuration switch is implemented based on spring.profiles. Active. Multi-environment configuration steps are supported based on Spring Boot project:
Create different configurations for different environments in the Resource directory:
- application-dev.properties
- application-test.properties
- application-prod.properties
Define an application.properties configuration file that determines which environment is currently configured by specifying spring.profiles.active= env, if {env} determines which environment is currently configured, and if env determines which environment is currently configured. If the value of {env} is prod, it indicates that application-prod.properties is used. VM options= -dspring.profiles. active=prod can also be set to specify the environment configuration to be used.