【Dubbo】源码导入

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

    4.0.0
    
        com.alibaba
        dubbo-parent
        2.6.1
    
    dubbo-demo
    pom
    ${project.artifactId}
    The demo module of dubbo project
    
        true
    
    
        dubbo-demo-api
        dubbo-demo-provider
        dubbo-demo-consumer
    
    
        
            
                com.alibaba
                dubbo-bom
                ${project.parent.version}
                pom
                import
            
        
    

  • API接口
package com.alibaba.dubbo.demo;
public interface DemoService {
    String sayHello(String name);
}
  • provider配置文件
    配置的是广播方式注册,如果有zk的话也可以直接配置注册到zk上

    
    
    
    
    
    
    
    
    
    
    
    
    

  • 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】源码导入)