Java 使用 jConsole 连接远程 tomcat JMX 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1263991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:34:38  来源:igfitidea点击:

Connecting remote tomcat JMX instance using jConsole

javatomcatjmxjconsole

提问by Niger

I am trying to connect to a remote tomcat JMX instance using jConsole. But can't connect successfully. Any Idea?

我正在尝试使用 jConsole 连接到远程 tomcat JMX 实例。但是无法成功连接。任何的想法?

I included the following option in remote tomcat catalina.sh:

我在远程 tomcat 中包含了以下选项catalina.sh

JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote \
    -Dcom.sun.management.jmxremote.port=9004 \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false"

回答by joe p

What exactly do you mean when you say "But can't connect successfully."? Is there an error message? Try turning on logging in jconsole and see if that helps debug it.

当您说“但无法成功连接”时,您到底是什么意思?有错误信息吗?尝试打开 jconsole 的登录功能,看看这是否有助于调试它。

To turn on jconsole logging, edit a file named logging.properties in the directory you will be running jconsole in, add:

要打开 jconsole 日志记录,请在您将运行 jconsole 的目录中编辑一个名为 logging.properties 的文件,添加:

handlers= java.util.logging.ConsoleHandler

.level=INFO

java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

javax.management.level=FINEST
javax.management.remote.level=FINEST

Then, start jconsole with:

然后,使用以下命令启动 jconsole:

jconsole -J-Djava.util.logging.config.file=logging.properties

回答by waxwing

I had a similar, if not the same, problem. I could connect to the JMX server if I started jconsole locally on the machine.

我有一个类似的问题,如果不一样的话。如果我在机器上本地启动 jconsole,我可以连接到 JMX 服务器。

It appears the RMI server was not listening on the correct ip. So, as was suggested in this related question, I added the following:

看来 RMI 服务器没有在正确的 IP 上侦听。所以,正如在这个相关问题中所建议的,我添加了以下内容:

-Djava.rmi.server.hostname=<host ip>

to JAVA_OPTSas well, and then it worked.

JAVA_OPTS一样,然后它起作用了。

回答by Matt

what string are you using as the JMX connection url. I don't mean to point out the obvious but JConsole has a terrible interface and to me requires an overly complex url before it will connect to a remote jmx app. Mine looks like this:

您使用什么字符串作为 JMX 连接 url。我并不是要指出显而易见的问题,但 JConsole 有一个糟糕的界面,对我来说,它需要一个过于复杂的 url,然后才能连接到远程 jmx 应用程序。我的看起来像这样:

service:jmx:rmi:///jndi/rmi://(hostname):(jmxport)/jmxrmi

回答by PedroG

Changing the /etc/hostson linux, where I replaced the localhost address associated to my account to the machine ip, solved this problem for me.

更改/etc/hosts在 linux 上,我将与我的帐户关联的本地主机地址替换为机器 ip,为我解决了这个问题。

回答by Hajo Thelen

I've collected information spread over the net, found with hints from other members.

我收集了通过网络传播的信息,并从其他成员那里找到了提示。

Most pain caused by JMX is (imo) the fact that JMX opens a second dynamically allocated network port. A firewall (like iptables) will block this.

JMX 引起的最大痛苦是 (imo) JMX 打开第二个动态分配的网络端口的事实。防火墙(如 iptables)会阻止它。

Solution for tomcat on linux :

linux上tomcat的解决方法:

use tomcat 6.0.24 or newer download catalina-jmx-remote.jar from apache tomcat extras (use browse on tomcat download page) copy it in the $CTALINA_HOME\lib

使用 tomcat 6.0.24 或更新版本从 apache tomcat extras 下载 catalina-jmx-remote.jar(使用浏览 tomcat 下载页面)将其复制到 $CTALINA_HOME\lib

This allows you to set both ports used by JMX

这允许您设置 JMX 使用的两个端口

edit Server section in your server.xml

编辑 server.xml 中的 Server 部分

<Server port="8005" ..>
  ...
  <Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener" rmiRegistryPortPlatform="9840" rmiServerPortPlatform="9841"/>

set some environment variables (e.g. in setenv.sh)

