0
点赞
收藏
分享

微信扫一扫

SpringBoot 06 集成Nacos实现配置中心

慎壹 2022-02-17 阅读 71

1.为什么用Nacos

Nacos 因其出色的的读写性能以及简单灵活的配置方式,被很多公司应用于配置管理.将Nacos当做配置服务器或者配置中心。Nacos是阿里巴巴研发, 社区是中文,学习和问题解决更加便利。Nacos还可以很方便的和Spring Boot集成并支持Spring Cloud,因此现在你还不知道Nacos那么你就Out了

2. Nacos  Server使用

如何搭建Nacos Server环境并启动Nacos Server用于测试, 请进入下方传送门:

链接:Nacos服务设置

访问 http://127.0.0.1:8848/nacos 创建测试所需配置用于下方测试

  1. namespace : nacos-test
  2. Data Id: nacos-demo.yaml
  3. Group: TEST_GROUP

 配置内容如下

useLocalCache: true

3.SpringBoot+Nacos示例

1. 创建Springboot项目,pom.xml引入nacos依赖 nacos-config-spring-boot-starter,参考信息如下

   <properties>
        <java.version>11</java.version>
        <nacos-version>0.2.10</nacos-version>
   </properties>
   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>${nacos-version}</version>
        </dependency>

    </dependencies>

2. applicaiton.properties中添加 nacos配置

server.port=8033

#开启配置预加载功能,默认=false
nacos.config.bootstrap.enable=true
#主配置文件格式
nacos.config.type=yaml
#主配置服务地址
nacos.config.server-addr=127.0.0.1:8848
#主配置命名空间
nacos.config.namespace=nacos-test
#主配置 data-id
nacos.config.data-id=nacos-demo.yaml
#主配置 group
nacos.config.group=TEST_GROUP
#主配置 最大重试次数
nacos.config.max-retry=10
#主配置 是否开启自动刷新
nacos.config.auto-refresh=true
#主配置重试时间
nacos.config.config-retry-time=2333
# 主配置监听轮询超时时间
nacos.config.config-long-poll-timeout=46000
# 主配置开启注册监听器预加载配置服务
nacos.config.enable-remote-sync-config=false

3. 创建测试Controller对象ConfigController.java

@Controller
@RequestMapping("config")
public class ConfigController {
    @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
    private boolean useLocalCache;

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }
}

4. 启动主类

@RestController
@SpringBootApplication
public class NacosApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        //用于测试
        return String.format("Hello %s!", name);
    }

}

5. 测试配置

测试访问:http://127.0.0.1:8033/config/get  得到useLocalCache配置的值为true

 直接http://127.0.0.1:8848/nacos/修改Nacos Server中的配置useLocalCache=false后再次访问,发现配置已经更新

4.参考源码


https://github.com/PNZBEIJINGL/springboot/tree/master/demo-nacos


5.常见问题

1. ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata

//日志如下
 :: Spring Boot ::                (v2.6.2)

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na]

问题原因:springboot  和 nacos版本不匹配引起的,虽然mvnrepository 上nacos 0.2.10版本指代到springboot 263, 但是实际测试不匹配, 因为spring boot 2.4之后就已经删除了ConfigurationBeanFacoryMetadata 这个类

解决方法:将springbooot版本降低到2.4以前,经测试2.3.9.REASE版本的spring-boot-starter可以用 

2. 配置不能自动刷新

问题原因: 配置属性上的autoRefreshed 和application.properties配置文件中的nacos.config.auto-refresh 都必须配置为true的时候才能实现自动刷新

6. @NacosValue实现自动刷新代码分析

待补充

举报

相关推荐

0 条评论