美文网首页
nacos-spring-boot配置中心使用手册

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

作者: 翔哥不哭 | 来源:发表于2020-12-24 16:26 被阅读0次

目录

[TOC]

1.nacos 服务器

服务地址:127.0.0.1:8848

账号密码:nacos / nacos

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

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

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

pom依赖

<!-- 实现对 Nacos 作为配置中心的自动化配置 -->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.4</version>
        </dependency>

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配置中心使用手册

    目录 [TOC] 1.nacos 服务器 服务地址:127.0.0.1:8848账号密码:nacos / naco...

  • conda

    conda 使用手册1conda 使用手册2conda 环境变量配置 删除虚拟环境:删除环境:使用命令conda ...

  • [全] Appium使用手册

    Appium使用手册 Appium介绍 Appium环境安装配置 Appium Api Appium键盘事件 An...

  • 包的管理-软件列表

    gem使用手册npm使用手册yarn使用手册pip-使用手册maven-使用手册composer-使用手册 cho...

  • 抓包神器之Charles配置和使用手册

    抓包神器之--Charles配置和使用手册 天降大任 charles简介 Charles[http://www.c...

  • apollo+nacos 总结

    配置中心 配置中心简介 说到配置中心, 大家可能都不陌生。我们携程现在用的qconfig, 就是一个典型的配置中心...

  • Nacos注册中心之概要设计

    前言 在之前的文章中分析了Nacos配置中心,配置中心的核心是配置的创建、读取、推送。 注册中心的核心比配置中心多...

  • SpringCloud-Spring Cloud Config分

    分布式配置中心可以理解成 注册中心+配置中心的整合。具体实现思路:搭建一个注册中心,在注册中心的基础上添加配置中心...

  • macPro必备软件——虚拟机

    virtualbox 优点: 免费 缺点: 配置相对麻烦,bug比较多,速度比较慢 安装官网参照使用手册 para...

  • git常用命令

    获取Git的升级 列出所有能找到的配置 检查某一项配置 获取Git命令的使用手册 在现有目录中初始化仓库 克隆现有...

网友评论

      本文标题:nacos-spring-boot配置中心使用手册

      本文链接:https://www.haomeiwen.com/subject/kybmnktx.html