nacos-spring-boot配置中心使用手册

目录

[TOC]

1.nacos 服务器

服务地址:127.0.0.1:8848

账号密码:nacos / nacos

服务器版本:1.3.1官方推荐稳定版本

数据:采用内嵌式数据库持久化

2.spring-boot-nacos配置详细解释

pom依赖


        
            com.alibaba.boot
            nacos-config-spring-boot-starter
            0.2.4
        

2.1主配置

# 开启配置预加载功能
nacos.config.bootstrap.enable=true
nacos.config.bootstrap.log-enable=true
# 开启配置自动刷新
nacos.config.auto-refresh=true
# 主配置 开启注册监听器预加载配置服务(除非特殊业务需求,否则不推荐打开该参数)
nacos.config.enable-remote-sync-config=true

data-id配置
  • data-ids配置,可以配置多个,排在前面的优先级最高,比如data-id为
    example的配置中有 server.port=5081,application.properties的配置中有server.port=8080,生效的是5081,因为example排在前面
  • data-id配置一个
nacos.config.data-ids=example,application.properties
nacos.config.data-id=application.properties
配置文件类型,枚举值ConfigType
nacos.config.type=properties

配置组,如果不配置nacos会默认为【DEFAULT_GROUP】,单体应用建议不配置用默认,配置需要分组用data-id进行区分
nacos.config.group=order-service
address与命名空间,可以用nacos 地址与 namespace 来区分环境
nacos.config.server-addr=192.168.16.104:8848
nacos.config.namespace=demo-dev

2.2扩展ext-config

ext-config[index] 的优先级,index越小,优先级越高,从0开始
  • 当需要用多个group 时,可以开启ext-config进行扩展,如果需要配置多个
    data-id,可以直接在data-ids后面追加,以逗号隔开
nacos.config.ext-config[0].data-id=order.properties
nacos.config.ext-config[0].group=DEFAULT_GROUP
nacos.config.ext-config[0].max-retry=10
nacos.config.ext-config[0].type=yaml
nacos.config.ext-config[0].auto-refresh=true
nacos.config.ext-config[0].enable-remote-sync-config=true

2.3查看配置加载顺序

  • springboot 代码
@SpringBootApplication
public class NacosDemoApplication {
    ConfigurableApplicationContext run = SpringApplication.run(NacosDemoApplication.class, args);
        // 查看 Environment
        Environment environment = run.getEnvironment();
        //断点调试查看environment对象
        System.out.println(environment);
}

  • 对象environment 的PropertySource、PropertySourceList属性
  [MapPropertySource {name='server.ports'}, ConfigurationPropertySourcesPropertySource {name='configurationProperties'}, StubPropertySource {name='servletConfigInitParams'}, ServletContextPropertySource {name='servletContextInitParams'}, PropertiesPropertySource {name='systemProperties'}, OriginAwareSystemEnvironmentPropertySource {name='systemEnvironment'}, RandomValuePropertySource {name='random'}, OriginTrackedMapPropertySource {name='applicationConfig: [classpath:/application-dev.properties]'}, OriginTrackedMapPropertySource {name='applicationConfig: [classpath:/application.properties]'}, NacosPropertySource {name='example|DEFAULT_GROUP|demo-dev||129.28.119.76:8848|||||||'}, NacosPropertySource {name='application.properties|DEFAULT_GROUP|demo-dev||129.28.119.76:8848|||||||'}]

application.properties配置文件如下
nacos.config.data-ids=example,application.properties
PropertySourceList属性显示 的name = example的配置对象排序靠前,如果nacos上的example文件与application.properties文件有相同属性,以example上的值生效

3.spring-boot中使用

3.1 单个配置使用
@NacosValue(value = "${useLocalCache1:k1}", autoRefreshed = true)
private String testtext1;

@NacosValue 注解0.4.2版本不支持指定data-id,如果需要新建data-id进行分组,需要在application.properties 主文件中 nacos.config.data-ids=example,application.properties 中追加
如:nacos.config.data-ids=example,application.properties,newDataId ,其中newDataId为你新增的data-id名称

在 nacos的后台新增配置文件,data-id 为newDataId,此文件中可以配置新增的配置内容

3.2 一组配置使用
/**
 * 0.4.2版本
 * @prefix 配置前缀,prefix + 属性名 = 配置名,如:order.payTimeoutSeconds=10
 * @dataId 配置id(必填),如果保持与主配置一样,也可以写el表达式 如:${nacos.config.data-id}
 * @type 配置文件类型,ConfigType枚举值,默认是ConfigType.PROPERTIES
 * @autoRefreshed 自动刷新配置,一般需要加上,因为默认是false
 * @groupId 分组id ,如果不填写,则nacos会默认 DEFAULT_GROUP,单体建议不配置,使用默认
 */
@Configuration
@NacosConfigurationProperties(prefix = "order", dataId = "example",groupId = "ORDER-SERVICE",type = ConfigType.PROPERTIES,autoRefreshed = true)
public class OrderProperties {
    private Integer payTimeoutSeconds;
    private Integer name;   
    public Integer getPayTimeoutSeconds() {
        return payTimeoutSeconds;
    }
    public void setPayTimeoutSeconds(Integer payTimeoutSeconds) {
        this.payTimeoutSeconds = payTimeoutSeconds;
    }
    public Integer getName() {
        return name;
    }
    public void setName(Integer name) {
        this.name = name;
    }
}
  • 也可以使用 spring-boot的注解 @ConfigurationProperties ,但是无法接收到nacos的配置更新,想要自动更新需要实现 nacos 客户端监听器自行实现
  • 也可以使用 spring-boot 的注解 @Value 替代@NacosValue 注解,同意是无法实现nacos自动更新,需要实现 nacos客户端件铜器自行实现

你可能感兴趣的:(nacos-spring-boot配置中心使用手册)