telnet訪問dubbo
絕對神器,可以脫離consumer,在只有provider的情況下,訪問dubbo服務(wù),驗證provider的可用性和準(zhǔn)確性;假設(shè)用telnet方式調(diào)用某dubbo服務(wù):
Teacher getTeacher(Student stu, String remark);
對應(yīng)的調(diào)用方式為:
telnet 127.0.0.1 20880(telnet進(jìn)入后,可以通過help命令查詢telnet模式下dubbo支持哪些命令)
invoke com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"}, "invokeByTelnet")
調(diào)用關(guān)系
由于dubbo服務(wù)默認(rèn)依賴netty啟動,所以即使是telnet方式訪問,第一入口還是這里:
--> NettyHandler.messageReceived(ChannelHandlerContext ctx, MessageEvent e)
--> AbstractPeer.received(Channel ch, Object msg)
--> MultiMessageHandler.received(Channel channel, Object message)
--> AllChannelHandler.received(Channel channel, Object message)
--> DecodeHandler.received(Channel channel, Object message)
-
DecodeHandler.received(Channel channel, Object message)
源碼如下,telnet方式訪問時,message是String類型,所以直接調(diào)用handler.received(channel, message):
public void received(Channel channel, Object message) throws RemotingException {
if (message instanceof Decodeable) {
decode(message);
}
if (message instanceof Request) {
decode(((Request)message).getData());
}
if (message instanceof Response) {
decode( ((Response)message).getResult());
}
handler.received(channel, message);
}
- HeaderExchangeHandler.received(Channel channel, Object message),走如下業(yè)務(wù)邏輯:
... ...
else if (message instanceof String) {
// 只有Provider側(cè)才支持telnet方式訪問;
if (isClientSide(channel)) {
Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl());
logger.error(e.getMessage(), e);
} else {
String echo = handler.telnet(channel, (String) message);
if (echo != null && echo.length() > 0) {
channel.send(echo);
}
}
}
-
TelnetHandlerAdapter.telnet(Channel channel, String message)
假定telnet時執(zhí)行invoke com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"}),即message的值就是這個字符串,command就是"invoke";又是通過ExtensionLoader根據(jù)"invoke"得到調(diào)用的具體實現(xiàn)類為InvokeTelnetHandler(在META-INF/dubbo/internal/com.alibaba.dubbo.remoting.telnet.TelnetHandler中有申明:invoke=com.alibaba.dubbo.rpc.protocol.dubbo.telnet.InvokeTelnetHandler),部分核心源碼如下:
if (extensionLoader.hasExtension(command)) {
try {
String result = extensionLoader.getExtension(command).telnet(channel, message);
if (result == null) {
return null;
}
buf.append(result);
} catch (Throwable t) {
buf.append(t.getMessage());
}
} else {
// 如果輸入的命令不支持,,例如輸入"afei",那么提示"Unsupported command: afei"
buf.append("Unsupported command: ");
buf.append(command);
}
-
InvokeTelnetHandler.telnet(Channel channel, String message)
InvokeTelnetHandler類為dubbo處理telnet中invoke命令的實現(xiàn),
假定tlenet中執(zhí)行invoke com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"}),那么到這里后message的值為"com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"})",部分核心源碼如下:
// 將參數(shù)"{"id":66,"name":"afei"}"反序列化;
Object[] array = PojoUtils.realize(list.toArray(), invokeMethod.getParameterTypes());
RpcContext.getContext().setLocalAddress(channel.getLocalAddress()).setRemoteAddress(channel.getRemoteAddress());
long start = System.currentTimeMillis();
// 構(gòu)造Invocation對象,執(zhí)行invoker.invoke(Invocation),到這里后與其他調(diào)用方式的實現(xiàn)完全一樣;
Object result = invoker.invoke(new RpcInvocation(invokeMethod, array)).recreate();
long end = System.currentTimeMillis();
// 將結(jié)果利用fastjson序列化并append耗時信息然后返回
buf.append(JSON.json(result));
buf.append("\r\nelapsed: ");
buf.append(end - start);
buf.append(" ms.");
- telnet訪問返回結(jié)果參考:
dubbo>invoke com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"}, "invokeByTelnet")
{"level":"2","name":"afei","id":66,"age":91}
elapsed: 0 ms.
附TelnetHandler關(guān)系圖
每個實現(xiàn)類對應(yīng)一種telnet訪問dubbo的方式,例如InvokeTelnetHandler對應(yīng)invoke命令,ListTelnetHandler對應(yīng)ls命令:
