当前位置: 首页 > news >正文

(3)Dubbo启动时qos-server can not bind localhost22222错误解决

本节介绍

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

相关文章:

  • (2020)Java后端开发----(面试题和笔试题)
  • lt;JVM调优gt;为什么内存过大?
  • InputMismatchException异常
  • 定时器及案例
  • 代谢组学研究的十大误区——误区十
  • 微服务项目:尚融宝(8)(后端接口:积分等级CRUD)
  • 《用Go语言自制解释器》之第4章 扩展解释器
  • SQL每日一练(牛客新题库)——第9天:检索数据
  • JQuery系列之事件切换
  • 【第5天】SQL快速入门-必会的常用函数(SQL 小虚竹)
  • Java日志系列——概述,JUL
  • 猿创征文 |简单入门 redis6【基础命令】
  • L2-008 最长对称子串/【力扣5】 最长回文子串
  • 【Lua 入门基础篇(八)】元表
  • 创新案例分享 | 建设医院绩效管理系统,促进医院健康良性发展
  • php的引用
  • (三)从jvm层面了解线程的启动和停止
  • 08.Android之View事件问题
  • JavaScript对象详解
  • Laravel Telescope:优雅的应用调试工具
  • leetcode讲解--894. All Possible Full Binary Trees
  • overflow: hidden IE7无效
  • PhantomJS 安装
  • Vue2 SSR 的优化之旅
  • 大快搜索数据爬虫技术实例安装教学篇
  • 关于字符编码你应该知道的事情
  • #define,static,const,三种常量的区别
  • (Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (一)基于IDEA的JAVA基础1
  • (转)Linq学习笔记
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (转载)Google Chrome调试JS
  • ... 是什么 ?... 有什么用处?
  • .net core使用RPC方式进行高效的HTTP服务访问
  • .Net IE10 _doPostBack 未定义
  • .NET 中什么样的类是可使用 await 异步等待的?
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually
  • @Autowired自动装配
  • @data注解_一枚 架构师 也不会用的Lombok注解,相见恨晚
  • [.NET]桃源网络硬盘 v7.4
  • [1] 平面(Plane)图形的生成算法
  • [Android]Android P(9) WIFI学习笔记 - 扫描 (1)
  • [BZOJ4566][HAOI2016]找相同字符(SAM)
  • [c++] 什么是平凡类型,标准布局类型,POD类型,聚合体
  • [C++]类和对象(中)
  • [ComfyUI进阶教程] animatediff视频提示词书写要点
  • [GN] Vue3.2 快速上手 ---- 核心语法2
  • [GXYCTF2019]禁止套娃
  • [HackMyVM]靶场Boxing
  • [hive] 窗口函数 ROW_NUMBER()
  • [IE技巧] 让IE 以全屏模式启动
  • [Kubernetes]9. K8s ingress讲解借助ingress配置http,https访问k8s集群应用
  • [Linux] CE知识随笔含Ansible、防火墙、VIM、其他服务
  • [Linux] PHP程序员玩转Linux系列-telnet轻松使用邮箱