简体   繁体   English

哪个 Java 线程监听哪个端口?

[英]Which Java thread listens to which port?

I have a Tomcat 9.0.60 Server running on OpenJDK 1.8.0 on Linux where a netstat -ltpn|grep java shows that it listens to more than the configured ports:我有一个在 Linux 上的 OpenJDK 1.8.0 上运行的 Tomcat 9.0.60 服务器,其中netstat -ltpn|grep java显示它侦听的端口数量超过了配置的端口:

tcp        0      0 0.0.0.0:37917           0.0.0.0:*               LISTEN      50384/java          
tcp        0      0 10.100.0.188:39780      0.0.0.0:*               LISTEN      50384/java          
tcp        0      0 10.100.0.188:41415      0.0.0.0:*               LISTEN      50384/java          
tcp        0      0 0.0.0.0:34476           0.0.0.0:*               LISTEN      51139/java          

(I removed all the configured ports from that list.) (我从该列表中删除了所有配置的端口。)

So I think that several applications also opened "their own" sockets they listen to.所以我认为几个应用程序也打开了他们监听的“他们自己的”套接字。 I want to find out for each port, which of the Java threads is listening to, so I can then further find out the application and maybe if it is still required or just a leftover one of the devs forgot.我想找出每个端口,哪个 Java 线程正在侦听,这样我就可以进一步找出应用程序,也许它是否仍然需要,或者只是开发人员忘记的剩余一个。

So far I tried to find out things with the VisualVM via a JMX connection, but did not get very far - probably because I'm not experienced enough.到目前为止,我试图通过 JMX 连接来了解VisualVM的内容,但没有走得很远——可能是因为我的经验不够。

Can you give me any hints - or maybe a complete solution - how I can find out the port to thread mapping please?你能给我任何提示 - 或者可能是一个完整的解决方案 - 我如何找出端口到线程映射?

I don't need to automate this, it's more of a once-in-a-while manual job for documentation and checking.我不需要自动执行此操作,它更像是偶尔进行的文档和检查手动工作。

If I was going to figure out what Java threads are listening, I would try the following things:如果我要弄清楚 Java 线程在听什么,我会尝试以下方法:

  1. Search the web application codebase for Java code that uses the ServerSocket class, and work backwards to what application code is responsible.在 Web 应用程序代码库中搜索使用ServerSocket类的 Java 代码,然后返回到负责的应用程序代码。

  2. Use jstack (or similar) to capture a snapshot of all of the Java thread stacks.使用jstack (或类似工具)捕获所有 Java 线程堆栈的快照。 Then scan them to find threads that are blocked in calls to the accept method.然后扫描它们以查找在调用accept方法时被阻塞的线程。

  3. Use lsof with the -K option to get the thread ids for the llistening threads.使用带有-K选项的lsof来获取监听线程的线程 ID。 Then use jstack and the technique described in How to obtain the JVM thread name/id through the known PID/TID to map the thread id to the Java thread.然后使用jstack如何通过已知的 PID/TID 获取 JVM 线程名称/id中描述的技术将线程 id 映射到 Java 线程。

Getting the Linux thread id for a Java thread in a Java thread involves using native code.在 Java 线程中获取 Java 线程的 Linux 线程 ID 涉及使用本机代码。 But I don't think you need to get down to that level to get the information you need.但我不认为你需要达到那个水平来获得你需要的信息。


I'm puzzled as to why you are doing this.我很困惑你为什么要这样做。 This kind of information is unlikely to help you debug the webapps.这类信息不太可能帮助您调试 web 应用程序。 And if you are doing this because you suspect there is a security problem ... ask your Devs to "please explain" the unexpected TCP / listens on unexpected ports.如果您这样做是因为您怀疑存在安全问题……请您的开发人员“请解释”意外的 TCP / 侦听意外的端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM