dubbo在java中的使用_java相关:Dubbo在Spring和Spring Boot中的使用详解

java相关:Dubbo在Spring和Spring Boot中的使用详解

发布于 2020-3-18|

复制链接

摘记: 一、在Spring中使用Dubbo1、Maven依赖

```java

com.alibaba

..

一、在Spring中使用Dubbo1、Maven依赖

```java

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生产者注册到zookeeper的xml配置方式

```java

-->

```

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

```java

```

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

```java

org.springframework.boot

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

```java

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

```java

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

```java

@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;

}

}

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值