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

网友投稿 272 2023-03-22


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

一、在Spring中使用Dubbo

1、Maven依赖

com.alibaba

dubbo

2.5.3.6

log4j

log4j

commons-logging

http://commons-logging

org.springframework

spring

com.alibaba

fastjson

com.github.sgroschupf

zkclient

0.1

2、DUBBO生产者注册到zookeeper的xml配置方式

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">

class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />

ref="demoService" />

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">

class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />

ref="demoService" />

class="com.unj.dubbotest.provider.impl.DemoServiceImpl" />

ref="demoService" />

ref="demoService" />

3、DUBBO消费者注册到zookeeper的xml配置方式

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://code.alibabatech.com/schema/dubbo

http://code.alibabatech.com/schema/dubbo/dubbo.xsd

">

二、在Spring Boot中使用Dubbo

在Spring Boot中使用Dubbo,不需要使用xml的方式来配置生产者和消费者,需要使用@Bean注解的方式来进行配置。

1、Maven依赖

org.springframework.boot

<artifactId>spring-boot-starter-web

1.2.5.RELEASE

org.springframework.boot

spring-boot-starter

1.2.5.RELEASE

com.alibaba

dubbo

2.5.3.6

log4j

log4j

commons-logging

commons-logging

org.springframework

spring

com.alibaba

fastjson

com.github.sgroschupf

zkclient

0.1

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 personServiceExport(Person person) {

ServiceBean serviceBean = new ServiceBean();

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() {

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序顶部可滚动导航效果
下一篇:接口测试用例英文简称是(接口测试用例英文简称是什么)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~