Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1352] update service metadata error when instance info not write to Database (#1353) #1355

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,10 +74,7 @@ public void register(Registration registration) {
for (NamingServiceManager serviceManager : namingServiceManagers) {
try {
serviceManager.getNamingService().registerInstance(serviceId, group, instance);
Map<String, String> 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) {
Expand All @@ -92,6 +90,28 @@ public void register(Registration registration) {
}
}

private void updateServiceMetadata(NamingMaintainService maintainService, String serviceId, String group) {
Map<String, String> serviceMetadata = NacosMicroserviceHandler.createMicroserviceMetadata();
if (!CollectionUtils.isEmpty(serviceMetadata)) {
tryUpdateService(maintainService, serviceId, group, serviceMetadata);
}
}

private void tryUpdateService(NamingMaintainService maintainService, String serviceId, String group,
Map<String, String> 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())) {
Expand Down
Loading