in solutions/solution-basic/src/main/java/org/apache/servicecomb/solution/basic/integration/InstanceOpenAPIRegistry.java [85:129]
public Map<String, OpenAPI> loadOpenAPI(String application, String serviceName) {
List<? extends DiscoveryInstance> discoveryInstances =
discoveryManager.getOrCreateVersionedCache(application, serviceName).data();
if (discoveryInstances.isEmpty()) {
throw new InvocationException(Status.INTERNAL_SERVER_ERROR, "no instances");
}
discoveryInstances.sort((a, b) -> VersionCompareUtil.compareVersion(b.getVersion(), a.getVersion()));
String version = null;
for (DiscoveryInstance instance : discoveryInstances) {
if (version != null && !version.equals(instance.getVersion())) {
break;
}
version = instance.getVersion();
for (String endpoint : instance.getEndpoints()) {
URI uri = URI.create(endpoint);
String transportName = uri.getScheme();
Transport transport = transportManager.findTransport(transportName);
if (transport == null) {
continue;
}
// Use myself service name instead of the target. Because can avoid create
// MicroserviceReferenceConfig for the target.
Invocation invocation = InvokerUtils.createInvocation(BootStrapProperties.readServiceName(environment),
transportName,
ManagementEndpoint.NAME, "schemaContents",
new HashMap<>(), new TypeReference<Map<String, String>>() {
}.getType());
invocation.setEndpoint(new Endpoint(transport, endpoint, discoveryInstances.get(0)));
try {
Map<String, String> contents = (Map<String, String>) InvokerUtils.syncInvoke(invocation);
Map<String, OpenAPI> result = new HashMap<>(contents.size());
contents.forEach((k, v) -> result.put(k, SwaggerUtils.parseSwagger(v)));
return result;
} catch (InvocationException e) {
LOGGER.warn("Get schema contents {}/{}/{} from endpoint {} failed. {}",
instance.getApplication(),
instance.getServiceName(),
instance.getInstanceId(), endpoint, e.getMessage());
}
}
}
throw new InvocationException(Status.INTERNAL_SERVER_ERROR, "Get schema contents fail from all latest version.");
}