一、提出需求
某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;
我们现在 需要创建两个服务模块进行测试:
订单服务 web 模块 查询用户地址等
用户服务 service 模块 查询用户地址等
测试预期结果:订单服务 web 模块在 A 服务器,用户服务模块在 B 服务器,A 可以远程调用 B 的功能。

二、工程架构
根据 dubbo《服务化最佳实践》
1、分包
建议将服务接口,服务模型,服务异常等均放在 API 包中,因为服务模型及异常也是 API 的一部分,同时,这样做也符合分包原则:重用发布等价原则(REP),共同重用原则(CRP)。
如果需要,也可以考虑在 API 包中放置一份 spring 的引用配置,这样使用方,只需在 spring 加载过程中引用此配置即可,配置建议放在模块的包目录下,以免冲突,如:com/alibaba/china/xxx/dubbo-reference.xml。
2、粒度
服务接口尽可能大粒度,每个服务方法应代表一个功能,而不是某功能的一个步骤,否则将面临分布式事务问题,Dubbo 暂未提供分布式事务支持。
服务接口建议以业务场景为单位划分,并对相近业务做抽象,防止接口数量爆炸。
不建议使用过于抽象的通用接口,如:Map query(Map),这样的接口没有明确语义,会给后期维护带来不便。

三、创建模块
1、gmall-interface:公共接口层(model,service,exception...)
作用:定义公共接口,也可以导入公共依赖。
(1)创建 Bean 模型
public class UserAddress implements Serializable {
private Integer id;
private String userAddress; //用户地址
private String userId; //用户id
private String consignee; //收货人
private String phoneNum; //电话号码
private String isDefault; //是否为默认地址 Y-是 N-否
}
(2)创建 Service 接口
OrderService
public interface OrderService {
/**
* 初始化订单
* @param userId
*/
public List<UserAddress> initOrder(String userId);
}
------------
UserService
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}

2、user-service-provider:用户模块(对用户接口的实现)【提供者】
(1)引入公共模块
<dependency>
<groupId>com.njf.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
(2)实现 UserService
public class UserServiceImpl implements UserService {
@Override
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N");
return Arrays.asList(address1,address2);
}
}
3、order-service-consumer:订单模块(调用用户模块)【消费者】
(1)引入公共模块
<dependency>
<groupId>com.njf.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
(2)实现 OrderService,并调用 UserService
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户ID:" + userId);
//1.查询用户的收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userId);
System.out.println("userAddressList = " + userAddressList);
userAddressList.forEach(t -> System.out.println(t.getUserAddress()));
System.out.println("调用完成!");
return userAddressList;
}
}
4、存在的问题
这样是无法进行调用的,我们在 order-service-consumer 中引入了 gmall-interface,但是 interface 的实现是 user-service-provider,我们并没有引入,而且实际它可能还在别的服务器中。
四、使用 dubbo 改造
1、改造 user-service-provider 作为服务提供者
改造步骤:
-
将服务提供者注册到注册中心(暴露服务) -
导入 dubbo 依赖(2.6.2 版本) \ 操作 zookeeper 的客户端 (curator) -
配置服务提供者 -
让服务消费者去注册中心订阅服务提供者的服务地址
(1)引入 dubbo 依赖和操作 zookeeper 的客户端
<dependencies>
<dependency>
<groupId>com.njf.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入 dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是 zookeeper,引入操作 zookeeper的客户端 -->
<!-- 由于我们使用zookeeper作为注册中心,所以需要操作zookeeper
dubbo 2.6以前的版本引入zkclient操作zookeeper
dubbo 2.6及以后的版本引入curator操作zookeeper
下面两个zk客户端根据dubbo版本2选1即可
-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
(2)配置提供者,创建 provider.xml 进行配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="user-service-provider"></dubbo:application>
<!-- 2、指定注册中心的位置 -->
<!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
<!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="com.njf.gmall.service.UserService" ref="userServiceImpl01" />
<!-- 服务的实现 -->
<bean id="userServiceImpl01" class="com.njf.gmall.service.impl.UserServiceImpl"></bean>
</beans>
dubbo 的 XML 配置:https://dubbo.apache.org/zh/docs/references/xml/
(3)启动服务,进行测试,同时启动 zk 注册中心和 dubbo-admin 管理平台
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
//按任意键退出
System.in.read();
}
}
(4)测试
在 dubbo-admin 管理平台就可以看到启动的提供者服务了。




2、改造 order-service-consumer 作为服务消费者
(1)引入 dubbo 和 zkClient 客户端
<dependencies>
<dependency>
<groupId>com.njf.gmall</groupId>
<artifactId>gmall-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--引入 dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的是 zookeeper,引入操作 zookeeper的客户端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
(2)创建 consumer.xml 并配置,通过 Spring 配置引用远程服务
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<context:component-scan base-package="com.njf.gmall.service.impl"></context:component-scan>
<dubbo:application name="order-service-consumer"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--声明需要调用的远程服务的接口;生成远程服务代理 -->
<dubbo:reference interface="com.njf.gmall.service.UserService" id="userService" >
</dubbo:reference>
</beans>
(3)使用远程的服务
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户ID:" + userId);
//1.查询用户的收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userId);
System.out.println("userAddressList = " + userAddressList);
userAddressList.forEach(t -> System.out.println(t.getUserAddress()));
System.out.println("调用完成!");
return userAddressList;
}
}
(4)启动测试
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = ioc.getBean(OrderService.class);
orderService.initOrder("1");
System.in.read();
}
}
测试结果:


可以看到在消费者中使用 UserService 获取用户地址,调用成功了,说明 order-service 可以调用远程的 user-service 了。
至此,使用 dubbo 进行 RPC 的远程调用过程结束。
五、注解版
1、服务提供方
在配置文件中添加注解方式:
<!--使用 @Service 使用dubbo提供的service注解,注册暴露服务-->
<dubbo:annotation package="com.njf.gmall.service.impl"/>
UserService:
import com.alibaba.dubbo.config.annotation.Service;
@Service //使用dubbo提供的service注解,注册暴露服务
public class UserServiceImpl implements UserService {}
2、服务消费方
在配置文件中添加注解方式:
<!--使用 @Service 使用dubbo提供的service注解,注册暴露服务-->
<dubbo:annotation package="com.njf.gmall.service.impl"/>
OrderService:
import com.alibaba.dubbo.config.annotation.Reference;
@Service
public class OrderServiceImpl implements OrderService {
@Reference //使用dubbo提供的reference注解引用远程服务
UserService userService;
@Override
public List<UserAddress> initOrder(String userId) {
System.out.println("用户ID:" + userId);
//1.查询用户的收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userId);
System.out.println("userAddressList = " + userAddressList);
userAddressList.forEach(t -> System.out.println(t.getUserAddress()));
System.out.println("调用完成!");
return userAddressList;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现