Dubbox——应用

我们在spring中使用dubbox相当简单,只需要配置一下即可

1. dubbox的配置

1.1 提供方

所谓的提供方,在我们的开发中一般指的是服务层

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 访问Dubbox所要占用的端口(自己要占用的端口) 该配置这里不可以省略,否则后面会出现端口占用的情况-->
    <dubbo:protocol name="dubbo" port="20884"></dubbo:protocol>
    
	<dubbo:application name="search-service"/>  
	
	<!-- 这里的端口是服务端提供的端口号,是服务器上注册中心提供的端口 -->
    <dubbo:registry address="zookeeper://192.168.25.130:2181"/>
    <dubbo:annotation package="com.search.service.impl" />  
   
   
</beans>

1.2 消费方

所谓的消费方,在我们开发中一般指的是web层,也就是调用方

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:property-placeholder location="classpath:config/application.properties" />
	
	<mvc:annotation-driven>
	  <mvc:message-converters register-defaults="true">
	    <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  
	      <property name="supportedMediaTypes" value="application/json"/>
	      <property name="features">
	        <array>
	          <value>WriteMapNullValue</value>
	          <value>WriteDateUseDateFormat</value>
	        </array>
	      </property>
	    </bean>
	  </mvc:message-converters>  
	</mvc:annotation-driven>

	<!-- 引用dubbo 服务 -->
	<dubbo:application name="search-web" />
	<dubbo:registry address="zookeeper://192.168.25.130:2181"/>
	<dubbo:annotation package="com.search.controller" />  	

</beans>

注意:服务方与消费方的注册中心地址要一致

2. dubbox的开发

在提供方,不需要做额外的操作,我们关心的是在消费方也就是web层的调用方怎么能访问到注册中心的服务,用dubbox提供的注解@Reference就可以。

例如;

@RestController
@RequestMapping("/itemsearch")
public class ItemSearchController {
	
	@Reference
	private ItemSearchService itemSearchService;
	
	@RequestMapping("/search")
	public Map<String, Object> search(@RequestBody Map searchMap ){
		return itemSearchService.search(searchMap);
	}

}

通过@Reference注解可以实现远程注入,在web层我们就可以调用该接口的方法了。

3. dubbox的超时配置

        我们在进行dubbox的开发的时候,经常会遇到dubbox的timeout的错误,这个错误的原因是dubbox默认连接时间是1秒钟,当我们的连接时间超过了这个时间的时候就会报超时错误了,所以我们可以给dubbox配置一个超时时间。

配置的方式有两种,一种是在提供方配置,例如;

注意:这里的service注解的包不是spring的是dubbox的com.alibaba.dubbo.config.annotation.Service; 

另一种是在消费方配置,例如:

当然,通常情况下,我们都是在提供方配置超时时间。 但是,我们要是两边都配置了超时时间,而且时间还不一样的时候,这个超时时间按照消费方配置的为准,也就是dubbox的超时时间优先读取消费方的超时时间。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值