【Dubbo】二、入门案例
程序员文章站
2022-06-02 15:28:01
...
文章目录
环境搭建
-
搭建服务器
java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
命令打开服务,后访问http://localhost:7001/
即可在dubbo admin 控制台看到服务者和消费者的信息
- 准备两个mvn普通java工程
3. 对服务者、消费者工程pom.xml引入依赖
注:版本问题这里两个依赖搭配必须同时引入
<!-- 引入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>
公共接口模块
项目名称:gmall_interface
<modelVersion>4.0.0</modelVersion>
<groupId>zy.code</groupId>
<artifactId>gmall_interface</artifactId>
<version>1.0-SNAPSHOT</version>
用户接口 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);
}
JavaBean UserAddress
/**
* 用户地址
* @author lfy
*
*/
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;
}
}
服务提供者
1. 在服务提供方实现接口
UserServiceImpl
public class UserServiceImpl implements UserService {
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);
}
}
2. spring配置暴露服务
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 protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 3、指定通信规则(通信协议?通信端口) -->
<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
<!-- 4、暴露服务 ref:指向服务的真正的实现对象 -->
<dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl">
</dubbo:service>
<!-- 服务的实现 -->
<bean id="userServiceImpl" class="com.atguigu.gmall.service.impl.UserServiceImpl"></bean>
</beans>
3. 测试服务者
public class providerTest {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext ioc =new ClassPathXmlApplicationContext("provider.xml");
ioc.start();
System.in.read();
}
}
服务消费者
1. 消费方远程调用服务方
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private UserService userService;
public void initOrder(String userId) {
// 查询用户的收货地址
List<UserAddress> userAddressList = userService.getUserAddressList(userId);
for (UserAddress userAddress:userAddressList){
System.out.println(userAddress);
}
}
}
2. Spring 配置消费方
consumer.xml
<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"
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://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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.atguigu.gmall.service.impl"></context:component-scan>
<!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
<dubbo:application name="order-service-consumer"></dubbo:application>
<!-- 2、指定注册中心的位置 -->
<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
<!-- 声明需要远程调用的服务的接口,生成远程服务代理-->
<dubbo:reference id="userService" interface="com.atguigu.gmall.service.UserService" ></dubbo:reference>
</beans>
3.测试消费方
consumerTest.java
public class ConsumerTest {
@SuppressWarnings("resource")
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();
}
}
下一篇: 正向代理和方向代理的区别和使用