需求
某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;
我们现在 需要创建两个服务模块进行测试
模块 | 功能 |
---|---|
订单服务web模块 | 创建订单等 |
用户服务service模块 | 查询用户地址等 |
测试预期结果:
订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。
创建Maven 工程 (dubbo_test)
引入依赖:
<dependencies>
<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 客户端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
创建子模块:(myinterface)
实体类:UserAddress
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-否
public UserAddress() {
super();
// TODO Auto-generated constructor stub
}
public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum,
String isDefault) {
super();
this.id = id;
this.userAddress = userAddress;
this.userId = userId;
this.consignee = consignee;
this.phoneNum = phoneNum;
this.isDefault = isDefault;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
public String getIsDefault() {
return isDefault;
}
public void setIsDefault(String isDefault) {
this.isDefault = isDefault;
}
@Override
public String toString() {
return "UserAddress{" +
"id=" + id +
", userAddress='" + userAddress + '\'' +
", userId='" + userId + '\'' +
", consignee='" + consignee + '\'' +
", phoneNum='" + phoneNum + '\'' +
", isDefault='" + isDefault + '\'' +
'}';
}
}
UserService
/**
* 用户服务
* @author lfy
*
*/
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List<UserAddress> getUserAddressList(String userId);
}
OrderService
public interface OrderService {
/**
* 初始化订单
* @param userId
*/
public List<UserAddress> initOrder(String userId);
}
创建用户服务模块:(user-service-provider)
引入myinterface依赖:
<dependencies>
<dependency>
<groupId>pers.zhang</groupId>
<artifactId>myinterface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
实现UserService,模拟从数据库中获得地址数据
public class UserServiceImpl implements UserService {
public List<UserAddress> getUserAddressList(String userId) {
UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层","1","李老师","101-56253825", "Y");
UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层","1","王老师", "010-562531", "N");
return Arrays.asList(address1, address2);
}
}
配置dubbo:(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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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="20880"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="pers.zhang.service.UserService"
ref="userServiceImpl" timeout="1000" version="1.0.0">
</dubbo:service>
<!-- 服务的实现 -->
<bean id="userServiceImpl" class="pers.zhang.service.impl.UserServiceImpl"></bean>
<!-- 连接监控中心 -->
<dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>
启动类:(MainApplication)
public class MainApplication {
public static void main(String[] args) throws Exception{
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
//启动spring会自动加载服务
ioc.start();
//阻塞
System.in.read();
}
}
创建订单模块:(order-service-consumer)
引入myinterface依赖:
<dependencies>
<dependency>
<groupId>pers.zhang</groupId>
<artifactId>myinterface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
实现OrderService:
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
UserService userService;
public List<UserAddress> initOrder(String userId) {
System.out.println(userId);
//调用UserService查询用户收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userId);
return userAddressList;
}
}
配置dubbo:(consumer.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">
<context:component-scan base-package="pers.zhang.service.impl"></context:component-scan>
<!-- 应用名 -->
<dubbo:application name="order-service-consumer"></dubbo:application>
<!-- zookeeper地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
<!--声明需要调用的远程服务的接口;生成远程服务代理 -->
<dubbo:reference interface="pers.zhang.service.UserService"
id="userService" timeout="5000" retries="3" version="*">
</dubbo:reference>
<!-- 监控器 -->
<dubbo:monitor protocol="registry"></dubbo:monitor>
</beans>
启动类:MainApplication
public class MainApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = ioc.getBean(OrderService.class);
//远程调用获取地址信息
List<UserAddress> userAddresses = orderService.initOrder("1");
for (UserAddress ua : userAddresses)
System.out.println(ua);
}
}
测试
启动zookeeper:
启动dubbo-admin:
访问7001端口
启动监视器:
启动User模块:
启动Order模块:
控制台打印:
1
UserAddress{id=1, userAddress='北京市昌平区宏福科技园综合楼3层', userId='1', consignee='李老师', phoneNum='101-56253825', isDefault='Y'}
UserAddress{id=2, userAddress='深圳市宝安区西部硅谷大厦B座3层', userId='1', consignee='王老师', phoneNum='010-562531', isDefault='N'}
调用成功!
查看Dubbo Admin:
查看监视器: