dubbo之快速书写出提供者消费者案例

参考:http://dubbo.apache.org/en-us/docs/user/quick-start.html

先启动zookeeper,在启动dubbo,最后进行测试

第一步:创建3个maven工程(user-service-provider、order-service-consumer、mall-interface)

第二步:书写mall-interface的bean和service接口

bean:

package com.jack.mall.bean;

import java.io.Serializable;

public class UserAddress implements Serializable{

	private Integer id;
	private String userAddress;
	private String userId;
	private String consignee;
	private String phoneNum;
	private String isDefault;
	
	public UserAddress() {
		super();
	}
	
	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:

package com.jack.mall.service;

import java.util.List;

import com.jack.mall.bean.UserAddress;
/**
 * 用户服务(提供者)
 * @author richard
 *
 */
public interface UserService {
	/**
	 * 按照用户id返回所有的用户地址
	 * @param userId
	 * @return
	 */
	public List<UserAddress> getUserAddressList(String userId);
}
package com.jack.mall.service;

public interface OrderService {
	/**
	 * 初始化订单
	 * @param userId
	 */
	public void initOrder(String userId);	
	
}

第三步:书写user-service-provider的接口实现类和provider.xml配置文件和pom.xml依赖文件

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.jack.mall</groupId>
  <artifactId>user-service-provider</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  	<dependency>
  		<groupId>com.jack.mall</groupId>
  		<artifactId>mall-interface</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<!-- 引入dobbo -->
  	<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>

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.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
	<!-- 1、配置应用名(同样的服务名字相同,不要和别的服务同名) -->
	<dubbo:application name="user-service-provider"/>
	<!-- 2、指定注册中心的位置 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
	<!-- 指定通信规则 -->
	<dubbo:protocol name="dubbo" port="20880"/>
	<!-- 3、暴露服务 ref指向服务真正的实现对象-->
	<dubbo:service interface="com.jack.mall.service.UserService" ref="userServiceImpl"/>
	<!-- 服务的实现 -->
    <bean id="userServiceImpl" class="com.jack.mall.service.impl.UserServiceImpl"/>
    
</beans>

UserServiceImpl.java:

package com.jack.mall.service.impl;

import java.util.Arrays;
import java.util.List;
import com.jack.mall.bean.UserAddress;
import com.jack.mall.service.UserService;

public class UserServiceImpl implements UserService{

	public List<UserAddress> getUserAddressList(String userId) {
		//模仿查询数据库
		UserAddress address1=new UserAddress(1,"北京","1","张三","123","1");
		UserAddress address2=new UserAddress(2,"北京","2","李四","456","2");
		return Arrays.asList(address1,address2);
	}
}

MainApplication.java:

package com.jack.mall.service.impl;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApplication {
	
	public static void main(String[] args) throws IOException {
		//读取provider.xml配置文件
		ClassPathXmlApplicationContext ioc=new ClassPathXmlApplicationContext("provider.xml");
		ioc.start();		
		//进行阻塞测试
		System.in.read();
	}
}

 

第四步:书写user-service-consumer的接口实现类和consumer.xml配置文件和pom.xml依赖文件

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.jack.mall</groupId>
  <artifactId>order-service-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>

 <dependencies>
 	<dependency>
 		<groupId>com.jack.mall</groupId>
 		<artifactId>mall-interface</artifactId>
 		<version>0.0.1-SNAPSHOT</version>
 	</dependency>
 	<dependency>
  		<groupId>com.jack.mall</groupId>
  		<artifactId>mall-interface</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<!-- 引入dobbo -->
  	<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>

consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-2.5.xsd"     
       >
    <!-- 包扫描 -->
	<context:component-scan base-package="com.jack.mall.service.impl"/>
	<!-- 1、配置应用名(同样的服务名字相同,不要和别的服务同名) -->
	<dubbo:application name="order-service-consumer"/>
	<!-- 2、指定注册中心的位置 -->
	<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
	<!-- 3、声明需要调用的远程服务接口,生成远程服务代理-->
	<dubbo:reference id="userService" interface="com.jack.mall.service.UserService" />   
</beans>

OrderSericeImpl.java:

package com.jack.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jack.mall.bean.UserAddress;
import com.jack.mall.service.OrderService;
import com.jack.mall.service.UserService;
/**
 * 同微信公众号是一个意思
 * 第一步:将服务提供者注册到注册中心(暴露服务)
 * 	1、在pom.xml导入dubbo依赖和引入操作zookeeper的客户端
 * 	2、配置服务提供者
 * 
 * 第二步:让服务消费者去注册中心订阅服务提供者的服务地址
 * 
 *
 */
@Service
public class OrderSericeImpl implements OrderService{
	@Autowired
	UserService userService;

	public void initOrder(String userId) {
		System.out.println("用户id"+userId);
		//第一步:查询用户的收获
		List<UserAddress> addressList=userService.getUserAddressList(userId);
		System.out.println(addressList);	
		for(UserAddress userAddress:addressList) {
			System.out.println(userAddress.getUserAddress());
		}
		
	}
}

MainApplication:

package com.jack.mall.service.impl;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jack.mall.service.OrderService;

public class MainApplication {
  @SuppressWarnings("resource")
  public static void main(String[] args) throws IOException {
	  //读取consumer.xml配置文件
	  ClassPathXmlApplicationContext application=new ClassPathXmlApplicationContext("consumer.xml");
	  //获取主键对象
	  OrderService orderService=application.getBean(OrderService.class);
	  
	  orderService.initOrder("1");
	  System.out.println("调用完成");
	  //阻塞
	  System.in.read();
  }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值