Skip to content

Commit

Permalink
Merge pull request #39 from andrewshan/main
Browse files Browse the repository at this point in the history
consul,对返回结果去重
  • Loading branch information
andrewshan authored Aug 23, 2022
2 parents 3212448 + 48ef0d9 commit e7f6514
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -199,20 +201,24 @@ private List<Instance> convertConsulInstance(Service service, List<HealthService
return outInstances;
}
Map<String, String> filters = (null == group ? null : group.getMetadataMap());
Set<HostAndPort> processedNodes = new HashSet<>();
for (HealthService healthService : instances) {
LOG.info("[Consul]convert instance {}", healthService);
HealthService.Service instance = healthService.getService();
HostAndPort hostAndPort = HostAndPort.build(instance.getAddress(), instance.getPort());
if (processedNodes.contains(hostAndPort)) {
continue;
}
processedNodes.add(hostAndPort);
Map<String, String> metadata = instance.getMeta();
boolean matched = CommonUtils.matchMetadata(metadata, filters);
LOG.info("[Consul]convert instance {}, matched {}", healthService, matched);
if (!matched) {
continue;
}
Instance.Builder builder = Instance.newBuilder();
builder.setNamespace(ResponseUtils.toStringValue(service.getNamespace()));
builder.setService(ResponseUtils.toStringValue(service.getService()));
builder.setHost(ResponseUtils.toStringValue(instance.getAddress()));
builder.setPort(ResponseUtils.toUInt32Value(instance.getPort()));
builder.setHost(ResponseUtils.toStringValue(hostAndPort.getHost()));
builder.setPort(ResponseUtils.toUInt32Value(hostAndPort.getPort()));
if (!CollectionUtils.isEmpty(instance.getMeta())) {
builder.putAllMetadata(instance.getMeta());
}
Expand Down

0 comments on commit e7f6514

Please sign in to comment.