使用 Ehcache 多播获取“无法分配请求的地址”java.net.SocketException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18747134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting `Can't assign requested address` java.net.SocketException using Ehcache multicast
提问by eebbesen
Getting java.net.SocketException
when trying to start a multicast provider:
获取java.net.SocketException
试图启动一个多播提供程序时:
2013-09-11 11:45:44,204 [main] ERROR net.sf.ehcache.distribution.MulticastRMICacheManagerPeerProvider: Error starting heartbeat. Error was: Can't assign requested address
java.net.SocketException: Can't assign requested address
at java.net.PlainDatagramSocketImpl.join(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.join(AbstractPlainDatagramSocketImpl.java:178)
at java.net.MulticastSocket.joinGroup(MulticastSocket.java:319)
at net.sf.ehcache.distribution.MulticastKeepaliveHeartbeatReceiver.init(MulticastKeepaliveHeartbeatReceiver.java:88)
at net.sf.ehcache.distribution.MulticastRMICacheManagerPeerProvider.init(MulticastRMICacheManagerPeerProvider.java:95)
采纳答案by eebbesen
This was caused by an IPv6 address being returned from java.net.NetworkInterface.getDefault()
. I'm on a Macbook and was using wireless -- p2p0 (used for AirDrop) was returned as the default network interface but my p2p0 only has an IPv6 ether
entry (found by running ipconfig
).
这是由从 返回的 IPv6 地址引起的java.net.NetworkInterface.getDefault()
。我在 Macbook 上使用无线——p2p0(用于 AirDrop)作为默认网络接口返回,但我的 p2p0 只有一个 IPv6ether
条目(通过运行找到ipconfig
)。
Two solutions, both of which worked for me (I prefer the first because it works whether you are using a wired or wireless connection)
两种解决方案,都对我有用(我更喜欢第一种,因为无论您使用有线还是无线连接,它都适用)
- Start the JVM with
-Djava.net.preferIPv4Stack=true
. This causedjava.net.NetworkInterface.getDefault()
to return my vboxnet0 network interface -- not sure what you'll get if you're not running a host-only VM. - Turn off wireless and use a wired connection
- 使用
-Djava.net.preferIPv4Stack=true
. 这导致java.net.NetworkInterface.getDefault()
返回我的 vboxnet0 网络接口 - 如果您没有运行仅主机虚拟机,不确定您会得到什么。 - 关闭无线并使用有线连接
回答by Michael Gannon
In my case I had just began using a VPN to a network that required authentication. My app would start and could connect to its databases through the pipe fine but my configuration for distributed cache using the IP 230.0.0.1 in ehcach.xml was the cause. In production all was well, locally it would simply fail and rollback to a different strategy but via the VPN the multicast requests were met with an authentication challenge and this error was the result. I only required a short term fix so in these environments I disable the ehcache multicast configuration and things returned to normal.
就我而言,我刚刚开始使用 VPN 连接到需要身份验证的网络。我的应用程序将启动并可以通过管道连接到它的数据库,但我在 ehcach.xml 中使用 IP 230.0.0.1 的分布式缓存配置是原因。在生产中一切都很好,在本地它会简单地失败并回滚到不同的策略,但是通过 VPN,多播请求遇到了身份验证挑战,结果就是这个错误。我只需要一个短期修复,所以在这些环境中我禁用了 ehcache 多播配置,事情恢复正常。
This was the offending line in ehcache.xml
which was simply commented out
这是ehcache.xml
被简单注释掉的违规行
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1, multicastGroupPort=4446, timeToLive=32"
/>
回答by user3697700
A slight variation on the accepted answer: You can also add the following line of code to your java code:
接受的答案略有不同:您还可以将以下代码行添加到您的 java 代码中:
System.setProperty("java.net.preferIPv4Stack", "true");
回答by Camilo Ortegón
You need to add certain configurations to Java VM before you can join a Multicast socket in any machine.
您需要先向 Java VM 添加某些配置,然后才能在任何机器上加入多播套接字。
First add this line before attempting any connection to make sure you will get only IPv4 addresses:
在尝试任何连接之前先添加这一行,以确保您只会获得 IPv4 地址:
System.setProperty("java.net.preferIPv4Stack", "true");
In most of the cases your computer has more than one network interface, so you need to choose the correct one:
在大多数情况下,您的计算机具有多个网络接口,因此您需要选择正确的一个:
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
Enumeration<InetAddress> addressesFromNetworkInterface = networkInterface.getInetAddresses();
while (addressesFromNetworkInterface.hasMoreElements()) {
InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
if (inetAddress.isSiteLocalAddress()
&& !inetAddress.isAnyLocalAddress()
&& !inetAddress.isLinkLocalAddress()
&& !inetAddress.isLoopbackAddress()
&& !inetAddress.isMulticastAddress()) {
socket.setNetworkInterface(NetworkInterface.getByName(networkInterface.getName()));
}
}
}