Spring boot 整合Dubbo
Spring boot 整合Dubbo环境- Spring boot:1.5.3.RELEASE- zookeeper:3.4.10- dubbo:2.5.3
·
Spring boot 整合Dubbo
环境
- Spring boot:1.5.3.RELEASE
- zookeeper:3.4.10
- dubbo:2.5.3
-
+----------------+
|dubbo-interface |
+----------------+
|
|
+------++------+------++------+
| |
| |
+----------------+ +----------------+
| dubbo-provider | | dubbo-comsumer |
+----------------+ +----------------+
- dubbo-interface:定义接口
- dubbo-provider:提供服务
- dubbo-cosumer:消费服务
1、接口定义代码
package com.hhly.dubbo.service;
public interface HelloProvider {
public String sayHello(String name);
}
2、服务提供者
2.1、服务提供者配置类
package com.hhly.dubbo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.rpc.Exporter;
@Configuration
@ConditionalOnClass(Exporter.class)
@PropertySource(value = "classpath:/dubbo.properties")
public class DubboConfiguration {
@Value("${dubbo.application.name}")
private String applicationName;
@Value("${dubbo.registr.protocol}")
private String protocol;
@Value("${dubbo.registry.address}")
private String registryAddress;
@Value("${dubbo.protocol.name}")
private String protocolName;
@Value("${dubbo.protocol.port}")
private int protocolPort;
@Value("${dubbo.provider.timeout}")
private int timeout;
@Value("${dubbo.provider.retries}")
private int retries;
@Value("${dubbo.provider.delay}")
private int delay;
/**
* 设置dubbo扫描包
*
* @param packageName
* @return
*/
@Bean
public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) {
AnnotationBean annotationBean = new AnnotationBean();
annotationBean.setPackage(packageName);
return annotationBean;
}
/**
* 注入dubbo上下文
*
* @return
*/
@Bean
public ApplicationConfig applicationConfig() {
// 当前应用配置
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(this.applicationName);
return applicationConfig;
}
/**
* 注入dubbo注册中心配置,基于zookeeper
*
* @return
*/
@Bean
public RegistryConfig registryConfig() {
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol(protocol);
registry.setAddress(registryAddress);
return registry;
}
/**
* 默认基于dubbo协议提供服务
*
* @return
*/
@Bean
public ProtocolConfig protocolConfig() {
// 服务提供者协议配置
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName(protocolName);
protocolConfig.setPort(protocolPort);
protocolConfig.setThreads(200);
//System.out.println("默认protocolConfig:" + protocolConfig.hashCode());
return protocolConfig;
}
/**
* dubbo服务提供
*
* @param applicationConfig
* @param registryConfig
* @param protocolConfig
* @return
*/
@Bean(name = "myProvider")
public ProviderConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig,
ProtocolConfig protocolConfig) {
ProviderConfig providerConfig = new ProviderConfig();
providerConfig.setTimeout(timeout);
providerConfig.setRetries(retries);
providerConfig.setDelay(delay);
providerConfig.setApplication(applicationConfig);
providerConfig.setRegistry(registryConfig);
providerConfig.setProtocol(protocolConfig);
return providerConfig;
}
// getter/setter
}
2.2、服务提供者实现接口提供者接口
package com.hhly.dubbo.service;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
@Component
@Service(version="1.0.0")
public class HelloProviderImpl implements HelloProvider {
@Override
public String sayHello(String name) {
return "hello dubbo-" + name;
}
}
2.3、配置文件
# 当前应用名称,用于注册中心计算应用间依赖关系,
#
# 注意:消费者和提供者应用名不要一样,此参数不是匹配条件,
#
# 你当前项目叫什么名字就填什么,
# 和提供者消费者角色无关,比如:kylin应用调用了morgan应用的服务,则kylin项目配成kylin,
# morgan项目配成morgan,可能kylin也提供其它服务给别人使用,但kylin项目永远配成kylin,
# 这样注册中心将显示kylin依赖于morgan
dubbo.application.name=application_provider
# 日志输出方式,可选:slf4j,jcl,log4j,jdk
dubbo.application.logger=slf4j
# 扫描注解包路径,多个包用逗号分隔,不填pacakge表示扫描当前ApplicationContext中所有的类
dubbo.annotation.package=com.hhly.dubbo.service
# 服务端口
dubbo.protocol.name=dubbo
# 协议端口
dubbo.protocol.port=20880
# 设为true,将向logger中输出访问日志,
# 也可填写访问日志文件路径,直接把访问日志输出到指定文件
dubbo.protocol.accessLog=true
# 服务方法调用超时时间(毫秒)
dubbo.provider.timeout=3000
# 远程服务调用重试次数,不包括第一次调用,不需要重试请设为0
dubbo.provider.retries=1
# 延迟注册服务时间(毫秒)- ,设为-1时,表示延迟到Spring容器初始化完成时暴露服务
dubbo.provider.delay=-1
# 只调用指定协议的服务提供方,其它协议忽略。
dubbo.registr.protocol=zookeeper
# 注册地址
dubbo.registry.address=192.168.10.242:2181
# 该协议的服务是否注册到注册中心
dubbo.registry.register=true
# 是否向此注册中心订阅服务,如果设为false,将只注册,不订阅
dubbo.registry.subscribe=true
3、服务消费者代码
3.1、服务消费者配置类
package com.hhly.dubbo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.rpc.Invoker;
@Configuration
@ConditionalOnClass(Invoker.class)
@PropertySource(value = "classpath:/dubbo.properties")
public class DubboConfiguration {
@Value("${dubbo.application.name}")
private String applicationName;
@Value("${dubbo.registr.protocol}")
private String protocol;
@Value("${dubbo.registry.address}")
private String registryAddress;
@Value("${dubbo.protocol.name}")
private String protocolName;
@Value("${dubbo.protocol.port}")
private int protocolPort;
@Value("${dubbo.provider.timeout}")
private int timeout;
/**
* 设置dubbo扫描包
* @param packageName
* @return
*/
@Bean
public static AnnotationBean annotationBean(@Value("${dubbo.annotation.package}") String packageName) {
AnnotationBean annotationBean = new AnnotationBean();
annotationBean.setPackage(packageName);
return annotationBean;
}
/**
* 注入dubbo上下文
*
* @return
*/
@Bean
public ApplicationConfig applicationConfig() {
// 当前应用配置
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName(this.applicationName);
return applicationConfig;
}
/**
* 注入dubbo注册中心配置,基于zookeeper
*
* @return
*/
@Bean
public RegistryConfig registryConfig() {
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol(protocol);
registry.setAddress(registryAddress);
return registry;
}
/**
* 默认基于dubbo协议提供服务
*
* @return
*/
/* @Bean
public ProtocolConfig protocolConfig() {
// 服务提供者协议配置
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName(protocolName);
protocolConfig.setPort(protocolPort);
protocolConfig.setThreads(200);
System.out.println("默认protocolConfig:" + protocolConfig.hashCode());
return protocolConfig;
}*/
/**
* dubbo消费
*
* @param applicationConfig
* @param registryConfig
* @param protocolConfig
* @return
*/
@Bean(name="defaultConsumer")
public ConsumerConfig providerConfig(ApplicationConfig applicationConfig, RegistryConfig registryConfig) {
ConsumerConfig providerConfig = new ConsumerConfig();
return providerConfig;
}
// getter/setter
}
3.2、消费者服务调用类
package com.hhly.dubbo.service.impl;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Reference;
import com.hhly.dubbo.service.HelloProvider;
import com.hhly.dubbo.service.HelloService;
@Component
public class HelloServiceImpl implements HelloService{
@Reference(version = "1.0.0")
private HelloProvider helloService;
public String sayHello(String name) {
String sayHello = helloService.sayHello(name);
return sayHello;
}
}
3.3、配置文件
修改
dubbo.application.name=application_comsumer
需要注意一点,所定义的dubbo注解类是在dubbo配置dubbo.annotation.package的包下面,否则将无法识别,无法初始化。
4、pom.xml文件配置参考
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<packaging>jar</packaging>
<name>dubbo-provider</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.hhly</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.hhly</groupId>
<artifactId>dubbo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<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>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5、遇到的问题
5.1、dubbo-provider项目无法启动
报错,报错信息如下:
Exception 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依赖
<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无法启动
报错信息如下:
java.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()方法
@Bean(name="defaultConsumer")
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;
}
深层原因,为什么会获得两个配置,还不是很清楚,囧
6、参考
更多推荐
已为社区贡献1条内容
所有评论(0)