Dubbo启动时qos-server can not bind localhost:22222错误解决

摘要: 原创出处 https://blog.csdn.net/u012988901/article/details/84503672

本节介绍

上一节中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

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自动装配方式

其中,上述方式的优先顺序为JVM系统属性 > dubbo.properties > XML/Spring-boot自动装配方式。

使用系统属性方式进行配置

-Ddubbo.application.qos.enable=true
-Ddubbo.application.qos.port=33333
-Ddubbo.application.qos.accept.foreign.ip=false

使用dubbo.properties文件进行配置

在项目的src/main/resources目录下添加dubbo.properties文件,内容如下:

dubbo.application.qos.enable=true
dubbo.application.qos.port=33333
dubbo.application.qos.accept.foreign.ip=false

使用XML方法进行配置

如果要通过XML配置响应的QoS相关的参数,可以进行如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
  <dubbo:application name="demo-provider">
    <dubbo:parameter key="qos.enable" value="true"/>
    <dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
    <dubbo:parameter key="qos.port" value="33333"/>
  </dubbo:application>
  <dubbo:registry address="multicast://224.5.6.7:1234"/>
  <dubbo:protocol name="dubbo" port="20880"/>
  <dubbo:service interface="org.apache.dubbo.demo.provider.DemoService" ref="demoService"/>
  <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
</beans>

使用spring-boot自动装配方式配置

如果是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改成如下即可解决这个问题了。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="simple-consumer">
        <dubbo:parameter key="qos.enable" value="true" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
        <dubbo:parameter key="qos.port" value="33333" />
    </dubbo:application>

    <!-- 注册中心地址 -->
    <dubbo:registry address="zookeeper://192.168.74.4:2181?backup=192.168.74.5:2181,192.168.74.6:2181" />

    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService 检查级联依赖关系 默认为true 当有依赖服务的时候,需要根据需求进行设置 -->
    <dubbo:reference id="simpleService" check="false" interface="com.wkp.service.simple.SimpleService" />
</beans>

Telnet命令使用介绍

要使用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)

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

Qos提供的新的Telnet命令

Telnet连接命令格式如下:

telnet [host-name [port]]

因为服务控制通常都是针对于服务提供者的,其中动态上下线服务是比较常用的,所以我们演示Telnet命令的时候也是连接的provider,我们将simple-provider.xml改成如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
    <!-- 具体的实现bean -->
    <bean id="simpleService" class="com.wkp.service.simple.impl.SimpleServiceImpl" />

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="simple-provider">
        <dubbo:parameter key="qos.enable" value="true" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
        <dubbo:parameter key="qos.port" value="22222" />
    </dubbo:application>

    <!-- 使用zookeeper注册中心暴露服务地址(zookeeper单节点时,address的值例如:zookeeper://192.168.74.4:2181) -->
    <dubbo:registry address="zookeeper://192.168.74.4:2181?backup=192.168.74.5:2181,192.168.74.6:2181" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- 声明需要暴露的服务接口  写操作可以设置retries=0 避免重复调用SOA服务 -->
    <dubbo:service retries="0" interface="com.wkp.service.simple.SimpleService" ref="simpleService" />

</beans>

当qos不允许远程连接时(<dubbo:parameter key="qos.accept.foreign.ip" value="false" />),我们从另外机器尝试访问当前provider,结果如下:

image

出现了"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

推荐阅读更多精彩内容