Java 中 HttpURLConnection 的荒谬连接超时

发布于 2024-09-04 13:46:51 字数 545 浏览 7 评论 0原文

在我的一个应用程序中,我们使用 HttpURLConnection 访问另一台服务器 - 该应用程序工作了几个月,但现在突然所有访问都面临连接超时。间歇性地,少数调用(五百个中有一个)成功,而所有其他调用都失败。该应用程序部署在运行 Java 5 和 Tomcat 5.5 的 Linux 上。我已经从命令行尝试了 curlwget ,效果很好。

另一个使用完全相同的代码库的 Java 进程能够从命令行建立连接。我们已经重新启动了tomcat服务器以及机器,但没有效果。我们已经彻底检查了机器,没有任何阻塞进程/防火墙阻碍(显然其他 Java 进程能够建立连接)。

在间歇性突发情况下(例如每天一次),应用程序能够建立一些连接,然后返回到中断状态。从堆栈跟踪中也看不出太多东西。

有什么建议可能会出错吗?

[更新] 如果应用程序运行时服务器 DNS 发生变化,Java 将不会检测到此变化,并且旧 IP 的连接将超时(考虑到 IP 已关闭)。因此,在 CDN 的情况下,如果一个节点发生故障并被另一个 IP 替换,则可能会出现问题。

In one of my applications, we hit another server using HttpURLConnection - the application worked for fine for months, and now suddenly all hits are facing a connection timeout. Intermittently, a few calls (1 in 500) succeed while all others fail. The application is deployed on Linux running on Java 5 with Tomcat 5.5. I have tried a curl and wget from command line which work fine.

Another Java process using exactly the same code base is able to make connections from command line. We have restarted the tomcat server as well as the machine but to no avail. We have thoroughly checked the machine and there are no blocking processes/firewalls hindering with this (evident that other Java processes are able to make connections).

In intermittent bursts (say once a day) the application is able to establish a few connections and then returns back to the broken state. Not much is evident from the stack trace as well.

Any suggestions where it might be going wrong?

[Update] In case the server DNS changes while the application is running, Java will not pick this up and connections will timeout from the older IP (considering the IP is down). Thus, in case of CDN if a node goes down and it is replaced with another IP, the problem may occur.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-09-11 13:46:52

我遇到了同样的问题,这是由 HttpURLConnection 对 keepalive 的处理引起的。当我们通过设置此系统属性禁用 keepalive 时,问题就消失了,

http.keepAlive=false

我们还有另一个与防火墙相关的问题。如果目标URL被防火墙阻止,则需要很长时间才能超时(超过2分钟)。我们必须在另一个线程中运行所有 HttpURLConnection,以便我们可以在几秒钟后中断它。

I had the same problem and it was caused by HttpURLConnection's handling of keepalive. The problem went away when we disabled keepalive by setting this system property,

http.keepAlive=false

We have another issue related to firewall. If the destination URL is blocked by firewall, it takes long time to time out (over 2 minutes). We have to run all our HttpURLConnection in another thread so we can interrupt it after a few seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文