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
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,498评论 0 11
  • 彩排完,天已黑
    刘凯书法阅读 4,147评论 1 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 120,545评论 2 7