公司内部的优付系统通过http调用外部服务商系统的API。上周五夜间,服务端系统发版期间,客户端在通过http调用服务端接口时出现一堆 “拒绝连接 (Connection refused) ” 异常。

2022-04-08 22:22:28,676 ERROR [2022040822222771523885] ? (com.cn.yft.pay.util.HttpClientHelper:) - 请求urlParam 异常:
org.apache.http.conn.HttpHostConnectException: Connect to 123.56.223.21:8280 [/123.56.223.21] failed: 拒绝连接 (Connection refused)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:158) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.InternalHttpClient.doExecute$original$szvaDZwu(InternalHttpClient.java:184) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.InternalHttpClient.doExecute$original$szvaDZwu$accessor$2wpTSoQJ(InternalHttpClient.java) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.InternalHttpClient$auxiliary$f5vLomHg.call(Unknown Source) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86) ~[skywalking-agent.jar:8.6.0]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107) ~[httpclient-4.5.2.jar:4.5.2]
at com.cn.yft.pay.util.HttpClientHelper.httpClientPost(HttpClientHelper.java:506) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
at com.cn.yft.bank.p600001.own.PaymentTransByOWN.doBusiness$original$a5alvy8U(PaymentTransByOWN.java:150) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
at com.cn.yft.bank.p600001.own.PaymentTransByOWN.doBusiness$original$a5alvy8U$accessor$RlW8FGim(PaymentTransByOWN.java) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
at com.cn.yft.bank.p600001.own.PaymentTransByOWN$auxiliary$GTU0SZKs.call(Unknown Source) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:86) [skywalking-agent.jar:8.6.0]
at com.cn.yft.bank.p600001.own.PaymentTransByOWN.doBusiness(PaymentTransByOWN.java) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
at com.cn.yft.pay.service.PCPaymentRunnable.run(PCPaymentRunnable.java:50) [pctrans-core-0.0.1-SNAPSHOT.jar:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_191]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_191]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_191]
Caused by: java.net.ConnectException: 拒绝连接 (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:1.8.0_191]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_191]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_191]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_191]
at java.net.Socket.connect(Socket.java:589) ~[?:1.8.0_191]
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:74) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141) ~[httpclient-4.5.2.jar:4.5.2]
... 23 more

 

参考csdn上网友的Connection refused: connect问题解决方案集合,整理一下http调用出现connection refused异常的各种情况:

java.net.ConnectException: Connect to  XXXhost:XXXport failed: 拒绝连接 (Connection refused)_jar

 

客户端:

  • 写错了主机和端口
  • 通信协议不正确,http://,https://,rmi://
  • 客户端服务不在网络中。它可能没有连接到LAN或互联网或任何其他网络。
  • 客户端网络防火墙拦截请求

服务端:

  • 服务挂掉了 / 服务未运行
  • 服务在重启中 / 服务处于kill -15过程中
  • 服务端网络防火墙拦截请求
  • 服务端服务不在网络中。它可能没有连接到LAN或互联网或任何其他网络。
  • 服务器正在运行但没有侦听端口,或者服务器正在运行但是正在侦听不同的端口。

 

网站挂掉,网页无法访问,如下这个页面大家应该熟知。可以注意到code是ERR_CONNECTION_REFUSED

java.net.ConnectException: Connect to  XXXhost:XXXport failed: 拒绝连接 (Connection refused)_apache_02

 

 

再简单测试一下上面的几种情况。结论是:上面总结的几种情况并不都返回connection refused异常。至少如下几种不是。

我司公司网络限制访问淘宝、优酷等视频购物网站。就拿访问淘宝网(https://www.taobao.com)来说, 我们来 curl https://www.taobao.com 或 curl http://www.taobao.com

C:\Users\zhangguozhan>curl https://www.taobao.com
curl: (35) schannel: next InitializeSecurityContext failed: SEC_E_INVALID_TOKEN (0x80090308) - 给函数提供的标志无效

 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" /
><title>Forbidden</title>
</head>
<body>
你被禁止访问这个网站,请联系网站管理员!
</body>
</html>

 

 

把网络切换成自己手机的4G,再 curl https://www.taobao.com/,  此时可以正常返回响应结果。

java.net.ConnectException: Connect to  XXXhost:XXXport failed: 拒绝连接 (Connection refused)_jar_03

 

然后,自己手机4G情况下,把上面的https换成http, 即, 此时,返回的是我们熟知的301(重定向)错误。 

C:\Users\zhangguozhan>curl http://www.taobao.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<h1>301 Moved Permanently</h1>
<p>The requested resource has been assigned a new permanent URI.</p>
<hr/>Powered by Tengine</body>
</html>

 

以上,做个记录,方便日后工作中查证。