(3)Dubbo啟動時qos-server can not bind localhost:22222錯誤解決


注意:我本地測試時發現在xml中配置無效,在消費者端添加dubbo.properties文件的方式有效】

 

轉:

(3)Dubbo啟動時qos-server can not bind localhost:22222錯誤解決

 


沒頭腦遇到不高興 2018-11-26 00:22:07 24602 收藏 15
展開
本節介紹

上一節中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,結果如下:

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

 
————————————————
版權聲明:本文為CSDN博主「沒頭腦遇到不高興」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/u012988901/article/details/84503672

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM