diff --git a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java index 857e51ce5..eba82dfa8 100644 --- a/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java +++ b/spring-cloud-huawei-nacos/spring-cloud-huawei-nacos-discovery/src/main/java/com/huaweicloud/nacos/discovery/registry/NacosServiceRegistry.java @@ -32,6 +32,7 @@ import org.springframework.util.CollectionUtils; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.google.common.eventbus.EventBus; import com.huaweicloud.common.event.EventManager; @@ -73,10 +74,7 @@ public void register(Registration registration) { for (NamingServiceManager serviceManager : namingServiceManagers) { try { serviceManager.getNamingService().registerInstance(serviceId, group, instance); - Map serviceMetadata = NacosMicroserviceHandler.createMicroserviceMetadata(); - if (!CollectionUtils.isEmpty(serviceMetadata)) { - serviceManager.getNamingMaintainService().updateService(serviceId, group, 0, serviceMetadata); - } + updateServiceMetadata(serviceManager.getNamingMaintainService(), serviceId, group); successCount++; LOGGER.info("nacos registry, {} {}:{} register finished", serviceId, instance.getIp(), instance.getPort()); } catch (Exception e) { @@ -92,6 +90,28 @@ public void register(Registration registration) { } } + private void updateServiceMetadata(NamingMaintainService maintainService, String serviceId, String group) { + Map serviceMetadata = NacosMicroserviceHandler.createMicroserviceMetadata(); + if (!CollectionUtils.isEmpty(serviceMetadata)) { + tryUpdateService(maintainService, serviceId, group, serviceMetadata); + } + } + + private void tryUpdateService(NamingMaintainService maintainService, String serviceId, String group, + Map serviceMetadata) { + for (int i = 1; i < 4; i++) { + try { + // because of nacos register instance using async type, using delay mode to prevent service info update failed + Thread.sleep(i * i * 1000); + maintainService.updateService(serviceId, group, 0, serviceMetadata); + break; + } catch (Exception e) { + LOGGER.warn("update service metadata failed, serviceName: {}, try times: {}, message: {}", serviceId, + i, e.getMessage()); + } + } + } + @Override public void deregister(Registration registration) { if (StringUtils.isEmpty(registration.getServiceId())) {