##简介
Dubbox是当当网对阿里的Dubbo进行增强的一个分支。在使用springboot之后,我们发现很多配置并不一定要使用xml。这篇文章的目的是让你使用Dubbox时能像使用springboot的其它功能一样可以在application.properties中配置。
##基础整合
mvn clean install -DskipTests
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>com.github.sgroschupfgroupId>
<artifactId>zkclientartifactId>
<version>0.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>dubboartifactId>
<version>2.8.4version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
@Configuration
public class DubboConfig {
@Bean
@ConfigurationProperties(prefix="dubbo.application")
public ApplicationConfig applicationConfig() {
ApplicationConfig applicationConfig = new ApplicationConfig();
return applicationConfig;
}
@Bean
@ConfigurationProperties(prefix="dubbo.registry")
public RegistryConfig registryConfig() {
RegistryConfig registryConfig = new RegistryConfig();
return registryConfig;
}
@Bean
@ConfigurationProperties(prefix="dubbo.annotation")
public AnnotationBean annotationBean() {
AnnotationBean annotationBean = new AnnotationBean();
return annotationBean;
}
@Bean
@ConfigurationProperties(prefix="dubbo.protocol")
public ProtocolConfig protocolConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
return protocolConfig;
}
}
#应用名
dubbo.application.name=
#负责人
dubbo.application.owner=
#注册zookeeper的地址
dubbo.registry.address=zookeeper://localhost:2181
#扫包
dubbo.annotation.package=com.yeauty.service
#协议 (有 dubbo、rest、http、hessian、webservice)
dubbo.protocol.name=dubbo
#协议暴露服务的端口(Integer类型)
dubbo.protocol.port=20880
提供者注解(实现类上):
@Service(包:com.alibaba.dubbo.config.annotation.Service)
@Component(包:org.springframework.stereotype.Component)
消费者注解(对应Interface类型的属性上):
@Reference(包:com.alibaba.dubbo.config.annotation.Reference)
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
dependency>
#####现在已经可以正常使用了,若想进行扩展,可以继续往下~
##指定序列化配置
<dependency>
<groupId>com.esotericsoftware.kryogroupId>
<artifactId>kryoartifactId>
<version>2.24.0version>
dependency>
<dependency>
<groupId>de.javakaffeegroupId>
<artifactId>kryo-serializersartifactId>
<version>0.26version>
dependency>
#序列化方式(kryo是目前效率最高的)
dubbo.protocol.serialization=kryo
#序列化优化的实现类
dubbo.protocol.optimizer=
public class SerializationOptimizerImpl implements SerializationOptimizer {
public Collection<Class> getSerializableClasses() {
List<Class> classes = new LinkedList<Class>();
return classes;
}
}
需要进行kryo序列化的类在这里add进list里面
详细可参考:https://dangdangdotcom.github.io/dubbox/serialization.html
###Dubbox的REST支持
<dependency>
<groupId>org.jboss.resteasygroupId>
<artifactId>resteasy-jaxrsartifactId>
<version>3.0.7.Finalversion>
dependency>
<dependency>
<groupId>org.jboss.resteasygroupId>
<artifactId>resteasy-clientartifactId>
<version>3.0.7.Finalversion>
dependency>
<dependency>
<groupId>javax.validationgroupId>
<artifactId>validation-apiartifactId>
<version>1.1.0.Finalversion>
dependency>
<dependency>
<groupId>org.jboss.resteasygroupId>
<artifactId>resteasy-jackson-providerartifactId>
<version>3.0.7.Finalversion>
dependency>
<dependency>
<groupId>org.jboss.resteasygroupId>
<artifactId>resteasy-jaxb-providerartifactId>
<version>3.0.7.Finalversion>
dependency>
@Bean
@ConfigurationProperties(prefix="dubbo.protocol.rest")
public ProtocolConfig protocolRestConfig() {
ProtocolConfig protocolConfig = new ProtocolConfig();
return protocolConfig;
}
dubbo.protocol.rest.name=rest
dubbo.protocol.rest.port=8082
dubbo.protocol.rest.server=tomcat
更多属性配置和使用方法请参考:https://dangdangdotcom.github.io/dubbox/rest.html
######注:未经同意不得转载