问题描述:已有生产者(com.xxx.ProviderService#getInfo)本地的项目,需要从生产者的接口中获取数据。
第一步:引入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <artifactId>spring</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency>
第二步:创建dubbo配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <dubbo:application name="dubbo-customer" /> <!--zookeeper注册中心 --> <dubbo:registry protocol="zookeeper" address="xxx.xxx.xxx.xx:2181" /> <!-- 生成远程服务代理,可以和本地bean一样使用 --> <dubbo:reference id="providerService" interface="com.xxx.ProviderService" /> </beans>
第三步:在本地使用
@Resource private ProviderService providerService;使用这样的方式,直接将接口注入到本地
第四步:出现问题
Caused by: java.lang.NoClassDefFoundError: org/I0Itec/zkclient/exception/ZkNoNodeException
Caused by: java.lang.ClassNotFoundException:org.I0Itec.zkclient.exception.ZkNoNodeException
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'providerService': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: org/I0Itec/zkclient/exception/ZkNoNodeException
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localMothed': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'providerService': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: org/I0Itec/zkclient/exception/ZkNoNodeException
报错解读:providerService在spring加载的时候,找不到,断点检查后,发现在getBean方法是报错,providerService在beanDefineMap中,但是没有加载到一级缓冲中
解决办法:如下
<!-- 更新dubbo的版本 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 新加两个依赖 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.8.0</version>
</dependency>
在写代码的时候参考的例子中,用的spring版本是 4.1.5.RELEASE
在该项目中,用的spring版本是 4.3.3.RELEASE
从上述问题,本人猜测是与spring的版本冲突了,但是具体是否是这个原因本人不太明确,如果有知道的大神,望不吝赐教,也希望该博客能解决各位看官的问题!!!