以下结合Dubbo的官方文档,写了一个Dubbo的简单应用,包括注册中心、消费者和生产者的关系调用。
一、公共配置:api工程
2.api/UserAddress.java
package com.nyist.api.model;
import java.io.Serializable;
/**
* 用户地址
* @author lhg
*
*/
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 + "]";
}
}
3.service包下的类
package com.nyist.api.service;
import com.nyist.api.model.UserAddress;
import java.util.List;
public interface OrderService {
/**
* 初始化订单
* @param userId
*/
public List initOrder(String userId);
}
package com.nyist.api.service;
import com.nyist.api.model.UserAddress;
import java.util.List;
/**
* 用户接口
*/
public interface UserService {
/**
* 按照用户id返回所有的收货地址
* @param userId
* @return
*/
public List getUserAddressList(String userId);
}
二、produce工程(生产者)
1.工程结构大致如下:
2.provider.xml配置
3.UserServiceImpl.java实现
package com.nyist.produce.service;
import java.util.Arrays;
import java.util.List;
import com.nyist.api.model.UserAddress;
import com.nyist.api.service.UserService;
public class UserServiceImpl implements UserService {
@Override
public List getUserAddressList(String userId) {
UserAddress userAddress = new UserAddress(1,"北京市朝阳区","1","吕老师","1803931xxx","Y");
UserAddress userAddress1 = new UserAddress(2,"广东省深圳市","1","吕爸爸","1803931xxxx","N");
return Arrays.asList(userAddress,userAddress1);
}
}
4.测试
MainApplication.java
package com.nyist.produce;
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();
}
}
三、consumer 工程(消费者)
1.工程结构如下:
2.OrderServiceImpl.java如下:
package com.nyist.consumer.ServiceImpl;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nyist.api.model.UserAddress;
import com.nyist.api.service.OrderService;
import com.nyist.api.service.UserService;
/**
* 1.将服务提供者注册到注册中心(如何暴露服务)
* 1.导入Dubbo依赖(2.6.2)
* 2.配置服务
* 2.让服务消费者去服务中心订阅提供者订阅服务
*
* @author lhg
*
*/
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private UserService userService;
@Override
public List initOrder(String userId) {
System.out.println("用户ID:"+userId);
List addresses = userService.getUserAddressList(userId);
for (UserAddress userAddress : addresses) {
System.out.println(userAddress);
}
return addresses;
}
}
3.consumer.xml
4.pom.xml文件
4.0.0
com.nyist
consumer
0.0.1-SNAPSHOT
com.nyist
api
0.0.1-SNAPSHOT
com.alibaba
dubbo
2.6.2
org.apache.curator
curator-framework
2.12.0
5.测试
MainApplication.java
package com.nyist.consumer;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.nyist.api.service.OrderService;
public class MainApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
OrderService orderService = context.getBean(OrderService.class);
orderService.initOrder("1");
System.out.println("调用完成!!!");
System.in.read();
}
}