上一节中consumer启动时报了"Fail to start qos server"、"qos-server can not bind localhost:22222" 的异常,这节将会解释一下为什么会出现这个错误,怎么去解决它,还有qos中的一些Dubbo 内建的telnet命令的使用方法。
上一节Dubbo入门案例中,consumer工程启动时的报错信息如下:
2018-11-25 22:10:32,642 main [server.Server] 102 [ERROR] [DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.6.4, current host: 169.254.68.252
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:125)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:498)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1271)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:413)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:399)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:198)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:349)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
at java.lang.Thread.run(Thread.java:748)
2018-11-25 22:10:32,650 main [protocol.QosProtocolWrapper] 101 [WARN ] [DUBBO] Fail to start qos server: , dubbo version: 2.6.4, current host: 169.254.68.252
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:125)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:498)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1271)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:413)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:399)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:198)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:349)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:358)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
at java.lang.Thread.run(Thread.java:748)
最关键的就是这两句:
[DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.6.4, current host: 169.254.68.252
java.net.BindException: Address already in use: bind
[DUBBO] Fail to start qos server: , dubbo version: 2.6.4, current host: 169.254.68.252
java.net.BindException: Address already in use: bind
Qos=Quality of Service,qos是Dubbo的在线运维命令,可以对服务进行动态的配置、控制及查询,Dubboo2.5.8新版本重构了telnet(telnet是从Dubbo2.0.5开始支持的
)模块,提供了新的telnet命令支持,新版本的telnet端口与dubbo协议的端口是不同的端口,默认为22222,可以通过配置文件dubbo.properties修改。telnet 模块现在同时支持 http 协议和 telnet 协议,方便各种情况的使用。
QoS提供了一些启动参数,来对启动进行配置,他们主要包括:
参数 | 说明 | 默认值 |
---|---|---|
qosEnable | 是否启动QoS | true |
qosPort | 启动QoS绑定的端口 | 22222 |
qosAcceptForeignIp | 是否允许远程访问 | false |
注意,从2.6.4/2.7.0开始,qosAcceptForeignIp默认配置改为false(拒绝远端主机发出的命令,只允许服务本机执行),如果qosAcceptForeignIp设置为true,有可能带来安全风险,请仔细评估后再打开。
QoS参数可以通过如下方式进行配置
其中,上述方式的优先顺序为JVM系统属性 > dubbo.properties > XML/Spring-boot自动装配方式。
-Ddubbo.application.qos.enable=true
-Ddubbo.application.qos.port=33333
-Ddubbo.application.qos.accept.foreign.ip=false
在项目的src/main/resources
目录下添加dubbo.properties文件,内容如下:
dubbo.application.qos.enable=true
dubbo.application.qos.port=33333
dubbo.application.qos.accept.foreign.ip=false
如果要通过XML配置响应的QoS相关的参数,可以进行如下配置:
如果是spring-boot的应用,可以在application.properties
或者application.yml
上配置:
dubbo.application.qosEnable=true
dubbo.application.qosPort=33333
dubbo.application.qosAcceptForeignIp=false
通过上面的介绍,我们已经找到了问题出现的原因:consumer启动时qos-server也是使用的默认的22222端口,但是这时候端口已经被provider给占用了,所以才会报错的。我们将simple-consumer.xml改成如下即可解决这个问题了。
要使用Telnet命令的话,我们的provider或者consumer工程的pom文件中要添加commons-lang3的依赖,否则会无法使用并且控制台会报 如下异常
2018-11-25 23:21:18,680 qos-worker-3-1 [channel.DefaultChannelPipeline] 1217 [WARN ] An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at com.alibaba.dubbo.qos.server.handler.TelnetProcessHandler.channelRead0(TelnetProcessHandler.java:45)
at com.alibaba.dubbo.qos.server.handler.TelnetProcessHandler.channelRead0(TelnetProcessHandler.java:37)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
org.apache.commons
commons-lang3
3.4
Telnet连接命令格式如下:
telnet [host-name [port]]
因为服务控制通常都是针对于服务提供者的,其中动态上下线服务是比较常用的,所以我们演示Telnet命令的时候也是连接的provider,我们将simple-provider.xml改成如下:
当qos不允许远程连接时(
出现了"Foreign Ip Not Permitted",说明不允许远程连接。
然后我们修改simple-provider.xml中 qos.accept.foreign.ip 的值为 true ,再次尝试访问provider。
[root@wkp5 ~]# telnet 192.168.74.1 22222
Trying 192.168.74.1...
Connected to 192.168.74.1.
Escape character is '^]'.
___ __ __ ___ ___ ____
/ _ \ / / / // _ ) / _ ) / __ \
/ // // /_/ // _ |/ _ |/ /_/ /
/____/ \____//____//____/ \____/
dubbo>help
+---------+----------------------------------------------------------------------------------+
| help | help command |
+---------+----------------------------------------------------------------------------------+
| ls | ls service |
+---------+----------------------------------------------------------------------------------+
| offline | offline dubbo |
+---------+----------------------------------------------------------------------------------+
| online | online dubbo |
+---------+----------------------------------------------------------------------------------+
| quit | quit telnet console |
+---------+----------------------------------------------------------------------------------+
dubbo>help online
+--------------+----------------------------------------------------------------------------------+
| COMMAND NAME | online |
+--------------+----------------------------------------------------------------------------------+
| EXAMPLE | online dubbo |
| | online xx.xx.xxx.service |
+--------------+----------------------------------------------------------------------------------+
dubbo>help offline
+--------------+----------------------------------------------------------------------------------+
| COMMAND NAME | offline |
+--------------+----------------------------------------------------------------------------------+
| EXAMPLE | offline dubbo |
| | offline xx.xx.xxx.service |
+--------------+----------------------------------------------------------------------------------+
dubbo>ls
As Provider side:
+------------------------------------+---+
| Provider Service Name |PUB|
+------------------------------------+---+
|com.wkp.service.simple.SimpleService| Y |
+------------------------------------+---+
As Consumer side:
+---------------------+---+
|Consumer Service Name|NUM|
+---------------------+---+
dubbo>offline
OK
dubbo>ls
As Provider side:
+------------------------------------+---+
| Provider Service Name |PUB|
+------------------------------------+---+
|com.wkp.service.simple.SimpleService| N |
+------------------------------------+---+
As Consumer side:
+---------------------+---+
|Consumer Service Name|NUM|
+---------------------+---+
dubbo>online
OK
dubbo>ls
As Provider side:
+------------------------------------+---+
| Provider Service Name |PUB|
+------------------------------------+---+
|com.wkp.service.simple.SimpleService| Y |
+------------------------------------+---+
As Consumer side:
+---------------------+---+
|Consumer Service Name|NUM|
+---------------------+---+
dubbo>quit
BYE!
Connection closed by foreign host.
[root@wkp5 ~]#
上面我们演示了五个命令:help,ls,online,offline,quit。
help 列出所有命令, help online:列出某个命令的使用方法
quit 退出命令
ls 列出消费者和提供者列表
online 上线服务命令
当使用延迟发布功能的时候(通过设置 com.alibaba.dubbo.config.AbstractServiceConfig#register 为 false),后续需要上线的时候,可通过 Online 命令
//上线所有服务
dubbo>online
OK
//根据正则,上线部分服务
dubbo>online com.*
OK
常见使用场景:
当线上的 QPS 比较高的时候,当刚重启机器的时候,由于没有进行JIT 预热或相关资源没有预热,可能会导致大量超时,这个时候,可通过分批发布服务,逐渐加大流量
当由于某台机器由于某种原因,需要下线服务,然后又需要重新上线服务
offline下线服务
由于故障等原因,需要临时下线服务保持现场,可以使用 Offline 下线命令。
//下线所有服务
dubbo>offline
OK
//根据正则,下线部分服务
dubbo>offline com.*
OK