diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java b/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java index 0c6f639f9..b32ca481b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/CatalogServiceV2Impl.java @@ -29,6 +29,9 @@ import com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata; import com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager; import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata; import com.alibaba.nacos.naming.core.v2.pojo.Service; +import com.alibaba.nacos.naming.pojo.ClusterInfo; +import com.alibaba.nacos.naming.pojo.IpAddressInfo; +import com.alibaba.nacos.naming.pojo.ServiceDetailInfo; import com.alibaba.nacos.naming.pojo.ServiceView; import com.alibaba.nacos.naming.utils.ServiceUtil; import com.fasterxml.jackson.databind.node.ObjectNode; @@ -37,8 +40,10 @@ import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.StringJoiner; import java.util.stream.Collectors; @@ -167,7 +172,44 @@ public class CatalogServiceV2Impl implements CatalogService { @Override public Object pageListServiceDetail(String namespaceId, String groupName, String serviceName, int pageNo, int pageSize) throws NacosException { - return null; + List result = new ArrayList<>(); + Collection services = patternServices(namespaceId, groupName, serviceName); + services = doPage(services, pageNo - 1, pageSize); + for (Service each : services) { + ServiceDetailInfo serviceDetailInfo = new ServiceDetailInfo(); + serviceDetailInfo.setServiceName(each.getName()); + serviceDetailInfo.setGroupName(each.getGroup()); + ServiceMetadata serviceMetadata = metadataManager.getServiceMetadata(each).orElseGet(ServiceMetadata::new); + serviceDetailInfo.setMetadata(serviceMetadata.getExtendData()); + serviceDetailInfo.setClusterMap(getClusterMap(each)); + result.add(serviceDetailInfo); + } + return result; + } + + private Map getClusterMap(Service service) { + Map result = new HashMap<>(1); + for (Instance each : serviceStorage.getData(service).getHosts()) { + final IpAddressInfo info = transferToIpAddressInfo(each); + if (!result.containsKey(each.getClusterName())) { + ClusterInfo clusterInfo = new ClusterInfo(); + clusterInfo.setHosts(new LinkedList<>()); + result.put(each.getClusterName(), clusterInfo); + } + result.get(each.getClusterName()).getHosts().add(info); + } + return result; + } + + private IpAddressInfo transferToIpAddressInfo(Instance instance) { + IpAddressInfo result = new IpAddressInfo(); + result.setIp(instance.getIp()); + result.setPort(instance.getPort()); + result.setEnabled(instance.isEnabled()); + result.setValid(instance.isHealthy()); + result.setWeight(instance.getWeight()); + result.setMetadata(instance.getMetadata()); + return result; } private Collection patternServices(String namespaceId, String group, String serviceName) { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImpl.java b/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImpl.java index eea1335aa..158c1d713 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImpl.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/InstanceOperatorClientImpl.java @@ -46,6 +46,7 @@ import com.alibaba.nacos.naming.misc.SwitchDomain; import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.pojo.InstanceOperationInfo; import com.alibaba.nacos.naming.pojo.Subscriber; +import com.alibaba.nacos.naming.push.UdpPushService; import com.alibaba.nacos.naming.utils.ServiceUtil; import java.util.HashMap; @@ -74,16 +75,19 @@ public class InstanceOperatorClientImpl implements InstanceOperator { private final SwitchDomain switchDomain; + private final UdpPushService pushService; + public InstanceOperatorClientImpl(ClientManagerDelegate clientManager, ClientOperationServiceProxy clientOperationService, ServiceStorage serviceStorage, NamingMetadataOperateService metadataOperateService, NamingMetadataManager metadataManager, - SwitchDomain switchDomain) { + SwitchDomain switchDomain, UdpPushService pushService) { this.clientManager = clientManager; this.clientOperationService = clientOperationService; this.serviceStorage = serviceStorage; this.metadataOperateService = metadataOperateService; this.metadataManager = metadataManager; this.switchDomain = switchDomain; + this.pushService = pushService; } /** @@ -168,7 +172,8 @@ public class InstanceOperatorClientImpl implements InstanceOperator { public ServiceInfo listInstance(String namespaceId, String serviceName, Subscriber subscriber, String cluster, boolean healthOnly) { Service service = getService(namespaceId, serviceName, true); - if (subscriber.getPort() > 0) { + // For adapt 1.X subscribe logic + if (subscriber.getPort() > 0 && pushService.canEnablePush(subscriber.getAgent())) { String clientId = IpPortBasedClient.getClientId(subscriber.getAddrStr(), true); createIpPortClientIfAbsent(clientId, true); clientOperationService.subscribeService(service, subscriber, clientId);