spring-boot以及spring-cloud接入nacos管理配置方案

时间:2024-04-09 06:58:12

应用接入nacos方案:

  • SpringBoot接入nacos(nacos-config-spring-boot-starter0.2.4版本以下
  1. 引入依赖

<dependency>
   <groupId>
com.alibaba.boot</groupId>
   <artifactId>
nacos-config-spring-boot-starter</artifactId>
   <version>
0.2.2</version>
</dependency>
<dependency>
   <groupId>
com.alibaba.nacos</groupId>
   <artifactId>
nacos-spring-context</artifactId>
   <version>
0.2.2-RC1</version>
</dependency>

 

  1. 启动类添加注解

@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "${spring.cloud.nacos.config.server-addr}",namespace = "${spring.cloud.nacos.config.namespace}"))
@NacosPropertySource(dataId = "",autoRefreshed = true)

  1. 启动类添加静态代码块

static {
   
try {
        Properties properties =
new Properties();
       
properties.load(OrderApplication.class.getClassLoader().getResourceAsStream("bootstrap.properties"));
       
String groupId = properties.getProperty("spring.cloud.nacos.config.group");
       
String dataId = properties.getProperty("spring.application.name");
       
NacosPropertySource annotation = OrderApplication.class.getAnnotation(NacosPropertySource.class);
       
InvocationHandler ih = Proxy.getInvocationHandler(annotation);
       
Field memberValuesField = ih.getClass().getDeclaredField("memberValues");
       
memberValuesField.setAccessible(true);
       
Map map = (Map) memberValuesField.get(ih);
       
map.put("groupId", groupId);
       
map.put("dataId", dataId);
   
} catch (Exception ex) {
        ex.printStackTrace()
;
   
}
}

样例如下:

spring-boot以及spring-cloud接入nacos管理配置方案

 

  1. bootstrap.properties配置文件添加配置
spring.cloud.nacos.config.server-addr=xxx.xxx.xxx.xxx:xxxx

spring.cloud.nacos.discovery.server-addr=xxx.xxx.xxx.xxx:xxxx

spring.cloud.nacos.config.namespace=dev

spring.application.name=test.properties

spring.cloud.nacos.config.group=ceshi

其中spring.cloud.nacos.config.server-addr为nacos访问地址

        spring.cloud.nacos.config.namespace为配置环境

        spring.cloud.nacos.config.group 为groupId

        spring.application.name为dataId

 

5、获取配置

              @NacosValue(value = "${test1:123}", autoRefreshed = true)

       autoRefreshed可指定字段是否热更新,默认为false 如需动态更新必须添加为true

       value中为配置项 :后可指定初始默认值

              @Value("${test1}") 不支持热更行

  • SpringBoot接入nacos(nacos-config-spring-boot-starter0.2.4版本以下)推荐使用
  1. 引入依赖

<dependency>
    <groupId>
com.alibaba.boot</groupId>
    <artifactId>
nacos-config-spring-boot-starter</artifactId>
    <version>
0.2.4</version>
</dependency>

  1. application.properties文件添加配置

nacos.config.bootstrap.enable=true
nacos.config.server-addr
=xxx.xxx.xxx.xxx:xxxx
nacos.config.namespace=dev
nacos.config.data-id=test.properties
nacos.config.group=ceshi
nacos.config.auto-refresh=true

3、获取配置

              @NacosValue(value = "${test1:123}", autoRefreshed = true)

       autoRefreshed可指定字段是否热更新,默认为false 如需动态更新必须添加为true

       value中为配置项 :后可指定初始默认值

              @Value("${test1}") 不支持热更行

 

三、SpringCloud接入nacos

       1、引入依赖

<dependency>

   <groupId>org.springframework.cloud</groupId>

   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>

   <version>0.2.2.RELEASE</version>

</dependency>

 

      

  1. bootstrap.properties配置文件添加配置

spring.cloud.nacos.discovery.server-addr=xxx.xxx.xxx.xxx:xxxx
spring.cloud.nacos.config.server-addr=xxx.xxx.xxx.xxx:xxxx
spring.cloud.nacos.config.namespace=dev
spring.application.name=example
spring.cloud.nacos.config.group=0.0.1
spring.cloud.nacos.config.file-extension=properties

 

其中spring.cloud.nacos.config.server-addr为nacos访问地址

              spring.cloud.nacos.config.namespace为配置环境

              spring.application.name为dataId

              spring.cloud.nacos.config.group为groupId

              spring.cloud.nacos.config.file-extension固定为properties

             

3、获取配置

              @NacosValue(value = "${test1:123}", autoRefreshed = true)

       autoRefreshed可指定字段是否热更新,默认为false 如需动态更新必须添加为true

       value中为配置项 :后可指定初始默认值

              @Value("${test1}") 原生不支持热更行  若想支持热更新可在类上加@RefreshScope注解才可