dubbo源码调试

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

其中all-dubbo的pom如下:

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

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

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">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="echo-provider"/>
    <!-- use multicast registry center to export service -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- service implementation, as same as regular local bean -->
    <bean id="echoService" class="provider.EchoServiceImpl"/>
    <!-- declare the service interface to be exported -->
    <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]):(临时节点)

猜你喜欢

转载自www.cnblogs.com/lccsblog/p/11520871.html