Dubbo的簡單配置

1.新增一個父項目赏表,并在父項目中添加依賴

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.5.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.7</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

2.在當前父項目中添加子模塊DubboInferface, 并新增一個IHelloInterface的接口

package com.qfedu.dubbo;

public interface IHelloInterface {

    String sayHello(String name);
}

3.在當前父項目中再添加子模塊DubboProvider

(1)在當前子模塊的pom文件中添加DubboInterface接口模塊的依賴

<dependency>
    <groupId>com.qfedu</groupId>
    <artifactId>DubboInterface</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

(2)新增接口子模塊中接口的實現(xiàn)類

package com.qfedu.dubbo;

public class HelloClass implements IHelloInterface {

    @Override
    public String sayHello(String name) {
        return "hello " + name;
    }
}

(3)在當前子模塊的resources下新增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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--
        使用bean來創(chuàng)建一個HelloClass的實例對象
    -->
    <bean id="myDubbo" class="com.qfedu.dubbo.HelloClass" />

    <!--
        定義一個dubbo的應(yīng)用程序的名稱参滴,將來會在dubbo的管理中心查看
    -->
    <dubbo:application name="DubboProvider" />

    <!--
        配置dubbo協(xié)議,dubbo缆镣,hession搓茬, RMI
    -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!--
        將當前應(yīng)用注冊到zookeeper注冊中心服務(wù)器之上
    -->
    <dubbo:registry address="zookeeper://ip:2181"></dubbo:registry>

    <!--
        將當前服務(wù)的接口暴露出去犹赖,以供消費方來消費
    -->
    <dubbo:service interface="com.qfedu.dubbo.IHelloInterface" ref="myDubbo" />
</beans>

(4)在當前子模塊中新增一個測試類將當前的spring-dubbo配置文件加載起來

package com.qfedu.dubbo;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestProvider {

    @Test
    public void testProvider(){
        //  加載spring-dubbo的配置文件
        new ClassPathXmlApplicationContext("classpath:spring-dubbo.xml");

        //  提示信息
        System.out.println("provider is ready");

        //  死循環(huán)保證"長連接"
        while (true){}
    }
}

4.新增子模塊DubboConsumer

(1)在當前子模塊中resources下新增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.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--
        配置當前應(yīng)用的名稱,給監(jiān)控中心來使用
    -->
    <dubbo:application name="DubboConsumer" />

    <!--
        設(shè)置注冊中心卷仑,從該注冊中心去消費服務(wù)
    -->
    <dubbo:registry address="zookeeper://ip:2181" />

    <!--
        獲取服務(wù)提供方所提供的接口數(shù)據(jù)
    -->
    <dubbo:reference interface="com.qfedu.dubbo.IHelloInterface" id="myDubbo" />
</beans>

(2)在當前子模塊中新增測試類加載spring-dubbo的配置信息峻村,并測試數(shù)據(jù)是否獲取

package com.qfedu.dubbo;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestConsumer {

    IHelloInterface hello;

    @Test
    public void testConsumer(){
        //   加載spring-dubbo的配置文件
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-dubbo.xml");

        //  通過id拿到spring-dubbo中定義的myDubbo節(jié)點對應(yīng)的對象
        hello = ac.getBean("myDubbo", IHelloInterface.class);

        //  調(diào)用mydubbo對象中的方法,完成測試
        String content = hello.sayHello("zhangsan");

        //  打印測試結(jié)果
        System.out.println(content);

        //while (true){}
    }
}

5.啟動zookeeper之后锡凝,運行provide測試類進行測試

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
provider is ready

6.運行consumer的測試類

com.qfedu.dubbo.TestConsumer,testConsumer
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
hello zhangsan

7.至此dubbo的簡單的配置完成

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末粘昨,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子窜锯,更是在濱河造成了極大的恐慌张肾,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锚扎,死亡現(xiàn)場離奇詭異吞瞪,居然都是意外死亡,警方通過查閱死者的電腦和手機驾孔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門芍秆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來惯疙,“玉大人,你說我怎么就攤上這事妖啥∶沟撸” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵荆虱,是天一觀的道長蒿偎。 經(jīng)常有香客問我,道長克伊,這世上最難降的妖魔是什么酥郭? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮愿吹,結(jié)果婚禮上不从,老公的妹妹穿的比我還像新娘。我一直安慰自己犁跪,他們只是感情好椿息,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著坷衍,像睡著了一般寝优。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上枫耳,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天乏矾,我揣著相機與錄音,去河邊找鬼迁杨。 笑死钻心,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的铅协。 我是一名探鬼主播捷沸,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼狐史!你這毒婦竟也來了痒给?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤骏全,失蹤者是張志新(化名)和其女友劉穎苍柏,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吟温,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡序仙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了鲁豪。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片潘悼。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡律秃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出治唤,到底是詐尸還是另有隱情棒动,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布宾添,位于F島的核電站船惨,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏缕陕。R本人自食惡果不足惜粱锐,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望扛邑。 院中可真熱鬧怜浅,春花似錦、人聲如沸蔬崩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽沥阳。三九已至跨琳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間桐罕,已是汗流浹背脉让。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留功炮,地道東北人侠鳄。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像死宣,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子碴开,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355