Nacos初探

一:Nacos概念

       Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。

       Nacos文档:https://nacos.io/zh-cn/docs/what-is-nacos.html

       Nacos默认用户名和密码:nacos

       

      Nacos支持数据格式

      

二:Springboot+Nacos使用

     假定Nacos配置redis信息,动态获取。

     

     新建一个Springboot项目,动态获取配置

     需要添加pom依赖

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

 

     

@NacosPropertySource(dataId = "example", groupId = "DEFAULT_GROUP", autoRefreshed = true)
@RestController
public class NacosController {

    @NacosValue(value = "${redisIp:default ip }", autoRefreshed = true)
    private String redisIp;

    @NacosValue(value = "${redisHost:default host }", autoRefreshed = true)
    private String redisHost;

    @GetMapping("getinfo")
    public String getInfo() {
        return redisIp +":"+ redisHost;
    }
}
View Code

     启动项目,浏览器访问:localhost:8080/getinfo

     结果为:localhost:6379

     改变Nacos中redis的配置信息

     

    浏览器访问:localhost:8080/getinfo

    结果为127.0.0.1:6379

三:Nacos的SDK使用

      

public class NacosClientDemo {
    public static void main(String[] args) throws NacosException {
        Properties properties = new Properties();
        properties.put("serverAddr","localhost:8848");
        ConfigService configService = NacosFactory.createConfigService(properties);
        String config = configService.getConfig("example", "DEFAULT_GROUP", 10);
        System.out.println(config);
        //监听
        configService.addListener("example", "DEFAULT_GROUP", new Listener() {
            @Override
            public Executor getExecutor() {
                return null;
            }
            @Override
            public void receiveConfigInfo(String configInfo) {

            }
        });
    }
}
View Code

 

 

 

posted @ 2019-08-24 10:59  Don'tYouSee  阅读(573)  评论(0编辑  收藏  举报