Skip to content

Commit 7b529a3

Browse files
chickenljbeiwei30
authored andcommitted
[Dubbo-2423] Multicast demo fails with message "Can't assign requested address". (#3317)
* Fix #2423, Multicast demo fails with message "Can't assign requested address" * temporarily disable ipv6 test
1 parent 1f02113 commit 7b529a3

File tree

3 files changed

+77
-15
lines changed

3 files changed

+77
-15
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
import org.apache.dubbo.common.logger.LoggerFactory;
2323

2424
import java.io.IOException;
25+
import java.net.Inet4Address;
2526
import java.net.Inet6Address;
2627
import java.net.InetAddress;
2728
import java.net.InetSocketAddress;
29+
import java.net.MulticastSocket;
2830
import java.net.NetworkInterface;
2931
import java.net.ServerSocket;
3032
import java.net.UnknownHostException;
@@ -343,4 +345,35 @@ public static String toURL(String protocol, String host, int port, String path)
343345
return sb.toString();
344346
}
345347

348+
public static void joinMulticastGroup (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException {
349+
setInterface(multicastSocket, multicastAddress);
350+
multicastSocket.setLoopbackMode(false);
351+
multicastSocket.joinGroup(multicastAddress);
352+
}
353+
354+
public static void setInterface (MulticastSocket multicastSocket, InetAddress multicastAddress) throws IOException{
355+
boolean interfaceSet = false;
356+
boolean ipV6 = multicastAddress instanceof Inet6Address;
357+
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
358+
while (interfaces.hasMoreElements()) {
359+
NetworkInterface i = (NetworkInterface) interfaces.nextElement();
360+
Enumeration addresses = i.getInetAddresses();
361+
while (addresses.hasMoreElements()) {
362+
InetAddress address = (InetAddress) addresses.nextElement();
363+
if (ipV6 && address instanceof Inet6Address) {
364+
multicastSocket.setInterface(address);
365+
interfaceSet = true;
366+
break;
367+
} else if (!ipV6 && address instanceof Inet4Address) {
368+
multicastSocket.setInterface(address);
369+
interfaceSet = true;
370+
break;
371+
}
372+
}
373+
if (interfaceSet) {
374+
break;
375+
}
376+
}
377+
}
378+
346379
}

dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import org.apache.dubbo.common.URL;
2121
import org.apache.dubbo.common.logger.Logger;
2222
import org.apache.dubbo.common.logger.LoggerFactory;
23+
import org.apache.dubbo.common.utils.CollectionUtils;
2324
import org.apache.dubbo.common.utils.ConcurrentHashSet;
2425
import org.apache.dubbo.common.utils.ExecutorUtil;
2526
import org.apache.dubbo.common.utils.NamedThreadFactory;
2627
import org.apache.dubbo.common.utils.NetUtils;
2728
import org.apache.dubbo.common.utils.UrlUtils;
28-
import org.apache.dubbo.common.utils.CollectionUtils;
2929
import org.apache.dubbo.registry.NotifyListener;
3030
import org.apache.dubbo.registry.support.FailbackRegistry;
3131

