[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:
杨翊 SionYang 2021-06-07 16:37:01 +08:00 committed by GitHub
parent 90cb967763
commit ae39b9502a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 3 deletions

View File

@ -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.NamingMetadataManager;
import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata; import com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata;
import com.alibaba.nacos.naming.core.v2.pojo.Service; 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.pojo.ServiceView;
import com.alibaba.nacos.naming.utils.ServiceUtil; import com.alibaba.nacos.naming.utils.ServiceUtil;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
@ -37,8 +40,10 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -167,7 +172,44 @@ public class CatalogServiceV2Impl implements CatalogService {
@Override @Override
public Object pageListServiceDetail(String namespaceId, String groupName, String serviceName, int pageNo, public Object pageListServiceDetail(String namespaceId, String groupName, String serviceName, int pageNo,
int pageSize) throws NacosException { 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) { private Collection<Service> patternServices(String namespaceId, String group, String serviceName) {

View File

@ -46,6 +46,7 @@ import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.UtilsAndCommons; import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.pojo.InstanceOperationInfo; import com.alibaba.nacos.naming.pojo.InstanceOperationInfo;
import com.alibaba.nacos.naming.pojo.Subscriber; import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.push.UdpPushService;
import com.alibaba.nacos.naming.utils.ServiceUtil; import com.alibaba.nacos.naming.utils.ServiceUtil;
import java.util.HashMap; import java.util.HashMap;
@ -74,16 +75,19 @@ public class InstanceOperatorClientImpl implements InstanceOperator {
private final SwitchDomain switchDomain; private final SwitchDomain switchDomain;
private final UdpPushService pushService;
public InstanceOperatorClientImpl(ClientManagerDelegate clientManager, public InstanceOperatorClientImpl(ClientManagerDelegate clientManager,
ClientOperationServiceProxy clientOperationService, ServiceStorage serviceStorage, ClientOperationServiceProxy clientOperationService, ServiceStorage serviceStorage,
NamingMetadataOperateService metadataOperateService, NamingMetadataManager metadataManager, NamingMetadataOperateService metadataOperateService, NamingMetadataManager metadataManager,
SwitchDomain switchDomain) { SwitchDomain switchDomain, UdpPushService pushService) {
this.clientManager = clientManager; this.clientManager = clientManager;
this.clientOperationService = clientOperationService; this.clientOperationService = clientOperationService;
this.serviceStorage = serviceStorage; this.serviceStorage = serviceStorage;
this.metadataOperateService = metadataOperateService; this.metadataOperateService = metadataOperateService;
this.metadataManager = metadataManager; this.metadataManager = metadataManager;
this.switchDomain = switchDomain; 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, public ServiceInfo listInstance(String namespaceId, String serviceName, Subscriber subscriber, String cluster,
boolean healthOnly) { boolean healthOnly) {
Service service = getService(namespaceId, serviceName, true); 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); String clientId = IpPortBasedClient.getClientId(subscriber.getAddrStr(), true);
createIpPortClientIfAbsent(clientId, true); createIpPortClientIfAbsent(clientId, true);
clientOperationService.subscribeService(service, subscriber, clientId); clientOperationService.subscribeService(service, subscriber, clientId);