您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何在Spring與Spring Boot項目中使用Dubbo,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
一、在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; } }
關于如何在Spring與Spring Boot項目中使用Dubbo就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。