duubo duuboMock mockDubbo源代码解析 模拟dubbo调用,类似依赖注入获取对应信息

 
  
期待能完成类似与http的集成测试 mvcmock调用.. 获取到servlet,然后模拟集成测试dubbo接口. 

网上很多mock servlet都是说的是单测mock

(
import org.springframework.test.web.servlet.MockMvc;

这里有例子: git clone https://github.com/spring-guides/gs-rest-service.git

GreetingControllerTests

)


1.先看执行流程.
 
  
 1.1 这里面用到了FilterChain类进行filter控制和actualInvoker的控制.
 1.2 filter之间的调用实际上隔了一个FilterChain类的判断控制.
 
  
 
  
 filter模式也可以用责任链来实现,不过静态.不好动态修改.(Intercepting Filter模式详解 https://yq.aliyun.com/articles/46958)
doFilter(ServletRequest request,  FilterChain chain)doFilter(ServletRequest request, Invoker chain)要清楚很多,FilterChain就是filterchain   
 
  
 
  

 
  
"DubboServerHandler-172.23.65.76:10886-thread-4@6260" daemon prio=5 tid=0x7d nid=NA runnable  java.lang.Thread.State: RUNNABLE      
at com..Method.method()     
at com.alibaba.dubbo.common.bytecode.Wrapper78.invokeMethod(Wrapper78.java:-1)      
at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:46)      
at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:72)     
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)at com..pay.common.dubbo.PayProviderFilter.invoke(PayProviderFilter.java:64)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com..framework.dubbo.ProviderContextSwitchFilter.invoke(ProviderContextSwitchFilter.java:67)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)    
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:60)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:112)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)   
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)      
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)      
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:108)    //重要节点  
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:84)      
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:170)      
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)      
at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:82)   //ChannelEventRunnable   
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)      
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)      
at java.lang.Thread.run(Thread.java:722)





显然这个只是个task,并不是对接通信mina的类.要看在哪被调用了.

通过ChannelEventRunnable调查发现是ExecutionChannelHandler 这里面线程池调用了. 然后最终结合到com.alibaba.dubbo.remoting.transport.netty.NettyServer的


 
  
bootstrap = new ServerBootstrap(channelFactory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
    public ChannelPipeline getPipeline() {
        NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), NettyServer.this);
        ChannelPipeline pipeline = Channels.pipeline();
        /*int idleTimeout = getIdleTimeout();
        if (idleTimeout > 10000) {
            pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));        }*/
        pipeline.addLast("decoder", adapter.getDecoder());
        pipeline.addLast("encoder", adapter.getEncoder());
        pipeline.addLast("handler", nettyHandler); //就是ExecutionChannelHandler.
       return pipeline;    
      }
     });


再看后面的执行类:

 
  
public class DubboProtocol extends AbstractProtocol {

这里面从请求参数里得到对应的invoker

DubboExporter exporter = (DubboExporter) exporterMap.get(serviceKey);

if (exporter == null)
    throw new RemotingException(channel, "Not found exported service: " + serviceKey + " in " + exporterMap.keySet() + ", may be version or group mismatch " + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress() + ", message:" + inv);

return exporter.getInvoker();


2. 看启动流程.

 
  
com.alibaba.dubbo.remoting.transport.netty.NettyServer 启动的时候需要监听端口,配置各种handler,这些handler又是从
DubboBeanDefinitionParser里解析的.



你可能感兴趣的:(代码阅读,dubbo)