[ISSUE#5921] Adapt some Naming API after upgrading to v2.x. (#5972)
* Fix #5921, Adapt some Naming API after upgrading to v2.x. * Fix pmd
This commit is contained in:
parent
90cb967763
commit
ae39b9502a
@ -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<ServiceDetailInfo> result = new ArrayList<>();
|
||||
Collection<Service> 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<String, ClusterInfo> getClusterMap(Service service) {
|
||||
Map<String, ClusterInfo> 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<Service> patternServices(String namespaceId, String group, String serviceName) {
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user