1. Add page configuration information
1.1 create a new namespace
Enter the nacos console page – > namespace – > new namespace – > fill in the namespace ID, name and description, and click OK to save
1.2 adding configuration information
Enter the nacos console page – > configuration list – > select the namespace we need – > click the "+" button to add – > fill in the configuration information – > publish
Example of configuration information filling:
Data ID
Configuration set ID is one of the dimensions of organization partition configuration. Data ID is usually used to organize the configuration set of the partition system. A system or application can contain multiple configuration sets, and each configuration set can be identified by a meaningful name.
In the Nacos Spring Cloud, the complete format of dataId is as follows:
${prefix}-${spring.profiles.active}.${file-extension}
- Prefix defaults to the value of spring.application.name, which can also be configured through the configuration item spring.cloud.nacos.config.prefix.
- Spring.profiles.active is the profile corresponding to the current environment. For details, please refer to the Spring Boot document. Note: when spring.profiles.active is empty, the corresponding connector - will not exist, and the splicing format of dataId will become p r e f i x . {prefix}. prefix.{file-extension}
- File exception is the data format of the configuration content, which can be configured through the configuration item spring.cloud.nacos.config.file extension. Currently, only properties and yaml types are supported.
Group
Configuration grouping is one of the dimensions of organization configuration. Configuration sets are grouped by a meaningful string, such as Buy or Trade, to distinguish configuration sets with the same Data ID. When creating a configuration on Nacos, if the name of the configuration group is not filled in, the name of the configuration group adopts default by DEFAULT_GROUP. Common scenarios for configuration grouping: different applications or components use the same configuration type, such as database_url configuration and MQ_topic configuration.
Configuration format and configuration content
Configuration format: configure the file format of the content
Configuration content: configuration information that needs dynamic configuration
2 Dependence
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.1.RELEASE</version> </dependency>
Note: version 2.1.x.RELEASE corresponds to spring boot version 2.1. X. Version 2.0.x.RELEASE corresponds to spring boot version 2.0. X, and version 1.5.x.RELEASE corresponds to spring boot version 1.5. X.
3 configuration information settings
Add the bootstrap.yml configuration file in the resource directory and add the configuration information related to nacos
spring: application: # It is used to combine data IDS, which can not be set name: NacosConfigManage cloud: nacos: config: # Configuration center address server-addr: 127.0.0.1:8848 # Data ID prefix prefix: nacos-config-test # Profile type file-extension: yaml # Configure grouping group: LOCAL_GROUP # Namespace namespace: 4fecf2dc-ef91-4370-ad2f-d77f013ce346
3 test class
package com.test.springbootplus.controller; import io.swagger.annotations.Api; 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.RestController; /** * <pre> * nacos Configure API interface * </pre> * * @author yjj * @since 2021-12-03 */ @RestController // @RefreshScope enables automatic configuration updates @RefreshScope @Api(tags="nacos to configure API") public class NacosConfigController { @Value("${service.enable:true}") private String status; @Value("${service.message:You have turned on configuration}") private String message; @GetMapping("/status") public String getStatus() { return status; } @GetMapping("/message") public String getMessage() { return message; } }
4 access before changing configuration
Call http://127.0.0.1:8089/message
Call http://127.0.0.1:8089/status
4 change configuration
Enter the nacos console page – > configuration list – > select the namespace we need – > click the Edit button in the configuration operation column to be modified – > modify the configuration information in the configuration content – > publish
4 access after configuration change
Call http://127.0.0.1:8089/message
Call http://127.0.0.1:8089/status
You can see that the configuration information has been changed.