dubbo入门案例(xml配置)
dubbo入门案例(xml配置)
创建server-common-api项目
server-common-api中创建消费者和提供者都需要的类,实现代码复用
用户地址实体类
package com.yl.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.io.Serializable;
/**
* 用户地址
*
* @author Y-wee
*/
@AllArgsConstructor
@Getter
@Setter
@ToString
public class UserAddress implements Serializable {
/**
* 主键
*/
private Integer id;
/**
* 用户地址
*/
private String userAddress;
/**
* 用户id
*/
private String userId;
/**
* 收货人
*/
private String consignee;
/**
* 电话号码
*/
private String phoneNum;
/**
* 是否为默认地址:Y-是,N-否
*/
private String isDefault;
}
订单服务接口
package com.yl.service;
import com.yl.entity.UserAddress;
import java.util.List;
/**
* 订单
*
* @author Y-wee
*/
public interface OrderService {
/**
* 初始化订单
*
* @param userId
*/
List initOrder(String userId);
}
用户服务接口
package com.yl.service;
import com.yl.entity.UserAddress;
import java.util.List;
/**
* 用户
*
* @author Y-wee
*/
public interface UserService {
/**
* 按照用户id返回所有的收货地址
*
* @param userId
* @return
*/
List getUserAddressList(String userId);
}
创建server-provider项目
server-provider实现用户相关业务,作为服务提供者
导入依赖
com.yl
server-common-api
1.0-SNAPSHOT
compile
com.alibaba
dubbo
2.6.2
org.apache.curator
curator-framework
2.12.0
用户业务实现类
package com.yl.user.service.impl;
import com.yl.entity.UserAddress;
import com.yl.service.UserService;
import java.util.Arrays;
import java.util.List;
/**
* 用户
*
* @author Y-wee
*/
public class UserServiceImpl implements UserService {
/**
* 获取用户地址
*
* @param userId
* @return
*/
@Override
public List getUserAddressList(String userId) {
System.out.println("---UserServiceImpl.....old...");
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) {
e.printStackTrace();
}*/
return Arrays.asList(address1, address2);
}
}
创建配置文件provider.xml,编写相关配置
<?xml version="1.0" encoding="UTF-8"?>
启动类
package com.yl.user;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
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();
}
}
启动服务,将提供者以user-service-provider名称注册到zookeeper,可以通过dubbo-admin控制台查看
创建server-consumer项目
导入依赖,与server-provider一致
订单业务实现类
package com.yl.order.service.impl;
import com.yl.entity.UserAddress;
import com.yl.service.OrderService;
import com.yl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 订单
*
* @author Y-wee
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private UserService userService;
/**
* 调用用户服务
*
* @param userId
* @return
*/
@Override
public List initOrder(String userId) {
System.out.println("用户id:" + userId);
// 查询用户的收货地址
List addressList = userService.getUserAddressList(userId);
for (UserAddress userAddress : addressList) {
System.out.println(userAddress.getUserAddress());
}
return addressList;
}
}
创建配置文件consumer.xml,编写相关配置
<?xml version="1.0" encoding="UTF-8"?>
启动类
package com.yl.order;
import java.io.IOException;
import com.yl.service.OrderService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
orderService.initOrder("1");
System.out.println("---调用完成");
System.in.read();
}
}
启动服务,调用实现user-service-provider服务调用