Fix get batch service instances error (#11845)

* fix get batch service instances error

* fix checkstyle

* fix get wrong res when batch instance

* add the clientId to return data
This commit is contained in:
chenyiqin 2024-03-25 18:03:48 +08:00 committed by GitHub
parent 082c5073bb
commit a7d9ef2fe8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 100 additions and 18 deletions

View File

@ -32,6 +32,7 @@ import com.alibaba.nacos.naming.core.v2.client.impl.ConnectionBasedClient;
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager;
import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager;
import com.alibaba.nacos.naming.core.v2.pojo.BatchInstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
@ -137,20 +138,35 @@ public class ClientInfoControllerV2 {
Collection<Service> allPublishedService = client.getAllPublishedService();
ArrayList<ObjectNode> res = new ArrayList<>();
for (Service service : allPublishedService) {
ObjectNode item = JacksonUtils.createEmptyJsonNode();
item.put("namespace", service.getNamespace());
item.put("group", service.getGroup());
item.put("serviceName", service.getName());
InstancePublishInfo instancePublishInfo = client.getInstancePublishInfo(service);
ObjectNode instanceInfo = JacksonUtils.createEmptyJsonNode();
instanceInfo.put("ip", instancePublishInfo.getIp());
instanceInfo.put("port", instancePublishInfo.getPort());
instanceInfo.put("cluster", instancePublishInfo.getCluster());
item.set("registeredInstance", instanceInfo);
res.add(item);
if (instancePublishInfo instanceof BatchInstancePublishInfo) {
List<InstancePublishInfo> instancePublishInfos = ((BatchInstancePublishInfo) instancePublishInfo).getInstancePublishInfos();
for (InstancePublishInfo publishInfo : instancePublishInfos) {
res.add(wrapSingleInstanceNode(publishInfo, service));
}
} else {
res.add(wrapSingleInstanceNode(instancePublishInfo, service));
}
}
return Result.success(res);
}
private ObjectNode wrapSingleInstanceNode(InstancePublishInfo instancePublishInfo, Service service) {
ObjectNode item = JacksonUtils.createEmptyJsonNode();
item.put("namespace", service.getNamespace());
item.put("group", service.getGroup());
item.put("serviceName", service.getName());
item.set("registeredInstance", wrapSingleInstance(instancePublishInfo));
return item;
}
private ObjectNode wrapSingleInstance(InstancePublishInfo instancePublishInfo) {
ObjectNode instanceInfo = JacksonUtils.createEmptyJsonNode();
instanceInfo.put("ip", instancePublishInfo.getIp());
instanceInfo.put("port", instancePublishInfo.getPort());
instanceInfo.put("cluster", instancePublishInfo.getCluster());
return instanceInfo;
}
/**
* Query the services to which the specified client subscribes.
@ -207,15 +223,22 @@ public class ClientInfoControllerV2 {
for (String clientId : allClientsRegisteredService) {
Client client = clientManager.getClient(clientId);
InstancePublishInfo instancePublishInfo = client.getInstancePublishInfo(service);
if (!Objects.equals(instancePublishInfo.getIp(), ip) || !Objects
.equals(port, instancePublishInfo.getPort())) {
continue;
if (instancePublishInfo instanceof BatchInstancePublishInfo) {
List<InstancePublishInfo> list = ((BatchInstancePublishInfo) instancePublishInfo).getInstancePublishInfos();
for (InstancePublishInfo info : list) {
if (!Objects.equals(info.getIp(), ip) || !Objects
.equals(port, info.getPort())) {
continue;
}
res.add(wrapSingleInstance(info).put("clientId", clientId));
}
} else {
if (!Objects.equals(instancePublishInfo.getIp(), ip) || !Objects
.equals(port, instancePublishInfo.getPort())) {
continue;
}
res.add(wrapSingleInstance(instancePublishInfo).put("clientId", clientId));
}
ObjectNode item = JacksonUtils.createEmptyJsonNode();
item.put("clientId", clientId);
item.put("ip", instancePublishInfo.getIp());
item.put("port", instancePublishInfo.getPort());
res.add(item);
}
return Result.success(res);
}

View File

@ -23,6 +23,9 @@ import com.alibaba.nacos.naming.core.v2.client.impl.ConnectionBasedClient;
import com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient;
import com.alibaba.nacos.naming.core.v2.client.manager.ClientManager;
import com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager;
import com.alibaba.nacos.naming.core.v2.pojo.BatchInstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Assert;
@ -36,6 +39,7 @@ import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.Arrays;
@ -93,4 +97,59 @@ public class ClientInfoControllerV2Test extends BaseTest {
MockHttpServletResponse response = mockmvc.perform(mockHttpServletRequestBuilder).andReturn().getResponse();
Assert.assertEquals(200, response.getStatus());
}
@Test
public void testGetPublishedServiceList() throws Exception {
// single instance
when(clientManager.getClient("test1")).thenReturn(connectionBasedClient);
Service service = Service.newService("test", "test", "test");
connectionBasedClient.addServiceInstance(service, new InstancePublishInfo("127.0.0.1", 8848));
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/publish/list")
.param("clientId", "test1");
mockmvc.perform(mockHttpServletRequestBuilder)
.andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
// batch instances
BatchInstancePublishInfo instancePublishInfo = new BatchInstancePublishInfo();
instancePublishInfo.setInstancePublishInfos(Arrays.asList(new InstancePublishInfo("127.0.0.1", 8848),
new InstancePublishInfo("127.0.0.1", 8849)));
connectionBasedClient.addServiceInstance(service, instancePublishInfo);
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/publish/list")
.param("clientId", "test1");
mockmvc.perform(mockHttpServletRequestBuilder)
.andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(2));
}
@Test
public void testGetPublishedClientList() throws Exception {
String baseTestKey = "nacos-getPublishedClientList-test";
// single instance
Service service = Service.newService(baseTestKey, baseTestKey, baseTestKey);
when(clientServiceIndexesManager.getAllClientsRegisteredService(service)).thenReturn(Arrays.asList("test"));
when(clientManager.getClient("test")).thenReturn(connectionBasedClient);
connectionBasedClient.addServiceInstance(service, new InstancePublishInfo("127.0.0.1", 8848));
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/publisher/list")
.param("namespaceId", baseTestKey)
.param("groupName", baseTestKey)
.param("serviceName", baseTestKey)
.param("ip", "127.0.0.1")
.param("port", "8848");
mockmvc.perform(mockHttpServletRequestBuilder)
.andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
// batch instances
when(clientServiceIndexesManager.getAllClientsRegisteredService(service)).thenReturn(Arrays.asList("test"));
when(clientManager.getClient("test")).thenReturn(connectionBasedClient);
BatchInstancePublishInfo instancePublishInfo = new BatchInstancePublishInfo();
instancePublishInfo.setInstancePublishInfos(Arrays.asList(new InstancePublishInfo("127.0.0.1", 8848),
new InstancePublishInfo("127.0.0.1", 8849)));
connectionBasedClient.addServiceInstance(service, instancePublishInfo);
mockHttpServletRequestBuilder = MockMvcRequestBuilders.get(URL + "/service/publisher/list")
.param("namespaceId", baseTestKey)
.param("groupName", baseTestKey)
.param("serviceName", baseTestKey)
.param("ip", "127.0.0.1")
.param("port", "8848");
mockmvc.perform(mockHttpServletRequestBuilder)
.andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1));
}
}