From e24ed1e8ef54de748c44926308dc3aa4bd65295f Mon Sep 17 00:00:00 2001 From: gosonzhang Date: Tue, 15 Dec 2020 20:19:02 +0800 Subject: [PATCH] [TUBEMQ-450]TubeClientException: Generate producer id failed --- .../client/config/TubeClientConfig.java | 7 ++ .../tubemq/corebase/utils/AddressUtils.java | 104 +++++++++++------- 2 files changed, 71 insertions(+), 40 deletions(-) diff --git a/tubemq-client/src/main/java/org/apache/tubemq/client/config/TubeClientConfig.java b/tubemq-client/src/main/java/org/apache/tubemq/client/config/TubeClientConfig.java index 32c0a6fb365..b1bd3b1f5c9 100644 --- a/tubemq-client/src/main/java/org/apache/tubemq/client/config/TubeClientConfig.java +++ b/tubemq-client/src/main/java/org/apache/tubemq/client/config/TubeClientConfig.java @@ -546,6 +546,12 @@ public boolean equals(Object o) { public String toJsonString() { int num = 0; + String localAddress = null; + try { + localAddress = AddressUtils.getLocalAddress(); + } catch (Throwable e) { + // + } StringBuilder sBuilder = new StringBuilder(512); sBuilder.append("{\"masterInfo\":["); for (String item : this.masterInfo.getAddrMap4Failover().keySet()) { @@ -580,6 +586,7 @@ public String toJsonString() { .append(",\"enableUserAuthentic\":").append(this.enableUserAuthentic) .append(",\"usrName\":\"").append(this.usrName) .append("\",\"usrPassWord\":\"").append(this.usrPassWord) + .append("\",\"localAddress\":\"").append(localAddress) .append("\",").append(this.tlsConfig.toString()) .append("}").toString(); } diff --git a/tubemq-core/src/main/java/org/apache/tubemq/corebase/utils/AddressUtils.java b/tubemq-core/src/main/java/org/apache/tubemq/corebase/utils/AddressUtils.java index 206cb934056..5a76af21507 100644 --- a/tubemq-core/src/main/java/org/apache/tubemq/corebase/utils/AddressUtils.java +++ b/tubemq-core/src/main/java/org/apache/tubemq/corebase/utils/AddressUtils.java @@ -51,35 +51,31 @@ public static boolean validLocalIp(String currLocalHost) { return checkValidIp(allInterface, currLocalHost); } - private static boolean checkValidIp(Enumeration allInterface, String currLocalHost) { - String localIp; - try { - while (allInterface.hasMoreElements()) { - NetworkInterface oneInterface = allInterface.nextElement(); - if (oneInterface == null - || oneInterface.isLoopback() - || !oneInterface.isUp()) { - continue; + private static boolean checkValidIp(Enumeration allInterface, + String currLocalHost) { + String fstV4IP = null; + while (allInterface.hasMoreElements()) { + try { + Tuple2 result = + getValidIPV4Address(allInterface.nextElement(), currLocalHost); + if (result.f0) { + localIPAddress = currLocalHost; + return true; } - Enumeration allAddress = oneInterface.getInetAddresses(); - while (allAddress.hasMoreElements()) { - InetAddress oneAddress = allAddress.nextElement(); - localIp = oneAddress.getHostAddress(); - if (TStringUtils.isBlank(localIp) - || "127.0.0.1".equals(localIp)) { - continue; - } - if (localIp.equals(currLocalHost)) { - localIPAddress = localIp; - return true; - } + if (TStringUtils.isEmpty(fstV4IP)) { + fstV4IP = result.f1; } + } catch (Throwable e) { + // } - } catch (SocketException e) { - throw new AddressException("error get local ip, ex {}", e); } - throw new AddressException(new StringBuilder(256).append("Illegal parameter: not found the ip(") - .append(currLocalHost).append(") in local networkInterfaces!").toString()); + if (fstV4IP != null) { + localIPAddress = fstV4IP; + return true; + } + throw new AddressException(new StringBuilder(256) + .append("Illegal parameter: not found the ip(").append(currLocalHost) + .append(") or ip v4 address in local networkInterfaces!").toString()); } public static int ipToInt(String ipAddr) { @@ -155,8 +151,10 @@ public static String getIPV4LocalAddress() { } while (enumeration.hasMoreElements()) { try { - tmpAdress = getValidIPV4Address(enumeration.nextElement()); - if (tmpAdress != null) { + Tuple2 result = + getValidIPV4Address(enumeration.nextElement(), null); + if (result.f0) { + tmpAdress = result.f1; break; } } catch (Throwable e) { @@ -180,9 +178,9 @@ public static String getIPV4LocalAddress() { public static String getIPV4LocalAddress(String defEthName) { boolean foundNetInter = false; - String tmpAdress = null; try { - Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); + Enumeration enumeration = + NetworkInterface.getNetworkInterfaces(); if (enumeration == null) { throw new AddressException("Get NetworkInterfaces is null"); } @@ -196,10 +194,11 @@ public static String getIPV4LocalAddress(String defEthName) { } foundNetInter = true; try { - tmpAdress = getValidIPV4Address(oneInterface); - if (tmpAdress != null) { - localIPAddress = tmpAdress; - return tmpAdress; + Tuple2 result = + getValidIPV4Address(oneInterface, null); + if (result.f0) { + localIPAddress = result.f1; + return localIPAddress; } } catch (Throwable e) { // @@ -219,14 +218,28 @@ public static String getIPV4LocalAddress(String defEthName) { } } - public static String getValidIPV4Address(NetworkInterface networkInterface) { + /** + * get valid IPV4 address from networkInterface. + * + * @param networkInterface need check networkInterface + * @param checkIp The IP address to be searched, + * if not specified, set to null + * @return Search result, field 0 indicates whether it is successful, + * field 1 carries the matched IP value; + * if the checkIp is specified but not found the IP, + * field 1 will return the first IPV4 address + * @throws AddressException throw exception if found no ipv4 address + */ + public static Tuple2 getValidIPV4Address( + NetworkInterface networkInterface, String checkIp) { try { if (networkInterface == null || !networkInterface.isUp() || networkInterface.isLoopback() || "docker0".equals(networkInterface.getName())) { - return null; + return new Tuple2<>(false, null); } + String fstV4IP = null; Enumeration addrs = networkInterface.getInetAddresses(); while (addrs.hasMoreElements()) { InetAddress address = addrs.nextElement(); @@ -236,16 +249,27 @@ public static String getValidIPV4Address(NetworkInterface networkInterface) { continue; } String localIP = address.getHostAddress(); - if (TStringUtils.isEmpty(localIP) || localIP.startsWith("127.0")) { + if (TStringUtils.isEmpty(localIP) + || localIP.startsWith("127.0")) { + continue; + } + if (!TStringUtils.isEmpty(checkIp)) { + if (TStringUtils.isEmpty(fstV4IP)) { + fstV4IP = localIP; + } + if (localIP.equals(checkIp)) { + return new Tuple2<>(true, localIP); + } continue; } - return localIP; + return new Tuple2<>(true, localIP); } - return null; + return new Tuple2<>(false, fstV4IP); } catch (Throwable e) { throw new AddressException(new StringBuilder(256) - .append("Illegal parameter: ").append("unable to obtain valid IP from network card ") - .append(networkInterface).toString(), e); + .append("Illegal parameter: ") + .append("unable to obtain valid IP from network card ") + .append(networkInterface).toString(), e); } }