问题概述
项目中加入注册中心,注册中心组件使用的是Alibaba Nacos,
依赖包:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.4.0</version>
</dependency>
配置加入完成后,项目启动编译通过,
但,alibaba-nacos连接初始化失败,
“ Could not initialize class com.alibaba.nacos.common.remote.client.grpc.GrpcSdkClient ”,
如下图:
具体信息如下:
java.lang.RuntimeException: ErrCode:-400, ErrMsg:Could not initialize class com.alibaba.nacos.common.remote.client.grpc.GrpcSdkClient
at com.alibaba.cloud.nacos.NacosServiceManager.createNewNamingService(NacosServiceManager.java:102)
at com.alibaba.cloud.nacos.NacosServiceManager.buildNamingService(NacosServiceManager.java:90)
at com.alibaba.cloud.nacos.NacosServiceManager.getNamingService(NacosServiceManager.java:46)
at com.alibaba.cloud.nacos.discovery.NacosWatch.stop(NacosWatch.java:173)
at com.alibaba.cloud.nacos.discovery.NacosWatch.destroy(NacosWatch.java:204)
at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:213)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:587)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:559)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:1161)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:520)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:1154)
at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1106)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:596)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
at com.huazai.bayou.admin.BayouMallAdminApplication.main(B...Application.java:12)
Caused by: com.alibaba.nacos.api.exception.NacosException: java.lang.reflect.InvocationTargetException
at com.alibaba.nacos.api.naming.NamingFactory.createNamingService(NamingFactory.java:61)
at com.alibaba.nacos.api.NacosFactory.createNamingService(NacosFactory.java:77)
at com.alibaba.cloud.nacos.NacosServiceManager.createNewNamingService(NacosServiceManager.java:99)
... 19 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at com.alibaba.nacos.api.naming.NamingFactory.createNamingService(NamingFactory.java:59)
... 21 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.alibaba.nacos.common.remote.client.grpc.GrpcSdkClient
at com.alibaba.nacos.common.remote.client.RpcClientFactory.lambda$createClient$0(RpcClientFactory.java:81)
at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1705)
at com.alibaba.nacos.common.remote.client.RpcClientFactory.createClient(RpcClientFactory.java:79)
at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.<init>(NamingGrpcClientProxy.java:87)
at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.<init>(NamingClientProxyDelegate.java:76)
at com.alibaba.nacos.client.naming.NacosNamingService.init(NacosNamingService.java:95)
at com.alibaba.nacos.client.naming.NacosNamingService.<init>(NacosNamingService.java:81)
... 26 common frames omitted
解决办法
经过分析,初步判断,这个nacos的初始化问题,
是由于 版本兼容问题导致的,因为之前有过版本升级,
基本环境,
JDK 11
Spring Boot 2.6.6
Alibaba Nacos 2021.0.4.0
兼容版本对应:
按照上面的版本依赖,理论上是可以正常运行的,
但,alibaba-nacos:2021.0.4.0中的nacos-client版本与nacos-server版本冲突,导致初始化失败,
问题定位到了,
首先,忽略原alibaba-nacos下的nacos-client(可忽略,要求严谨,还是要去掉的)
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.4.0</version>
<exclusions>
<exclusion>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</exclusion>
</exclusions>
</dependency>
手动导入nacos-client新版本,
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>2.3.2</version>
</dependency>
新增依赖后,Reload All Maven Projects
如下图:
现在nacos-client即可正常的初始化了!!!
如下图:
参考文档
好了,关于 java.lang.RuntimeException: ErrCode:-400, ErrMsg:Could not initialize class com.alibaba.nacos..的解决办法 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。
作 者: | 华 仔 |
联系作者: | who.seek.me@java98k.vip |
来 源: | 优快云 (Chinese Software Developer Network) |
原 文: | https://blog.youkuaiyun.com/Hello_World_QWP/article/details/140499845 |
版权声明: | 本文为博主原创文章,请在转载时务必注明博文出处! |