设置一些环境变量(例如在 setenv.sh 中)

CATALINA_OPTS="
  -Djava.rmi.server.hostname=IP-TO-LISTEN
  -Dcom.sun.management.jmxremote.password.file=$CATALINA_BASE/conf/jmxremote.password 
  -Dcom.sun.management.jmxremote.access.file=$CATALINA_BASE/conf/jmxremote.access 
  -Dcom.sun.management.jmxremote.ssl=false"

this activates access control for JMX

这将激活 JMX 的访问控制

jmxremote.access will look like

jmxremote.access 看起来像

monitorRole readonly
controlRole readwrite

end jmxremote.password will be

结束 jmxremote.password 将是

monitorRole tomcat
controlRole tomcat

(just simple spaces)

(只是简单的空格)

restart tomcat.

重启tomcat。

Now configure firewall on the server (e.g. iptables)

现在在服务器上配置防火墙(例如 iptables)

/etc/sysconfig/iptables

/etc/sysconfig/iptables

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 9840 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 9841 -j ACCEPT

and /etc/sysconfig/ip6tables

和 /etc/sysconfig/ip6tables

-A RH-Firewall-1-INPUT -m tcp -p tcp --dport 9840 -j ACCEPT
-A RH-Firewall-1-INPUT -m tcp -p tcp --dport 9841 -j ACCEPT

restart iptables

重启iptables

Done!

完毕!

Now use VisualVM or JConsole on your workstation to establish a connection to rmiRegistryPortPlatform, 9840 in our sample.

现在在您的工作站上使用 VisualVM 或 JConsole 建立到 rmiRegistryPortPlatform,在我们的示例中为 9840 的连接。

If there are no more firewalls between workstation and server it should work.

如果工作站和服务器之间没有更多的防火墙,它应该可以工作。

回答by Icarokun

Well, I had this problem in a Linux box (virtual machine) and I fixed it using -Djava.rmi.server.hostname property but there's a thing I can't understand. My machine has 5 tomcat servers, all of them has jmx enabled in consecutive ports (8008,8018,8028...) and only one of them had this issue connecting JMX. No firewall, no -Djava.rmi.server.hostname property in any tomcat....

好吧,我在 Linux 机器(虚拟机)中遇到了这个问题,我使用 -Djava.rmi.server.hostname 属性修复了它,但有一点我无法理解。我的机器有 5 个 tomcat 服务器,所有这些服务器都在连续端口(8008,8018,8028...)中启用了 jmx,并且其中只有一个连接 JMX 时出现此问题。没有防火墙,任何 tomcat 中都没有 -Djava.rmi.server.hostname 属性....

So the thing is that I understand the problem but I can't understand why 4 of my tomcats worked and 1 of them not.

所以问题是我理解这个问题,但我不明白为什么我的 4 个 tomcat 工作而其中 1 个没有。

P.D: My english is very poor, I know. My Apologies.

PD:我的英语很差,我知道。我很抱歉。

回答by supdog

