Dubbo在Spring和Spring Boot中的使用详解
程序员文章站
2024-02-16 12:47:52
一、在spring中使用dubbo
1、maven依赖
com.alibaba<...
一、在spring中使用dubbo
1、maven依赖
<dependency> <groupid>com.alibaba</groupid> <artifactid>dubbo</artifactid> <version>2.5.3.6</version> <exclusions> <exclusion> <groupid>log4j</groupid> <artifactid>log4j</artifactid> </exclusion> <exclusion> <groupid>commons-logging</groupid> <artifactid>commons-logging</artifactid> </exclusion> <exclusion> <groupid>org.springframework</groupid> <artifactid>spring</artifactid> </exclusion> <exclusion> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>com.github.sgroschupf</groupid> <artifactid>zkclient</artifactid> <version>0.1</version> </dependency>
2、dubbo生产者注册到zookeeper的xml配置方式
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 具体的实现bean --> <bean id="demoservice" class="com.unj.dubbotest.provider.impl.demoserviceimpl" /> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="xixi_provider" /> <!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" />--> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.unj.dubbotest.provider.demoservice" version="mys" ref="demoservice" /> </beans>
3、dubbo消费者注册到zookeeper的xml配置方式
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd "> <!-- 消费者应用信息,用于提供依赖关系 --> <dubbo:application name="consumer-of-helloworld-app" /> <!-- 注册地址,用于消费者寻找服务 --> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181,10.128.3.33:2181" /> <dubbo:consumer timeout="5000" /> <!-- 引用的服务 --> <dubbo:reference id="demoservice"interface="com.unj.dubbotest.provider.demoservice" version="mys" /> </beans>
二、在spring boot中使用dubbo
在spring boot中使用dubbo,不需要使用xml的方式来配置生产者和消费者,需要使用@bean注解的方式来进行配置。
1、maven依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <version>1.2.5.release</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> <version>1.2.5.release</version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>dubbo</artifactid> <version>2.5.3.6</version> <exclusions> <exclusion> <groupid>log4j</groupid> <artifactid>log4j</artifactid> </exclusion> <exclusion> <groupid>commons-logging</groupid> <artifactid>commons-logging</artifactid> </exclusion> <exclusion> <groupid>org.springframework</groupid> <artifactid>spring</artifactid> </exclusion> <exclusion> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>com.github.sgroschupf</groupid> <artifactid>zkclient</artifactid> <version>0.1</version> </dependency>
2、dubbo基础配置
public class dubbobaseconfig { @bean public registryconfig registry() { registryconfig registryconfig = new registryconfig(); registryconfig.setaddress("127.0.0.1:2181"); registryconfig.setprotocol("zookeeper"); return registryconfig; } @bean public applicationconfig application() { applicationconfig applicationconfig = new applicationconfig(); applicationconfig.setname("testapp"); return applicationconfig; } @bean public monitorconfig monitorconfig() { monitorconfig mc = new monitorconfig(); mc.setprotocol("registry"); return mc; } @bean public referenceconfig referenceconfig() { referenceconfig rc = new referenceconfig(); rc.setmonitor(monitorconfig()); return rc; } @bean public protocolconfig protocol() { protocolconfig protocolconfig = new protocolconfig(); protocolconfig.setport(20880); return protocolconfig; } @bean public providerconfig provider() { providerconfig providerconfig = new providerconfig(); providerconfig.setmonitor(monitorconfig()); return providerconfig; } }
3、dubbo生产者配置,需要继承dubbo基础配置
@configuration public class exportserviceconfig extends dubbobaseconfig { @bean public servicebean<person> personserviceexport(person person) { servicebean<person> servicebean = new servicebean<person>(); servicebean.setproxy("javassist"); servicebean.setversion("myversion"); servicebean.setinterface(person.class.getname()); servicebean.setref(person); servicebean.settimeout(5000); servicebean.setretries(3); return servicebean; } }
4、dubbo消费者配置,需要继承dubbo基础配置
@configuration public class referenceconfig extends dubbobaseconfig { @bean public referencebean<person> person() { referencebean<person> ref = new referencebean<>(); ref.setversion("myversion"); ref.setinterface(person.class); ref.settimeout(5000); ref.setretries(3); ref.setcheck(false); return ref; } }
5、直接从spring容器中拿去person接口即可。
总结
以上所述是小编给大家介绍的dubbo在spring和spring boot中的使用详解,希望对大家有所帮助
推荐阅读
-
Dubbo在Spring和Spring Boot中的使用详解
-
Spring Boot如何使用JDBC获取相关的数据详解
-
详解在Spring中如何使用AspectJ来实现AOP
-
详解在Spring-Boot中实现通用Auth认证的几种方式
-
详解Spring Boot Profiles 配置和使用
-
spring boot中内嵌redis的使用方法示例
-
使用dubbo+zookeeper+spring boot构建服务的方法详解
-
详解使用Spring Boot的AOP处理自定义注解
-
Spring Boot中使用Spring-data-jpa的配置方法详解
-
使用Spring Boot和OAuth构建安全的SPA