ehcache 节点之一错误地尝试连接到 127.0.0.1

exp*_*ert 1 java rmi ehcache

你们能帮我理解为什么 ehcache 节点之一错误地尝试连接到 127.0.0.1 吗?

我正在使用 ehcache 2.8.3。我的一个节点在 NAT 模式下在 VMWare 下运行。因此,主机具有 ip 192.168.10.1(Windows 7),而 VMWare 中的一个是192.168.10.128(CentOS 6)。

我有以下 ehcache 配置

<cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
                                 properties="peerDiscovery=manual, rmiUrls=//192.168.10.128:51000/myCache1|//192.168.10.1:51000/myCache1"/>

<cacheManagerPeerListenerFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
                                 properties="hostName=0.0.0.0,port=51000,socketTimeoutMillis=2000"/>

<diskStore path="java.io.tmpdir"/>

<defaultCache
        maxEntriesLocalHeap="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        diskSpoolBufferSizeMB="30"
        maxEntriesLocalDisk="10000000"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU"
        statistics="false">
    <persistence strategy="localTempSwap"/>
</defaultCache>

<cache name="myCache1"
       maxEntriesLocalHeap="10000"
       maxEntriesLocalDisk="10000"
       eternal="false"
       diskSpoolBufferSizeMB="20"
       timeToIdleSeconds="300"
       timeToLiveSeconds="600"
       memoryStoreEvictionPolicy="LFU"
       transactionalMode="off">
    <persistence strategy="localTempSwap"/>

    <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>
Run Code Online (Sandbox Code Playgroud)

来自192.168.10.128要192.168.10.1成功路由的消息。但是相反的方向不起作用。我在登录时遇到以下错误192.168.10.1

2014-07-11 02:02:19.260 +0400 DEBUG Lookup URL //192.168.10.128:51000/myCache1
2014-07-11 02:02:20.262 +0400 DEBUG Lookup URL //192.168.10.1:51000/myCache1
2014-07-11 02:02:21.264 +0400 WARN  Unable to send message to remote peer.  Message was: Connection refused to host: 127.0.0.1; nested exception is:
        java.net.ConnectException: Connection refused: connect
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
        java.net.ConnectException: Connection refused: connect
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619) ~[na:1.7.0_60]
        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) ~[na:1.7.0_60]
        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) ~[na:1.7.0_60]
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:129) ~[na:1.7.0_60]
        at net.sf.ehcache.distribution.RMICachePeer_Stub.send(Unknown Source) ~[services.jar:1.1]
        at net.sf.ehcache.distribution.RMIAsynchronousCacheReplicator.writeReplicationQueue(RMIAsynchronousCacheReplicator.java:314) [services.jar:1.1]
        at net.sf.ehcache.distribution.RMIAsynchronousCacheReplicator.replicationThreadMain(RMIAsynchronousCacheReplicator.java:127) [services.jar:1.1]
        at net.sf.ehcache.distribution.RMIAsynchronousCacheReplicator.access$000(RMIAsynchronousCacheReplicator.java:58) [services.jar:1.1]
        at net.sf.ehcache.distribution.RMIAsynchronousCacheReplicator$ReplicationThread.run(RMIAsynchronousCacheReplicator.java:389) [services.jar:1.1]
Caused by: java.net.ConnectException: Connection refused: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method) ~[na:1.7.0_60]
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) ~[na:1.7.0_60]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[na:1.7.0_60]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[na:1.7.0_60]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[na:1.7.0_60]
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.7.0_60]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.7.0_60]
        at java.net.Socket.connect(Socket.java:579) ~[na:1.7.0_60]
        at java.net.Socket.connect(Socket.java:528) ~[na:1.7.0_60]
        at java.net.Socket.<init>(Socket.java:425) ~[na:1.7.0_60]
        at java.net.Socket.<init>(Socket.java:208) ~[na:1.7.0_60]
        at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) ~[na:1.7.0_60]
        at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:147) ~[na:1.7.0_60]
        at net.sf.ehcache.distribution.ConfigurableRMIClientSocketFactory.createSocket(ConfigurableRMIClientSocketFactory.java:71) ~[services.jar:1.1]
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613) ~[na:1.7.0_60]
        ... 8 common frames omitted
Run Code Online (Sandbox Code Playgroud)

如果我不在配置文件中的任何位置,它为什么要尝试连接到 127.0.0.1?

我可以从 192.168.10.1 telnet 到 192.168.10.128:51000。

我还尝试启用引导程序并开始看到以下日志消息

