telnet访问dubbo
绝对神器,可以脱离consumer,在只有provider的情况下,访问dubbo服务,验证provider的可用性和准确性;假设用telnet方式调用某dubbo服务:
Teacher getTeacher(Student stu, String remark);
对应的调用方式为:
telnet 127.0.0.1 20880(telnet进入后,可以通过help命令查询telnet模式下dubbo支持哪些命令)
invoke com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"}, "invokeByTelnet")
调用关系
由于dubbo服务默认依赖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类型,所以直接调用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),走如下业务逻辑:
... ...
else if (message instanceof String) {
// 只有Provider侧才支持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时执行invoke com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"}),即message的值就是这个字符串,command就是"invoke";又是通过ExtensionLoader根据"invoke"得到调用的具体实现类为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命令的实现,
假定tlenet中执行invoke com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"}),那么到这里后message的值为"com.alibaba.dubbo.demo.TestService.getTeacher({"id":66,"name":"afei"})",部分核心源码如下:
// 将参数"{"id":66,"name":"afei"}"反序列化;
Object[] array = PojoUtils.realize(list.toArray(), invokeMethod.getParameterTypes());
RpcContext.getContext().setLocalAddress(channel.getLocalAddress()).setRemoteAddress(channel.getRemoteAddress());
long start = System.currentTimeMillis();
// 构造Invocation对象,执行invoker.invoke(Invocation),到这里后与其他调用方式的实现完全一样;
Object result = invoker.invoke(new RpcInvocation(invokeMethod, array)).recreate();
long end = System.currentTimeMillis();
// 将结果利用fastjson序列化并append耗时信息然后返回
buf.append(JSON.json(result));
buf.append("\r\nelapsed: ");
buf.append(end - start);
buf.append(" ms.");
- telnet访问返回结果参考:
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关系图
每个实现类对应一种telnet访问dubbo的方式,例如InvokeTelnetHandler对应invoke命令,ListTelnetHandler对应ls命令: