Skip to content

Commit

Permalink
fix #1380
Browse files Browse the repository at this point in the history
  • Loading branch information
呈铭 committed Jan 17, 2024
1 parent 98a9b71 commit 24d3f60
Showing 1 changed file with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class NacosRegistry extends Registry {

private NamingService namingService;

private NacosRegistryProviderObserver providerObserver;
private static volatile NacosRegistryProviderObserver providerObserver;

private List<String> defaultCluster;

Expand Down Expand Up @@ -147,6 +147,14 @@ public synchronized void init() {
}

try {
if (providerObserver == null) {
synchronized (NacosRegistryProviderObserver.class) {
if (providerObserver == null) {
providerObserver = new NacosRegistryProviderObserver();
}
}
}

namingService = NamingFactory.createNamingService(nacosConfig);
} catch (NacosException e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_INIT_NACOS_NAMING_SERVICE, address), e);
Expand Down Expand Up @@ -272,26 +280,19 @@ public List<ProviderGroup> subscribe(final ConsumerConfig config) {
}

try {
if (providerObserver == null) {
providerObserver = new NacosRegistryProviderObserver();
}

ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
providerObserver.addProviderListener(config, providerInfoListener);

EventListener eventListener = new EventListener() {
@Override
public void onEvent(Event event) {
if (event instanceof NamingEvent) {
NamingEvent namingEvent = (NamingEvent) event;
List<Instance> instances = namingEvent.getInstances();
// avoid npe
if (null == instances) {
instances = new ArrayList<Instance>();
}
instances.removeIf(i -> !i.isEnabled());
providerObserver.updateProviders(config, instances);
EventListener eventListener = event -> {
if (event instanceof NamingEvent) {
NamingEvent namingEvent = (NamingEvent) event;
List<Instance> instances = namingEvent.getInstances();
// avoid npe
if (null == instances) {
instances = new ArrayList<>();
}
instances.removeIf(i -> !i.isEnabled());
providerObserver.updateProviders(config, instances);
}
};
namingService.subscribe(serviceName, defaultCluster, eventListener);
Expand Down

0 comments on commit 24d3f60

Please sign in to comment.