[
https://issues.apache.org/jira/browse/SCB-986?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16686016#comment-16686016
]
ASF GitHub Bot commented on SCB-986:
------------------------------------
yangbor commented on a change in pull request #990: [SCB-986] Cache instances
result in CseDiscoveryClient
URL:
https://github.com/apache/servicecomb-java-chassis/pull/990#discussion_r233294191
##########
File path:
spring-boot-starter/spring-boot-starter-discovery/src/main/java/org/apache/servicecomb/springboot/starter/discovery/CseDiscoveryClient.java
##########
@@ -44,24 +51,55 @@ public String description() {
@Override
public List<ServiceInstance> getInstances(final String serviceId) {
+ class InstanceDiscoveryFilter implements DiscoveryFilter {
+ @Override
+ public int getOrder() {
+ return Short.MAX_VALUE;
+ }
+
+ @Override
+ public DiscoveryTreeNode discovery(DiscoveryContext context,
DiscoveryTreeNode parent) {
+ return parent.children()
+ .computeIfAbsent(context.getInputParameters(), etn ->
createDiscoveryTreeNode(context, parent));
+ }
+
+ @SuppressWarnings("unchecked")
+ protected DiscoveryTreeNode createDiscoveryTreeNode(DiscoveryContext
context,
+ DiscoveryTreeNode parent) {
+ String serviceName = context.getInputParameters();
+ List<ServiceInstance> instances = new ArrayList<>();
+ for (MicroserviceInstance instance : ((Map<String,
MicroserviceInstance>) parent.data()).values()) {
+ for (String endpoint : instance.getEndpoints()) {
+ String scheme = endpoint.split(":", 2)[0];
+ if (!scheme.equalsIgnoreCase(Const.RESTFUL)) {
+ LOGGER.info("Endpoint {} is not supported in Spring Cloud,
ignoring.", endpoint);
+ continue;
+ }
+ URIEndpointObject uri = new URIEndpointObject(endpoint);
+ instances.add(new DefaultServiceInstance(serviceId,
uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
+ }
+ }
+ return new DiscoveryTreeNode()
+ .subName(parent, serviceName)
+ .data(instances);
+ }
+ };
+
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);
+
DiscoveryTree discoveryTree = discoveryTrees.computeIfAbsent(serviceId,
key -> {
- return new DiscoveryTree();
+ DiscoveryTree tree = new DiscoveryTree();
+ tree.addFilter(new InstanceDiscoveryFilter());
+ return tree;
});
+
VersionedCache serversVersionedCache = discoveryTree.discovery(context,
RegistryUtils.getAppId(),
serviceId,
DefinitionConst.VERSION_RULE_ALL);
- Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
- List<ServiceInstance> instances = new ArrayList<>(servers.size());
- for (MicroserviceInstance s : servers.values()) {
- for (String endpoint : s.getEndpoints()) {
- URIEndpointObject uri = new URIEndpointObject(endpoint);
- instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(),
uri.getPort(), uri.isSslEnabled()));
- }
- }
- return instances;
+
+ return serversVersionedCache.data();
Review comment:
It's OK for the versionedCache to be empty. As it will not be NULL. When
there are associated filters and the children data is null, an Exception will
be raised.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> ServerInstances can be cached in spring-boot2-starter-discovery
> ---------------------------------------------------------------
>
> Key: SCB-986
> URL: https://issues.apache.org/jira/browse/SCB-986
> Project: Apache ServiceComb
> Issue Type: Improvement
> Components: Java-Chassis
> Affects Versions: java-chassis-1.1.0
> Reporter: Yang Bo
> Assignee: Yang Bo
> Priority: Minor
>
> ServerInstances can be cached in spring-boot2-starter-discovery and
> spring-boot-starter-discovery
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)