Nacos作为配置中心,如何加载和读取配置

启动文件  bootstrap.properties

spring.application.name=andon-basic-dev
spring.cloud.nacos.discovery.server-addr=192.168.1.127:8848
spring.cloud.nacos.config.server-addr=192.168.1.127:8848
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.group=ANDON_GROUP


nacos对应的配置文件 andon-basic-dev.yaml

server:
  port: 9876

spring:
  activiti:
    database-schema-update: true
    check-process-definitions: false
  datasource:
    primary:
      driver-class-name: oracle.jdbc.driver.OracleDriver
      jdbc-url: jdbc:oracle:thin:@192.168.1.221:1521/tzinfo
      url: jdbc:oracle:thin:@192.168.1.221:1521/tzinfo
      username: ANDON
      password: andon
      maximum-pool-size: 5
      max-idle: 5
      max-wait: 10000
      min-idle: 5
      initial-size: 5
      validation-query: SELECT 1
      #ConnectionTestQuery: SELECT 1
      test-on-borrow: false
      auto-commit: true
      test-while-idle: true
      default-auto-commit: true
  rabbitmq:
    host: 192.168.1.41
    port: 5672
    username: konkamesdev
    password: konkamesdev
    virtual-host: konkamesdev
    listener:
      simple:
        acknowledge-mode: auto
        concurrency: 1
        max-concurrency: 5
        prefetch: 1
  messages:
    basename: static.i18n.message
    cache-duration: 3600
    encoding: UTF-8
  redis:
    cluster:
      nodes: 192.168.1.41:7001,192.168.1.41:7004,192.168.1.42:7002,192.168.1.42:7005,192.168.1.43:7003,192.168.1.43:7006
    password:
    timeout: 5000
    redisson:
      cluster:
        nodes: 192.168.1.41:7001,192.168.1.41:7004,192.168.1.42:7002,192.168.1.42:7005,192.168.1.43:7003,192.168.1.43:7006
      password:
      
      
      
      


配置加载 Nacos 配置中心 多个配置文件

bootstrap.properties配置内容

spring.cloud.nacos.config.ext-config[0].data-id=test1.properties
spring.cloud.nacos.config.ext-config[0].group=ANDON_GROUP
spring.cloud.nacos.config.ext-config[0].refresh=true

spring.cloud.nacos.config.ext-config[1].data-id=test2.properties
spring.cloud.nacos.config.ext-config[1].group=ANDON_GROUP
spring.cloud.nacos.config.ext-config[1].refresh=true


代码中如何读取配置
@Value("${configName}")
private String configName;


nacos上 test1.properties文件的内容
configName=张三


参考文档
1、https://blog.csdn.net/Drug_/article/details/122397268
2、https://my.oschina.net/u/4269090/blog/3329087
3、https://blog.csdn.net/apple_52109766/article/details/126669160

如何加载不同环境下的nacos配置

bootstrap.properties

spring.application.name=andon-basic-${spring.profiles.active}
spring.cloud.nacos.discovery.server-addr=${server.addr}
spring.cloud.nacos.config.server-addr=${server.addr}
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.group=ANDON_GROUP

${spring.profiles.active}  加载不同的配置文件(dev环境可能每个人都有自己的配置,所以才要做成变量)
${server.addr}  这个是nocos每个环境的地址(dev、测试环境、生产环境地址不同)

你可能感兴趣的:(java,开发语言)