最近在做老系统升级(springboot2+dubbo2.7.1+zookeeper+nacos-config),去掉zookeeper的注册中心,替换成nacos2.1版本(阿里云已经不支持1.X版本了)-对应的需要升级springboot和dubbo3。最终升级完成了,其中遇到的诸多问题,能记住多少就总结多少
有在升级过程中,遇到问题的小伙伴,欢迎交流。
抛弃zookeeper做为注册中和es job中心
升级技术组件
使用阿里云微服务中心:nacos
阿里云微服务中心nacos不再支持1.X版本
分布式体系
原技术体系:springboot+dubbo+nacos配置中心+zookeeper+redis+rocketmq+elastic-job+mysql
升级后体系:springboot+dubbo+nacos配置注册中心+redis+rocketmq+xxl-job+mysql
技术 | 旧版本 | 新版本 |
---|---|---|
springboot | 2.1.1.release | 2.3.1.release |
dubbo | 2.7.1 | 3.1.0 |
nacos 配置中心 | 1.4.1 | 2.1.0 |
nacos 注册中心 | 无 | 2.1.0 |
zk 注册中心 | 3.5.5 | 无 |
elastic-job | 2.1.2 | 无 |
xxl-job | 无 | 2.3.1 |
需要注意版本依赖
https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-autoconfigure/3.1.0
https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-config-spring/3.1.0
https://mvnrepository.com/artifact/com.alibaba.boot/nacos-config-spring-boot-starter/0.2.10
https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter/3.1.0
https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-registry-nacos/3.1.0
nacos官网
https://nacos.io/zh-cn/docs/v2/ecology/use-nacos-with-dubbo.html
dubbo官网和参数说明
https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/config/properties/#application
尝试dubbo升到2.7.22,2.x版本的最新版本,查了下依赖,
最高支持到nacos-cliient 1.4.2,不符合咱们的2.1.0版本,怎么办?
那就排除掉1.4.2版本,增加2.1.0 nacos-client依赖
说下关键的点和问题
目前dubbo往注册中心上注册的数据是接口级,而应用级服务发现是往注册中心上注册实例(ip+port),两者的区别只是注册的粒度不同。
至于为什么会出现应用级服务发现,有如下几点原因
与业界主流微服务模型对齐,比如 SpringCloud、Kubernetes Native Service
等减少注册数据,提升性能
如果只是注册接口级别,其实很烦,一堆的接口,还是应用级别比较爽
怎么配置,网上找了多久资料,还看了看源码,最后在注册中心的注册数据中发现端倪
提供者
dubbo.registry.parameters.registry-type: service
消费者同样要配置,而且需要指定消费的服务者
说实话,放到parameters参数中真的是。。。。。
config、注册、metadata是2.7之后引入的“三大中心”,老版本都是缓存到本的用户和元数据,为什么新版本直接到config了??
网上查资料,说这么配置
dubbo.registry.use-as-config-center: false
dubbo.registry.use-as-metadata-center: false
启动后,第一个参数配置起作用了,第二个直接报错,
java.util.NoSuchElementException: null
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1447)
at java.util.HashMap$EntryIterator.next(HashMap.java:1479)
at java.util.HashMap$EntryIterator.next(HashMap.java:1477)
at org.apache.dubbo.registry.client.metadata.store.RemoteMetadataServiceImpl.lambda$publishMetadata$0(RemoteMetadataServiceImpl.java:69)
at java.util.concurrent.ConcurrentHashMap.forEach(ConcurrentHashMap.java:1597)
at org.apache.dubbo.registry.client.metadata.store.RemoteMetadataServiceImpl.publishMetadata(RemoteMetadataServiceImpl.java:63)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.publishMetadataToRemote(DubboBootstrap.java:1211)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.doRegisterServiceInstance(DubboBootstrap.java:1193)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.registerServiceInstance(DubboBootstrap.java:1174)
at org.apache.dubbo.config.bootstrap.DubboBootstrap.start(DubboBootstrap.java:894)
at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onContextRefreshedEvent(DubboBootstrapApplicationListener.java:70)
at org.apache.dubbo.config.spring.context.DubboBootstrapApplicationListener.onApplicationContextEvent(DubboBootstrapApplicationListener.java:63)
at com.alibaba.spring.context.OnceApplicationContextEventListener.onApplicationEvent(OnceApplicationContextEventListener.java:52)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.domes.base.support.BaseSupportApplication.main(BaseSupportApplication.java:13)
查了老半天源码,反正,就是得有元数据存储地方,尝试配置缓存到本地,参数没起作用
没办法,在nacos配置中心新增一个命名空间,专门做元数据存储,
dubbo.metadata-report.address: nacos://ip:8848
dubbo.metadata-report.parameters.namespace: 211d0b5e-2267-4309-9df8-xxxxxxx4
反正能起来也能跑,就是还有一些小的问题,就不一一说了,
如果有兴趣的同学可以一起交流
果断放弃了,毕竟老项目升级,搞稳妥一些,找一个支持nacos2.1的dubbo版本,最后选了dubbo3.1.0
查了dubbo3.1.0的一些相关的资料,是一个很大的版本升级,使用Triple协议,支持云原生,据说,性能提升很明显,等有时间研究下
中间过程先省略。。。
pom.xml配置
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<dubbo.version>3.1.0dubbo.version>
<spring-boot.version>2.3.1.RELEASEspring-boot.version>
properties>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-starterartifactId>
<version>0.2.10version>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
exclusion>
<exclusion>
<groupId>com.alibaba.nacosgroupId>
<artifactId>nacos-clientartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-spring-boot-starterartifactId>
<version>${dubbo.version}version>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
exclusion>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-autoconfigureartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.dubbogroupId>
<artifactId>dubbo-registry-nacosartifactId>
<version>${dubbo.version}version>
dependency>
<dependency>
<groupId>com.alibaba.springgroupId>
<artifactId>spring-context-supportartifactId>
<version>1.0.11version>
dependency>
application.yaml配置
spring:
application:
name: xx-xxx-xxx-support
# 允许bean重写,因为重写了事物
main:
allow-bean-definition-overriding: true
nacos:
config:
server-addr: ip:8848
namespace: 2c047fc5-bcf4-47eb-a9ce-fxxxxxxx4
bootstrap:
enable: true
type: yaml
data-id: xx-xxx-xxx-support
auto-refresh: true
nacos配置中心—dubbo部分
dubbo.registry.address: nacos://ip:8848
dubbo.registry.register-mode: instance
dubbo.registry.parameters.namespace: 2c047fc5-bcf4-47eb-a9ce-xxx
dubbo.scan.base-packages: com.xxx.dubbo
dubbo.metadata-report.address: nacos://ip:8848
dubbo.metadata-report.parameters.namespace: 211d0b5e-2267-4309-9df8-xxx
dubbo.protocol.name: dubbo
dubbo.protocol.port: -1
dubbo.protocol.payload: 41943040
dubbo.consumer.check: false
dubbo.consumer.timeout: 6000
SpringBootApplication 启动类去掉NacosPropertySource
去掉dubbo自动配置加载类,使用默认的配置加载
增加nacos dubbo 应用名
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* @author weidong
* @date 20230429
* @description nacos消费应用名称配置
*/
@Configuration
public class ProjectNameConfig implements EnvironmentAware {
@Value("${spring.application.name}")
private String applicationName;
@Override
public void setEnvironment(Environment environment) {
if(StringUtils.isBlank(System.getProperty("project.name"))){
System.setProperty("project.name",applicationName);
}
}
}