Dubbo和Spring整合Demo

來源:互聯網
上載者:User

標籤:config   5.x   服務   character   解壓   下載   provider   初始化   splay   

Zookeeper安裝和啟動
  • http://mirrors.hust.edu.cn/apache/zookeeper/下載,我的版本是 3.4.5。

  • 解壓到 D:\zookeeper-3.4.5

  • 配置

    到目錄conf 下建立 zoo.cfg 檔案,預設就是載入這個檔案,檔案內容 我直接copy 的sample裡面的

    zoo.cfg 的內容 
    # 心跳檢查的時間 2秒 
    tickTime=2000 
    # 初始化時 串連到伺服器端的間隔次數,總時間10*2=20秒 
    initLimit=10 
    # ZK Leader 和follower 之間通訊的次數,總時間5*2=10秒  
    syncLimit=5 
    # 儲存記憶體中資料庫快照集的位置,如果不設定參數,更新交易記錄將被儲存到預設位置。 
    dataDir=D:\zk\tmp\zookeeper 
    # 錯誤記錄檔的存放位置 
    dataLogDir=D:\zk\logs\zookeeper 
    # ZK 伺服器端的監聽連接埠 
    clientPort=2181

  • 運行

    然後 cd 到bin 目錄下 執行zkServer.cmd 就啟動成功了。利用jps命令列命令可以驗證zookeeper是否啟動成功。

Dubbo服務註冊
建立WEB項目,引入相應Jar檔案。

配置web.xml檔案
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID" version="3.0">    <display-name>dubbo-service</display-name>    <welcome-file-list>        <welcome-file>index.html</welcome-file>        <welcome-file>index.htm</welcome-file>        <welcome-file>index.jsp</welcome-file>        <welcome-file>default.html</welcome-file>        <welcome-file>default.htm</welcome-file>        <welcome-file>default.jsp</welcome-file>    </welcome-file-list>    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            WEB-INF/dubbo-provider.xml        </param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <filter>        <filter-name>characterEncodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>UTF-8</param-value>        </init-param>        <init-param>            <param-name>forceEncoding</param-name>            <param-value>true</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>characterEncodingFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping></web-app>

 

 

寫服務類:介面:
public interface IDemoService {    /**     *      *      * @Function: com.test.dubbo.service.IDemoService.sayHello     * @Description:     *     * @param name     * @return name string     *     * @version:v1.0     * @author:cjq     * @date:2015-4-30 下午5:45:30     *     * Modification History:     * Date         Author      Version     Description     * -----------------------------------------------------------------     * 2015-4-30    cjq      v1.0.0         create     */    public String sayHello(String name);}

 

 

實現:
public class DemoServiceImpl implements IDemoService {    /*     * (non-Javadoc)     *      * @see com.test.dubbo.service.IDemoService#sayHello(java.lang.String)     */    public String sayHello(String name) {        return "Hello Dubbo,Hello " + name;    }}

 

 

註冊服務:建立dubbo-provider.xml檔案,檔案內容為:
<?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.xsd              http://code.alibabatech.com/schema/dubbo              http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!-- 提供方應用資訊,用於計算依賴關係 -->    <dubbo:application name="dubbo-service" />    <!-- 使用multicast廣播註冊中心暴露服務地址 -->    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <!-- 使用zookeeper註冊中心暴露服務地址 -->    <dubbo:registry address="zookeeper://xxxx:2181" />    <!-- 用dubbo協議在20880連接埠暴露服務 -->    <dubbo:protocol name="dubbo" port="20880" />    <!-- 聲明需要暴露的服務介面 -->    <dubbo:service interface="com.test.dubbo.service.IDemoService"        ref="demoService" />    <!-- 和本地bean一樣實現服務 -->    <bean id="demoService" class="com.test.dubbo.service.impl.DemoServiceImpl" /></beans>  

 

 

部署啟動服務:

Dubbo服務調用
建立WEB或者普通JAVA工程,引入Dubbo服務註冊項目中的jar檔案。需要匯入一個服務介面(從上個程式中匯入介面的jar並且匯入到此項目中。)配置web.xml檔案(和上工程一樣),編寫調用設定檔dubbo-consumer.xml檔案,內容如下:
<?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.xsd              http://code.alibabatech.com/schema/dubbo              http://code.alibabatech.com/schema/dubbo/dubbo.xsd">    <!-- 提供方應用資訊,用於計算依賴關係 -->    <dubbo:application name="dubbo-service-consumer" />    <!-- 使用multicast廣播註冊中心暴露服務地址 -->    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->    <!-- 使用zookeeper註冊中心暴露服務地址 -->    <dubbo:registry address="zookeeper://xxxx:2181" />    <!-- 聲明需要暴露的服務介面 -->    <dubbo:reference id="demoService" interface="com.test.dubbo.service.IDemoService"/></beans>  

 

編寫測試代碼:
/** * Copyright (C) 2015 * * * @className:com.test.dubbo.consumer.ConsumerTest * @description:TODO *  * @version:v1.0.0  * @author:cjq *  * Modification History: * Date         Author      Version     Description * ----------------------------------------------------------------- * 2015-4-30     cjq       v1.0.0        create * * */package com.test.dubbo.consumer;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.test.dubbo.service.IDemoService;public class ConsumerTest {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                "file:D:/javaworkspace/dubbo-service-consumer/WebContent/WEB-INF/dubbo-consumer.xml");        context.start();        IDemoService demoService = (IDemoService) context.getBean("demoService"); // 擷取遠程服務代理        String hello = demoService.sayHello("world"); // 執行遠程方法        System.out.println(hello);    }}

 

Dubbo服務調用結果
調用測試代碼,執行結果為:

Dubbo介紹結尾
如果你在做分布式系統的話,不妨用一下這個技術。

Dubbo和Spring整合Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.