摘要: 原創(chuàng)出處 https://blog.csdn.net/u012988901/article/details/84503672
本節(jié)介紹
上一節(jié)中consumer啟動(dòng)時(shí)報(bào)了"Fail to start qos server"、"qos-server can not bind localhost:22222" 的異常,這節(jié)將會(huì)解釋一下為什么會(huì)出現(xiàn)這個(gè)錯(cuò)誤仑乌,怎么去解決它每界,還有qos中的一些Dubbo 內(nèi)建的telnet命令的使用方法候引。
啟動(dòng)的錯(cuò)誤
上一節(jié)Dubbo入門案例中渊迁,consumer工程啟動(dòng)時(shí)的報(bào)錯(cuò)信息如下:
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)
最關(guān)鍵的就是這兩句:
[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的在線運(yùn)維命令飞主,可以對(duì)服務(wù)進(jìn)行動(dòng)態(tài)的配置、控制及查詢前鹅,Dubboo2.5.8新版本重構(gòu)了telnet(telnet是從Dubbo2.0.5開始支持的
)模塊摘悴,提供了新的telnet命令支持,新版本的telnet端口與dubbo協(xié)議的端口是不同的端口嫡纠,默認(rèn)為22222烦租,可以通過配置文件dubbo.properties修改。telnet 模塊現(xiàn)在同時(shí)支持 http 協(xié)議和 telnet 協(xié)議除盏,方便各種情況的使用叉橱。
QoS提供了一些啟動(dòng)參數(shù),來對(duì)啟動(dòng)進(jìn)行配置者蠕,他們主要包括:
參數(shù) | 說明 | 默認(rèn)值 |
---|---|---|
qosEnable | 是否啟動(dòng)QoS | true |
qosPort | 啟動(dòng)QoS綁定的端口 | 22222 |
qosAcceptForeignIp | 是否允許遠(yuǎn)程訪問 | false |
注意窃祝,從2.6.4/2.7.0開始,qosAcceptForeignIp默認(rèn)配置改為false(拒絕遠(yuǎn)端主機(jī)發(fā)出的命令踱侣,只允許服務(wù)本機(jī)執(zhí)行)粪小,如果qosAcceptForeignIp設(shè)置為true,有可能帶來安全風(fēng)險(xiǎn)抡句,請(qǐng)仔細(xì)評(píng)估后再打開探膊。
QoS參數(shù)可以通過如下方式進(jìn)行配置
- JVM系統(tǒng)屬性
- dubbo.properties
- XML方式
- Spring-boot自動(dòng)裝配方式
其中,上述方式的優(yōu)先順序?yàn)镴VM系統(tǒng)屬性 > dubbo.properties > XML/Spring-boot自動(dòng)裝配方式待榔。
使用系統(tǒng)屬性方式進(jìn)行配置
-Ddubbo.application.qos.enable=true
-Ddubbo.application.qos.port=33333
-Ddubbo.application.qos.accept.foreign.ip=false
使用dubbo.properties文件進(jìn)行配置
在項(xiàng)目的src/main/resources
目錄下添加dubbo.properties文件逞壁,內(nèi)容如下:
dubbo.application.qos.enable=true
dubbo.application.qos.port=33333
dubbo.application.qos.accept.foreign.ip=false
使用XML方法進(jìn)行配置
如果要通過XML配置響應(yīng)的QoS相關(guān)的參數(shù),可以進(jìn)行如下配置:
<?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自動(dòng)裝配方式配置
如果是spring-boot的應(yīng)用锐锣,可以在application.properties
或者application.yml
上配置:
dubbo.application.qosEnable=true
dubbo.application.qosPort=33333
dubbo.application.qosAcceptForeignIp=false
解決問題
通過上面的介紹腌闯,我們已經(jīng)找到了問題出現(xiàn)的原因:consumer啟動(dòng)時(shí)qos-server也是使用的默認(rèn)的22222端口,但是這時(shí)候端口已經(jīng)被provider給占用了雕憔,所以才會(huì)報(bào)錯(cuò)的姿骏。我們將simple-consumer.xml改成如下即可解決這個(gè)問題了。
<?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">
<!-- 消費(fèi)方應(yīng)用名斤彼,用于計(jì)算依賴關(guān)系分瘦,不是匹配條件蘸泻,不要與提供方一樣 -->
<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" />
<!-- 生成遠(yuǎn)程服務(wù)代理,可以像使用本地bean一樣使用demoService 檢查級(jí)聯(lián)依賴關(guān)系 默認(rèn)為true 當(dāng)有依賴服務(wù)的時(shí)候擅腰,需要根據(jù)需求進(jìn)行設(shè)置 -->
<dubbo:reference id="simpleService" check="false" interface="com.wkp.service.simple.SimpleService" />
</beans>
Telnet命令使用介紹
要使用Telnet命令的話蟋恬,我們的provider或者consumer工程的pom文件中要添加commons-lang3的依賴翁潘,否則會(huì)無法使用并且控制臺(tái)會(huì)報(bào) 如下異常
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]]
因?yàn)榉?wù)控制通常都是針對(duì)于服務(wù)提供者的趁冈,其中動(dòng)態(tài)上下線服務(wù)是比較常用的,所以我們演示Telnet命令的時(shí)候也是連接的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">
<!-- 具體的實(shí)現(xiàn)bean -->
<bean id="simpleService" class="com.wkp.service.simple.impl.SimpleServiceImpl" />
<!-- 提供方應(yīng)用信息渗勘,用于計(jì)算依賴關(guān)系 -->
<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注冊中心暴露服務(wù)地址(zookeeper單節(jié)點(diǎn)時(shí),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協(xié)議在20880端口暴露服務(wù) -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 聲明需要暴露的服務(wù)接口 寫操作可以設(shè)置retries=0 避免重復(fù)調(diào)用SOA服務(wù) -->
<dubbo:service retries="0" interface="com.wkp.service.simple.SimpleService" ref="simpleService" />
</beans>
當(dāng)qos不允許遠(yuǎn)程連接時(shí)(<dubbo:parameter key="qos.accept.foreign.ip" value="false" />)俩莽,我們從另外機(jī)器嘗試訪問當(dāng)前provider旺坠,結(jié)果如下:
出現(xiàn)了"Foreign Ip Not Permitted",說明不允許遠(yuǎn)程連接扮超。
然后我們修改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 ~]#
上面我們演示了五個(gè)命令:help出刷,ls璧疗,online,offline馁龟,quit崩侠。
help 列出所有命令, help online:列出某個(gè)命令的使用方法
quit 退出命令
ls 列出消費(fèi)者和提供者列表
online 上線服務(wù)命令
當(dāng)使用延遲發(fā)布功能的時(shí)候(通過設(shè)置 com.alibaba.dubbo.config.AbstractServiceConfig#register 為 false)坷檩,后續(xù)需要上線的時(shí)候却音,可通過 Online 命令
//上線所有服務(wù)
dubbo>online
OK
//根據(jù)正則,上線部分服務(wù)
dubbo>online com.*
OK
常見使用場景:
當(dāng)線上的 QPS 比較高的時(shí)候矢炼,當(dāng)剛重啟機(jī)器的時(shí)候系瓢,由于沒有進(jìn)行JIT 預(yù)熱或相關(guān)資源沒有預(yù)熱,可能會(huì)導(dǎo)致大量超時(shí)句灌,這個(gè)時(shí)候夷陋,可通過分批發(fā)布服務(wù),逐漸加大流量
當(dāng)由于某臺(tái)機(jī)器由于某種原因涯塔,需要下線服務(wù)肌稻,然后又需要重新上線服務(wù)
offline下線服務(wù)
由于故障等原因,需要臨時(shí)下線服務(wù)保持現(xiàn)場匕荸,可以使用 Offline 下線命令爹谭。
//下線所有服務(wù)
dubbo>offline
OK
//根據(jù)正則,下線部分服務(wù)
dubbo>offline com.*
OK