cache onRegister
@@ -19,10 +19,15 @@ package com.alibaba.cloud.nacos;
import java.util.Objects;
import java.util.Properties;
+import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingMaintainService;
import com.alibaba.nacos.api.naming.NamingService;
+import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent;
+import org.springframework.cloud.client.serviceregistry.Registration;
+import org.springframework.context.event.EventListener;
+
import static com.alibaba.nacos.api.NacosFactory.createMaintainService;
import static com.alibaba.nacos.api.NacosFactory.createNamingService;
import static org.springframework.beans.BeanUtils.copyProperties;
@@ -54,12 +59,8 @@ public class NacosServiceManager {
public boolean isNacosDiscoveryInfoChanged(
NacosDiscoveryProperties nacosDiscoveryProperties) {
- if (Objects.isNull(nacosDiscoveryPropertiesCache)) {
- nacosDiscoveryPropertiesCache = new NacosDiscoveryProperties();
- copyProperties(nacosDiscoveryProperties, nacosDiscoveryPropertiesCache);
- return false;
- }
- if (this.nacosDiscoveryPropertiesCache.equals(nacosDiscoveryProperties)) {
+ if (Objects.isNull(nacosDiscoveryPropertiesCache)
+ || this.nacosDiscoveryPropertiesCache.equals(nacosDiscoveryProperties)) {
return false;
}
copyProperties(nacosDiscoveryProperties, nacosDiscoveryPropertiesCache);
@@ -114,4 +115,19 @@ public class NacosServiceManager {
public void nacosServiceShutDown() throws NacosException {
this.namingService.shutDown();
}
+
+ @EventListener
+ public void onInstancePreRegisteredEvent(
+ InstancePreRegisteredEvent instancePreRegisteredEvent) {
+ Registration registration = instancePreRegisteredEvent.getRegistration();
+ if (Objects.isNull(nacosDiscoveryPropertiesCache)
+ && registration instanceof NacosRegistration) {
+ NacosDiscoveryProperties nacosDiscoveryProperties = ((NacosRegistration) registration)
+ .getNacosDiscoveryProperties();
+
+ nacosDiscoveryPropertiesCache = new NacosDiscoveryProperties();
+ copyProperties(nacosDiscoveryProperties, nacosDiscoveryPropertiesCache);
+ }
+ }
+
}
