springboot集成nacos

1.版本

nacos版本:2.3.2
springboot版本:2.1.3.RELEASE

2. POM依赖

           <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

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

sh startup.sh -m standalone

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

bash startup.sh -m standalone

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

startup.cmd -m standalone

4. yaml配置

nacos:
  discovery:
    server-addr: 127.0.0.1:8848
    enabled: true
    autoRegister: true

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

5.Demo

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

在这里插入图片描述

在这里插入图片描述

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

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

    }

结果:
在这里插入图片描述
在这里插入图片描述

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

@NacosPropertySources(value = {
        @NacosPropertySource(dataId = "example", groupId = "TEST_GROUP", autoRefreshed = true),
        @NacosPropertySource(dataId = "example", autoRefreshed = true),
        @NacosPropertySource(dataId = "testList", autoRefreshed = true)

})

5.2 获取json格式的demo

@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
在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot可以通过集成Nacos来实现配置管理和服务发现的功能。 首先,通过集成Nacos的配置管理功能,SpringBoot可以从Nacos中获取配置项的值。Nacos提供了动态配置的能力,可以在Nacos中添加多个配置集,每个配置集下可以有多个配置项。当Nacos中的配置项发生修改时,SpringBoot可以及时获取到最新的配置值。这样,在开发和部署过程中不需要重新打包部署,可以动态更新配置,提高了系统的灵活性和可维护性。你可以在中找到更多关于Spring BootNacos的新功能使用手册。 其次,通过集成Nacos的服务发现功能,SpringBoot可以将自己注册到Nacos服务器,并从Nacos中获取其他服务的信息。Nacos作为服务发现的中心,可以管理和监控所有注册在其上的服务实例。SpringBoot可以使用Nacos提供的服务发现功能,实现服务之间的调用和负载均衡。你可以在中找到关于Spring Boot集成Nacos服务发现的具体配置和使用方式。 要完成SpringBoot集成Nacos,你需要在Spring Boot项目的pom.xml文件中添加Nacos相关的依赖。例如,你可以在中找到Nacos的配置依赖和中找到Nacos的服务发现依赖。 总结起来,通过集成NacosSpringBoot可以实现配置管理和服务发现的功能,提高系统的灵活性和可维护性。你可以参考上述提供的链接和依赖来了解更多关于SpringBoot集成Nacos的详细信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值