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

[#1399] fixed microservice cannot auto-discover the address of the restored engine node problem #1400

Merged
merged 2 commits into from
Nov 5, 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 @@ -45,8 +45,6 @@ public class ServiceAddressManager {

private static final Logger LOGGER = LoggerFactory.getLogger(ServiceAddressManager.class);

private boolean initialized = false;

private final ServiceCenterClient serviceCenterClient;

private final MicroserviceInstance myselfInstance;
Expand All @@ -57,6 +55,8 @@ public class ServiceAddressManager {

private final String myselfServiceId;

private final Map<String, HashSet<String>> lastEngineEndpointsCache = new HashMap<>();

public ServiceAddressManager(DiscoveryBootstrapProperties discoveryProperties,
ServiceCenterClient serviceCenterClient,
ServiceCombRegistration serviceCombRegistration) {
Expand All @@ -69,9 +69,6 @@ public ServiceAddressManager(DiscoveryBootstrapProperties discoveryProperties,

@Subscribe
public void onHeartBeatEvent(HeartBeatEvent event) {
if (initialized) {
return;
}
if (event.isSuccess() && discoveryProperties.isAutoDiscovery()) {
for (Type type : Type.values()) {
initEndPort(type.name());
Expand All @@ -82,18 +79,30 @@ public void onHeartBeatEvent(HeartBeatEvent event) {
private void initEndPort(String key) {
List<MicroserviceInstance> instances = findServiceInstance(DiscoveryConstants.DEFAULT_APPID,
key, DiscoveryConstants.VERSION_RULE_LATEST);
if (DiscoveryConstants.SERVICE_CENTER.equals(key) && !instances.isEmpty()) {
initialized = true;
}
Map<String, List<String>> zoneAndRegion = generateZoneAndRegionAddress(instances);
HashSet<String> currentEngineEndpoints = new HashSet<>();
Map<String, List<String>> zoneAndRegion = generateZoneAndRegionAddress(instances, currentEngineEndpoints);
LOGGER.info("auto discovery service [{}] addresses: [{}]", key, zoneAndRegion);
if (zoneAndRegion == null) {
return;
}
EventManager.post(new RefreshEndpointEvent(zoneAndRegion, key));
if (isEngineEndpointsChanged(lastEngineEndpointsCache.get(key), currentEngineEndpoints)) {
lastEngineEndpointsCache.put(key, currentEngineEndpoints);
EventManager.post(new RefreshEndpointEvent(zoneAndRegion, key));
}
}

private boolean isEngineEndpointsChanged(Set<String> lastEngineEndpoints,
Set<String> currentEngineEndpoints) {
if (lastEngineEndpoints == null || lastEngineEndpoints.isEmpty()) {
return true;
}
HashSet<String> compareTemp = new HashSet<>(lastEngineEndpoints);
compareTemp.removeAll(currentEngineEndpoints);
return !compareTemp.isEmpty() || lastEngineEndpoints.size() != currentEngineEndpoints.size();
}

private Map<String, List<String>> generateZoneAndRegionAddress(List<MicroserviceInstance> instances) {
private Map<String, List<String>> generateZoneAndRegionAddress(List<MicroserviceInstance> instances,
Set<String> currentEngineEndpoints) {
if (instances.isEmpty()) {
return null;
}
Expand All @@ -109,6 +118,7 @@ private Map<String, List<String>> generateZoneAndRegionAddress(List<Microservice
} else {
sameRegion.addAll(microserviceInstance.getEndpoints());
}
currentEngineEndpoints.addAll(microserviceInstance.getEndpoints());
}
zoneAndRegion.put("sameZone", new ArrayList<>(sameZone));
zoneAndRegion.put("sameRegion", new ArrayList<>(sameRegion));
Expand Down
Loading