远程连接不适用于JMX从主方法开始

时间:2017-12-18 17:03:29

标签: java jmx

当服务器和客户端在同一台计算机上时,代码可以正常工作。当我在另一台机器上运行服务器代码时,我得到以下例外。

[WARNING] 
java.rmi.ConnectException: Connection refused to host: 127.0.1.1; nested exception is: 
    java.net.ConnectException: Operation timed out (Connection timed out)
    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:129)
    at javax.management.remote.rmi.RMIServerImpl_Stub.newClient(Unknown Source)
    at javax.management.remote.rmi.RMIConnector.getConnection(RMIConnector.java:2430)
    at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:308)
    at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:270)
    at Client.main(Client.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:282)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Operation timed out (Connection timed out)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
    at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:148)
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
    ... 14 more

当客户端真正使用下面的JMXServiceURL时,有异常说127.0.1.1拒绝连接,这是可疑的。

service:jmx:rmi:///jndi/rmi://10.10.173.196:8474/jmxrmi

如果我使用-Dcom.sun.management.jmxremote。*设置启动应用程序,则客户端不会遇到远程连接问题。

代码粘贴在下面:

import javax.management.MBeanServer;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.*;

public class Server {

    public static void main(String[] args) throws Exception {
        if(args.length != 2) {
            throw new IllegalArgumentException("Server <port> <path suffix>"
                            + "\nservice:jmx:rmi://0.0.0.0:<port>/jndi/rmi://0.0.0.0:<port>/<pathsuffix>"
                            );
        }

        int port = Integer.parseInt(args[0]);
        String pathSuffix = args[1];

        LocateRegistry.createRegistry(port);

        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://0.0.0.0:"
                        + port + "/jndi/rmi://0.0.0.0:" + port + "/" + pathSuffix);

        Map<String, Object> envConf = new HashMap<>();
        envConf.put("jmx.remote.x.password.file", "password.properties");
        envConf.put("jmx.remote.x.access.file", "access.properties");

        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, envConf, mbs);
        cs.start();

        while(true) {
            Thread.sleep(1000);
        }
    }

}

我还在服务器上使用了JMXServiceURL:

service:jmx:rmi:///jndi/rmi://0.0.0.0:<port>/jmxrmi

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/a/11654322/1810962提到必须正确配置/ etc / hosts。在JMX工作的机器上我有:

hostname -i
10.10.172.72 10.10.172.72

在没有工作的机器上:

hostname -i
127.0.1.1