Skip to content

Commit

Permalink
[#1399] fixed microservice cannot auto-discover the address of the re…
Browse files Browse the repository at this point in the history
…stored engine node problem (#1400)
  • Loading branch information
chengyouling authored Nov 5, 2024
1 parent 7acff71 commit cb17ba7
Showing 1 changed file with 21 additions and 11 deletions.
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

0 comments on commit cb17ba7

Please sign in to comment.