【Dubbo】源碼導(dǎo)入

1.下載源碼

git clone https://github.com/apache/incubator-dubbo.git dubbo

2.導(dǎo)入Idea

源碼目錄結(jié)構(gòu)

3.運(yùn)行示例

dubbo源碼中提供了dubbo-demo這個moudle,它提供了Consumer、Provider的最精簡的配置(更多的復(fù)雜示例已經(jīng)挪到https://github.com/dubbo/dubbo-samples)。

dubbo-demo
  • pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo-parent</artifactId>
        <version>2.6.1</version>
    </parent>
    <artifactId>dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>
    <description>The demo module of dubbo project</description>
    <properties>
        <skip_maven_deploy>true</skip_maven_deploy>
    </properties>
    <modules>
        <module>dubbo-demo-api</module>
        <module>dubbo-demo-provider</module>
        <module>dubbo-demo-consumer</module>
    </modules>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${project.parent.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>
  • API接口
package com.alibaba.dubbo.demo;
public interface DemoService {
    String sayHello(String name);
}
  • provider配置文件
    <dubbo:registry address="multicast://224.5.6.7:1234"/> 配置的是廣播方式注冊,如果有zk的話也可以直接配置注冊到zk上
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>
    <!--<dubbo:monitor protocol="registry"></dubbo:monitor>-->
    <!-- use multicast registry center to export service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!--訂閱并注冊在zk上-->
    <!-- use dubbo protocol to export service on port 20880 -->
    <!--配置線程模式-->
    <dubbo:protocol name="dubbo" port="20881" dispatcher="execution" threadpool="cached" threads="5"/>
    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo"/>
</beans>
  • Provider提供者實(shí)現(xiàn)
package com.alibaba.dubbo.demo.provider;

public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddressString();
    }
}
  • 啟動服務(wù)提供者
public class Provider {
    public static void main(String[] args) throws Exception {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.in.read(); // press any key to exit
    }
}
  • 啟動服務(wù)消費(fèi)者
    Consumer 會從注冊中心拿到Provider 的地址,然后調(diào)用Provider并打印結(jié)果 。
public class Consumer {

    public static void main(String[] args) {
        //Prevent to get IPV6 address,this way only work in debug mode
        //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
        while (true) {
            try {
                Thread.sleep(1000);
                String hello = demoService.sayHello("world"); // call remote method
                System.out.println(hello); // get result
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
}

可以看到Consumer已經(jīng)調(diào)用成功了

Hello world, response from provider: 192.168.0.1:20881
[11/06/18 02:07:53:053 CST] DubboClientHandler-192.168.0.1:20882-thread-1 DEBUG transport.DecodeHandler:  [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult, dubbo version: 2.0.0, current host: 192.168.0.1
Hello world, response from provider: 192.168.0.1:20882
[11/06/18 02:07:54:054 CST] DubboClientHandler-192.168.0.1:20881-thread-1 DEBUG transport.DecodeHandler:  [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult, dubbo version: 2.0.0, current host: 192.168.0.1
Hello world, response from provider: 192.168.0.1:20881
[11/06/18 02:07:55:055 CST] DubboClientHandler-192.168.0.1:20882-thread-1 DEBUG transport.DecodeHandler:  [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult, dubbo version: 2.0.0, current host: 192.168.0.1
Hello world, response from provider: 192.168.0.1:20882
[11/06/18 02:07:56:056 CST] DubboClientHandler-192.168.0.1:20881-thread-1 DEBUG transport.DecodeHandler:  [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult, dubbo version: 2.0.0, current host: 192.168.0.1
Hello world, response from provider: 192.168.0.1:20881
[11/06/18 02:07:57:057 CST] DubboClientHandler-192.168.0.1:20882-thread-1 DEBUG transport.DecodeHandler:  [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult, dubbo version: 2.0.0, current host: 192.168.0.1
Hello world, response from provider: 192.168.0.1:20882
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、準(zhǔn)備 在分析探索Dubbo架構(gòu)原理之前,我們需要準(zhǔn)備一下環(huán)境,用于后面我們來分析dubbo的架構(gòu)。 1.1 Z...
    墨淵丶閱讀 2,737評論 1 20
  • Dubbo簡介 Dubbo,相信做后端的同學(xué)應(yīng)該都用過,或者有所耳聞。沒錯,我就是那個有所耳聞中的一員。 公司在好...
    Jackie_Zheng閱讀 851評論 3 8
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,648評論 19 139
  • 0 準(zhǔn)備 安裝注冊中心:Zookeeper、Dubbox自帶的dubbo-registry-simple;安裝Du...
    七寸知架構(gòu)閱讀 14,106評論 0 88
  • 文/5178-十月 今日目標(biāo) 1.能量按鈕 2.今日復(fù)盤 3.記賬、清算花錢情況 4.剽悍英社打卡 5.騎車半小時...
    小圣姑娘閱讀 372評論 0 0

友情鏈接更多精彩內(nèi)容