当前位置:网站首页>Manage configuration using Nacos

Manage configuration using Nacos

2022-07-06 06:14:00 Snow peak expensive


One 、 Plus dependence

<dependency>
   <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

Two 、 Appointment

The two ends of the arrow connection should be consistent , There is a bootstrap.yml, Corresponding Nacos Configuration of multiple specified environments on . Specify when the microservice starts active, Will go Nacos Find the configuration of the corresponding environment .
 Insert picture description here

3、 ... and 、bootstrap.yml

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
  application:
    name: content-center
  profiles:
    active: dev

Four 、 Add configuration  Insert picture description here

At this point, this configuration has been configured to the development environment

5、 ... and 、 Business code fetching configuration

@Value("${your.config}")
private String config;

6、 ... and 、@RefreshScope

Annotate the class that gets the configuration , Can dynamically obtain Nacos To configure .

7、 ... and 、 Rollback configuration

 Insert picture description here

8、 ... and 、 A general configuration of microservices

1. The general configuration of all environments is written in :{ Microservice name }.yaml in

2.yaml The priority of the :

Appoint profile Of yaml > { Microservice name }.yaml

Nine 、 Configuration sharing of different microservices

ext-config The way

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
        ext-config:
          #  Shared DataId,yaml Suffixes must not be less , Only support yaml/properties
          #  More backward , The higher the priority   priority common2.yaml > common1.yaml
          - data-id: common1.yaml
            # common1.yaml Where group
            group: DEFAULT_GROUP
            #  Whether to allow refresh , Default false
            refresh: true
          - data-id: common2.yaml
            group: DEFAULT_GROUP
            refresh: true
  application:
    name: content-center
  profiles:
    active: dev

shared-dataids The way

spring:
  cloud:
    nacos:
      config:
        #  Shared configuration DataId, Multiple uses , Separate 
        #  More backward , The higher the priority ;common2.yml > common1.yaml
        # .yaml Suffixes must not be less , Only support yaml/properties
        shared-dataids: common1.yaml,common2.yaml
        #  Which sharing configurations support dynamic refresh , Multiple uses , Separate 
        refreshable-dataids: common1.yaml
        server-addr: 127.0.0.1:8848
        file-extension: yaml
  application:
    name: content-center
  profiles:
    active: dev

Just copy the code in two ways to bootstrap in , And in Nacos Configure in the corresponding group common1.yaml,common2.yaml that will do .

Ten 、 Remote configuration and local configuration priority

Default Nacos Configuration on >application.yml & bootstrap.yml

Also available at Nacos Modification priority of a configuration file on

 Insert picture description here

原网站

版权声明
本文为[Snow peak expensive]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060607584029.html