標籤:
dubbo源碼分析1-reference bean建立 dubbo源碼分析2-reference bean發起服務方法調用 dubbo源碼分析3-service bean的建立與發布 dubbo源碼分析4-基於netty的dubbo協議的server dubbo源碼分析5-dubbo的擴充點機制
dubbo提供了telnet的方式,直接用命令查看服務資訊等。怎麼實現的呢。
1. 轉碼器
com.alibaba.dubbo.remoting.transport.netty.NettyCodecAdapter.InternalDecoder
實現了SimpleChannelUpstreamHandler類
然後註冊給ChannelPipeline
後面的都是用netty的套路
2. dubbo中URL類
com.alibaba.dubbo.common.URL
該類的作用類似於我們通常用的上下文,它描述了調用過程中一些配置資訊,形式如下:
dubbo://192.168.1.101:20880/org.simonme.dubbo.demo.provider.service.HelloService?anyhost=true&application=hello-world-app&channel.readonly.sent=true&codec=dubbo&dubbo=2.4.9&heartbeat=60000&interface=org.simonme.dubbo.demo.provider.service.HelloService&methods=sayHello&monitor=dubbo%3A%2F%2F192.168.1.100%3A2174%2Fcom.alibaba.dubbo.registry.RegistryService%3Fapplication%3Dhello-world-app%26dubbo%3D2.4.9%26pid%3D8976%26protocol%3Dregistry%26refer%3Ddubbo%253D2.4.9%2526interface%253Dcom.alibaba.dubbo.monitor.MonitorService%2526pid%253D8976%2526timestamp%253D1468370420113%26registry%3Dzookeeper%26timestamp%3D1468370419870&pid=8976&side=provider×tamp=1468370419920
比如我需要怎樣的轉碼器,我會到這url中取
com.alibaba.dubbo.remoting.transport.AbstractEndpoint.AbstractEndpoint(URL, ChannelHandler)
this.codec = ExtensionLoader.getExtensionLoader(Codec.class).getExtension(url.getParameter(Constants.CODEC_KEY, "telnet"));
我們在上述的url樣本中能找到 codec=dubbo
所以就知道這次採用dubbo的轉碼器。
略微和我們之前用上下文方式不同的是,上下文中可以防止一些比較複雜的對象,比如放置一個根表單容器之類的。
用了URL這種形式,結合ZooKeeper使用比較方便。
3. dubbo的擴充點實現
分析 com.alibaba.dubbo.common.extension.ExtensionLoader<T>
本質上還是將構建出來的執行個體放在cachedInstances屬性中,這個事本來可以交給spring幹,spring專業幹IOC的。但是dubbo這麼幹了之後一個明顯的好處是:dubbo在不依賴任何第三方架構的情況下可以跑起來。說的簡單一點:dubbo自己做了一個簡易的IOC機制,它這裡面稱之為SPI。自己做IOC的架構真是不少,Struts2也自己幹了IOC。詳細分析見之前畫的一份思維導圖
4. telnet協議轉碼器
DubboCountCodec與netty對接,DubboCountCodec 關聯了DubboCodec。DubboCodec繼承體繫上有個TelnetCodec。TelnetCodec完成了telenet協議的編解碼,在decode方法中會將InputStream 執行個體轉換成message。
5. 解碼後的動作鏈路
NettyHandler extends SimpleChannelHandler 這個類會註冊給netty,netty在解碼後會觸發下面的messageReceived邏輯。
com.alibaba.dubbo.remoting.transport.netty.NettyHandler.messageReceived(ChannelHandlerContext, MessageEvent)
這個handler又關聯了真正處理邏輯的handler,com.alibaba.dubbo.remoting.telnet.support.TelnetHandlerAdapter
TelnetHandlerAdapter觸發telnet方法 調用真正的命令處理邏輯
根據拿到的命令 再通過擴充點機制擷取具體的命令實作類別的執行個體。
可以以com.alibaba.dubbo.rpc.protocol.dubbo.telnet.ListTelnetHandler為例子分析一下。
命令的實現使用了@Help註解,描述了命令的協助。
6. telnet串連後,按向上或者向下方向鍵的時候能回顯曆史命令,怎麼實現的
實現在com.alibaba.dubbo.remoting.telnet.codec.TelnetCodec.decode(Channel, InputStream, int, byte[])中,轉碼器會將曆史命令記錄在channel中,然後根據方向來擷取。
boolean down = endsWith(message, DOWN); if (up || down) { LinkedList<String> history = (LinkedList<String>) channel.getAttribute(HISTORY_LIST_KEY); if (history == null || history.size() == 0) { return NEED_MORE_INPUT; }
--EOF--
dubbo源碼分析6-telnet方式的管理實現