dubbo基於http協議嵌入現有框架配置方法。
網上dubbo基於其它協議的文章有很多,基於http協議的沒搜尋到,正好近期在研究dubbo框架,把實現方法共享下。
基礎的東西就不多說了,官方首頁足夠詳細,放個連結給大家參考http://dubbo.io/User+Guide-zh.htm。
首先因為dubbo開源框架基於早期的spring版本,在使用http協議的時候,使用了低版本的spring程式碼,對現有工程有一定的侵入性,因此對spring3.0以上版本的工程,需要獲取dubbo原始碼(最新的是2.5.4),在dubbo-rpc-http工程中引入spring老版本 remoting包的程式碼,並重構出jar包。
在老工程中引入重構的jar包,如果你是maven工程匯入jar的話,還需要注意去掉老版本的spring依賴,具體配置如下<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.4-SNAPSHOT</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>。
web.xml 中加入以下配置
<servlet>
<servlet-name>dubbo</servlet-name>
<servlet-class>
com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dubbo</servlet-name>
<url-pattern>/httpService/*</url-pattern>
</servlet-mapping>
spring 配置檔案中,引入dubbo配置
<?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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:service interface="com.xxx.xxxOrderService" ref="xxxOrderService" />
<dubbo:application name="xxxName" />
<dubbo:registry protocol="zookeeper" address="xxx.xxx.xxx.xxx:2181,xxx.xxx.xxx.xxx:2181" />
<!--web是應用路徑 httpService是web.xml中配置的監控路徑 --> <dubbo:protocol name="http" port="8080" server="servlet" contextpath="web/httpService"/>
</beans>
啟動應用就可以,可能會缺少一些dubbo的依賴包。