2014-07-11 02:35:30.515 +0400 DEBUG cache peers: [RMICachePeer_Stub[UnicastRef2 [liveRef: [endpoint:[127.0.0.1:18405,net.sf.ehcache.distribution.ConfigurableRMIClientSocketFactory@7d0](remote),objID:[-43892557:1472247d06b:-7fff, -5287536613776006259]]]]]
2014-07-11 02:35:30.516 +0400 DEBUG Bootstrapping myCache1 from RMICachePeer_Stub[UnicastRef2 [liveRef: [endpoint:[127.0.0.1:18405,net.sf.ehcache.distribution.ConfigurableRMIClientSocketFactory@7d0](remote),objID:[-43892557:1472247d06b:-7fff, -5287536613776006259]]]]
Run Code Online (Sandbox Code Playgroud)

为什么它认为我有同行127.0.0.1:18405?

exp*_*ert 5

在 JDK 和 ehcache 源代码中调试了几个小时后,我想通了。

我的核心错误假设是我看到错误的 Windows 节点有问题。原来是 linux 节点提供了不正确的地址。

官方 Ehcache 常见问题解答说:

这是由 2008 年对 Ubuntu/Debian Linux 默认网络配置的更改引起的。本质上,Java 调用 InetAddress.getLocalHost(); 始终返回环回地址,即 127.0.0.1。为什么?因为在最近的这些发行版中,$ hostname 的系统调用总是返回映射到环回设备的地址,这会导致 Ehcache 的 RMI 对等创建逻辑始终分配环回地址,从而导致您看到的错误。您需要做的就是打开网络配置并确保机器的主机名返回网络上其他对等方可以访问的有效网络地址。

Linux 节点在以下类的方法中获得“127.0.0.1” java.rmi.registry.LocateRegistry

public static Registry getRegistry(String host, int port, RMIClientSocketFactory csf) throws RemoteException
{
    Registry registry = null;

    if (port <= 0)
        port = Registry.REGISTRY_PORT;

    if (host == null || host.length() == 0) {
        // If host is blank (as returned by "file:" URL in 1.0.2 used in
        // java.rmi.Naming), try to convert to real local host name so
        // that the RegistryImpl's checkAccess will not fail.
        try {
            host = java.net.InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            // If that failed, at least try "" (localhost) anyway...
            host = "";
        }
    }

    LiveRef liveRef = new LiveRef(new ObjID(ObjID.REGISTRY_ID), new TCPEndpoint(host, port, csf, null), false);
    RemoteRef ref = (csf == null) ? new UnicastRef(liveRef) : new UnicastRef2(liveRef);

    return (Registry) Util.createProxy(RegistryImpl.class, ref, false);
}
Run Code Online (Sandbox Code Playgroud)

我的 Windows 节点正在net.sf.ehcache.distribution.ManualRMICacheManagerPeerProvider它调用的以下类方法中接收它lookupRemoteCachePeer

public final synchronized List listRemoteCachePeers(Ehcache cache) throws CacheException {
    List remoteCachePeers = new ArrayList();
    List staleList = new ArrayList();
    for (Iterator iterator = peerUrls.keySet().iterator(); iterator.hasNext();) {
        String rmiUrl = (String) iterator.next();
        String rmiUrlCacheName = extractCacheName(rmiUrl);

        if (!rmiUrlCacheName.equals(cache.getName())) {
            continue;
        }
        Date date = (Date) peerUrls.get(rmiUrl);
        if (!stale(date)) {
            CachePeer cachePeer = null;
            try {
                cachePeer = lookupRemoteCachePeer(rmiUrl);
                remoteCachePeers.add(cachePeer);
            } catch (Exception e) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Looking up rmiUrl " + rmiUrl + " through exception " + e.getMessage()
                            + ". This may be normal if a node has gone offline. Or it may indicate network connectivity"
                            + " difficulties", e);
                }
            }
        } else {
                LOG.debug("rmiUrl {} should never be stale for a manually configured cluster.", rmiUrl);
            staleList.add(rmiUrl);
        }

    }

    //Remove any stale remote peers. Must be done here to avoid concurrent modification exception.
    for (int i = 0; i < staleList.size(); i++) {
        String rmiUrl = (String) staleList.get(i);
        peerUrls.remove(rmiUrl);
    }
    return remoteCachePeers;
}
Run Code Online (Sandbox Code Playgroud)

Terracotta 的官方建议是修改hosts文件,这对我来说似乎太残酷了。我得出的结论是,运维团队在我的服务器的命令行中简单地提供正确的绑定地址会更容易,如下所示

java -Djava.rmi.server.hostname=192.168.10.128 -jar services.jar
Run Code Online (Sandbox Code Playgroud)