Dubbo在Spring和Spring Boot中的使用详解

时间:2021-05-20

一、在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://.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基础配置

@Configurationpublic 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基础配置

@Configurationpublic 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中的使用详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章