nacos-springboot配置中心使用

启动nacos服务
案例如下:
  • 结合springboot,application.properties配置:
nacos.config.server-addr=127.0.0.1:8848

 endpoint http://localhost:8080/actuator/nacos-config
 health http://localhost:8080/actuator/health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
  • pom配置:
 <properties>
        <spring-boot.version>2.0.3.RELEASE</spring-boot.version>
        <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-actuator</artifactId>
            <version>${nacos-config-spring-boot.version}</version>
        </dependency>
       
    </dependencies>
  • 启动主类:NacosConfigApplication.java
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApplication.class, args);
    }
}
  • 测试demo:ConfigController.java

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

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

    private static final Logger log = LoggerFactory.getLogger(ConfigController.class);

    @NacosInjected
    ConfigService configService;

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

    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public String get() {
    // http://localhost:8080/config/get
        return useLocalCache;
    }

    @RequestMapping(value = "/publish", method = GET)
    @ResponseBody
    public String doPublish() {
    // http://localhost:8080/config/publish
        try {
            log.info("原始值{}", configService.getConfig("example", "DEFAULT_GROUP", 20000));
            configService.publishConfig("example", "DEFAULT_GROUP", "useLocalCache=api更改");
            log.info("更改后的值{}", configService.getConfig("example", "DEFAULT_GROUP", 20000));

            return "SUCESS";
        } catch (NacosException e) {
            e.printStackTrace();
            return "FALSE";
        }
    }
  • 此时我们访问http://localhost:8080/config/get,得到的值是默认值:xxx
  • 自定义配置,登陆nacos控制台:http://127.0.0.1:8848/nacos/
    在配置列表中,打开dateId为example的配置,编辑。如果没有这个dateId,就点击“+”,自行添加
    在这里插入图片描述
    添加配置:
    useLocalCache=ssss
    在这里插入图片描述
    再次访问http://localhost:8080/config/get,得到结果:ssss,配置实时生效。
使用ConfigService,改变控制台的值

代码如上,思路就是使用NacosInjected注解,获取ConfigService,然后利用api更改nacos配置
访问http://localhost:8080/config/publish,刷新控制台,可以看到控制台的值已经变成如下,已经实时更改
在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EmineWang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值