dubbo 簡單的provider與consumer實現

    項目用到了rest+dubbo的架構,使得服務能夠在一個點死掉以後用其它點的服務來代替響應。java

    這裏先實現一個最簡單的dubbo消費者與提供者。官網說明:http://dubbo.io/git

    首先須要解決的是dubbo的各類依賴,最簡單的實現方法即將github上dubbo項目在本地maven install一遍,所須要的各類相關依賴就到本地庫中了。可是貌似dubbo項目已經沒更新了。github

    最基本的咱們須要一個zookeeper,這個主要是用來調度各類服務,管理註冊中心,對zookeeper的穩定性有比較高的要求。咱們寫兩個最簡單的工程,來模擬Provider和Consume。spring

            其中,TestDubbo用來提供服務,是Provider。而TestDubbo2用來消費服務,是Consumer。嗯,隨便取的工程名。apache

    在Provider中須要一個配置文件,將所提供的服務,以及zk註冊中心的地址,以及Dubbo的服務在哪一個端口暴露出來。其中有接口TestApi以及它的實架構

    現TestApiImpl,以及一個啓動項Start。app

            而以後的消費者工程,只須要配置好Consumer.xml文件,然後將其加載啓動便可調用到Provider所發佈的服務。maven

       

           pom中須要依賴dubbo,zk相關的依賴。因爲引入了父工程中的某些版本,因此此處有些版本須要讀者自行添加。服務提供者的Start採用最原始ide

    的ClassPathXmlApplicationContext來加載配置文件,加載以後start(),爲了讓提供者保持這個狀態,能夠加一行System.in.read();同理適用於服工具

    務消費者。

   

           準備工做完成後,啓動zk,啓動服務提供者,再啓動服務消費者,消費者使用該接口,打印出(TestApiImpl中的實現即打印一行字符,以檢驗提

    供與消費的效果)對應的字符就算OK。在其對應的可視化工具dubbo-admin中能夠觀察到更多詳細的信息。(下個小節介紹)。

   

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.15.0-GA</version>
        </dependency>

 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  
        ">

    <!-- 具體的實現bean -->
    <bean id="testApiImpl" class="com.changjiang.test.TestApiImpl" />

    <!-- 提供方應用信息,用於計算依賴關係 -->
    <dubbo:application name="xixi_provider" />

    <!-- 使用multicast廣播註冊中心暴露服務地址 <dubbo:registry address="multicast://224.5.6.7:1234" 
        /> -->

    <!-- 使用zookeeper註冊中心暴露服務地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 用dubbo協議在20880端口暴露服務 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 聲明須要暴露的服務接口 -->
    <dubbo:service interface="com.changjiang.test.TestApi"
        ref="testApiImpl" />

</beans> 

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="hehe_consumer" />

    <!-- 使用zookeeper註冊中心暴露服務地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成遠程服務代理,能夠像使用本地bean同樣使用demoService -->
    <dubbo:reference id="testApiImpl" interface="com.changjiang.test.TestApi" />


</beans>  

在Consumer中加載了Consumer.xml以後,直接調用Provider提供的服務,然後直接使用接口,能夠檢測提供-註冊-消費是否成功。

public class App {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(new String[] { "Consumer.xml" });
        ac.start();
        TestApi ta = (TestApi) ac.getBean("testApiImpl");
        ta.hello();
        System.in.read();
    }
}

運行結果(沒有添加log4j.properties)

=================>

另外一種啓動方式,在src/main/resources目錄下設置dubbo.properties文件

dubbo.spring.config=classpath*:spring/*.xml

然後將Provider.xml放到指定目錄src/main/resources/spring下

啓動時只須要在Main中:

public class App {
    public static void main(String[] args) {
        /**
         * 模擬啓動
         */
//        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(new String[] { "spring/Provider.xml" });
//        ac.start();
//        try {
//            System.in.read();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
        /**
         * main啓動
         */
        String[] ars = {};
        com.alibaba.dubbo.container.Main.main(ars);
    }
}
相關文章
相關標籤/搜索