diff --git a/polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/api/SDKContext.java b/polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/api/SDKContext.java index bc5ab496f..3bc6c3970 100644 --- a/polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/api/SDKContext.java +++ b/polaris-common/polaris-client/src/main/java/com/tencent/polaris/client/api/SDKContext.java @@ -43,6 +43,7 @@ import com.tencent.polaris.api.plugin.stat.ReporterMetaInfo; import com.tencent.polaris.api.plugin.stat.StatReporter; import com.tencent.polaris.api.utils.CollectionUtils; +import com.tencent.polaris.api.utils.IPAddressUtils; import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.client.flow.AbstractFlow; import com.tencent.polaris.client.util.NamedThreadFactory; @@ -64,6 +65,7 @@ import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; /** * SDK初始化相关的上下文信息 @@ -74,6 +76,12 @@ public class SDKContext extends Destroyable implements InitContext, AutoCloseabl private static final Logger LOG = LoggerFactory.getLogger(SDKContext.class); private static final String DEFAULT_ADDRESS = "127.0.0.1"; + + /** + * 客户端ID自增序列 + */ + private static final AtomicInteger CLIENT_ID_SEQ = new AtomicInteger(0); + /** * 配置对象 */ @@ -105,7 +113,8 @@ public SDKContext(Configuration configuration, Manager plugins, ValueContext val this.configuration = configuration; this.plugins = plugins; this.valueContext = valueContext; - this.valueContext.setClientId(generateClientId(this.valueContext.getHost())); + this.valueContext.setClientId(generateClientId(getHostNameOrDefaultToHost(this.valueContext.getHost()))); + LOG.info("init SDKContext with clientId={}", this.valueContext.getClientId()); List services = new ArrayList<>(); //加载系统服务配置 SystemConfig system = configuration.getGlobal().getSystem(); @@ -130,7 +139,24 @@ public SDKContext(Configuration configuration, Manager plugins, ValueContext val } private static String generateClientId(String host) { - return host + "-" + getProcessId("0"); + if (!StringUtils.equalsIgnoreCase(host, DEFAULT_ADDRESS)) { + return host + "_" + getProcessId("0") + "_" + CLIENT_ID_SEQ.getAndIncrement(); + } else { + return UUID.randomUUID().toString(); + } + } + + private static String getHostNameOrDefaultToHost(String host) { + try { + String hostName = IPAddressUtils.getHostName(); + if (StringUtils.isBlank(hostName)) { + hostName = host; + } + return hostName; + } catch (Throwable throwable) { + LOG.error("fail to get host name", throwable); + return host; + } } private static String getProcessId(String fallback) { diff --git a/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/IPAddressUtils.java b/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/IPAddressUtils.java index f70b777e7..271812bd7 100644 --- a/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/IPAddressUtils.java +++ b/polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/IPAddressUtils.java @@ -17,11 +17,18 @@ package com.tencent.polaris.api.utils; +import com.tencent.polaris.logging.LoggerFactory; +import org.slf4j.Logger; + +import java.net.InetAddress; + /** * @author Haotian Zhang */ public class IPAddressUtils { - + + private static final Logger LOG = LoggerFactory.getLogger(IPAddressUtils.class); + public static String getIpCompatible(String ip) { if (StringUtils.isEmpty(ip)) { return ip; @@ -31,4 +38,20 @@ public static String getIpCompatible(String ip) { } return ip; } + + public static String getHostName() { + try { + String hostname = System.getenv("HOSTNAME"); + if (StringUtils.isBlank(hostname)) { + hostname = System.getProperty("HOSTNAME"); + } + if (StringUtils.isBlank(hostname)) { + hostname = InetAddress.getLocalHost().getHostName(); + } + return hostname; + } catch (Exception e) { + LOG.warn("get host name error", e); + return ""; + } + } } diff --git a/polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/GrpcConnector.java b/polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/GrpcConnector.java index 52d3e64d9..b95d7d41b 100644 --- a/polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/GrpcConnector.java +++ b/polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/GrpcConnector.java @@ -170,7 +170,7 @@ private void initActually(InitContext ctx, ServerConnectorConfig connectorConfig updateServiceExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(getName() + "-update-service")); updateServiceExecutor.setMaximumPoolSize(1); - clientInstanceId = UUID.randomUUID().toString(); + clientInstanceId = ctx.getValueContext().getClientId(); initialized = true; } diff --git a/pom.xml b/pom.xml index 573052e6d..6245838f9 100644 --- a/pom.xml +++ b/pom.xml @@ -66,7 +66,7 @@ - 2.0.0.0-RC5 + 2.0.0.0-SNAPSHOT ${maven.build.timestamp} false