我試圖使用jconsole作爲一個JMX客戶端通過ssh連接到遠程Java應用JConsole連接到遠程JVM通過SSH
我已經建立了CustomAgent上https://blogs.oracle.com/jmxetc/entry/connecting_through_firewall_using_jmx
描述的代碼代理在這裏
/**
* CustomAgent.java
* https://blogs.oracle.com/jmxetc/entry/connecting_through_firewall_using_jmx
*/
package example.rmi.agent;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
/**
* This CustomAgent will start an RMI COnnector Server using only
* port "example.rmi.agent.port".
*
* @author dfuchs
*/
public class CustomAgent {
private CustomAgent() { }
public static void premain(String agentArgs)
throws IOException {
// Ensure cryptographically strong random number generator used
// to choose the object number - see java.rmi.server.ObjID
//
System.setProperty("java.rmi.server.randomIDs", "true");
// Start an RMI registry on port specified by example.rmi.agent.port
// (default 3000).
//
final int port= Integer.parseInt(
System.getProperty("example.rmi.agent.port","3000"));
System.out.println("Create RMI registry on port "+port);
LocateRegistry.createRegistry(port);
// Retrieve the PlatformMBeanServer.
//
System.out.println("Get the platform's MBean server");
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
// Environment map.
//
System.out.println("Initialize the environment map");
HashMap<String,Object> env = new HashMap<String,Object>();
// This where we would enable security - left out of this
// for the sake of the example....
//
// Create an RMI connector server.
//
// As specified in the JMXServiceURL the RMIServer stub will be
// registered in the RMI registry running in the local host on
// port 3000 with the name "jmxrmi". This is the same name the
// out-of-the-box management agent uses to register the RMIServer
// stub too.
//
// The port specified in "service:jmx:rmi://"+hostname+":"+port
// is the second port, where RMI connection objects will be exported.
// Here we use the same port as that we choose for the RMI registry.
// The port for the RMI registry is specified in the second part
// of the URL, in "rmi://"+hostname+":"+port
//
System.out.println("Create an RMI connector server");
final String hostname = InetAddress.getLocalHost().getHostName();
//JMXServiceURL url =
// new JMXServiceURL("service:jmx:rmi://"+hostname+
// ":"+port+"/jndi/rmi://"+hostname+":"+port+"/jmxrmi");
//Added to allow connection through a firewall
// We use (port+1) to export the RMI connection objects
JMXServiceURL url =
new JMXServiceURL("service:jmx:rmi://"+hostname+
":"+(port+1)+"/jndi/rmi://"+hostname+":"+port+"/jmxrmi");
// Now create the server from the JMXServiceURL
//
JMXConnectorServer cs =
JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
// Start the RMI connector server.
//
System.out.println("Start the RMI connector server on port "+port);
cs.start();
}
}
構建腳本是在這裏
<project name="Agent" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="example.rmi.agent.CustomAgent"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
<!-- Builds dist.agent.jar -->
<target name="build-agent-jar" description="build an agent jar that can be used with -javaagent" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar basedir="${classes.dir}" destfile="${jar.dir}/${ant.project.name}.jar">
<manifest>
<attribute name="Premain-Class" value="example.rmi.agent.CustomAgent"/>
</manifest>
</jar>
<echo>To use this application with agent try:</echo>
<echo>java -Dexample.rmi.port=3000 -javaagent:${dist.agent.jar} -classpath [application-classpath] [application-classpath] [application-main-class]</echo>
</target>
</project>
開始使用在遠程機器上的應用程序
的java -javaagent:試劑/建造/罐/ agent.jar中 -Dexample.rmi.agent.port = 6004 -server -server -Dcom.sun.management .jmxremote.authenticate =「false」-Dcom.sun.management.jmxremote =「true」-Dcom.sun.management.jmxremote.ssl =「false」-Dcom.sun.management.jmxremote.local.only =「false」 -jar路徑到我的應用程序內/ app.jar
兩個端口的主機,6004和6005,我可以通過JConsole的連接應用程序。如果JConsole的是本地的應用程序開放。
我再創建2條SSH隧道
SSH隧道SSH-L 6004:本地主機:6004 FOO @服務器SSH隧道 的ssh -L 6005:本地主機:6005 FOO @服務器
的隧道已成功創建。我可以在這些端口上遠程登錄。
現在的問題是,我不能使用JConsole雖然
服務遠程連接:JMX:RMI://本地主機:6005/JNDI/RMI://本地主機:6004/jmxrmi
:JConsole的-J-Djava.util.logging.config.file = logging.properties -debug
JConsole的是由啓動
錯誤日誌:
Dec 18, 2011 4:57:46 PM RMIConnector connect FINER: [javax.management.remote.rmi.RMIConnector: jmxServiceURL=service:jmx:rmi://localhost:6005/jndi/rmi://localhost:6004/jmxrmi]
連接... 2011年12月18日下午4時57分46秒RMIConnector連接 FINER:[javax.management.remote.rmi.RMIConnector:JMXServiceURL中=服務:jmx:rmi:// localhost:6005/jndi/rmi:// localhost:6004/jmxrmi] 找到存根... 2011年12月18日下午4時58分49秒RMIConnector連接 FINER:[javax.management .remote.rmi.RMIConnector:jmxServiceURL = service:jmx:rmi:// localhost:6005/jndi/rmi:// localhost:6004/jmxrmi] 連接存根... D ec 18,2011 4:58:49 PM RMIConnector connect FINER:[javax.management.remote.rmi.RMIConnector:jmxServiceURL = service:jmx:rmi:// localhost:6005/jndi/rmi:// localhost:6004/jmxrmi] 正在建立連接... 2011年12月18日下午4:59:52 RMIConnector connect FINER:[javax.management.remote.rmi.RMIConnector:jmxServiceURL = service:jmx:rmi:// localhost:6005/jndi/rmi:// localhost:6004/jmxrmi] 未能連接:java.rmi.ConnectException:連接拒絕到 主機:172.16.0.111;嵌套異常是: java.net.ConnectException:連接超時 java.rmi.ConnectException:連接拒絕主機:172.16.0.111;嵌套異常是: java.net.ConnectException:連接超時 at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel。 java:216) at Sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:128) at javax.management.remote。 rmi.RMIServerImpl_Stub.newClient(未知 源) 在javax.management.remote.rmi.RMIConnector.getConnection(RMIConnector.java:2343) 在javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:296) at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:267) at sunx.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:226) at sun.tools.jconsole.ProxyClient.tryConnect(ProxyClient.java:366) at sun.tools.jconsole.ProxyClient.connect(ProxyClient。 java:316) at sun.tools.jconsole.VMPanel $ 2.run(VMPanel.java:298) 引起:java.net.ConnectException:連接超時 at java.net.PlainSocketImpl.socketConnect(Native Method) 在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327) 在java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193) 在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180) 在java的.net.SocksSocketImpl.connec t(SocksSocketImpl.java:384) at java.net.Socket.connect(Socket.java:546) at java.net.Socket.connect(Socket.java:495) at java.net.Socket。(Socket .java:392) at java.net.Socket。(Socket.java:206) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) at sun.rmi.transport.proxy。 RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146) 在sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613) ...... 11多個
是的,萬一你有多個網絡適配器。您需要使用上述屬性指定要偵聽遠程連接的適配器的IP地址。 –