手记

深入浅出微服务框架dubbo(三):流程篇

三、流程篇

画流程图为了记录debug过程,探究原理,而dubbo有各种扩展比如protocol有多种,注册中心也有多种选择,通信框架也有多种,不可能每个分支都覆盖到。所以只能选择自己比较熟悉使用比较多的来说明,力求讲清主线,熟悉了主线,就能举一反三熟悉支线了。容器选择spring,通信选择netty,注册中心选择zookeeper,zookeeper client选择curator,协议选择dubbo。

3.1 Provider启动
  • 准备工作
    以demoService为例,代码如下:
package com.alibaba.dubbo.demo;
public interface DemoService{
    String sayHello(String name);
}

public class DemoServiceImpl implements DemoService{
     public String sayHello(String name){
           System.out.println(RpcContext.getContext().getRemoteAddress());
           return"Hello "+ name +", response form provider:"+RpcContext.getContext().getLocalAddress();
     }
}

public class DemoProvider{
    public static void main(String[]args){
          com.alibaba.dubbo.container.Main.main(args);
    }
}

配置
dubbo.properties

dubbo.container=log4j,spring
dubbo.monitor.protocol=registry

dubbo-demo-provider.xml


<beansxmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <beanid="demoService"class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>  
    <dubbo:application name="demo-provider"owner="whf"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181"transporter="curator"/>
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService"ref="demoService"protocol="dubbo"loadbalance="roundrobin"timeout=6000000/> 
    <dubbo:protocol name="dubbo"port="20880"heartbeat=600/> 
</beans>
  • 流程图

provider启动流程图

3.2 consumer启动

代码清单1demoAction.java

Package com.alibaba.dubbo.demo.consumer;
Import com.alibaba.dubbo.demo.DemoService;
public class DemoAction{
   private DemoServicedemoService;
   public void setDemoService(DemoServicedemoService){
         this.demoService=demoService;
    }

    public void start()throws Exception {
       for(inti=0;i<Integer.MAX_VALUE;i++){
       try{
        String hello =demoService.sayHello("world"+i);
        System.out.println(hello);
        }catch(Exception e){
           e.printStackTrace();
        }
       Thread.sleep(2000);
          }
    }
}

配置清单1 dubbo.properties

dubbo.container=log4j,spring
dubbo.application.name=demo-consumer
dubbo.application.owner=whf
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.transporter=curator
dubbo.protocol.serialization=hession2
dubbo.protocol.heartbeat=6000000
dubbo.reference.timeout=6000000
dubbo.monitor.protocol=registry

配置清单2 dubbo-demo-consumer.xml

<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>

consumer启动图

3.3 服务处理流程图

7人推荐
随时随地看视频
慕课网APP