美文网首页
【Dubbo】源码导入

【Dubbo】源码导入

作者: 半个橙子 | 来源:发表于2018-05-31 14:03 被阅读0次

1.下载源码

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

2.导入Idea

源码目录结构

3.运行示例

dubbo源码中提供了dubbo-demo这个moudle,它提供了Consumer、Provider的最精简的配置(更多的复杂示例已经挪到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提供者实现
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();
    }
}
  • 启动服务提供者
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
    }
}
  • 启动服务消费者
    Consumer 会从注册中心拿到Provider 的地址,然后调用Provider并打印结果 。
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已经调用成功了

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

相关文章

网友评论

      本文标题:【Dubbo】源码导入

      本文链接:https://www.haomeiwen.com/subject/ewjqsftx.html