遇到问题:
在dubbo整合springboot的时候,使用io.dubbo.springboot的jar包,配合的是dubbo2.5.3的版本,会出现Bean创建失败的错误。
SpringBoot 整合 Dubbo + Zookeeper :https://github.com/i-Javan/springboot
pom:
<!-- SpringBoot 整合 Dubbo 依赖 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.0</version>
</dependency>
报错代码:
public class DubboServiceImpl implements DubboService{
public List<User> getList() {
return Arrays.asList(new User[]{new User("1","小红","18","18@qq.com"),new User("1","❤","17","17@qq.com")});
}
public User getById(String id) {
return new User("1","小红","18","18@qq.com");
}
}
错误信息:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-26 10:58:23.439 ERROR 13992 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dubboServiceImpl' defined in file [E:\work space\movebrick\movebrick-zookeeper-dubbo-server\target\classes\com\movebrick\dubbo\module\service\impl\DubboServiceImpl.class]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at com.movebrick.dubbo.ServerApplication.main(ServerApplication.java:18) [classes/:na]
Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.I0Itec.zkclient.ZkClient.<clinit>(ZkClient.java:63) ~[zkclient-0.7.jar:0.7]
at com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient.<init>(ZkclientZookeeperClient.java:25) ~[dubbo-2.5.3.jar:2.5.3]
at
报错原因:因为2.5.3版本的dubbo增加了ServiceBean,applicationEventPublisher,初始化报错.
Spring Boot自带的Dubbo版本与io.dubbo.springboot有冲突,可以查看io.dubbo.springboot 的Maven库找到对应的Dubbo版本与之对应。
依赖:
解决方法一:
如果使用 io.dubbo.springboot这个包,则搭配dubbo 2.6.2以下的版本:
<!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
解决方法二:
使用官方正常维护的Springboot-dubbo, com.alibaba.boot,则dubbo版本都可以兼容
<!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
SpringBoot 整合 Dubbo + Zookeeper :https://github.com/i-Javan/springboot
SpringBoot 整合 Reids :https://github.com/i-Javan/springboot/tree/master/movebrick-mybatis-annotation
SpringBoot 整合 Mybatis :https://github.com/i-Javan/springboot/tree/master/movebrick-mybatis
SpringBoot 整合 MybatisPlus :https://github.com/i-Javan/springboot/tree/master/movebrick-mybatisPlus
SpringBoot 整合 MybatisAnnotation :https://github.com/i-Javan/springboot/tree/master/movebrick-mybatis-annotation
SpringBoot 整合 Elasticsearch:https://github.com/i-Javan/springboot/tree/master/movebrick-elasticsearch
SpringBoot 整合 ActiveMQ:https://github.com/i-Javan/springboot/tree/master/movebrick-activeMQ