All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redismq.rebalance.ClientConfig Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package com.redismq.rebalance;

import java.io.File;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * @Author: hzh
 * @Date: 2022/4/27 17:12
 * 获取客户端id的方法
 */
public class ClientConfig {

    private static boolean isLinuxPlatform = false;
    private static boolean isWindowsPlatform = false;
    public static final String OS_NAME = System.getProperty("os.name");
    static {
        if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
            isLinuxPlatform = true;
        }

        if (OS_NAME != null && OS_NAME.toLowerCase().contains("windows")) {
            isWindowsPlatform = true;
        }
    }
    public static String getLocalAddress() {
        try {
            // Traversal Network interface to get the first non-loopback and non-private address
            Enumeration enumeration = NetworkInterface.getNetworkInterfaces();
            ArrayList ipv4Result = new ArrayList();
            ArrayList ipv6Result = new ArrayList();
            while (enumeration.hasMoreElements()) {
                final NetworkInterface networkInterface = enumeration.nextElement();
                if (isBridge(networkInterface)) {
                    continue;
                }

                final Enumeration 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) {
        }

        return null;
    }
    private static boolean isBridge(NetworkInterface networkInterface) {
        try {
            if (isLinuxPlatform) {
                String interfaceName = networkInterface.getName();
                File file = new File("/sys/class/net/" + interfaceName + "/bridge");
                return file.exists();
            }
        } catch (SecurityException e) {
            //Ignore
        }
        return false;
    }
    public static String normalizeHostAddress(final InetAddress localHost) {
        if (localHost instanceof Inet6Address) {
            return "[" + localHost.getHostAddress() + "]";
        } else {
            return localHost.getHostAddress();
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy