nacos-springboot配置中心使用

爱被打了一巴掌 2022-03-18 15:59 387阅读 0赞
先启动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配置:


    2.0.3.RELEASE
    0.2.1
    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-web</artifactId>
    5. <version>${spring-boot.version}</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>org.springframework.boot</groupId>
    9. <artifactId>spring-boot-starter-actuator</artifactId>
    10. <version>${spring-boot.version}</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>com.alibaba.boot</groupId>
    14. <artifactId>nacos-config-spring-boot-starter</artifactId>
    15. <version>${nacos-config-spring-boot.version}</version>
    16. </dependency>
    17. <dependency>
    18. <groupId>com.alibaba.boot</groupId>
    19. <artifactId>nacos-config-spring-boot-actuator</artifactId>
    20. <version>${nacos-config-spring-boot.version}</version>
    21. </dependency>
    22. </dependencies>
  • 启动主类:NacosConfigApplication.java

    @SpringBootApplication
    @NacosPropertySource(dataId = “example”, autoRefreshed = true)
    public class NacosConfigApplication {

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

    }

  • 测试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 {

    1. private static final Logger log = LoggerFactory.getLogger(ConfigController.class);
    2. @NacosInjected
    3. ConfigService configService;
    4. @NacosValue(value = "${useLocalCache:xxx}", autoRefreshed = true)
    5. private String useLocalCache;
    6. @RequestMapping(value = "/get", method = GET)
    7. @ResponseBody
    8. public String get() {
    9. // http://localhost:8080/config/get
    10. return useLocalCache;
    11. }
    12. @RequestMapping(value = "/publish", method = GET)
    13. @ResponseBody
    14. public String doPublish() {
    15. // http://localhost:8080/config/publish
    16. try {
    17. log.info("原始值{}", configService.getConfig("example", "DEFAULT_GROUP", 20000));
    18. configService.publishConfig("example", "DEFAULT_GROUP", "useLocalCache=api更改");
    19. log.info("更改后的值{}", configService.getConfig("example", "DEFAULT_GROUP", 20000));
    20. return "SUCESS";
    21. } catch (NacosException e) {
    22. e.printStackTrace();
    23. return "FALSE";
    24. }
    25. }
  • 此时我们访问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,刷新控制台,可以看到控制台的值已经变成如下,已经实时更改
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,387人围观)

还没有评论,来说两句吧...

相关阅读