作者:陈树义
Dubbo 是一个分布式服务框架,致力于提供高性能和透明化的 RPC 远程服务调用方案,是阿里巴巴 SOA 服务化治理方案的核心框架,每天为 2,000+ 个服务提供 3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点。
Dubbo 采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入,只需用Spring加载Dubbo的配置即可,Dubbo基于Spring的Schema扩展进行加载。
根据 DUBBO 官方文档,配置 DUBBO 有 4 种方式,分别是:
- 1. XML 配置文件方式
- 2. properties 配置文件方式
- 3. annotation 配置方式
- 4. API 配置方式
- 这里我们只介绍前两种方式的配置。
1、XML 配置文件方式
创建 DubboDemo 项目,并创建 interface 模块、provider 模块、consumer 模块,它们都是 DubboDemo 的子模块。其中 interface 模块存放所有的接口、provider 模块提供服务、consumer 消费服务。创建完成后的项目结构如下:
![44ba69806525734e086764bd1e449325.png](https://i-blog.csdnimg.cn/blog_migrate/eeba05073a6eb5d6a3f48d1e2ef4ec47.jpeg)
在 DubboDemo 模块 pom.xml 加入如下依赖,为所有模块提供 JUnit 和 LOG4J 依赖。
+ View Code
同时在 provider 模块和 consumer 模块的 resources 目录里加入 log4j.properties 配置文件:
![05067b39de16c8e8db78d24f10efdf20.gif](https://i-blog.csdnimg.cn/blog_migrate/2b7919844403fedd6fae6e272b55b38a.gif)
log4j.properties
在 interface 模块中创建接口 com.chanshuyi.service.IUserService:
![05067b39de16c8e8db78d24f10efdf20.gif](https://i-blog.csdnimg.cn/blog_migrate/2b7919844403fedd6fae6e272b55b38a.gif)
IUserService.java
interface 模块配置完毕。
在 provider 模块中引入 Spring、Dubbo、interface 模块依赖:
![05067b39de16c8e8db78d24f10efdf20.gif](https://i-blog.csdnimg.cn/blog_migrate/2b7919844403fedd6fae6e272b55b38a.gif)
pom.xml
创建 com.chanshuyi.service.impl.UserServiceImpl 类,实现 IUserService 接口:
![4ba049aabd674c5c57dc26677662015d.gif](https://i-blog.csdnimg.cn/blog_migrate/6ce330901290a14dba1882762fc554a6.gif)
package com.chanshuyi.service.impl;import com.chanshuyi.service.IUserService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Service;/** * Created by chanshuyi on 2016/1/19. */@Service("userService")public class UserServiceImpl implements IUserService { private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); @Override public boolean login(String username, String password) { logger.info("用户登录:[username:{}, password:{}]