@@ -82,13 +82,11 @@ public MulticastRegistry(URL url) {
8282
try {
8383
multicastAddress = InetAddress.getByName(url.getHost());
8484
if (!multicastAddress.isMulticastAddress()) {
85-
throw new IllegalArgumentException("Invalid multicast address " + url.getHost() +
86-
", ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255.");
85+
throw new IllegalArgumentException("Invalid multicast address " + url.getHost() + ", ipv4 multicast address scope: 224.0.0.0 - 239.255.255.255.");
8786
}
8887
multicastPort = url.getPort() <= 0 ? DEFAULT_MULTICAST_PORT : url.getPort();
8988
multicastSocket = new MulticastSocket(multicastPort);
90-
multicastSocket.setLoopbackMode(false);
91-
multicastSocket.joinGroup(multicastAddress);
89+
NetUtils.joinMulticastGroup(multicastSocket, multicastAddress);
9290
Thread thread = new Thread(new Runnable() {
9391
@Override
9492
public void run() {
@@ -153,11 +151,7 @@ private void clean() {
153151
}
154152

155153
private boolean isExpired(URL url) {
156-
if (!url.getParameter(Constants.DYNAMIC_KEY, true)
157-
|| url.getPort() <= 0
158-
|| Constants.CONSUMER_PROTOCOL.equals(url.getProtocol())
159-
|| Constants.ROUTE_PROTOCOL.equals(url.getProtocol())
160-
|| Constants.OVERRIDE_PROTOCOL.equals(url.getProtocol())) {
154+
if (!url.getParameter(Constants.DYNAMIC_KEY, true) || url.getPort() <= 0 || Constants.CONSUMER_PROTOCOL.equals(url.getProtocol()) || Constants.ROUTE_PROTOCOL.equals(url.getProtocol()) || Constants.OVERRIDE_PROTOCOL.equals(url.getProtocol())) {
161155
return false;
162156
}
163157
Socket socket = null;
@@ -208,8 +202,7 @@ private void receive(String msg, InetSocketAddress remoteAddress) {
208202
if (CollectionUtils.isNotEmpty(urls)) {
209203
for (URL u : urls) {
210204
if (UrlUtils.isMatch(url, u)) {
211-
String host = remoteAddress != null && remoteAddress.getAddress() != null
212-
? remoteAddress.getAddress().getHostAddress() : url.getIp();
205+
String host = remoteAddress != null && remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : url.getIp();
213206
if (url.getParameter("unicast", true) // Whether the consumer's machine has only one process
214207
&& !NetUtils.getLocalHost().equals(host)) { // Multiple processes in the same machine cannot be unicast with unicast or there will be only one process receiving information
215208
unicast(Constants.REGISTER + " " + u.toFullString(), host);
@@ -275,8 +268,7 @@ public void doSubscribe(URL url, NotifyListener listener) {
275268

276269
@Override
277270
public void doUnsubscribe(URL url, NotifyListener listener) {
278-
if (!Constants.ANY_VALUE.equals(url.getServiceInterface())
279-
&& url.getParameter(Constants.REGISTER_KEY, true)) {
271+
if (!Constants.ANY_VALUE.equals(url.getServiceInterface()) && url.getParameter(Constants.REGISTER_KEY, true)) {
280272
unregister(url);
281273
}
282274
multicast(Constants.UNSUBSCRIBE + " " + url.toFullString());

dubbo-registry/dubbo-registry-multicast/src/test/java/org/apache/dubbo/registry/multicast/MulticastRegistryTest.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
import org.junit.jupiter.api.BeforeEach;
2525
import org.junit.jupiter.api.Test;
2626

27+
import java.net.InetAddress;
2728
import java.net.MulticastSocket;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Set;
3132

3233
import static org.hamcrest.CoreMatchers.is;
34+
import static org.hamcrest.MatcherAssert.assertThat;
3335
import static org.junit.jupiter.api.Assertions.assertEquals;
3436
import static org.junit.jupiter.api.Assertions.assertFalse;
35-
import static org.hamcrest.MatcherAssert.assertThat;
3637
import static org.junit.jupiter.api.Assertions.assertTrue;
3738

3839
public class MulticastRegistryTest {
@@ -220,4 +221,40 @@ public void testCustomedPort() {
220221
}
221222
}
222223

224+
@Test
225+
public void testMulticastAddress() {
226+
InetAddress multicastAddress = null;
227+
MulticastSocket multicastSocket = null;
228+
try {
229+
// ipv4 multicast address
230+
try {
231+
multicastAddress = InetAddress.getByName("224.55.66.77");
232+
multicastSocket = new MulticastSocket(2345);
233+
multicastSocket.setLoopbackMode(false);
234+
NetUtils.setInterface(multicastSocket, multicastAddress);
235+
multicastSocket.joinGroup(multicastAddress);
236+
} finally {
237+
if (multicastSocket != null) {
238+
multicastSocket.close();
239+
}
240+
}
241+
242+
// multicast ipv6 address,
243+
/*try {
244+
multicastAddress = InetAddress.getByName("ff01::1");
245+
multicastSocket = new MulticastSocket();
246+
multicastSocket.setLoopbackMode(false);
247+
NetUtils.setInterface(multicastSocket, multicastAddress);
248+
multicastSocket.joinGroup(multicastAddress);
249+
} finally {
250+
if (multicastSocket != null) {
251+
multicastSocket.close();
252+
}
253+
}*/
254+
255+
} catch (Exception e) {
256+
Assertions.fail(e);
257+
}
258+
}
259+
223260
}

0 commit comments

Comments
 (0)