---
title: "Dubbo+Spring消费方和服务方配置"
author: "mvplee"
date: "2015-11-11T14:00:32Z"
tags: []
categories: ["企业架构"]
slug: "dubbo-spring"
draft: false
lastmod: "2015-11-11T13:59:04Z"
id: 2256148
url: "https://mvplee.iteye.com/blog/2256148"
---

Dubbo\[![](http://dubbo.io/dubbo-pronounced.jpg-version=1&modificationDate=1325057267000.jpg)]是一个分布式服务框架，致力于提供高性能和透明化的[RPC]()远程服务调用方案，以及[SOA]()服务治理方案。

其核心部分包含:

- **远程通讯:** 提供对多种基于长连接的NIO框架抽象封装，包括多种线程模型，序列化，以及“请求-响应”模式的信息交换方式。
- **集群容错:** 提供基于接口方法的透明远程过程调用，包括多协议支持，以及软负载均衡，失败容错，地址路由，动态配置等集群支持。
- **自动发现:** 基于注册中心目录服务，使服务消费方能动态的查找服务提供方，使地址透明，使服务提供方可以平滑增加或减少机器。

### []()Dubbo能做什么？

- 透明化的远程方法调用，就像调用本地方法一样调用远程方法，只需简单配置，没有任何API侵入。
- 软负载均衡及容错机制，可在内网替代F5等硬件负载均衡器，降低成本，减少单点。
- 服务自动注册与发现，不再需要写死服务提供方地址，注册中心基于接口名查询服务提供者的IP地址，并且能够平滑添加或删除服务提供者。

 

 

 

![](http://dl2.iteye.com/upload/attachment/0113/0056/91420c9d-5056-3c60-b016-fc9b83f5579a.jpg)

Spring配置本地bean时

```
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
<bean id=“xxxAction” class=“com.xxx.XxxAction”>
    <property name=“xxxService” ref=“xxxService” />
</bean>
```

远程服务配置时，需要增加在提供方增加暴露服务配置&lt;dubbo:service&gt;，在消费方增加引用服务配置&lt;dubbo:reference&gt;

服务方配置

```
 <!-- 和本地服务一样实现远程服务 -->
<bean id=“xxxService” class=“com.xxx.XxxServiceImpl” />
 <!-- 增加暴露远程服务配置 --> 
<dubbo:service interface=“com.xxx.XxxService” ref=“xxxService” />
```

消费方配置

```
<!-- 增加引用远程服务配置 -->
<dubbo:reference id=“xxxService” interface=“com.xxx.XxxService” /> 
 <!-- 和本地服务一样使用远程服务 -->
<bean id=“xxxAction” class=“com.xxx.XxxAction”>
    <property name=“xxxService” ref=“xxxService” />
</bean>
```

 

 来个栗子，接口

```
public interface DemoService {
    String sayHello(String name);
}
```

 实现

```
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    } 
}
```

 服务方配置

```
<!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
    <!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
```

 消费方配置

```
    <!-- 生成远程服务代理，可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
```

 

 bubbo用户指南

[http://dubbo.io/User+Guide-zh.htm](http://dubbo.io/User+Guide-zh.htm)