springboot集成nacos

springboot集成nacos

  • 1.版本
  • [2. POM依赖](#2. POM依赖)
  • [3. nacos服务](#3. nacos服务)
    • [3.1 下载nacos压缩包](#3.1 下载nacos压缩包)
    • [3.2 启动nacos](#3.2 启动nacos)
  • [4. yaml配置](#4. yaml配置)
  • 5.Demo
    • [5.1 配置中心简单格式获取方式](#5.1 配置中心简单格式获取方式)
    • 普通方式还可以再启动类上添加注解完成
    • [5.2 获取json格式的demo](#5.2 获取json格式的demo)
    • [5.2 自动注册根据yaml配置](#5.2 自动注册根据yaml配置)

1.版本

nacos版本:2.3.2

springboot版本:2.1.3.RELEASE

2. POM依赖

c 复制代码
           <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>nacos-discovery-spring-boot-starter</artifactId>
                <version>0.2.3</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.boot</groupId>
                <artifactId>nacos-config-spring-boot-starter</artifactId>
                <version>0.2.3</version>
            </dependency>

3. nacos服务

3.1 下载nacos压缩包

https://github.com/alibaba/nacos/releases

3.2 启动nacos

c 复制代码
Linux/Unix/Mac
启动命令(standalone代表着单机模式运行,非集群模式):

sh startup.sh -m standalone

如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:

bash startup.sh -m standalone

Windows
启动命令(standalone代表着单机模式运行,非集群模式):

startup.cmd -m standalone

4. yaml配置

c 复制代码
nacos:
  discovery:
    server-addr: 127.0.0.1:8848
    enabled: true
    autoRegister: true

    # 指定本项目的注册地址和端口号 这里配置的数元数据
    register:
      metadata:
        gRPC-port: 19090

5.Demo

5.1 配置中心简单格式获取方式

c 复制代码
  @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;

    @GetMapping("getNacos")
    public Result getNacos() throws NacosException {
        return ResultGenerator.getSuccessResult(useLocalCache);

    }

结果:

普通方式还可以再启动类上添加注解完成

c 复制代码
@NacosPropertySources(value = {
        @NacosPropertySource(dataId = "example", groupId = "TEST_GROUP", autoRefreshed = true),
        @NacosPropertySource(dataId = "example", autoRefreshed = true),
        @NacosPropertySource(dataId = "testList", autoRefreshed = true)

})

5.2 获取json格式的demo

c 复制代码
@Configuration
public class NacosConfig {

    @Value("${nacos.config.server-addr}")
    private String serverAdd;
    @Value("${nacos.config.namespace:}")
    private String namespace;

    @Bean
    public ConfigService configService() throws NacosException {
        final Properties properties = new Properties();
        //设置Nacos节点对应的IP地址
        properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverAdd);
        //设置命名空间
        properties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
        //如果开启了Nacos权限校验,设置用户名
//        properties.setProperty(PropertyKeyConst.USERNAME,"nacos");
//        properties.setProperty(PropertyKeyConst.PASSWORD,"nacos");
        //设置获取配置信息的轮询超时时间
        properties.setProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT, "3000");
        //设置获取配置信息失败后的重试次数
        properties.setProperty(PropertyKeyConst.CONFIG_RETRY_TIME, "5");
        //设置是否开启客户端主动拉取最新的配置信息
        properties.setProperty(PropertyKeyConst.MAX_RETRY, "5");
        //构造一个ConfigService实例
        ConfigService configService = NacosFactory.createConfigService(properties);
        return configService;

    }


}




@RestController
@RequestMapping("test")
@Slf4j
public class TestController {
   @Autowired
    private ConfigService configService;

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

    @GetMapping("getNacos")
    public Result getNacos() throws NacosException {
        String config = configService.getConfig("testjson", "DEFAULT_GROUP", 3000);
        getConfig();
        return ResultGenerator.getSuccessResult(useLocalCache);

    }
   }

结果:

5.2 自动注册根据yaml配置

enabled: true
autoRegister: true

当然可以在启动类上添加注解

@EnableNacosDiscovery

相关推荐
litGrey12 分钟前
Maven国内镜像(四种)
java·数据库·maven
ac-er888834 分钟前
在Flask中处理后台任务
后端·python·flask
丶白泽38 分钟前
重修设计模式-结构型-桥接模式
java·设计模式·桥接模式
ac-er888840 分钟前
Flask中的钩子函数
后端·python·flask
o独酌o44 分钟前
递归的‘浅’理解
java·开发语言
无问8171 小时前
数据结构-排序(冒泡,选择,插入,希尔,快排,归并,堆排)
java·数据结构·排序算法
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS在线文档管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Flying_Fish_roe2 小时前
Spring Boot-版本兼容性问题
java·spring boot·后端
程序猿进阶2 小时前
如何在 Visual Studio Code 中反编译具有正确行号的 Java 类?
java·ide·vscode·算法·面试·职场和发展·架构
slandarer2 小时前
MATLAB | R2024b更新了哪些好玩的东西?
java·数据结构·matlab