问题描述
在启动dubbo-consumer工程时报错,信息如下:
2019-11-29 09:22:18.001 ERROR [RMI TCP Connection(2)-127.0.0.1] [org.apache.dubbo.qos.server.Server:103] - [DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.7.2, current host: 172.16.10.62
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_201]
at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_201]
at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_201]
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_201]
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:128) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1358) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:366) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:163) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:465) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_201]
2019-11-29 09:22:25.857 WARN [RMI TCP Connection(2)-127.0.0.1] [org.apache.dubbo.qos.protocol.QosProtocolWrapper:103] - [DUBBO] Fail to start qos server: , dubbo version: 2.7.2, current host: 172.16.10.62
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_201]
at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_201]
at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_201]
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_201]
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:128) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1358) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:366) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$$$capture(AbstractEventExecutor.java:163) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:465) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-all-4.1.25.Final.jar:4.1.25.Final]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_201]
关键错误信息:
[DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.7.2, current host: 172.16.10.62
java.net.BindException: Address already in use: bind
[DUBBO] Fail to start qos server: , dubbo version: 2.7.2, current host: 172.16.10.62
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自动装配方式
- 使用JVM系统属性
-Ddubbo.application.qos.enable=true
-Ddubbo.application.qos.port=33333
-Ddubbo.application.qos.accept.foreign.ip=false
- 使用dubbo.properties文件进行配置
在项目的src/main/resources
目录下添加dubbo.properties文件,内容如下:
- 使用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="zookeeper://192.168.10.62:2181?backup=192.168.10.63:2181,192.168.10.64:2181" />
<!-- 协议 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 对外暴露的接口服务 -->
<dubbo:service interface="org.apache.dubbo.demo.provider.DemoService" ref="demoService"/>
<!-- bean定义 -->
<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工程给占用了,所以才会报错。
如果要避免qos默认端口号22222被占用的问题,则需要修改consumer工程的配置文件dubbo-consumer.xml,如下:
<?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
4 xmlns="http://www.springframework.org/schema/beans"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
6 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
7
8 <!-- 消费方应用 -->
9 <dubbo:application name="simple-consumer">
10 <dubbo:parameter key="qos.enable" value="true" />
11 <dubbo:parameter key="qos.accept.foreign.ip" value="false" />
12 <dubbo:parameter key="qos.port" value="33333" />
13 </dubbo:application>
14
15 <!-- 注册中心地址 -->
16 <dubbo:registry address="zookeeper://192.168.10.62:2181?backup=192.168.10.63:2181,192.168.10.64:2181" />
17
18 <!-- 引用接口服务 -->
19 <dubbo:reference id="exampleService" check="false" interface="com.lw.service.example.ExampleService" />
20
21 </beans>