dubbo源码调试

1.从github上clone下duboo的源码并checkout tag到2.6.5可以看到如下的结构:

 

dubbo源码调试_第1张图片

 

其中all-dubbo的pom如下:

 

dubbo源码调试_第2张图片

dubbo源码调试_第3张图片

 

这里会将dubbo的其他项目在package的时候打到一个包里,注意到这里依赖的其他模块全部都是optional,在只依赖dubbo时其他子模块是不会被依赖传递的,这也意味了子模块的依赖也不会传递,不注意这一点的话,在调试的时候很容易奇怪为什么相应的依赖没有传递下去。

3.mvn clean install -Dmaven.test.skip=true进行构建得到本地仓库下com.alibaba.*:

dubbo源码调试_第4张图片

 

4.新建一个项目依赖dubbo,resource下配置好log4j和dubbo的配置文件:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    
    <dubbo:application name="echo-provider"/>
    
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    
    <dubbo:protocol name="dubbo" port="20880"/>
    
    <bean id="echoService" class="provider.EchoServiceImpl"/>
    
    <dubbo:service interface="facade.EchoService" ref="echoService"/>
beans>

5.用spring的方式写一个provider:

import facade.EchoService;
import com.alibaba.dubbo.rpc.RpcContext;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EchoServiceImpl implements EchoService {
    public String echo(String message) {
    String now=new SimpleDateFormat("HH:mm:ss").format(new Date());
    System.out.println("["+now+"] Hello"+message+", request from consumer"+RpcContext.getContext().getRemoteAddressString());
    return message;
    }
}

 6.启动zk集群。

7.start Spring容器,可以看到/dubbo/facade.EchoService/providers下的节点(/root/service/[provider,consumer,routers,confugurators]):(临时节点)

8.同样新建一个conusmer,并start:

import facade.EchoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;

public class EchoConsumer {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"spring/echo-api.xml"});
        context.start();
        EchoService echoService=(EchoService)context.getBean("echoService");
        String status=echoService.echo("hello word");
        System.out.println("echo result:"+status);
        System.in.read();
    }
}

 

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    
    <dubbo:application name="echo-api"/>
    
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    

    
    <dubbo:reference id="echoService" check="false" interface="facade.EchoService"/>
beans>

 

 

 

你可能感兴趣的:(dubbo源码调试)