以下结合Dubbo的官方文档,写了一个Dubbo的简单应用,包括注册中心、消费者和生产者的关系调用。
一、公共配置:api工程
- 为了方便消费者和生成着的服务调用,采用分包的思想,抽取抽取公共配置,公共接口和公共模型等。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<UserAddress> 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<UserAddress> getUserAddressList(String userId);
}
二、produce工程(生产者)
1.工程结构大致如下:
2.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="produce"></dubbo:application>
<!-- 2.指定注册服务中心的位置 -->
<!-- <dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry> -->
<dubbo:registry protocol="zookeeper" address="localhost:2181"></dubbo:registry>
<!-- 3.指定通信规则(通信协议,通信规则) -->
<dubbo:protocol name="dubbo" port="20080"></dubbo:protocol>
<!-- 4.暴露服务 ref:指定服务的真正实现对象-->
<dubbo:service interface="com.nyist.api.service.UserService" ref="userServiceImpl"></dubbo:service>
<!-- 服务的实现 -->
<bean id="userServiceImpl" class="com.nyist.produce.service.UserServiceImpl"></bean>
</beans>
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<UserAddress> 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<UserAddress> initOrder(String userId) {
System.out.println("用户ID:"+userId);
List<UserAddress> addresses = userService.getUserAddressList(userId);
for (UserAddress userAddress : addresses) {
System.out.println(userAddress);
}
return addresses;
}
}
3.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.nyist.consumer.ServiceImpl"></context:component-scan>
<!-- 1.指定当前服务应用的名字(同一个服务名称相同,不能和别的服务名称相同) -->
<dubbo:application name="consumer"></dubbo:application>
<!-- 2.指定注册服务中心的位置 -->
<dubbo:registry protocol="zookeeper" address="localhost:2181"></dubbo:registry>
<!-- 3.声明需要调用的远程服务;生成远程服务代理-->
<dubbo:reference interface="com.nyist.api.service.UserService" id="userService"></dubbo:reference>
</beans>
4.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.nyist</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build/>
<dependencies>
<dependency>
<groupId>com.nyist</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入Dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.2</version>
</dependency>
<!-- 注册中心使用的事zookper,引入Zookper -->
<!-- 由于我们使用zookeeper作为注册中心,所以需要操作zookeeper
dubbo 2.6以前的版本引入zkclient操作zookeeper
dubbo 2.6及以后的版本引入curator操作zookeeper
下面两个zk客户端根据dubbo版本2选1即可
-->
<!-- curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
</project>
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();
}
}