1. 程式人生 > >dubbo開發環境搭建與tomcat集成、DEMO示例(最完整版本、帶管理控制臺、監控中心、zookeeper)

dubbo開發環境搭建與tomcat集成、DEMO示例(最完整版本、帶管理控制臺、監控中心、zookeeper)

-s http服務 ppr context 正常 windows web容器 web.xml配置 web.xml

以windows為例,linux基本相同,開發環境一般linux,個人環境一般windows(如果不開額外vm的話)。

示例以dubbo官方自帶demo為例子,進行整合和稍加修改測試。

0、dubbox是dubbo的當當fork版本,特性上最主要是集成了REST。就核心功能而言,dubbo和大部分其他rpc框架比如spring cloud類似,由客戶端、服務端、服務註冊與發現中心、監控中心以及管理中心組成。如下:

技術分享

1、安裝zookeeper,從https://zookeeper.apache.org/releases.html下載3.4.x最新穩定版,解壓後,執行bin/zkServer.cmd啟動zookeeper服務。如下:

技術分享

2、從dubbox git clone或者下載2.8.4版本,執行mvn install -Dmaven.test.skip=true生成各jar;

3、從http://git.oschina.net/handu/dubbo-monitor git clone,執行maven clean package創建監控war,並創建mysql表,並在application.properties設置登錄用戶名、密碼。

4、下載並安裝mysql。

5、maven編譯生成dubbo管理控制臺,技術分享,並在WEB-INF/dubbo.properties裏面設置登錄用戶名、密碼;

6、下載tomcat,將dubbo-admin.war,dubbo-monitor.war拷貝到tomcat webapps目錄下,啟動。如下:

技術分享

技術分享

6、上述步驟執行完成後,dubbox的基礎環境都完成了。剩下就是dubbox開發時集成的問題。

7、在現實中,大部分分布式系統的某個部件一般同時承擔服務消費端和生產端的角色。很少僅僅承擔客戶端或者服務端的場景。所以他倆基本上是一起出現的。

dubbo默認提供了5個容器(有基於spring xml的,spring annotation config的,也有支持http的jetty,log4j和logback是為了啟動多進程時日誌不會互串):

技術分享

默認是基於spring xml的容器。其啟動類com.alibaba.dubbo.container.spring.SpringContainer.start()會自動加載classpath*:META-INF/spring/*.xml下的配置文件。所以,就不提供REST的應用而言,采用這種方式也是可以的。以前公司采用自行開發的rpc框架時,就是通過這種方式運行(采用java service wrapper封裝),不使用web容器。

在我們現在工作中,除了需要暴露tcp服務外,還需要對外提供http服務,這意味著需要使用到servlet容器。而根據實際經驗,jetty在穩定性上並不如tomcat,故我們需要考慮是使用內嵌的tomcat還是將dubbo服務嵌入standalone的tomcat容器。個人不喜歡在應用中內嵌容器,就像有了房子再選擇裝修,而不是定了裝修再去選房一樣。

8、使用外部tomcat(官方叫servlet派發請求,其實就是采用spring dispatchservlet作為前端控制器,也是推薦的方式,不過默認的是嵌入的jetty,當當增強支持嵌入tomcat)。以dubbo自帶的dubbo-demo\dubbo-demo-provider為例子,dubbo REST的開發可參考http://dangdangdotcom.github.io/dubbox/rest.html,跟正常的REST服務開發一樣,只不過采用的是jboss resteasy而非spring mvc,兩者的差別可參考http://www.cnblogs.com/zhjh256/p/6883417.html,同時加dubbo相關的註解一起加上去。實際上dubbo-demo-provider自帶了外部容器的配置,只是默認采用內置的,如下:

    <!-- use embed tomcat server
    <dubbo:protocol name="rest" port="8888" threads="500" contextpath="services" server="tomcat" accepts="500"
                    extension="com.alibaba.dubbo.demo.extension.TraceInterceptor,
                    com.alibaba.dubbo.demo.extension.TraceFilter,
                    com.alibaba.dubbo.demo.extension.ClientTraceFilter,
                    com.alibaba.dubbo.demo.extension.DynamicTraceBinding,
                    com.alibaba.dubbo.demo.extension.CustomExceptionMapper,
                    com.alibaba.dubbo.rpc.protocol.rest.support.LoggingFilter"/>
     -->
    <!-- use the external tomcat or other server with the servlet approach; the port and contextpath must be exactly the same as those in external server -->
    <dubbo:protocol name="rest" port="8888" contextpath="services" server="servlet"/>
    
    <!-- disable other protocol
    <dubbo:protocol name="http" port="8889"/>
    <dubbo:protocol name="hessian" port="8890"/>
    <dubbo:protocol name="webservice" port="8892"/>
    -->

使用外部tomcat容器有一個要求:

  • 協議的端口<dubbo:protocol port="8080" />必須與servlet容器的端口相同,
  • 協議的上下文路徑<dubbo:protocol contextpath="foo" />必須與servlet應用的上下文路徑相同。

對於上述配置,對應的web.xml配置如下:

    <servlet>
        <servlet-name>dispatcher</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>dispatcher</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
這個DispatcherServlet和spring mvc的dispatcherservlet,也是繼承於HttpServlet,只不過在職責上相對於spring mvc輕量很多,比如並不需要提供mvc的特性。

eclipse中啟動tomcat:

技術分享

技術分享

9、REST訪問。

技術分享

10、dubbo rpc客戶端訪問。

技術分享

所以,可以將demo-provider.xml作為一個樣板進行修改或者合並到現有工程。

參考:

dubbox官網:https://github.com/dangdangdotcom/dubbox

dubbo用戶指南:http://dubbo.io/User+Guide-zh.htm

dubbox rest開發:http://dangdangdotcom.github.io/dubbox/rest.html

嵌入式tomcat 7.0,http://www.blogjava.net/wangxinsh55/archive/2016/07/18/431229.html

dubbo開發環境搭建與tomcat集成、DEMO示例(最完整版本、帶管理控制臺、監控中心、zookeeper)