Unable to connect to remote JMX

I have a tomcat application on a remote host and I need to connect it to JConsole. The application starts with the parameters:

IP=`ifconfig eth0 | grep 'inet addr:' | cut -d ':' -f2 | cut -d ' ' -f1` -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=$IP -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false 

Port 9999 is open, the IP value is valid, I checked it. I can access it through telnet ( telnet <my_host> <my_port> ). Netstat:

 netstat -a | grep LISTEN tcp 0 0 *:9999 *:* LISTEN 

But I can not connect it through jconsole, I always get the same error:

 Connection Failed: Retry? 

But netstat -a shows that the connection is ESTABLISHED

I tried different addresses:

 <my_host>:<my_port> service:jmx:rmi://<my_host>:<my_port>/jndi/rmi://<my_host>:<my_port>/jmxrmi service:jmx:rmi:///jndi/rmi://<my_host>:<my_port>/jmxrmi 

I also tried adding the files .../conf/remote.users and .../remote.acl and writing patches to these files in the versions -Dcom.sun.management.jmxremote.password.file and -Dcom.sun.management.jmxremote.access.file , but this did not affect.

When I deploy this application on my local machine, I can connect to it in "localhost:9999"

Help someone what might be the problem?

+5
source share
3 answers

If you are able to telnet <my_host> <my_port> , then the problem should be due to the firewall, because in JMX, after the connection is established, it runs on the configured port, that is, the new port is assigned for further communication, and probably your Firwall does not allow opening new port.

For cross-validation, just try running below the Java JMX Client, which will try to display HeapMemoryUsage.

 import java.util.Set; import javax.management.MBeanServerConnection; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; public class JMXDemo { private static final String HOST = ""; // configure host <my_host> here private static final String PORT = ""; // configure port <my_port> here private static void testJMX() throws Exception { JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://" + HOST + "/jndi/rmi://" + HOST + ":" + PORT + "/jmxrmi"); JMXConnector jmxConnector = JMXConnectorFactory.connect(url); try { MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection(); ObjectName mbeanName = new ObjectName("java.lang:type=Memory"); javax.management.openmbean.CompositeDataSupport obj = null; obj = (javax.management.openmbean.CompositeDataSupport) mbeanServerConnection.getAttribute(mbeanName, "HeapMemoryUsage"); Set<String> keySet = obj.getCompositeType().keySet(); for (String key : keySet) { System.out.print(key + "=" + obj.get(key) + ", "); } } finally { jmxConnector.close(); } System.out.println("\n=========================="); } public static void main(String args[]) throws Exception { testJMX(); } } 

Enabling JMX Logging Create a logging.properties file

 handlers= java.util.logging.ConsoleHandler .level=ALL java.util.logging.FileHandler.pattern = jmx.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 // Use FINER or FINEST for javax.management.remote.level - FINEST is // very verbose... // javax.management.level=FINEST javax.management.remote.level=FINEST java.security.debug=all 

Compile java code and run using the command below

  java -Djava.util.logging.config.file=./logging.properties JMXDemo 

This will print so many logs on the console, you can grep "port".

+5
source

Write a basic Java program:

 import java.util.*; import java.io.*; class testCode{ public static void main(String args[]){ System.out.println("just killing time...do not enter a value and kill me "); Scanner sc = new Scanner(System.in); sc.nextInt(); } } 

Compile and run it with the following options:

 java -Dcom.sun.management.jmxremote=true \ -Dcom.sun.management.jmxremote.port=12345 \ -Dcom.sun.management.jmxremote.authenticate=false \ -Dcom.sun.management.jmxremote.ssl=false \ -Djava.rmi.server.hostname=10.1.10.167 \ testCode 

Replace rmi.server.hostname IP address of the machine running the Java code, then use jconsole to connect remotely using 10.1.10.167:12345 . No user ID and password required.

+2
source

try all these flags:

 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=$IP 

if Dcom.sun.management.jmxremote.local.only is true, it will block remote connections.

+1
source

Source: https://habr.com/ru/post/1244999/


All Articles