个人网站建立,潍坊网络推广个人合作,企业门户网站制作,j建设网站备案流程我们在spring中使用dubbox相当简单#xff0c;只需要配置一下即可
1. dubbox的配置
1.1 提供方
所谓的提供方#xff0c;在我们的开发中一般指的是服务层
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springfram…我们在spring中使用dubbox相当简单只需要配置一下即可
1. dubbox的配置
1.1 提供方
所谓的提供方在我们的开发中一般指的是服务层
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:dubbohttp://code.alibabatech.com/schema/dubbo xmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd!-- 访问Dubbox所要占用的端口自己要占用的端口 该配置这里不可以省略否则后面会出现端口占用的情况--dubbo:protocol namedubbo port20884/dubbo:protocoldubbo:application namesearch-service/ !-- 这里的端口是服务端提供的端口号是服务器上注册中心提供的端口 --dubbo:registry addresszookeeper://192.168.25.130:2181/dubbo:annotation packagecom.search.service.impl / /beans
1.2 消费方
所谓的消费方在我们开发中一般指的是web层也就是调用方
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:phttp://www.springframework.org/schema/pxmlns:contexthttp://www.springframework.org/schema/contextxmlns:dubbohttp://code.alibabatech.com/schema/dubbo xmlns:mvchttp://www.springframework.org/schema/mvcxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdcontext:property-placeholder locationclasspath:config/application.properties /mvc:annotation-drivenmvc:message-converters register-defaultstruebean classcom.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter property namesupportedMediaTypes valueapplication/json/property namefeaturesarrayvalueWriteMapNullValue/valuevalueWriteDateUseDateFormat/value/array/property/bean/mvc:message-converters /mvc:annotation-driven!-- 引用dubbo 服务 --dubbo:application namesearch-web /dubbo:registry addresszookeeper://192.168.25.130:2181/dubbo:annotation packagecom.search.controller / /beans
注意服务方与消费方的注册中心地址要一致
2. dubbox的开发
在提供方不需要做额外的操作我们关心的是在消费方也就是web层的调用方怎么能访问到注册中心的服务用dubbox提供的注解Reference就可以。
例如
RestController
RequestMapping(/itemsearch)
public class ItemSearchController {Referenceprivate ItemSearchService itemSearchService;RequestMapping(/search)public MapString, 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的超时时间优先读取消费方的超时时间。