Dubbo Spring配置文件使用

一、API(分包)

建议将服务接口、服务模型、服务异常等均放在 API 包中,因为服务模型和异常也是 API 的一部分,这样做也符合分包原则:重用发布等价原则(REP),共同重用原则(CRP)。

  • bean
    • UserAddress
  • service
    • OrderService
    • UserService

二、服务提供者

2.1 引入Maven依赖

  1. API包
  2. dubbo包
  3. 操作zookeeper的包

pom.xml

<dependencies>
    <!-- 引入api -->
  	<dependency>
  		<groupId>com.cjm.gmall</groupId>
  		<artifactId>gmall-interface</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<!-- 引入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>
</dependencies>

2.2 编写实现类

UserServiceImpl

/**
 * 1、将服务提供者注册到注册中心
 * 		1)导入dubbo依赖(2.6.2)、操作zookeeper的客户端依赖(curator)
 * 		2)配置服务提供者
 * 2、让服务消费者去注册中心订阅服务提供者的服务地址
 * @author 陈嘉名
 *
 */
public class UserServiceImpl implements UserService {

	public List<UserAddress> getUserAddressList(String userId) {
		System.out.println("UserServiceImpl.....old...");
		// TODO Auto-generated method stub
		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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		return Arrays.asList(address1,address2);
	}
}

2.3 Spring配置文件

  1. 服务名:dubbo:application
  2. 注册中心:dubbo:registry
  3. 通信协议:dubbo:protocol
  4. 暴露服务:<dubbo:service
  5. 服务提供方:dubbo:provider

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
		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 address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
	<dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
	
	<!-- 3、指定通信规则(通信协议?通信端口) -->
	<dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
	
	<!-- 4、暴露服务   ref:指向服务的真正的实现对象 -->
	<dubbo:service interface="com.cjm.gmall.service.UserService" ref="userServiceImpl"></dubbo:service>
	
	<!-- 服务的实现 -->
	<bean id="userServiceImpl" class="com.cjm.gmall.service.impl.UserServiceImpl"></bean>
	
	<!--统一设置服务提供方的规则  -->
	<dubbo:provider timeout="1000"></dubbo:provider>
	
</beans>

2.4 启动类

读取并加载Spring配置文件provider.xml,启动项目。

MainApplication

public class MainApplication {
	public static void main(String[] args) throws IOException {
		ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
		ioc.start();
		System.in.read();
	}
}

三、服务消费者

3.1 引入Maven依赖

  1. API包
  2. dubbo包
  3. 操作zookeeper的包

pom.xml

<dependencies>
    <!-- 引入api -->
  	<dependency>
  		<groupId>com.cjm.gmall</groupId>
  		<artifactId>gmall-interface</artifactId>
  		<version>0.0.1-SNAPSHOT</version>
  	</dependency>
  	<!-- 引入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>
</dependencies>

3.2 编写实现类

OrderUserviceImpl

/**
 * 1、将服务提供者注册到注册中心
 * 		1)导入dubbo依赖(2.6.2)、操作zookeeper的客户端依赖(curator)
 * 		2)配置服务提供者
 * 2、让服务消费者去注册中心订阅服务提供者的服务地址
 * @author 陈嘉名
 *
 */
@Service
public class OrderUserviceImpl implements OrderService{
	
	@Autowired
	private UserService userService;
	
	public void initOrder(String userId) {
		// TODO Auto-generated method stub
		System.out.println("用户id:"+userId);
		//1、查询用户的收货地址
		List<UserAddress> addressList= userService.getUserAddressList(userId);
		System.out.println(addressList);
		for(UserAddress userAddress:addressList) {
			System.out.println(userAddress.getUserAddress());
		}
	}	
}

3.3 Spring配置文件

  1. 服务名:dubbo:application
  2. 注册中心:dubbo:registry
  3. 引用服务:dubbo:reference
  4. 服务消费方:dubbo:consumer

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.cjm.gmall.service.impl"></context:component-scan>


	<dubbo:application name="order-service-consumer"></dubbo:application>
	
	<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
	
	<dubbo:reference interface="com.cjm.gmall.service.UserService" id="userService"></dubbo:reference>
		
	<!-- 配置当前消费者的统一规则:所有的服务都不检查 -->
	<dubbo:consumer check="false" timeout="5000"></dubbo:consumer>
	
</beans>

3.4 启动类

MainApplication

public class MainApplication {

	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();
	}
}

GitHub
gmall-interface
user-service-provider
order-service-consumer

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChenBbMing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值