use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class InstanceControllerTest method batchDeleteMetadata.
@Test
public void batchDeleteMetadata() throws Exception {
Instance instance = new Instance("1.1.1.1", 8080, TEST_CLUSTER_NAME);
instance.setServiceName(TEST_SERVICE_NAME);
Map<String, String> metadata = new HashMap<>();
metadata.put("key1", "value1");
instance.setMetadata(metadata);
Instance instance2 = new Instance("2.2.2.2", 8080, TEST_CLUSTER_NAME);
instance2.setServiceName(TEST_SERVICE_NAME);
List<Instance> instanceList = new LinkedList<>();
instanceList.add(instance);
instanceList.add(instance2);
when(serviceManager.batchOperate(ArgumentMatchers.anyString(), ArgumentMatchers.any(InstanceOperationInfo.class), ArgumentMatchers.any(Function.class))).thenReturn(instanceList);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance/metadata/batch").param("namespace", "public").param("serviceName", TEST_SERVICE_NAME).param("instances", "[{\"ip\":\"1.1.1.1\",\"port\": \"8080\",\"ephemeral\":\"true\",\"clusterName\":\"test-cluster\"}," + "{\"ip\":\"2.2.2.2\",\"port\":\"8080\",\"ephemeral\":\"true\",\"clusterName\":\"test-cluster\"}]").param("metadata", "{\"age\":\"20\",\"name\":\"horizon\"}");
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
JsonNode result = JacksonUtils.toObj(actualValue);
JsonNode updated = result.get("updated");
Assert.assertEquals(updated.size(), 2);
Assert.assertTrue(updated.get(0).asText().contains("1.1.1.1"));
Assert.assertTrue(updated.get(0).asText().contains("8080"));
Assert.assertTrue(updated.get(0).asText().contains(TEST_CLUSTER_NAME));
Assert.assertTrue(updated.get(0).asText().contains("ephemeral"));
Assert.assertTrue(updated.get(1).asText().contains("2.2.2.2"));
Assert.assertTrue(updated.get(1).asText().contains("8080"));
Assert.assertTrue(updated.get(1).asText().contains(TEST_CLUSTER_NAME));
Assert.assertTrue(updated.get(1).asText().contains("ephemeral"));
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class ClientBeatCheckTaskTest method testHeartBeatNotTimeout.
@Test
public void testHeartBeatNotTimeout() {
Instance instance = new Instance();
instance.setLastBeat(System.currentTimeMillis());
instance.setMarked(false);
instance.setHealthy(true);
Map<String, String> metadata = new HashMap<>();
metadata.put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, "1000000000");
instance.setMetadata(metadata);
Mockito.doReturn("test").when(serviceSpy).getName();
Mockito.doReturn(true).when(distroMapperSpy).responsible(Mockito.anyString());
clientBeatCheckTask.run();
Assert.assertTrue(instance.isHealthy());
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class ClientBeatCheckTaskTest method testIpDeleteTimeOut.
@Test
public void testIpDeleteTimeOut() {
Instance instance = new Instance();
instance.setLastBeat(System.currentTimeMillis());
instance.setMarked(true);
instance.setHealthy(true);
Map<String, String> metadata = new HashMap<>();
metadata.put(PreservedMetadataKeys.IP_DELETE_TIMEOUT, "10");
instance.setMetadata(metadata);
Mockito.doReturn(true).when(distroMapperSpy).responsible(null);
clientBeatCheckTask.run();
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class JacksonSerializerTest method testDeserialize.
@Test
@SuppressWarnings("checkstyle:linelength")
public void testDeserialize() {
String example = "{\"instanceList\":[{\"ip\":\"1.1.1.1\",\"port\":1234,\"weight\":1.0,\"healthy\":true,\"enabled\":true,\"ephemeral\":true,\"clusterName\":\"cluster\",\"metadata\":{},\"lastBeat\":1590563397264,\"marked\":false,\"instanceIdGenerator\":\"simple\",\"instanceHeartBeatInterval\":5000,\"instanceHeartBeatTimeOut\":15000,\"ipDeleteTimeout\":30000}]}";
Instances actual = serializer.deserialize(ByteUtils.toBytes(example), Instances.class);
assertEquals(1, actual.getInstanceList().size());
Instance actualInstance = actual.getInstanceList().get(0);
assertEquals("1.1.1.1", actualInstance.getIp());
assertEquals("cluster", actualInstance.getClusterName());
assertEquals(1234, actualInstance.getPort());
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class AddressServerClusterController method deleteCluster.
/**
* Delete cluster.
*
* @param product Ip list of products to be associated
* @param cluster Ip list of product cluster to be associated
* @param ips will delete ips.
* @return delete result (the cluster information is return if success, exception information is return if fail)
*/
@RequestMapping(value = "", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteCluster(@RequestParam(required = false) String product, @RequestParam(required = false) String cluster, @RequestParam String ips) {
// 1. prepare the storage name for product and cluster
String productName = addressServerGeneratorManager.generateProductName(product);
String clusterName = addressServerManager.getDefaultClusterNameIfEmpty(cluster);
// 2. prepare the response name for product and cluster to client
String rawProductName = addressServerManager.getRawProductName(product);
String rawClusterName = addressServerManager.getRawClusterName(cluster);
ResponseEntity responseEntity = ResponseEntity.status(HttpStatus.OK).body("product=" + rawProductName + ", cluster=" + rawClusterName + " delete success.");
try {
String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
Service service = serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, serviceName);
if (service == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("product=" + rawProductName + " not found.");
}
if (StringUtils.isBlank(ips)) {
// delete all ips from the cluster
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("ips must not be empty.");
}
// delete specified ip list
String[] ipArray = addressServerManager.splitIps(ips);
String checkResult = InternetAddressUtil.checkIPs(ipArray);
if (InternetAddressUtil.checkOK(checkResult)) {
List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
serviceManager.removeInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, instanceList.toArray(new Instance[instanceList.size()]));
} else {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
}
} catch (Exception e) {
responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getCause());
}
return responseEntity;
}
Aggregations