##簡介 Dubbox是噹噹網對阿里的Dubbo進行加強的一個分支。在使用springboot以後,咱們發現不少配置並不必定要使用xml。這篇文章的目的是讓你使用Dubbox時能像使用springboot的其它功能同樣能夠在application.properties中配置。html
##基礎整合java
mvn clean install -DskipTests
<!-- zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> <!-- Dubbox --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.8.4</version> </dependency> <!-- springboot的註解處理器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </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.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency>
#####如今已經能夠正常使用了,若想進行擴展,能夠繼續往下~git
##指定序列化配置github
<dependency> <groupId>com.esotericsoftware.kryo</groupId> <artifactId>kryo</artifactId> <version>2.24.0</version> </dependency> <dependency> <groupId>de.javakaffee</groupId> <artifactId>kryo-serializers</artifactId> <version>0.26</version> </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.htmlweb
###Dubbox的REST支持spring
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>3.0.7.Final</version> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>3.0.7.Final</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <!-- 若是要使用json序列化 --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jackson-provider</artifactId> <version>3.0.7.Final</version> </dependency> <!-- 若是要使用xml序列化 --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>3.0.7.Final</version> </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.htmlapache
#####注:未經贊成不得轉載json