SpringCloud_Nacos配置中心实践总结

SpringCloud_Nacos配置中心实践总结

    ☆ 使用Nacos作为配置中心,与SpringCloud Config(从远程git/svn拉取配置)作用相同
    ☆ 对一些可变的配置类加上 @RefreshScope 注解,可以热刷新配置 “will get a new instance on the next method call, fully initialized and injected with all dependencies”
        - 对于SpringCloud config而言,需要在下一次调用时新配置生效(避免不了重启)
        - 对于Nacos而言,Nacos Config Starter 默认为所有获取数据成功的 Nacos 的配置项添加了监听功能,在监听到服务端配置发生变化时会实时触发org.springframework.cloud.context.refresh.ContextRefresher 的 refresh 方法 。如果需要对 Bean 进行动态刷新,给类添加 @RefreshScope 或 @ConfigurationProperties注解。

SpringCloud_Nacos配置中心实践总结_第1张图片
实践
 SpringCloud_Nacos配置中心实践总结_第2张图片
一、pom文件
1.完整的pom文件


  4.0.0
  
    com.xx.discovery
    springcloud-nepxion-discovery
    1.0.1
  
  cloud-nacos-config-client-3355
  
    
      com.alibaba.cloud
      spring-cloud-starter-alibaba-nacos-config
      1.5.0.RELEASE
    
    
      com.alibaba.cloud
      spring-cloud-starter-alibaba-nacos-discovery
      1.5.0.RELEASE
    
    
      org.springframework.boot
      spring-boot-starter-web
      
        
          spring-boot-starter-logging
          org.springframework.boot
        
      
    
    
    
      org.springframework.boot
      spring-boot-starter-log4j2
    
    
      com.lmax
      disruptor
      3.4.1
    
    
    
    
      org.springframework
      springloaded
    
    
      org.springframework.boot
      spring-boot-devtools
    
    
      org.springframework.cloud
      spring-cloud-starter-config
    
  

2.bootstrap.yaml

spring:
  application:
    name: cloud-nacos-config-3355
  profiles:
    # 可以用来获取配置中心的多个文件,这里拉取以下两个:
      # cloud-nacos-config-3355.properties
      # cloud-nacos-config-3355-a.properties
    active: a, public
  cloud:
    config:
      enabled: false
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        enabled: true
        server-addr: 127.0.0.1:8848
        file-extension: properties

3.application.yaml为拉取不到配置文件时的默认配置

server:
  port: 3355
company:
  name: bbc

4.NacosConfigController.java

@RestController
@RequestMapping("nacos")
//启用动态配置刷新
@RefreshScope
public class NacosConfigController {

    //获取配置的值
    @Value("${company.name}")
    private String cName;
    
    @RequestMapping("/getName")
    @RefreshScope
    public  Object getName(){
        String baseName ="公司名称:";
      return baseName + cName;
    }
}

5.测试,修改nacos中的配置文件
 SpringCloud_Nacos配置中心实践总结_第3张图片
  SpringCloud_Nacos配置中心实践总结_第4张图片
  SpringCloud_Nacos配置中心实践总结_第5张图片
最终生效的是:cloud-nacos-config-3355.properties、cloud-nacos-config-3355-a.properties配置文件

附1:遇到的问题
1.未解决的

202147:702        com.alibaba.nacos.client.config.impl.CacheData198    com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848        ERROR    [fixed-127.0.0.1_8848] [notify-error] dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a tx=null
202147:702        com.alibaba.nacos.client.config.impl.CacheData218    com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848        INFO    [fixed-127.0.0.1_8848] [notify-listener] time cost=1ms in ClientWorker, dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a 
202217:205        com.alibaba.nacos.client.config.impl.CacheData198    com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848        ERROR    [fixed-127.0.0.1_8848] [notify-error] dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a tx=null
202217:205        com.alibaba.nacos.client.config.impl.CacheData218    com.alibaba.nacos.client.Worker.longPolling.fixed-127.0.0.1_8848        INFO    [fixed-127.0.0.1_8848] [notify-listener] time cost=1ms in ClientWorker, dataId=cloud-nacos-config-3355.properties, group=DEFAULT_GROUP, md5=f10d6ba4a35994a70f0e21d418a0464d, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@5735a23a 

 - 当前的解决方法 -- 当在nacos中修改配置,而没有生效,就会报错,此时只有重启应用 (有知道的兄弟帮忙评论解答,感谢!)

2.Could not locate PropertySource: I/O error on GET request for "http://localhost:8888

附2:扩展
1.Spring Cloud 使用 Nacos 做配置中心,读取多个配制文件的方式  

附3:参考文献
1.nacos作为SpringCloud配置中心  
2.Spring Cloud Nacos 作为注册和配置中心 + Spring Cloud Gateway作为网关 + Sentinel 作为熔断服务  
3.Nacos整合SpringCloud(配置中心、注册中心)

你可能感兴趣的:(springcloud,java,spring)