Spring boot 整合Dubbo
环境
- Spring boot:1.5.3.RELEASE
- zookeeper:3.4.10
- dubbo:2.5.3
1 | - |
- dubbo-interface:定义接口
- dubbo-provider:提供服务
- dubbo-cosumer:消费服务
1、接口定义代码
1 | package com.hhly.dubbo.service; |
2、服务提供者
2.1、服务提供者配置类
1 | package com.hhly.dubbo.config; |
2.2、服务提供者实现接口提供者接口
1 | package com.hhly.dubbo.service; |
2.3、配置文件
1 | # 当前应用名称,用于注册中心计算应用间依赖关系, |
3、服务消费者代码
3.1、服务消费者配置类
1 | package com.hhly.dubbo.config; |
3.2、消费者服务调用类
1 | package com.hhly.dubbo.service.impl; |
3.3、配置文件
修改1
dubbo.application.name=application_comsumer
需要注意一点,所定义的dubbo注解类是在dubbo配置dubbo.annotation.package的包下面,否则将无法识别,无法初始化。
4、pom.xml文件配置参考
1 | <?xml version="1.0" encoding="UTF-8"?> |
5、遇到的问题
5.1、dubbo-provider项目无法启动
报错,报错信息如下:1
2
3
4
5
6
7
8Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotationAwareOrderComparator.sort(Ljava/util/List;)V
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:404)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:393)
at org.springframework.boot.SpringApplication.initialize(SpringApplication.java:260)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:236)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.hhly.dubbo.DubboProviderApplication.main(DubboProviderApplication.java:11)
问题分析:在没有引入dubbo-2.5.3.jar的情况下,项目是可以正常启动的,初步判断为jar包冲突,导致了该问题。于是去dubbo-2.5.3中找线索。
通过查看pom.xml文件发现dubbo-2.5.3有引入spring-2.5.6.SEC03
去spring-2.5.6.SEC03中找到AnnotationAwareOrderComparator类,发现该类中确实不存在sort()方法
解决办法:
将引入的dubbo-2.5.3去除spring依赖1
2
3
4
5
6
7
8
9
10
11<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
5.2、dubbo-cosumer无法启动
报错信息如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22java.lang.NullPointerException
at com.alibaba.dubbo.config.spring.ReferenceBean.afterPropertiesSet(ReferenceBean.java:102)
at com.alibaba.dubbo.config.spring.AnnotationBean.refer(AnnotationBean.java:292)
at com.alibaba.dubbo.config.spring.AnnotationBean.postProcessBeforeInitialization(AnnotationBean.java:233)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
at com.hhly.user.ComsumerApplication.main(ComsumerApplication.java:22)
断点到具体位置,发现该处有两个ApplicationConfig配置,并且其中一个为null
分析:
如果此时的getConsumer().getApplication() 有值的话,就不会进入这个,于是想到在Configuration配置文件里面setter对应的值,修改Configuration文件的providerConfig()方法1
2
3
4
5
6
7
8
9"defaultConsumer") (name=
public ConsumerConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
ConsumerConfig providerConfig = new ConsumerConfig();
// 手动注入,不再由spring容器去根据类名
// (com.alibaba.dubbo.config.ApplicationConfig)去获取对应的值
providerConfig.setApplication(applicationConfig);
providerConfig.setRegistry(registryConfig);
return providerConfig;
}
深层原因,为什么会获得两个配置,还不是很清楚,囧