使用Dubbo进行远程调用实现服务交互,它支持多种协议,如Hessian、HTTP、RMI、Memcached、Redis等等。由于Dubbo将这些协议的实现进行了封装了,无论是服务端(开发服务)还是客户端(调用服务),都不需要关心协议的细节,只需要在配置中指定使用的协议即可,从而保证了服务提供方与服务消费方之间的透明。
Dubbo的客户端和服务端有三种连接方式,分别是:广播,直连和使用zookeeper注册中心
服务端配置
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.taotao.manager.service.impl"/>
<!-- 配置dubbo -->
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-manager-service"/>
<!-- 使用multicast广播注册中心暴露服务地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<!--改用直连方式-->
<!--<dubbo:registry address="N/A"/>-->
<dubbo:registry protocol="zookeeper" address="192.168.37.161:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 声明需要暴露的服务接口 -->
<!-- <dubbo:service interface="com.taotao.manager.service.ItemCatService" -->
<!-- ref="itemCatServiceImpl" /> -->
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.taotao.manager.service.TestService" ref="testServiceImpl"/>
<dubbo:service interface="com.taotao.manager.service.ItemCatService" ref="itemCatServiceImpl"/>
<dubbo:service interface="com.taotao.manager.service.ItemDescService" ref="itemDescServiceImpl"/>
<dubbo:service interface="com.taotao.manager.service.ItemService" ref="itemServiceImpl"/>
<dubbo:service interface="com.taotao.manager.service.ContentService" ref="contentServiceImpl"/>
<dubbo:service interface="com.taotao.manager.service.ContentCategoryService" ref="contentCategoryServiceImpl"/>
</beans>
从配置文件可以看出,首先第一步
<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="taotao-manager-service"/>
第二步声明使用的通讯方式,和端口
<dubbo:protocol name="dubbo" port="20880"/>
通讯方式有三种
广播
<dubbo:registry address="multicast://224.5.6.7:1234"/>
直连
<dubbo:registry address="N/A"/>
注册中心。
<dubbo:registry protocol="zookeeper" address="192.168.37.161:2181"/>
第三步声明提供的服务接口
<dubbo:service interface ="接口" ref="接口实现类">
三步服务端完成。
接着接口调用方
<!-- 配置dubbo服务 -->
<dubbo:application name="taotao-manager-web" />
<!-- 使用广播 -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
<!--使用zookeeper-->
<dubbo:registry protocol="zookeeper" address="192.168.37.161:2181"/>
<!-- 声明要调用的服务,timeout是设置连接超时最长时间,如果不设置,超时时间默认是3秒 -->
<dubbo:reference interface="com.taotao.manager.service.TestService" id="testService" timeout="1000000" />
<dubbo:reference interface="com.taotao.manager.service.ItemCatService" id="itemCatService" timeout="1000000" />
<dubbo:reference interface="com.taotao.manager.service.ItemService" id="itemService" timeout="1000000"/>
<dubbo:reference interface="com.taotao.manager.service.ContentCategoryService" id="contentCategoryService" timeout="1000000"/>
<dubbo:reference interface="com.taotao.manager.service.ContentService" id="contentService" timeout="1000000"/>
<dubbo:reference interface="com.taotao.manager.service.TestService" id="testService" timeout="1000000" />
表示根据interface属性获取到的服务程序实例对象放入spring容器,其中id做唯一标识作用。在java代码中需要注入这个服务实例的时候根据这个id注入。