38
38
import com .tencent .polaris .factory .config .ConfigurationImpl ;
39
39
import java .io .Closeable ;
40
40
import java .io .IOException ;
41
+ import java .lang .management .ManagementFactory ;
41
42
import java .net .InetAddress ;
42
43
import java .net .NetworkInterface ;
43
44
import java .net .Socket ;
48
49
import java .util .Enumeration ;
49
50
import java .util .List ;
50
51
import java .util .ServiceLoader ;
51
- import java .util .UUID ;
52
52
import java .util .concurrent .atomic .AtomicBoolean ;
53
53
import org .slf4j .Logger ;
54
54
import org .slf4j .LoggerFactory ;
@@ -103,7 +103,7 @@ public SDKContext(Configuration configuration, Manager plugins, ValueContext val
103
103
this .configuration = configuration ;
104
104
this .plugins = plugins ;
105
105
this .valueContext = valueContext ;
106
- this .valueContext .setClientId (UUID . randomUUID (). toString ( ));
106
+ this .valueContext .setClientId (generateClientId ( this . valueContext . getHost () ));
107
107
List <ServerServiceInfo > services = new ArrayList <>();
108
108
//加载系统服务配置
109
109
SystemConfig system = configuration .getGlobal ().getSystem ();
@@ -122,6 +122,31 @@ public SDKContext(Configuration configuration, Manager plugins, ValueContext val
122
122
this .serverServices = Collections .unmodifiableCollection (services );
123
123
}
124
124
125
+ private static String generateClientId (String host ) {
126
+ return host + "-" + getProcessId ("0" );
127
+ }
128
+
129
+ private static String getProcessId (String fallback ) {
130
+ // Note: may fail in some JVM implementations
131
+ // therefore fallback has to be provided
132
+
133
+ // something like '<pid>@<hostname>', at least in SUN / Oracle JVMs
134
+ final String jvmName = ManagementFactory .getRuntimeMXBean ().getName ();
135
+ final int index = jvmName .indexOf ('@' );
136
+
137
+ if (index < 1 ) {
138
+ // part before '@' empty (index = 0) / '@' not found (index = -1)
139
+ return fallback ;
140
+ }
141
+
142
+ try {
143
+ return Long .toString (Long .parseLong (jvmName .substring (0 , index )));
144
+ } catch (NumberFormatException e ) {
145
+ // ignore
146
+ }
147
+ return fallback ;
148
+ }
149
+
125
150
public synchronized void init () throws PolarisException {
126
151
if (!initialized .compareAndSet (false , true )) {
127
152
return ;
0 commit comments