Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.common.protocol.NamespaceUtil;
import org.apache.rocketmq.common.utils.NameServerAddressUtils;
import org.apache.rocketmq.remoting.common.RemotingUtil;
import org.apache.rocketmq.remoting.netty.TlsSystemConfig;
import org.apache.rocketmq.remoting.protocol.LanguageCode;

Expand All @@ -35,7 +35,7 @@
public class ClientConfig {
public static final String SEND_MESSAGE_WITH_VIP_CHANNEL_PROPERTY = "com.rocketmq.sendMessageWithVIPChannel";
private String namesrvAddr = NameServerAddressUtils.getNameServerAddresses();
private String clientIP = RemotingUtil.getLocalAddress();
private String clientIP = MixAll.getLocalAddress();
private String instanceName = System.getProperty("rocketmq.client.name", "DEFAULT");
private int clientCallbackExecutorThreads = Runtime.getRuntime().availableProcessors();
protected String namespace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.rocketmq.common.topic.TopicValidator;
import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory;
import org.apache.rocketmq.remoting.common.RemotingUtil;

public class BrokerConfig {
private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.COMMON_LOGGER_NAME);
Expand All @@ -33,8 +32,8 @@ public class BrokerConfig {
@ImportantField
private String namesrvAddr = System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY, System.getenv(MixAll.NAMESRV_ADDR_ENV));
@ImportantField
private String brokerIP1 = RemotingUtil.getLocalAddress();
private String brokerIP2 = RemotingUtil.getLocalAddress();
private String brokerIP1 = MixAll.getLocalAddress();
private String brokerIP2 = MixAll.getLocalAddress();
@ImportantField
private String brokerName = localHostName();
@ImportantField
Expand Down
57 changes: 55 additions & 2 deletions common/src/main/java/org/apache/rocketmq/common/MixAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public static boolean isPropertiesEqual(final Properties p1, final Properties p2
public static List<String> getLocalInetAddress() {
List<String> inetAddressList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
Enumeration<NetworkInterface> enumeration = NetworkInterfaceUtil.getNetworkInterfaces();
while (enumeration.hasMoreElements()) {
NetworkInterface networkInterface = enumeration.nextElement();
Enumeration<InetAddress> addrs = networkInterface.getInetAddresses();
Expand Down Expand Up @@ -392,7 +392,7 @@ private static String localhost() {
//Reverse logic comparing to RemotingUtil method, consider refactor in RocketMQ 5.0
public static String getLocalhostByNetworkInterface() throws SocketException {
List<String> candidatesHost = new ArrayList<String>();
Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();
Enumeration<NetworkInterface> enumeration = NetworkInterfaceUtil.getNetworkInterfaces();

while (enumeration.hasMoreElements()) {
NetworkInterface networkInterface = enumeration.nextElement();
Expand Down Expand Up @@ -443,4 +443,57 @@ public static String humanReadableByteCount(long bytes, boolean si) {
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}

public static String getLocalAddress() {
try {
// Traversal Network interface to get the first non-loopback and non-private address
Enumeration<NetworkInterface> enumeration = NetworkInterfaceUtil.getNetworkInterfaces();
ArrayList<String> ipv4Result = new ArrayList<String>();
ArrayList<String> ipv6Result = new ArrayList<String>();
while (enumeration.hasMoreElements()) {
final NetworkInterface networkInterface = enumeration.nextElement();
final Enumeration<InetAddress> en = networkInterface.getInetAddresses();
while (en.hasMoreElements()) {
final InetAddress address = en.nextElement();
if (!address.isLoopbackAddress()) {
if (address instanceof Inet6Address) {
ipv6Result.add(normalizeHostAddress(address));
} else {
ipv4Result.add(normalizeHostAddress(address));
}
}
}
}

// prefer ipv4
if (!ipv4Result.isEmpty()) {
for (String ip : ipv4Result) {
if (ip.startsWith("127.0") || ip.startsWith("192.168")) {
continue;
}

return ip;
}

return ipv4Result.get(ipv4Result.size() - 1);
} else if (!ipv6Result.isEmpty()) {
return ipv6Result.get(0);
}
//If failed to find,fall back to localhost
final InetAddress localHost = InetAddress.getLocalHost();
return normalizeHostAddress(localHost);
} catch (Exception e) {
log.error("Failed to obtain local address", e);
}

return null;
}

public static String normalizeHostAddress(final InetAddress localHost) {
if (localHost instanceof Inet6Address) {
return "[" + localHost.getHostAddress() + "]";
} else {
return localHost.getHostAddress();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rocketmq.common;

import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public class NetworkInterfaceUtil {

private static final List<NetworkInterface> INTERFACES = new ArrayList<>();
private static final Throwable THROWABLE;

static {
Throwable exception = null;
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
for (; networkInterfaces.hasMoreElements(); ) {
INTERFACES.add(networkInterfaces.nextElement());
}
} catch (Throwable e) {
exception = e;
}
THROWABLE = exception;
}

public static Enumeration<NetworkInterface> getNetworkInterfaces() throws SocketException {
if (THROWABLE != null) {
SocketException socketException = new SocketException("Get network interfaces failed");
socketException.initCause(THROWABLE);
throw socketException;
} else {
Iterator<NetworkInterface> iterator = INTERFACES.iterator();
return new Enumeration<NetworkInterface>() {
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}

@Override
public NetworkInterface nextElement() {
return iterator.next();
}
};
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public static String ipToIPv6Str(byte[] ip) {

public static byte[] getIP() {
try {
Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
Enumeration allNetInterfaces = NetworkInterfaceUtil.getNetworkInterfaces();
InetAddress ip = null;
byte[] internalIP = null;
while (allNetInterfaces.hasMoreElements()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
*/
package org.apache.rocketmq.common;

import org.apache.rocketmq.remoting.common.RemotingUtil;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class RemotingUtilTest {
@Test
public void testGetLocalAddress() throws Exception {
String localAddress = RemotingUtil.getLocalAddress();
String localAddress = MixAll.getLocalAddress();
assertThat(localAddress).isNotNull();
assertThat(localAddress.length()).isGreaterThan(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public static boolean isLinuxPlatform() {
return isLinuxPlatform;
}

/**
* This method will be removed, use "org.apache.rocketmq.common.MixAll.getLocalAddress" instead.
* @return
*/
@Deprecated
public static String getLocalAddress() {
try {
// Traversal Network interface to get the first non-loopback and non-private address
Expand Down Expand Up @@ -135,6 +140,11 @@ public static String getLocalAddress() {
return null;
}

/**
* This method will be removed, use "org.apache.rocketmq.common.MixAll.normalizeHostAddress" instead.
* @return
*/
@Deprecated
public static String normalizeHostAddress(final InetAddress localHost) {
if (localHost instanceof Inet6Address) {
return "[" + localHost.getHostAddress() + "]";
Expand Down