Skip to content

Commit

Permalink
add feature -- list instance record which enabled was false (#9855)
Browse files Browse the repository at this point in the history
  • Loading branch information
YunWZ authored Feb 2, 2023
1 parent ade3f82 commit 71389b0
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 1999-2022 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.naming.controllers.v2;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.model.v2.Result;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.auth.annotation.Secured;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.naming.core.CatalogServiceV2Impl;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.plugin.auth.constant.ActionTypes;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* CatalogControllerV2.
*
* @author Weizhan▪Yun
* @date 2023/1/14 19:54
*/
@RestController
@RequestMapping(UtilsAndCommons.DEFAULT_NACOS_NAMING_CONTEXT_V2 + UtilsAndCommons.NACOS_NAMING_CATALOG_CONTEXT)
public class CatalogControllerV2 {

@Autowired
private CatalogServiceV2Impl catalogServiceV2;

/**
* List instances of special service.
*
* @param namespaceId namespace id
* @param serviceName service name
* @param healthyOnly instance health only
* @param enabledOnly instance enabled
* @param page number of page
* @param pageSize size of each page
* @return instances information
*/
@Secured(action = ActionTypes.READ)
@RequestMapping(value = "/instances")
public Result<ObjectNode> instanceList(
@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId,
@RequestParam String serviceName, @RequestParam(required = false) Boolean healthyOnly,
@RequestParam(required = false) Boolean enabledOnly, @RequestParam(name = "pageNo") int page,
@RequestParam int pageSize) {
String serviceNameWithoutGroup = NamingUtils.getServiceName(serviceName);
String groupName = NamingUtils.getGroupName(serviceName);
List<? extends Instance> instances = catalogServiceV2.listAllInstances(namespaceId, groupName,
serviceNameWithoutGroup);
int start = (page - 1) * pageSize;

if (start < 0) {
start = 0;
}
int end = start + pageSize;

if (start > instances.size()) {
start = instances.size();
}

if (end > instances.size()) {
end = instances.size();
}

Stream<? extends Instance> stream = instances.stream();
if (healthyOnly != null) {
stream = stream.filter(instance -> instance.isHealthy() == healthyOnly);
}
if (enabledOnly != null) {
stream = stream.filter(i -> i.isEnabled() == enabledOnly);
}
List<? extends Instance> ins = stream.collect(Collectors.toList());

ObjectNode result = JacksonUtils.createEmptyJsonNode();
if (ins.size() > start) {
result.replace("instances", JacksonUtils.transferToJsonNode(ins.subList(start, end)));
}
result.put("count", ins.size());

return Result.success(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public interface CatalogService {
List<? extends Instance> listInstances(String namespaceId, String groupName, String serviceName, String clusterName)
throws NacosException;

/**
* List all instances of specified services.
*
* @param namespaceId namespace id of service
* @param groupName group name of service
* @param serviceName service name
* @return instances list
*/
List<? extends Instance> listAllInstances(String namespaceId, String groupName, String serviceName);

/**
* List service by page.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.naming.constants.FieldsConstants;
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.index.ServiceStorage;
Expand All @@ -35,11 +36,11 @@
import com.alibaba.nacos.naming.pojo.ServiceView;
import com.alibaba.nacos.naming.utils.ServiceUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.alibaba.nacos.common.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -83,8 +84,8 @@ public Object getServiceDetail(String namespaceId, String groupName, String serv
serviceObject.put(FieldsConstants.GROUP_NAME, groupName);
serviceObject.put(FieldsConstants.PROTECT_THRESHOLD, detailedService.getProtectThreshold());
serviceObject.replace(FieldsConstants.SELECTOR, JacksonUtils.transferToJsonNode(detailedService.getSelector()));
serviceObject
.replace(FieldsConstants.METADATA, JacksonUtils.transferToJsonNode(detailedService.getExtendData()));
serviceObject.replace(FieldsConstants.METADATA,
JacksonUtils.transferToJsonNode(detailedService.getExtendData()));

ObjectNode detailView = JacksonUtils.createEmptyJsonNode();
detailView.replace(FieldsConstants.SERVICE, serviceObject);
Expand Down Expand Up @@ -127,6 +128,18 @@ public List<? extends Instance> listInstances(String namespaceId, String groupNa
return result.getHosts();
}

@Override
public List<? extends Instance> listAllInstances(String namespaceId, String groupName, String serviceName) {
Service service = Service.newService(namespaceId, groupName, serviceName);
if (!ServiceManager.getInstance().containSingleton(service)) {
return Collections.EMPTY_LIST;
}

ServiceInfo serviceInfo = serviceStorage.getData(service);

return serviceInfo.getHosts();
}

@Override
public Object pageListService(String namespaceId, String groupName, String serviceName, int pageNo, int pageSize,
String instancePattern, boolean ignoreEmptyService) throws NacosException {
Expand Down

0 comments on commit 71389b0

Please sign in to comment.