@@ -92,8 +92,8 @@ public class NacosRegistry extends FailbackRegistry {
92
92
/**
93
93
* All supported categories
94
94
*/
95
- private static final List <String > ALL_SUPPORTED_CATEGORIES =
96
- Arrays . asList ( PROVIDERS_CATEGORY , CONSUMERS_CATEGORY , ROUTERS_CATEGORY , CONFIGURATORS_CATEGORY );
95
+ private static final List <String > ALL_SUPPORTED_CATEGORIES = Arrays . asList ( PROVIDERS_CATEGORY , CONSUMERS_CATEGORY
96
+ , ROUTERS_CATEGORY , CONFIGURATORS_CATEGORY );
97
97
98
98
private static final int CATEGORY_INDEX = 0 ;
99
99
@@ -108,9 +108,8 @@ public class NacosRegistry extends FailbackRegistry {
108
108
private static final String UP = "UP" ;
109
109
110
110
/**
111
- * The separator for service name
112
- * Change a constant to be configurable, it's designed for Windows file name that is compatible with old
113
- * Nacos binary release(< 0.6.1)
111
+ * The separator for service name Change a constant to be configurable, it's designed for Windows file name that is
112
+ * compatible with old Nacos binary release(< 0.6.1)
114
113
*/
115
114
private static final String SERVICE_NAME_SEPARATOR = System .getProperty ("nacos.service.name.separator" , ":" );
116
115
@@ -166,8 +165,8 @@ public List<URL> lookup(final URL url) {
166
165
List <URL > urls = new LinkedList <>();
167
166
Set <String > serviceNames = getServiceNames (url , null );
168
167
for (String serviceName : serviceNames ) {
169
- List <Instance > instances =
170
- namingService . getAllInstances ( serviceName , getUrl ().getGroup (Constants .DEFAULT_GROUP ));
168
+ List <Instance > instances = namingService . getAllInstances ( serviceName ,
169
+ getUrl ().getGroup (Constants .DEFAULT_GROUP ));
171
170
urls .addAll (buildURLs (url , instances ));
172
171
}
173
172
return urls ;
@@ -198,7 +197,8 @@ public void doRegister(URL url) {
198
197
}
199
198
200
199
/**
201
- * namingService.registerInstance with {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
200
+ * namingService.registerInstance with
201
+ * {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
202
202
* default {@link DEFAULT_GROUP}
203
203
*
204
204
* in https://github.com/apache/dubbo/issues/5978
@@ -207,8 +207,8 @@ public void doRegister(URL url) {
207
207
namingService .registerInstance (service , getUrl ().getGroup (Constants .DEFAULT_GROUP ), instance );
208
208
}
209
209
} else {
210
- logger .info (
211
- "Please set 'dubbo.registry.parameters.register-consumer-url=true' to turn on consumer url registration." );
210
+ logger .info ("Please set 'dubbo.registry.parameters.register-consumer-url=true' to turn on consumer "
211
+ + " url registration." );
212
212
}
213
213
} catch (SkipFailbackWrapperException exception ) {
214
214
throw exception ;
@@ -236,8 +236,8 @@ public void doUnregister(final URL url) {
236
236
}
237
237
238
238
for (String service : serviceNames ) {
239
- namingService .deregisterInstance (
240
- service , getUrl (). getGroup ( Constants . DEFAULT_GROUP ), instance .getIp (), instance .getPort ());
239
+ namingService .deregisterInstance (service , getUrl (). getGroup ( Constants . DEFAULT_GROUP ),
240
+ instance .getIp (), instance .getPort ());
241
241
}
242
242
} catch (SkipFailbackWrapperException exception ) {
243
243
throw exception ;
@@ -250,8 +250,7 @@ public void doUnregister(final URL url) {
250
250
@ Override
251
251
public void doSubscribe (final URL url , final NotifyListener listener ) {
252
252
NacosAggregateListener nacosAggregateListener = new NacosAggregateListener (listener );
253
- originToAggregateListener
254
- .computeIfAbsent (url , k -> new ConcurrentHashMap <>())
253
+ originToAggregateListener .computeIfAbsent (url , k -> new ConcurrentHashMap <>())
255
254
.put (listener , nacosAggregateListener );
256
255
257
256
Set <String > serviceNames = getServiceNames (url , nacosAggregateListener );
@@ -267,14 +266,15 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
267
266
* Get all instances with serviceNames to avoid instance overwrite and but with empty instance mentioned
268
267
* in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
269
268
*
270
- * namingService.getAllInstances with {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
269
+ * namingService.getAllInstances with
270
+ * {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
271
271
* default {@link DEFAULT_GROUP}
272
272
*
273
273
* in https://github.com/apache/dubbo/issues/5978
274
274
*/
275
275
for (String serviceName : serviceNames ) {
276
- List <Instance > instances =
277
- namingService . getAllInstances ( serviceName , getUrl ().getGroup (Constants .DEFAULT_GROUP ));
276
+ List <Instance > instances = namingService . getAllInstances ( serviceName ,
277
+ getUrl ().getGroup (Constants .DEFAULT_GROUP ));
278
278
notifySubscriber (url , serviceName , listener , instances );
279
279
}
280
280
for (String serviceName : serviceNames ) {
@@ -283,8 +283,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
283
283
} else {
284
284
for (String serviceName : serviceNames ) {
285
285
List <Instance > instances = new LinkedList <>();
286
- instances .addAll (
287
- namingService . getAllInstances ( serviceName , getUrl ().getGroup (Constants .DEFAULT_GROUP )));
286
+ instances .addAll (namingService . getAllInstances ( serviceName ,
287
+ getUrl ().getGroup (Constants .DEFAULT_GROUP )));
288
288
String serviceInterface = serviceName ;
289
289
String [] segments = serviceName .split (SERVICE_NAME_SEPARATOR , -1 );
290
290
if (segments .length == 4 ) {
@@ -305,8 +305,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
305
305
}
306
306
307
307
/**
308
- * Since 2.7.6 the legacy service name will be added to serviceNames
309
- * to fix bug with https://github.com/apache/dubbo/issues/5442
308
+ * Since 2.7.6 the legacy service name will be added to serviceNames to fix bug with
309
+ * https://github.com/apache/dubbo/issues/5442
310
310
*
311
311
* @param url
312
312
* @return
@@ -322,13 +322,8 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
322
322
} else {
323
323
Map <NotifyListener , NacosAggregateListener > listenerMap = originToAggregateListener .get (url );
324
324
if (listenerMap == null ) {
325
- logger .warn (
326
- REGISTRY_NACOS_EXCEPTION ,
327
- "" ,
328
- "" ,
329
- String .format (
330
- "No aggregate listener found for url %s, this service might have already been unsubscribed." ,
331
- url ));
325
+ logger .warn (REGISTRY_NACOS_EXCEPTION , "" , "" , String .format ("No aggregate listener found for url %s, "
326
+ + "this service might have already been unsubscribed." , url ));
332
327
return ;
333
328
}
334
329
NacosAggregateListener nacosAggregateListener = listenerMap .remove (listener );
@@ -337,12 +332,8 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
337
332
try {
338
333
doUnsubscribe (url , nacosAggregateListener , serviceNames );
339
334
} catch (NacosException e ) {
340
- logger .error (
341
- REGISTRY_NACOS_EXCEPTION ,
342
- "" ,
343
- "" ,
344
- "Failed to unsubscribe " + url + " to nacos " + getUrl () + ", cause: " + e .getMessage (),
345
- e );
335
+ logger .error (REGISTRY_NACOS_EXCEPTION , "" , "" ,
336
+ "Failed to unsubscribe " + url + " to nacos " + getUrl () + ", cause: " + e .getMessage (), e );
346
337
}
347
338
}
348
339
if (listenerMap .isEmpty ()) {
@@ -352,8 +343,9 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
352
343
}
353
344
354
345
private void doUnsubscribe (
355
- final URL url , final NacosAggregateListener nacosAggregateListener , final Set <String > serviceNames )
356
- throws NacosException {
346
+ final URL url ,
347
+ final NacosAggregateListener nacosAggregateListener ,
348
+ final Set <String > serviceNames ) throws NacosException {
357
349
for (String serviceName : serviceNames ) {
358
350
unsubscribeEventListener (serviceName , url , nacosAggregateListener );
359
351
}
@@ -392,7 +384,8 @@ private Set<String> getServiceNames0(URL url) {
392
384
if (supportLegacyServiceName ) {
393
385
// Add the legacy service name since 2.7.6
394
386
String legacySubscribedServiceName = getLegacySubscribedServiceName (url );
395
- if (!serviceName .toString ().equals (legacySubscribedServiceName )) {
387
+ if (!serviceName .toString ()
388
+ .equals (legacySubscribedServiceName )) {
396
389
// avoid duplicated service names
397
390
serviceNames .add (legacySubscribedServiceName );
398
391
}
@@ -407,24 +400,22 @@ private Set<String> getServiceNames0(URL url) {
407
400
private Set <String > filterServiceNames (NacosServiceName serviceName ) {
408
401
try {
409
402
Set <String > serviceNames = new LinkedHashSet <>();
410
- serviceNames .addAll (
411
- namingService
412
- .getServicesOfServer (1 , Integer .MAX_VALUE , getUrl ().getGroup (Constants .DEFAULT_GROUP ))
413
- .getData ()
414
- .stream ()
415
- .filter (this ::isConformRules )
416
- .map (NacosServiceName ::new )
417
- .filter (serviceName ::isCompatible )
418
- .map (NacosServiceName ::toString )
419
- .collect (Collectors .toList ()));
403
+ serviceNames .addAll (namingService .getServicesOfServer (1 , Integer .MAX_VALUE ,
404
+ getUrl ().getGroup (Constants .DEFAULT_GROUP ))
405
+ .getData ()
406
+ .stream ()
407
+ .filter (this ::isConformRules )
408
+ .map (NacosServiceName ::new )
409
+ .filter (serviceName ::isCompatible )
410
+ .map (NacosServiceName ::toString )
411
+ .collect (Collectors .toList ()));
420
412
return serviceNames ;
421
413
} catch (SkipFailbackWrapperException exception ) {
422
414
throw exception ;
423
415
} catch (Throwable cause ) {
424
416
throw new RpcException (
425
417
"Failed to filter serviceName from nacos, url: " + getUrl () + ", serviceName: " + serviceName
426
- + ", cause: " + cause .getMessage (),
427
- cause );
418
+ + ", cause: " + cause .getMessage (), cause );
428
419
}
429
420
}
430
421
@@ -457,7 +448,8 @@ private String getLegacySubscribedServiceName(URL url) {
457
448
private void appendIfPresent (StringBuilder target , URL url , String parameterName ) {
458
449
String parameterValue = url .getParameter (parameterName );
459
450
if (!StringUtils .isBlank (parameterValue )) {
460
- target .append (SERVICE_NAME_SEPARATOR ).append (parameterValue );
451
+ target .append (SERVICE_NAME_SEPARATOR )
452
+ .append (parameterValue );
461
453
}
462
454
}
463
455
@@ -468,25 +460,21 @@ private boolean isAdminProtocol(URL url) {
468
460
private void scheduleServiceNamesLookup (final URL url , final NacosAggregateListener listener ) {
469
461
if (scheduledExecutorService == null ) {
470
462
scheduledExecutorService = Executors .newSingleThreadScheduledExecutor ();
471
- scheduledExecutorService .scheduleAtFixedRate (
472
- () -> {
473
- Set <String > serviceNames = getAllServiceNames ();
474
- filterData (serviceNames , serviceName -> {
475
- boolean accepted = false ;
476
- for (String category : ALL_SUPPORTED_CATEGORIES ) {
477
- String prefix = category + SERVICE_NAME_SEPARATOR ;
478
- if (serviceName != null && serviceName .startsWith (prefix )) {
479
- accepted = true ;
480
- break ;
481
- }
482
- }
483
- return accepted ;
484
- });
485
- doSubscribe (url , listener , serviceNames );
486
- },
487
- LOOKUP_INTERVAL ,
488
- LOOKUP_INTERVAL ,
489
- TimeUnit .SECONDS );
463
+ scheduledExecutorService .scheduleAtFixedRate (() -> {
464
+ Set <String > serviceNames = getAllServiceNames ();
465
+ filterData (serviceNames , serviceName -> {
466
+ boolean accepted = false ;
467
+ for (String category : ALL_SUPPORTED_CATEGORIES ) {
468
+ String prefix = category + SERVICE_NAME_SEPARATOR ;
469
+ if (serviceName != null && serviceName .startsWith (prefix )) {
470
+ accepted = true ;
471
+ break ;
472
+ }
473
+ }
474
+ return accepted ;
475
+ });
476
+ doSubscribe (url , listener , serviceNames );
477
+ }, LOOKUP_INTERVAL , LOOKUP_INTERVAL , TimeUnit .SECONDS );
490
478
}
491
479
}
492
480
@@ -506,8 +494,8 @@ private Set<String> getAllServiceNames() {
506
494
try {
507
495
final Set <String > serviceNames = new LinkedHashSet <>();
508
496
int pageIndex = 1 ;
509
- ListView <String > listView = namingService .getServicesOfServer (
510
- pageIndex , PAGINATION_SIZE , getUrl ().getGroup (Constants .DEFAULT_GROUP ));
497
+ ListView <String > listView = namingService .getServicesOfServer (pageIndex , PAGINATION_SIZE ,
498
+ getUrl ().getGroup (Constants .DEFAULT_GROUP ));
511
499
// First page data
512
500
List <String > firstPageData = listView .getData ();
513
501
// Append first page into list
@@ -523,17 +511,16 @@ private Set<String> getAllServiceNames() {
523
511
}
524
512
// If more than 1 page
525
513
while (pageIndex < pageNumbers ) {
526
- listView = namingService .getServicesOfServer (
527
- ++ pageIndex , PAGINATION_SIZE , getUrl ().getGroup (Constants .DEFAULT_GROUP ));
514
+ listView = namingService .getServicesOfServer (++ pageIndex , PAGINATION_SIZE ,
515
+ getUrl ().getGroup (Constants .DEFAULT_GROUP ));
528
516
serviceNames .addAll (listView .getData ());
529
517
}
530
518
return serviceNames ;
531
519
} catch (SkipFailbackWrapperException exception ) {
532
520
throw exception ;
533
521
} catch (Throwable cause ) {
534
- throw new RpcException (
535
- "Failed to get all serviceName from nacos, url: " + getUrl () + ", cause: " + cause .getMessage (),
536
- cause );
522
+ throw new RpcException ("Failed to get all serviceName from nacos, url: " + getUrl () + ", cause: "
523
+ + cause .getMessage (), cause );
537
524
}
538
525
}
539
526
@@ -614,11 +601,8 @@ private List<URL> toUrlWithEmpty(URL consumerURL, Collection<Instance> instances
614
601
List <URL > urls = buildURLs (consumerURL , instances );
615
602
// Nacos does not support configurators and routers from registry, so all notifications are of providers type.
616
603
if (urls .size () == 0 && !getUrl ().getParameter (ENABLE_EMPTY_PROTECTION_KEY , DEFAULT_ENABLE_EMPTY_PROTECTION )) {
617
- logger .warn (
618
- REGISTRY_NACOS_EXCEPTION ,
619
- "" ,
620
- "" ,
621
- "Received empty url address list and empty protection is disabled, will clear current available addresses" );
604
+ logger .warn (REGISTRY_NACOS_EXCEPTION , "" , "" , "Received empty url address list and empty protection is "
605
+ + "disabled, will clear current available addresses" );
622
606
URL empty = URLBuilder .from (consumerURL )
623
607
.setProtocol (EMPTY_PROTOCOL )
624
608
.addParameter (CATEGORY_KEY , DEFAULT_CATEGORY )
@@ -641,21 +625,25 @@ private List<URL> buildURLs(URL consumerURL, Collection<Instance> instances) {
641
625
return urls ;
642
626
}
643
627
644
- private void subscribeEventListener (String serviceName , final URL url , final NacosAggregateListener listener )
645
- throws NacosException {
646
- Map <NacosAggregateListener , Map <String , EventListener >> listeners =
647
- nacosListeners .computeIfAbsent (url , k -> new ConcurrentHashMap <>());
628
+ private void subscribeEventListener (
629
+ String serviceName ,
630
+ final URL url ,
631
+ final NacosAggregateListener listener ) throws NacosException {
632
+ Map <NacosAggregateListener , Map <String , EventListener >> listeners = nacosListeners .computeIfAbsent (url ,
633
+ k -> new ConcurrentHashMap <>());
648
634
649
635
Map <String , EventListener > eventListeners = listeners .computeIfAbsent (listener , k -> new ConcurrentHashMap <>());
650
636
651
- EventListener eventListener = eventListeners .computeIfAbsent (
652
- serviceName , k -> new RegistryChildListenerImpl (serviceName , url , listener ));
637
+ EventListener eventListener = eventListeners .computeIfAbsent (serviceName ,
638
+ k -> new RegistryChildListenerImpl (serviceName , url , listener ));
653
639
654
640
namingService .subscribe (serviceName , getUrl ().getGroup (Constants .DEFAULT_GROUP ), eventListener );
655
641
}
656
642
657
- private void unsubscribeEventListener (String serviceName , final URL url , final NacosAggregateListener listener )
658
- throws NacosException {
643
+ private void unsubscribeEventListener (
644
+ String serviceName ,
645
+ final URL url ,
646
+ final NacosAggregateListener listener ) throws NacosException {
659
647
Map <NacosAggregateListener , Map <String , EventListener >> listenerToServiceEvent = nacosListeners .get (url );
660
648
if (listenerToServiceEvent == null ) {
661
649
return ;
@@ -668,8 +656,8 @@ private void unsubscribeEventListener(String serviceName, final URL url, final N
668
656
if (eventListener == null ) {
669
657
return ;
670
658
}
671
- namingService .unsubscribe (
672
- serviceName , getUrl (). getParameter ( GROUP_KEY , Constants . DEFAULT_GROUP ), eventListener );
659
+ namingService .unsubscribe (serviceName , getUrl (). getParameter ( GROUP_KEY , Constants . DEFAULT_GROUP ),
660
+ eventListener );
673
661
if (serviceToEventMap .isEmpty ()) {
674
662
listenerToServiceEvent .remove (listener );
675
663
}
@@ -692,8 +680,8 @@ private void notifySubscriber(
692
680
// Instances
693
681
filterEnabledInstances (enabledInstances );
694
682
}
695
- List <URL > aggregatedUrls =
696
- toUrlWithEmpty ( url , listener . saveAndAggregateAllInstances ( serviceName , enabledInstances ));
683
+ List <URL > aggregatedUrls = toUrlWithEmpty ( url , listener . saveAndAggregateAllInstances ( serviceName ,
684
+ enabledInstances ));
697
685
NacosRegistry .this .notify (url , listener .getNotifyListener (), aggregatedUrls );
698
686
}
699
687
@@ -809,8 +797,7 @@ public boolean equals(Object o) {
809
797
return false ;
810
798
}
811
799
RegistryChildListenerImpl that = (RegistryChildListenerImpl ) o ;
812
- return Objects .equals (serviceName , that .serviceName )
813
- && Objects .equals (consumerUrl , that .consumerUrl )
800
+ return Objects .equals (serviceName , that .serviceName ) && Objects .equals (consumerUrl , that .consumerUrl )
814
801
&& Objects .equals (listener , that .listener );
815
802
}
816
803
0 commit comments