PROTIP: You need to fix (as in having a known number) the RMI Registry and JMX/RMI Server ports. You do this by putting jar-file in the lib-dir and configuring a special listener. (And ofcourse the usual flags for activating JMX

专业提示:您需要修复(如具有已知编号)RMI 注册表和 JMX/RMI 服务器端口。您可以通过将 jar-file 放入 lib-dir 并配置一个特殊的侦听器来完成此操作。(当然还有用于激活 JMX 的常用标志

    -Dcom.sun.management.jmxremote  \
    -Dcom.sun.management.jmxremote.port=8999 \
    -Dcom.sun.management.jmxremote.ssl=false \
    -Dcom.sun.management.jmxremote.authenticate=false \
    -Djava.rmi.server.hostname=<HOSTNAME> \

See: JMX Remote Lifecycle Listener at http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html

请参阅:位于http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html 的JMX 远程生命周期侦听器

回答by andhdo

if you are working on linux, modify the catalina.sh file adding:

如果您使用的是 linux,请修改 catalina.sh 文件,添加:

                CATALINA_OPTS="-Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=<HOST_IP> -Dcom.sun.management.jmxremote.port=<HOST_PORT> -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
            export CATALINA_OPTS

or modify the /etc/profile file as root and rerun the file (source /etc/profile)

或以 root 身份修改 /etc/profile 文件并重新运行该文件(源 /etc/profile)

if you are working on windows and you are starting tomcat from the command line, use the environment variable CATALINA_OPTS

如果您在 Windows 上工作并且从命令行启动 tomcat,请使用环境变量 CATALINA_OPTS

if you are working on windows and you are starting tomcat as a service, you'll need to use the monitor service utility to configure the service initialization parameters (neither setenv.bat, catalina.bat or env-vars will work). for that you'll need the service name that appears listed in services.msc (for example jasperreportsTomcat). After, you'll need to open a console as administrator and execute (for example): tomcat6w.exe //MS//jasperreportsTomcat

如果您在 Windows 上工作并且将 tomcat 作为服务启动,则需要使用监视器服务实用程序来配置服务初始化参数(setenv.bat、catalina.bat 或 env-vars 都不起作用)。为此,您需要在 services.msc 中列出的服务名称(例如 jasperreportsTomcat)。之后,您需要以管理员身份打开控制台并执行(例如):tomcat6w.exe //MS//jasperreportsTomcat

with this command will appear a tray icon where you can open a panel. In the "Java" tab now you can modify the jmx options. Be careful to not add trailing whitespaces and use the "[enter]" symbol to separate each option line by line.

使用此命令将出现一个托盘图标,您可以在其中打开面板。在“Java”选项卡中,您现在可以修改 jmx 选项。注意不要添加尾随空格并使用“[enter]”符号逐行分隔每个选项。

-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=192.168.61.101
-Dcom.sun.management.jmxremote.port=9999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false

Hope it helps

希望能帮助到你

回答by Victor

I got something for all of you, in order to complete the investigation of this whole thing. There is a trick, it happens that profiler tool connnects with the jvm using a port, but the jvm continues the conversation using another random port. If the jvm is running inside a remote machine (for example : a tomcat web-app server), and the remote machine has protection against outgoing and incoming connections, you must set the java system property com.sun.management.jmxremote.rmi.portto the same value of the property named com.sun.management.jmxremote.port

我为你们准备了一些东西,以便完成对整个事情的调查。有一个技巧,分析器工具碰巧使用端口与 jvm 连接,但 jvm 使用另一个随机端口继续对话。如果 jvm 在远程机器(例如:tomcat web-app 服务器)中运行,并且远程机器具有防止传出和传入连接的保护,则必须将 java 系统属性com.sun.management.jmxremote.rmi.port设置为与命名的属性相同的值com.sun.management.jmxremote.port

Source : https://serverfault.com/questions/308662/how-do-i-fix-a-failed-to-retrieve-rmiserver-stub-jmx-errorAnd also check this out : http://blog.cantremember.com/debugging-with-jconsole-jmx-ssh-tunnels/

来源:https ://serverfault.com/questions/308662/how-do-i-fix-a-failed-to-retrieve-rmiserver-stub-jmx-error并检查一下:http://blog.cantremember .com/debugging-with-jconsole-jmx-ssh-tunnels/

Hope to contribute guys!

希望各位大侠多多指教!

And good luck!

还有祝你好运!

回答by arganzheng

Check if your server is behind the firewall. JMX is base on RMI, which open two port when it start. One is the register port, default is 1099, and can be specified by the com.sun.management.jmxremote.port option. The other is for data communication, and is random, which is what cause problem. A good news is that, from JDK6, this random port can be specified by the com.sun.management.jmxremote.rmi.port option.

检查您的服务器是否在防火墙后面。JMX基于RMI,启动时打开两个端口。一个是注册端口,默认是1099,可以通过com.sun.management.jmxremote.port选项指定。另一个用于数据通信,并且是随机的,这就是导致问题的原因。好消息是,从 JDK6 开始,可以通过 com.sun.management.jmxremote.rmi.port 选项指定这个随机端口。

add the line in you {tomcat_dir}/bin/setenv.sh:

在 {tomcat_dir}/bin/setenv.sh 中添加以下行:

export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8991 -Dcom.sun.management.jmxremote.rmi.port=8991 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false"