dubbo的入门学习(二)
需求:
某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;
我们现在 需要创建两个服务模块进行测试
模块 | 功能 |
订单服务web模块 | 创建订单等 |
用户服务service模块 | 查询用户地址等 |
预期测试结果:
订单服务器web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。
这里制作的案例,三个模块都是在本地,真实环境可能是订单web模块在一台服务器A上,用户服务模块在另一台服务器B上,用户服务模块远程调用订单web模块的功能。
环境准备
所需:zookeeper、dubbo-admin管理控制台
zookeeper下载:
https://archive.apache.org/dist/zookeeper/zookeeper-3.4.13/
dubbo-admin下载:
https://github.com/apache/incubator-dubbo-ops 。
详细配置查看上一篇博客。
https://blog.csdn.net/qq_36654629/article/details/90052052
先将本地的zookerper服务跑起来,进入zookeeper下的bin目录,点击zkServer运行
成功跑起服务
运行dubbo-admin
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
浏览器访问:127.0.0.1:7001
默认使用root/root 登陆
创建模块
分别创建3个maven工程项目。
gmall-interface模块:公共接口层(model,service,exception…)
作用:定义公共接口,也可以导入公共依赖。
- bean
package com.atguigu.gmall.bean; import java.io.Serializable; /** * 用户地址 * @author jp * */ 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; } }
- Service接口
UserService:
package com.atguigu.gmall.service; import java.util.List; import com.atguigu.gmall.bean.UserAddress; /** * 用户服务 * @author jp * */ public interface UserService { /** * 按照用户id返回所有的收货地址 * @param userId * @return */ public List<UserAddress> getUserAddressList(String userId); }
OrderService
package com.atguigu.gmall.service; import java.util.List; import com.atguigu.gmall.bean.UserAddress; public interface OrderService { /** * 初始化订单 * @param userId */ public List<UserAddress> initOrder(String userId); }
gmall-user:用户模块(作为服务提供者,对用户接口的实现)
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.atguigu.gmall</groupId> <artifactId>user-server</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- 引入gmall-interface --> <dependency> <artifactId>gmall-interface</artifactId> <groupId>com.atguigu.gmall</groupId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- 引入dubbo --> <!-- https://mvnrepository.com/artifact/com.alibaba/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> </project>
- service
package com.atguigu.gmall.service.impl; import java.util.Arrays; import java.util.List; import com.atguigu.gmall.bean.UserAddress; import com.atguigu.gmall.service.UserService; public class UserServiceImpl implements UserService{ public List<UserAddress> getUserAddressList(String userId) { // TODO Auto-generated method stub System.out.println("UserServiceImpl.....old..."); // TODO Auto-generated method stub UserAddress address1 = new UserAddress(1, "北京市昌平区宏福科技园综合楼3层", "1", "李老师", "010-56253825", "Y"); UserAddress address2 = new UserAddress(2, "深圳市宝安区西部硅谷大厦B座3层(深圳分校)", "1", "王老师", "010-56253825", "N"); /*try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return Arrays.asList(address1,address2); } }
使用dubbo 改造gmall-user作为服务提供者,创建一个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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd"> <!-- 1.指定当前服务/应用名字(同样的服务名字相同,不要和别的服务同名 --> <dubbo:application name="user-provider" /> <!-- 2.指定注册中心的位置 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 3.指定通信规则(通信协议、通信端口) --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 4. 声明需要暴露的服务接口 ref指向服务的真正实现--> <dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userService" /> <!-- 5.服务的实现 --> <bean id="userService" class="com.atguigu.gmall.service.impl.UserServiceImpl" /> </beans>
- MainApplication.java 启动服务
package com.atguigu.gmall.service.impl; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApplication { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ioc= new ClassPathXmlApplicationContext("provider.xml"); ioc.start(); //启动容器 System.in.read(); //阻塞一下 } }
运行MainApplication.java后可以在dubbo-admin 首页看到,有一个服务数。
点击服务管理->提供者可以看到有一个服务提供者
点击IP可以看到服务的详细信息
gmall-order-web:订单模块(作为服务消费者,调用用户模块):
pom.xml 引入 gmall-interface
<dependencies> <dependency> <groupId>com.atguigu.dubbo</groupId> <artifactId>gmall-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
- Service
package com.atguigu.gmall.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.atguigu.gmall.bean.UserAddress; import com.atguigu.gmall.service.OrderService; import com.atguigu.gmall.service.UserService; @Service public class OrderServiceImpl implements OrderService{ @Autowired UserService userService; public List<UserAddress> initOrder(String userId) { // TODO Auto-generated method stub System.out.println("用户id:"+userId); //1、查询用户的收货地址 List<UserAddress> addressList = userService.getUserAddressList(userId); for (UserAddress userAddress : addressList) { System.out.println(userAddress.getUserAddress()); } return addressList; } }
配置消费者信息,创建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="com.atguigu.gmall.service.impl"></context:component-scan> <!-- 1.申明消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> <dubbo:application name="consumer-provider" /> <!--2. 使用zookeeper广播注册中心暴露发现服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!--3. 声明需要调用的远程服务 --> <dubbo:reference id="userService" interface="com.atguigu.gmall.service.UserService" /> </beans>
消费服务调用
- MainApplication.java
package com.atguigu.gmall.service.impl; import java.io.IOException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.atguigu.gmall.service.OrderService; 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(); } }
测试结果:
运行MainApplication.java可以在控制台看到,获取到的用户地址信息。说明订单模块调用到了用户模块的方法,获取到了用户信息。
并能在dubbo-admin服务治理-》应用里查看到