The spring boot project uses the nacos configuration center

1. Start nacos

2. Spring boot adds dependencies

com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config

3. Add the bootstrap.yml file under the resources folder and add configuration items

spring:
application:
name: service-name
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
namespace: 888ac051-ae5f-44f1-940a-30c7824a0e91
file-extension: yaml
group: DEFAULT_GROUP_DEV
profiles:
active: business,environment

4. Create a new configuration in nacos, and manage the Data ID here with a certain format, which needs to correspond to the configuration in the project.

The full format of Nacos is:

${
    
    prefix}-${
    
    spring.profile.active}.${
    
    file-extension}

prefix defaults to the value of spring.application.name, and can also be configured through the configuration item spring.cloud.nacos.config.prefix.
spring.profile.active is the profile corresponding to the current environment. For details, please refer to the Spring Boot documentation. Note: When spring.profile.active is empty, the corresponding connector - will also not exist, and the splicing format of dataId becomes

${
    
    prefix}.${
    
    file-extension}

file-exetension 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.
Ps: The comma-separated description in profiles.active means that this service has two configurations.
insert image description here

5. Realize dynamic refresh configuration center. That is, when the configuration center modifies the value, the value in the project will also be modified synchronously.

You need to add the @RefreshScope annotation on the class.
insert image description here

6. Configuration priority issues

When the project starts, it will go to the configuration center to read the configuration information (the local configuration file can still be used, but the priority is lower than the configuration in the configuration center)

7. Pull nacos configuration

Spring Cloud Alibaba Nacos Config currently provides three configuration capabilities to pull related configurations from Nacos.
• A: Support multiple shared Data Id configurations through spring.cloud.nacos.config.shared-configs[n].data-id
• B: Through spring.cloud.nacos.config.extension-configs[n].data The -id method supports the configuration of multiple extended Data Ids
• C: Through internal related rules (spring.cloud.nacos.config.prefix, spring.cloud.nacos.config.file-extension, spring.cloud.nacos.config. group) to automatically generate related Data Id configurations
When the three methods are used together, one of their priority relationships is: A < B < C
Shared configuration pitfall : Shared configuration is not recommended to use shared-dataids, which has been discarded in nacos , when using group in this process, shared-dataids cannot use group, the default default_group

8. Possible errors: Could not resolve placeholder

Reason 1: In yaml, if the value is empty, it must be written as ''.
Reason 2: No dependencies were introduced.

Guess you like

Origin blog.csdn.net/caixinyi1/article/details/106838817