当前位置: 代码迷 >> 综合 >> alibaba-nacos之config、discovery
  详细解决方案

alibaba-nacos之config、discovery

热度:41   发布时间:2024-01-16 06:38:22.0

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

nacos的官网地址:https://nacos.io/zh-cn/docs/what-is-nacos.html

nacos的server的启动,以及支持mysql存储,可以参考官网的快速启动

1.config

pom中引入jar包,Spring cloud不同版本,引入不同的version

<!-- nacos --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>0.1.1.RELEASE</version></dependency>

bootstrap.properties配置文件

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=yml
spring.application.name=yunlian-truck-ucenter
spring.cloud.nacos.config.namespace=598429e8-6c07-4ee3-b96e-ac242c40a382
spring.cloud.nacos.config.group=truck-impc
#spring.profiles.active=local

active namespace可以在程序启动时,放入启动参数中,namespace可以作为dev,test,prod等环境隔离

 

添加数据到nacos服务器,post数据

@RefreshScope 支持动态更新配置文件中数据,无需重启服务

原理是客户端通过每隔30秒http去服务器请求,加入到服务器到队列,服务器会在29.5秒时处理数据,如果此时服务器上的配置更改了,则从队列中取出客户端,进行实时返回数据

详细参考:https://my.oschina.net/u/3216837/blog/3017406

                  https://my.oschina.net/u/3216837/blog/3019744

@RestController
@IgnoreUserToken
@RefreshScope
public class NacosTestController {@Value("${ucenter.nacos.test}")private String test ;@GetMapping("/config")public String config(){return test ;}
}

多个项目共享同一配置的情况,例如开发环境多个应用都引用了mysql的配置,仅在0.2.0的版本以上支持

spring.cloud.nacos.config.server-addr=127.0.0.1:8848# 0.2.1.RELEASE \u4EE5\u4E0A\u652F\u6301\u591A\u4E2A\u914D\u7F6E\uFF0C\u6587\u6863\uFF1Ahttps://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Nacos-config# Nacos \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A
#   Data ID\uFF1Aapp.properties
#   Group\uFF1Amulti-data-ids
#   \u914D\u7F6E\u5185\u5BB9\uFF1Aapp.user.cache=false
spring.cloud.nacos.config.ext-config[0].data-id=app.properties
spring.cloud.nacos.config.ext-config[0].group=multi-data-ids
spring.cloud.nacos.config.ext-config[0].refresh=true# 1. \u672C\u5730\u5B89\u88C5 MySQL
# 2. Nacos \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A
#   Data ID\uFF1Adatasource.properties
#   Group\uFF1Amulti-data-ids
#   \u914D\u7F6E\u5185\u5BB9\u793A\u4F8B\uFF1A
#       spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
#       spring.datasource.username=root
#       spring.datasource.password=root
#       spring.datasource.initial-size=10
#       spring.datasource.max-active=20
spring.cloud.nacos.config.ext-config[1].data-id=datasource.properties
spring.cloud.nacos.config.ext-config[1].group=multi-data-ids# 1. \u672C\u5730\u5B89\u88C5 Redis
# 2. Nacos \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A
#   Data ID\uFF1Aredis.properties
#   Group\uFF1Amulti-data-ids
#   \u914D\u7F6E\u5185\u5BB9\u793A\u4F8B\uFF1A
#       spring.redis.host=localhost
#       spring.redis.password=20190101
#       spring.redis.timeout=5000
#       spring.redis.max-idle=5
#       spring.redis.max-active=10
#       spring.redis.max-wait=3000
#       spring.redis.test-on-borrow=false
spring.cloud.nacos.config.ext-config[2].data-id=redis.properties
spring.cloud.nacos.config.ext-config[2].group=multi-data-ids

 

2.discovery

pom properties

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>0.1.1.RELEASE</version>
</dependency>
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

discovery与eureka都是实现服务注册发现,需要去掉eureka的jar包解决 single bean问题

Field registration in org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration required a single bean, but 2 were found:
    - eurekaRegistration: defined by method 'eurekaRegistration' in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.class]
    - nacosRegistration: defined by method 'nacosRegistration' in class path resource [org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.class]

另外一个错误:
Caused by: java.lang.NoSuchMethodError: org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.<init>(Lorg/springframework/cloud/client/serviceregistry/ServiceRegistry;Lorg/springframework/cloud/client/serviceregistry/AutoServiceRegistrationProperties;)Vat org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistration.<init>(NacosAutoServiceRegistration.java:43)at org.springframework.cloud.alibaba.nacos.NacosDiscoveryAutoConfiguration.nacosAutoServiceRegistration(NacosDiscoveryAutoConfiguration.java:62)
由于NacosAutoServiceRegistration中代码super(serviceRegistry, autoServiceRegistrationProperties);是引用的spring-cloud-commons 2.0版本以上才有的方法,导致0.1.X版本的discovery启动会上述报错。这与spring cloud的对应版本的commons的包的版本冲突
public class NacosAutoServiceRegistrationextends AbstractAutoServiceRegistration<NacosRegistration> {private static final Logger LOGGER = LoggerFactory.getLogger(NacosAutoServiceRegistration.class);@Autowiredprivate NacosRegistration registration;public NacosAutoServiceRegistration(ServiceRegistry<NacosRegistration> serviceRegistry,AutoServiceRegistrationProperties autoServiceRegistrationProperties,NacosRegistration registration) {super(serviceRegistry, autoServiceRegistrationProperties);this.registration = registration;}

解决上述问题,把Spring cloud的版本升级到Edgware.SR5便可以,因为SR5引用的comms包是1.3.5,这个包中有super的方法。

 

discovery注册服务,分namespace注册,需要手动添加nacos-client 0.8版本以上

        <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-client --><dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-client</artifactId><version>0.8.2</version></dependency>

原因是控制台接受的namespace参数为namespaceId,但是0.8版本以下注册接口是参数tenat,所有都注册到了public到空间中了

 

 

综上:

如果使用nacos,建议还是使用0.2.x的版本,这样也就是要求Spring cloud是2.0的版本,也得多注意引入包到版本

 

  相关解决方案