@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);
    }
@Test
    void testInstanceList() throws Exception {
        String serviceNameWithoutGroup = NamingUtils.getServiceName(TEST_SERVICE_NAME);
        String groupName = NamingUtils.getGroupName(TEST_SERVICE_NAME);
        when(catalogServiceV2.listAllInstances(Constants.DEFAULT_NAMESPACE_ID, groupName, serviceNameWithoutGroup)).thenReturn(instances);
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(
                        UtilsAndCommons.DEFAULT_NACOS_NAMING_CONTEXT_V2 + UtilsAndCommons.NACOS_NAMING_CATALOG_CONTEXT + "/instances")
                .param("namespaceId", Constants.DEFAULT_NAMESPACE_ID).param("serviceName", TEST_SERVICE_NAME).param("pageNo", "1")
                .param("pageSize", "100");
        MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
        assertEquals(200, response.getStatus());
        JsonNode data = JacksonUtils.toObj(response.getContentAsString()).get("data").get("instances");
        assertEquals(instances.size(), data.size());
    }