@@ -158,57 +158,7 @@ public static void innerConfigDestroy() {
158
158
}
159
159
160
160
public void init () {
161
- if (null == serviceSdkContext ) {
162
- try {
163
- // init SDKContext
164
- serviceSdkContext = SDKContext .initContextByConfig (properties .configuration (modifierList ,
165
- () -> environment .getProperty ("spring.cloud.client.ip-address" ),
166
- () -> environment .getProperty ("spring.cloud.polaris.local-port" , Integer .class , 0 )));
167
- serviceSdkContext .init ();
168
-
169
- // init ProviderAPI
170
- providerAPI = DiscoveryAPIFactory .createProviderAPIByContext (serviceSdkContext );
171
-
172
- // init losslessAPI
173
- losslessAPI = DiscoveryAPIFactory .createLosslessAPIByContext (serviceSdkContext );
174
-
175
- // init ConsumerAPI
176
- consumerAPI = DiscoveryAPIFactory .createConsumerAPIByContext (serviceSdkContext );
177
-
178
- // init RouterAPI
179
- routerAPI = RouterAPIFactory .createRouterAPIByContext (serviceSdkContext );
180
-
181
- // init CircuitBreakAPI
182
- circuitBreakAPI = CircuitBreakAPIFactory .createCircuitBreakAPIByContext (serviceSdkContext );
183
-
184
- // init LimitAPI
185
- limitAPI = LimitAPIFactory .createLimitAPIByContext (serviceSdkContext );
186
-
187
- // init AssemblyAPI
188
- assemblyAPI = AssemblyAPIFactory .createAssemblyAPIByContext (serviceSdkContext );
189
-
190
- // add shutdown hook
191
- Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
192
- long startTimestamp = System .currentTimeMillis ();
193
- long delay = 0 ;
194
- while (true ) {
195
- if (!isRegistered || delay >= 60000 ) {
196
- innerDestroy ();
197
- break ;
198
- }
199
- else {
200
- delay = System .currentTimeMillis () - startTimestamp ;
201
- }
202
- }
203
- }));
204
- LOG .info ("create Polaris SDK context successfully. properties: {}" , properties );
205
- }
206
- catch (Throwable throwable ) {
207
- LOG .error ("create Polaris SDK context failed. properties: {}" , properties , throwable );
208
- throw throwable ;
209
- }
210
- }
211
-
161
+ initService ();
212
162
initConfig ();
213
163
}
214
164
@@ -225,37 +175,37 @@ public static void setConfigSDKContext(SDKContext context) {
225
175
}
226
176
227
177
public SDKContext getSDKContext () {
228
- init ();
178
+ initService ();
229
179
return serviceSdkContext ;
230
180
}
231
181
232
182
public ProviderAPI getProviderAPI () {
233
- init ();
183
+ initService ();
234
184
return providerAPI ;
235
185
}
236
186
237
187
public LosslessAPI getLosslessAPI () {
238
- init ();
188
+ initService ();
239
189
return losslessAPI ;
240
190
}
241
191
242
192
public ConsumerAPI getConsumerAPI () {
243
- init ();
193
+ initService ();
244
194
return consumerAPI ;
245
195
}
246
196
247
197
public RouterAPI getRouterAPI () {
248
- init ();
198
+ initService ();
249
199
return routerAPI ;
250
200
}
251
201
252
202
public CircuitBreakAPI getCircuitBreakAPI () {
253
- init ();
203
+ initService ();
254
204
return circuitBreakAPI ;
255
205
}
256
206
257
207
public LimitAPI getLimitAPI () {
258
- init ();
208
+ initService ();
259
209
return limitAPI ;
260
210
}
261
211
@@ -268,6 +218,59 @@ public SDKContext getConfigSDKContext() {
268
218
return configSDKContext ;
269
219
}
270
220
221
+ public void initService () {
222
+ if (null == serviceSdkContext ) {
223
+ try {
224
+ // init SDKContext
225
+ serviceSdkContext = SDKContext .initContextByConfig (properties .configuration (modifierList ,
226
+ () -> environment .getProperty ("spring.cloud.client.ip-address" ),
227
+ () -> environment .getProperty ("spring.cloud.polaris.local-port" , Integer .class , 0 )));
228
+ serviceSdkContext .init ();
229
+
230
+ // init ProviderAPI
231
+ providerAPI = DiscoveryAPIFactory .createProviderAPIByContext (serviceSdkContext );
232
+
233
+ // init losslessAPI
234
+ losslessAPI = DiscoveryAPIFactory .createLosslessAPIByContext (serviceSdkContext );
235
+
236
+ // init ConsumerAPI
237
+ consumerAPI = DiscoveryAPIFactory .createConsumerAPIByContext (serviceSdkContext );
238
+
239
+ // init RouterAPI
240
+ routerAPI = RouterAPIFactory .createRouterAPIByContext (serviceSdkContext );
241
+
242
+ // init CircuitBreakAPI
243
+ circuitBreakAPI = CircuitBreakAPIFactory .createCircuitBreakAPIByContext (serviceSdkContext );
244
+
245
+ // init LimitAPI
246
+ limitAPI = LimitAPIFactory .createLimitAPIByContext (serviceSdkContext );
247
+
248
+ // init AssemblyAPI
249
+ assemblyAPI = AssemblyAPIFactory .createAssemblyAPIByContext (serviceSdkContext );
250
+
251
+ // add shutdown hook
252
+ Runtime .getRuntime ().addShutdownHook (new Thread (() -> {
253
+ long startTimestamp = System .currentTimeMillis ();
254
+ long delay = 0 ;
255
+ while (true ) {
256
+ if (!isRegistered || delay >= 60000 ) {
257
+ innerDestroy ();
258
+ break ;
259
+ }
260
+ else {
261
+ delay = System .currentTimeMillis () - startTimestamp ;
262
+ }
263
+ }
264
+ }));
265
+ LOG .info ("create Polaris SDK context successfully. properties: {}" , properties );
266
+ }
267
+ catch (Throwable throwable ) {
268
+ LOG .error ("create Polaris SDK context failed. properties: {}" , properties , throwable );
269
+ throw throwable ;
270
+ }
271
+ }
272
+ }
273
+
271
274
public void initConfig () {
272
275
// get modifiers for configuration.
273
276
List <PolarisConfigModifier > configModifierList = new ArrayList <>();
0 commit comments