nacos配置文件监听

nacos配置文件监听

Java方式配置文件监听

添加依赖


    com.alibaba.nacos
    nacos-client
    1.4.1

添加配置监听代码,按照如下示例添加配置监听代码。用户名、密码为自动生成的当前项目在nacos中的用户名、密码,请妥善保管

/*
* Demo for Nacos
* pom.xml
    
        com.alibaba.nacos
        nacos-client
        ${version}
    
*/
package com.alibaba.nacos.example;

import java.util.Properties;
import java.util.concurrent.Executor;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;

/**
 * Config service example
 *
 * @author Nacos
 *
 */
public class ConfigExample {

  public static void main(String[] args) throws NacosException, InterruptedException {
    String serverAddr = "localhost";
    String namespace = "namespace";
    String dataId = "hrds-qa-default.yaml";
    String group = "dev";
    String username = "username";
    String password = "password";
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
    properties.put(PropertyKeyConst.NAMESPACE, namespace);
    properties.put(PropertyKeyConst.USERNAME, username);
    properties.put(PropertyKeyConst.PASSWORD, password);
    ConfigService configService = NacosFactory.createConfigService(properties);
    String content = configService.getConfig(dataId, group, 5000);
    System.out.println(content);
    configService.addListener(dataId, group, new Listener() {
      @Override
      public void receiveConfigInfo(String configInfo) {
        System.out.println("recieve:" + configInfo);
      }

      @Override
      public Executor getExecutor() {
        return null;
      }
    });

    boolean isPublishOk = configService.publishConfig(dataId, group, "content");
    System.out.println(isPublishOk);

    Thread.sleep(3000);
    content = configService.getConfig(dataId, group, 5000);
    System.out.println(content);

    boolean isRemoveOk = configService.removeConfig(dataId, group);
    System.out.println(isRemoveOk);
    Thread.sleep(3000);

    content = configService.getConfig(dataId, group, 5000);
    System.out.println(content);
    Thread.sleep(300000);

  }
}

SpringBoot方式配置文件监听

添加依赖

将如下依赖添加到配置文件中


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

配置application.properties,添加Nacos server 的地址、命名空间、用户名、密码

nacos.config.server-addr=localhost
nacos.config.namespace=namespace
nacos.config.username=username
nacos.config.password=password

使用 @NacosPropertySource设置监听的配置文件分类及dataId,并开启自动更新

@SpringBootApplication
@NacosPropertySource(groupId = "groupId", dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }
}

通过 Nacos 的 @NacosValue 注解设置属性值

@Controller
@RequestMapping("config")
public class ConfigController {

    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }
}

SpringCloud配置文件监听

添加依赖


    com.alibaba.cloud
    spring-cloud-starter-alibaba-nacos-config
    2.2.5.RELEASE

在 bootstrap.properties 中配置 Nacos server 的地址、文件格式、分组、命名空间、用户名、密码

spring.cloud.nacos.config.server-addr=172.23.16.106:8848
spring.cloud.nacos.config.namespace=hzero-hrds
spring.cloud.nacos.config.group=dev
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.username=username
spring.cloud.nacos.config.password=password

说明: 在 Nacos Spring Cloud 中,dataId 的完整格式如下: ${prefix}-${spring.profiles.active}.${file-extension}

  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置

  • spring.profiles.active 即为当前环境对应的 profile。当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 p r e f i x . {prefix}. prefix.{file-extension}

  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型

通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @RequestMapping("/get")
    public boolean get() {
        return useLocalCache;
    }
}

你可能感兴趣的:(nacos,nacos)