首页 文章

通过SSH将Jconsole连接到远程JVM

提问于
浏览
1

我试图通过ssh使用jconsole作为JMX客户端连接到远程Java应用程序

我已经构建了https://blogs.oracle.com/jmxetc/entry/connecting_through_firewall_using_jmx上描述的CustomAgent

代理的代码在这里

/**
* 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:agents / build / jar / 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 path-to-my-app / app.jar

在主机上打开两个端口6004和6005,如果jconsole是应用程序的本地端口,我可以通过jconsole连接到应用程序 .

然后我创建了2个ssh隧道

ssh tunnels ssh -L 6004:localhost:6004 foo @ server ssh tunnels ssh -L 6005:localhost:6005 foo @ server

隧道已成功创建 . 我可以在这些端口上远程登录 .

现在问题是我无法使用Jconsole远程连接

service:jmx:rmi:// localhost:6005 / jndi / rmi:// localhost:6004 / jmxrmi

jconsole开始于:

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

错误日志是:

2011年12月18日下午4:57:46 RMIConnector连接
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 = service: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]连接存根... 2011年12月18日下午4:58:49 RMIConnector连接FINER:[javax.management.remote.rmi.RMIConnector:jmxServiceURL = service:jmx:rmi:/ / localhost:6005 / jndi / rmi:// localhost:6004 / jmxrmi]获取连接... 2011年12月18日下午4:59:52 RMIConnector连接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:连接在sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)的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)javax.management.remote.JMXConnectorFactory中的未知来源 . 在sun.tools.jconsole.ProxyClient的sun.tools.jconsole.ProxyClient.tryConnect(ProxyClient.java:366)的javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:226)上连接(JMXConnectorFactory.java:267) .connect(ProxyClient.java:316)at sun.tools.jconsole.VMPanel $ 2.run(VMPanel.java:298)引起:java.net.ConnectException:连接在java.net.P超时位于java.net.AbstractPlainSocketImpl.mplConnect(AbstractPlainSocketImpl.java:327)的java.net.AbstractPocketImpl.mplConnect(AbstractPlainSocketImpl.java:193)中的lainSocketImpl.socketConnect(Native Method),java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java: 180)在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384)java.net.Socket.connect(Socket.java:546)java.net.Socket.connect(Socket.java:495)java . net.Socket . (Socket.java:392)at java..Socket . (Socket.java:206)at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)at sun.rmi.transport .proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)... 11更多

3 回答

  • 0

    我需要补充一下

    -Djava.rmi.server.hostname = localhost

    到java -server参数

  • 2

    BTW第二个端口现在由Hotspot 7 JVM直接支持 - http://dimovelev.blogspot.de/2013/10/out-of-box-jmx-over-firewall-in-hotspot.html

  • 1

    尝试使用以下选项运行服务器:

    -Dcom.sun.management.jmxremote
    -Dcom.sun.management.jmxremote.port=6004
    -Dcom.sun.management.jmxremote.rmi.port=6004
    -Dcom.sun.management.jmxremote.ssl=false 
    -Dcom.sun.management.jmxremote.authenticate=false 
    -Dcom.sun.management.jmxremote.local.only=true
    

    一旦你有6004隧道尝试使用jconsole:

    jconsole -J-DsocksProxyHost=127.0.0.1 -J-DsocksProxyPort=6004 service:jmx:rmi:///jndi/rmi://0.0.0.0:6004/jmxrmi -J-DsocksNonProxyHosts=
    

相关问题