use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class AddressServerClusterController method postCluster.
/**
* Create new cluster.
*
* @param product Ip list of products to be associated
* @param cluster Ip list of product cluster to be associated
* @param ips will post ip list.
* @return result of create new cluster
*/
@RequestMapping(value = "", method = RequestMethod.POST)
public ResponseEntity<String> postCluster(@RequestParam(required = false) String product, @RequestParam(required = false) String cluster, @RequestParam(name = "ips") 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);
Loggers.ADDRESS_LOGGER.info("put cluster node,the cluster name is " + cluster + "; the product name=" + product + "; the ip list=" + ips);
ResponseEntity<String> responseEntity;
try {
String serviceName = addressServerGeneratorManager.generateNacosServiceName(productName);
Cluster clusterObj = new Cluster();
clusterObj.setName(clusterName);
clusterObj.setHealthChecker(new AbstractHealthChecker.None());
serviceManager.createServiceIfAbsent(Constants.DEFAULT_NAMESPACE_ID, serviceName, false, clusterObj);
String[] ipArray = addressServerManager.splitIps(ips);
String checkResult = InternetAddressUtil.checkIPs(ipArray);
if (InternetAddressUtil.checkOK(checkResult)) {
List<Instance> instanceList = addressServerGeneratorManager.generateInstancesByIps(serviceName, rawProductName, clusterName, ipArray);
for (Instance instance : instanceList) {
serviceManager.registerInstance(Constants.DEFAULT_NAMESPACE_ID, serviceName, instance);
}
responseEntity = ResponseEntity.ok("product=" + rawProductName + ",cluster=" + rawClusterName + "; put success with size=" + instanceList.size());
} else {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(checkResult);
}
} catch (Exception e) {
responseEntity = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
return responseEntity;
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class AddressServerGeneratorManager method generateInstancesByIps.
/**
* Note: if the parameter inputted is empty then will return the empty list.
*
* @param serviceName service name
* @param clusterName cluster name
* @param ipArray array of ips
* @return instance list
*/
public List<Instance> generateInstancesByIps(String serviceName, String rawProductName, String clusterName, String[] ipArray) {
if (StringUtils.isEmpty(serviceName) || StringUtils.isEmpty(clusterName) || ipArray == null || ipArray.length == 0) {
return Collections.emptyList();
}
List<Instance> instanceList = new ArrayList<>(ipArray.length);
for (String ip : ipArray) {
String[] ipAndPort = generateIpAndPort(ip);
Instance instance = new Instance();
instance.setIp(ipAndPort[0]);
instance.setPort(Integer.parseInt(ipAndPort[1]));
instance.setClusterName(clusterName);
instance.setServiceName(serviceName);
instance.setTenant(Constants.DEFAULT_NAMESPACE_ID);
instance.setApp(rawProductName);
instance.setEphemeral(false);
instanceList.add(instance);
}
return instanceList;
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class CatalogControllerTest method testInstanceList.
@Test
public void testInstanceList() throws NacosException {
Instance instance = new Instance("1.1.1.1", 1234, TEST_CLUSTER_NAME);
cluster.updateIps(Collections.singletonList(instance), false);
ObjectNode result = catalogController.instanceList(Constants.DEFAULT_NAMESPACE_ID, TEST_GROUP_NAME + Constants.SERVICE_INFO_SPLITER + TEST_SERVICE_NAME, TEST_CLUSTER_NAME, 1, 10);
String actual = result.toString();
assertTrue(actual.contains("\"count\":1"));
assertTrue(actual.contains("\"list\":["));
assertTrue(actual.contains("\"clusterName\":\"test-cluster\""));
assertTrue(actual.contains("\"ip\":\"1.1.1.1\""));
assertTrue(actual.contains("\"port\":1234"));
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class InstanceControllerTest method getInstances.
@Test
public void getInstances() throws Exception {
Service service = new Service();
service.setName(TEST_SERVICE_NAME);
Cluster cluster = new Cluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME, service);
service.addCluster(cluster);
Instance instance = new Instance();
instance.setIp("10.10.10.10");
instance.setPort(8888);
instance.setWeight(2.0);
instance.setServiceName(TEST_SERVICE_NAME);
List<Instance> ipList = new ArrayList<>();
ipList.add(instance);
service.updateIPs(ipList, false);
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance/list").param("serviceName", TEST_SERVICE_NAME).header(HttpHeaderConsts.USER_AGENT_HEADER, "Nacos-Server:v1");
MockHttpServletResponse response = mockmvc.perform(builder).andReturn().getResponse();
String actualValue = response.getContentAsString();
JsonNode result = JacksonUtils.toObj(actualValue);
Assert.assertEquals(TEST_SERVICE_NAME, result.get("name").asText());
JsonNode hosts = result.get("hosts");
Assert.assertNotNull(hosts);
Assert.assertEquals(hosts.size(), 1);
JsonNode host = hosts.get(0);
Assert.assertNotNull(host);
Assert.assertEquals("10.10.10.10", host.get("ip").asText());
Assert.assertEquals(8888, host.get("port").asInt());
Assert.assertEquals(2.0, host.get("weight").asDouble(), 0.001);
}
use of com.alibaba.nacos.naming.core.Instance in project nacos by alibaba.
the class InstanceControllerTest method registerInstance.
@Test
public void registerInstance() throws Exception {
Service service = new Service();
service.setName(TEST_SERVICE_NAME);
Cluster cluster = new Cluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME, service);
service.addCluster(cluster);
Instance instance = new Instance();
instance.setIp("1.1.1.1");
instance.setPort(9999);
List<Instance> ipList = new ArrayList<>();
ipList.add(instance);
service.updateIPs(ipList, false);
when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/instance").param("serviceName", TEST_SERVICE_NAME).param("ip", "1.1.1.1").param("port", "9999");
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("ok", actualValue);
}
Aggregations