Nacos Configuration Center

Previous article: Nacos Server cluster construction

Add configuration set

  • Just create a new one in the nacos configuration center
    Insert image description here
    Insert image description here

Refresh configuration items in real time

Insert image description here

  • rely
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
  • Modify the configuration file application.ymltobootstrap.yaml
  • Add config configuration
    Insert image description here
server:
  port: 8083

spring:
  application:
    name: m-service-resume

  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8 &serverTimezone=UTC
    username: root
    password: root
  jpa:
    database: MySQL
    show-sql: true
    hibernate:
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl  #避免将驼峰命名转换为下划线命名
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
        #集群名称
        cluster-name: BJ
        #所属命名空间
        namespace: 59486577-18d5-459c-94ad-cbdf6f3d9d5a
       #nacos config 配置
      config:
        server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
        namespace: 59486577-18d5-459c-94ad-cbdf6f3d9d5a
        group: DEFAULT_GROUP
        file-extension: yaml



  • Get the configuration config class and add @RefreshScope annotations
@RestController
@RequestMapping("/config")
//实时刷新注解
@RefreshScope 
public class ConfigController {
    
    
    @Value("${my.message}")
    private String myMessage;

    @Value("${mysql.url}")
    private String mysqlUrl;


    @GetMapping("/viewconfig")
    public String viewconfig() {
    
    
        return "msg==>" + myMessage  + " mySqlUrl=>" + mysqlUrl;
    }
}
  • access
    Insert image description here
    • Modify the content in the configuration, 100 is 200
      Insert image description here
    • Real-time refresh completed

Listen for queries

  • nacos configuration center
    Insert image description here
    Insert image description here

dataId extension

Get multiple configuration files from nacos

  • Create multiple configuration files in nacos abc.yaml,def.yaml

Insert image description here
Insert image description here
Insert image description here

  • Get multiple configuration files
    Insert image description here
  cloud:
    nacos:
      discovery:
      config:
        server-addr: 127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850
        namespace: 59486577-18d5-459c-94ad-cbdf6f3d9d5a
        group: DEFAULT_GROUP
        file-extension: yaml
        #获取多个配置文件
        ext-config[0]:
         data-id: abc.yaml
         group: DEFAULT_GROUP
         refresh: true #扩展dataId 的动态刷新
        ext-config[1]:
         data-id: def.yaml
         group: DEFAULT_GROUP
         refresh: true
  • Restart service
    Insert image description here
    Insert image description here
  • Modify the configuration in nacos and revisit the interface (extended configuration file dynamically refreshes)
    Insert image description here
    Insert image description here

dataId priority issue

Priority: dataId generated according to rules > extended dataId (for extended dataId, [n] The larger n, the higher the priority)

  • Copy the configuration m-service-resume.yamlin and change the number to 300, 400my.messageabc.yamldef.yaml
    Insert image description here
    Insert image description here

  • Restart the project, the result
    Insert image description here

  • Priority between extended configuration files, new configuration items
    Insert image description here
    Insert image description here

  • result
    Insert image description here

Guess you like

Origin blog.csdn.net/u014535922/article/details/130644688