Skip to content

Commit 0f43ba3

Browse files
author
呈铭
committed
code format
1 parent d5ee59b commit 0f43ba3

File tree

1 file changed

+82
-95
lines changed
  • dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos

1 file changed

+82
-95
lines changed

dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java

+82-95
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public class NacosRegistry extends FailbackRegistry {
9292
/**
9393
* All supported categories
9494
*/
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);
9797

9898
private static final int CATEGORY_INDEX = 0;
9999

@@ -108,9 +108,8 @@ public class NacosRegistry extends FailbackRegistry {
108108
private static final String UP = "UP";
109109

110110
/**
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)
114113
*/
115114
private static final String SERVICE_NAME_SEPARATOR = System.getProperty("nacos.service.name.separator", ":");
116115

@@ -166,8 +165,8 @@ public List<URL> lookup(final URL url) {
166165
List<URL> urls = new LinkedList<>();
167166
Set<String> serviceNames = getServiceNames(url, null);
168167
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));
171170
urls.addAll(buildURLs(url, instances));
172171
}
173172
return urls;
@@ -198,7 +197,8 @@ public void doRegister(URL url) {
198197
}
199198

200199
/**
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}
202202
* default {@link DEFAULT_GROUP}
203203
*
204204
* in https://github.com/apache/dubbo/issues/5978
@@ -207,8 +207,8 @@ public void doRegister(URL url) {
207207
namingService.registerInstance(service, getUrl().getGroup(Constants.DEFAULT_GROUP), instance);
208208
}
209209
} 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.");
212212
}
213213
} catch (SkipFailbackWrapperException exception) {
214214
throw exception;
@@ -236,8 +236,8 @@ public void doUnregister(final URL url) {
236236
}
237237

238238
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());
241241
}
242242
} catch (SkipFailbackWrapperException exception) {
243243
throw exception;
@@ -250,8 +250,7 @@ public void doUnregister(final URL url) {
250250
@Override
251251
public void doSubscribe(final URL url, final NotifyListener listener) {
252252
NacosAggregateListener nacosAggregateListener = new NacosAggregateListener(listener);
253-
originToAggregateListener
254-
.computeIfAbsent(url, k -> new ConcurrentHashMap<>())
253+
originToAggregateListener.computeIfAbsent(url, k -> new ConcurrentHashMap<>())
255254
.put(listener, nacosAggregateListener);
256255

257256
Set<String> serviceNames = getServiceNames(url, nacosAggregateListener);
@@ -267,14 +266,15 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
267266
* Get all instances with serviceNames to avoid instance overwrite and but with empty instance mentioned
268267
* in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
269268
*
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}
271271
* default {@link DEFAULT_GROUP}
272272
*
273273
* in https://github.com/apache/dubbo/issues/5978
274274
*/
275275
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));
278278
notifySubscriber(url, serviceName, listener, instances);
279279
}
280280
for (String serviceName : serviceNames) {
@@ -283,8 +283,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
283283
} else {
284284
for (String serviceName : serviceNames) {
285285
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)));
288288
String serviceInterface = serviceName;
289289
String[] segments = serviceName.split(SERVICE_NAME_SEPARATOR, -1);
290290
if (segments.length == 4) {
@@ -305,8 +305,8 @@ private void doSubscribe(final URL url, final NacosAggregateListener listener, f
305305
}
306306

307307
/**
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
310310
*
311311
* @param url
312312
* @return
@@ -322,13 +322,8 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
322322
} else {
323323
Map<NotifyListener, NacosAggregateListener> listenerMap = originToAggregateListener.get(url);
324324
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));
332327
return;
333328
}
334329
NacosAggregateListener nacosAggregateListener = listenerMap.remove(listener);
@@ -337,12 +332,8 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
337332
try {
338333
doUnsubscribe(url, nacosAggregateListener, serviceNames);
339334
} 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);
346337
}
347338
}
348339
if (listenerMap.isEmpty()) {
@@ -352,8 +343,9 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
352343
}
353344

354345
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 {
357349
for (String serviceName : serviceNames) {
358350
unsubscribeEventListener(serviceName, url, nacosAggregateListener);
359351
}
@@ -392,7 +384,8 @@ private Set<String> getServiceNames0(URL url) {
392384
if (supportLegacyServiceName) {
393385
// Add the legacy service name since 2.7.6
394386
String legacySubscribedServiceName = getLegacySubscribedServiceName(url);
395-
if (!serviceName.toString().equals(legacySubscribedServiceName)) {
387+
if (!serviceName.toString()
388+
.equals(legacySubscribedServiceName)) {
396389
// avoid duplicated service names
397390
serviceNames.add(legacySubscribedServiceName);
398391
}
@@ -407,24 +400,22 @@ private Set<String> getServiceNames0(URL url) {
407400
private Set<String> filterServiceNames(NacosServiceName serviceName) {
408401
try {
409402
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()));
420412
return serviceNames;
421413
} catch (SkipFailbackWrapperException exception) {
422414
throw exception;
423415
} catch (Throwable cause) {
424416
throw new RpcException(
425417
"Failed to filter serviceName from nacos, url: " + getUrl() + ", serviceName: " + serviceName
426-
+ ", cause: " + cause.getMessage(),
427-
cause);
418+
+ ", cause: " + cause.getMessage(), cause);
428419
}
429420
}
430421

@@ -457,7 +448,8 @@ private String getLegacySubscribedServiceName(URL url) {
457448
private void appendIfPresent(StringBuilder target, URL url, String parameterName) {
458449
String parameterValue = url.getParameter(parameterName);
459450
if (!StringUtils.isBlank(parameterValue)) {
460-
target.append(SERVICE_NAME_SEPARATOR).append(parameterValue);
451+
target.append(SERVICE_NAME_SEPARATOR)
452+
.append(parameterValue);
461453
}
462454
}
463455

@@ -468,25 +460,21 @@ private boolean isAdminProtocol(URL url) {
468460
private void scheduleServiceNamesLookup(final URL url, final NacosAggregateListener listener) {
469461
if (scheduledExecutorService == null) {
470462
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);
490478
}
491479
}
492480

@@ -506,8 +494,8 @@ private Set<String> getAllServiceNames() {
506494
try {
507495
final Set<String> serviceNames = new LinkedHashSet<>();
508496
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));
511499
// First page data
512500
List<String> firstPageData = listView.getData();
513501
// Append first page into list
@@ -523,17 +511,16 @@ private Set<String> getAllServiceNames() {
523511
}
524512
// If more than 1 page
525513
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));
528516
serviceNames.addAll(listView.getData());
529517
}
530518
return serviceNames;
531519
} catch (SkipFailbackWrapperException exception) {
532520
throw exception;
533521
} 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);
537524
}
538525
}
539526

@@ -614,11 +601,8 @@ private List<URL> toUrlWithEmpty(URL consumerURL, Collection<Instance> instances
614601
List<URL> urls = buildURLs(consumerURL, instances);
615602
// Nacos does not support configurators and routers from registry, so all notifications are of providers type.
616603
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");
622606
URL empty = URLBuilder.from(consumerURL)
623607
.setProtocol(EMPTY_PROTOCOL)
624608
.addParameter(CATEGORY_KEY, DEFAULT_CATEGORY)
@@ -641,21 +625,25 @@ private List<URL> buildURLs(URL consumerURL, Collection<Instance> instances) {
641625
return urls;
642626
}
643627

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<>());
648634

649635
Map<String, EventListener> eventListeners = listeners.computeIfAbsent(listener, k -> new ConcurrentHashMap<>());
650636

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));
653639

654640
namingService.subscribe(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP), eventListener);
655641
}
656642

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 {
659647
Map<NacosAggregateListener, Map<String, EventListener>> listenerToServiceEvent = nacosListeners.get(url);
660648
if (listenerToServiceEvent == null) {
661649
return;
@@ -668,8 +656,8 @@ private void unsubscribeEventListener(String serviceName, final URL url, final N
668656
if (eventListener == null) {
669657
return;
670658
}
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);
673661
if (serviceToEventMap.isEmpty()) {
674662
listenerToServiceEvent.remove(listener);
675663
}
@@ -692,8 +680,8 @@ private void notifySubscriber(
692680
// Instances
693681
filterEnabledInstances(enabledInstances);
694682
}
695-
List<URL> aggregatedUrls =
696-
toUrlWithEmpty(url, listener.saveAndAggregateAllInstances(serviceName, enabledInstances));
683+
List<URL> aggregatedUrls = toUrlWithEmpty(url, listener.saveAndAggregateAllInstances(serviceName,
684+
enabledInstances));
697685
NacosRegistry.this.notify(url, listener.getNotifyListener(), aggregatedUrls);
698686
}
699687

@@ -809,8 +797,7 @@ public boolean equals(Object o) {
809797
return false;
810798
}
811799
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)
814801
&& Objects.equals(listener, that.listener);
815802
}
816803

0 commit comments

Comments
 (0)