Search in sources :

Example 11 with Instance

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;
}
Also used : Instance(com.alibaba.nacos.naming.core.Instance) Cluster(com.alibaba.nacos.naming.core.Cluster) AbstractHealthChecker(com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 12 with Instance

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;
}
Also used : Instance(com.alibaba.nacos.naming.core.Instance) ArrayList(java.util.ArrayList)

Example 13 with Instance

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"));
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.naming.core.Instance) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 14 with Instance

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);
}
Also used : Instance(com.alibaba.nacos.naming.core.Instance) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) Service(com.alibaba.nacos.naming.core.Service) Cluster(com.alibaba.nacos.naming.core.Cluster) JsonNode(com.fasterxml.jackson.databind.JsonNode) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) BaseTest(com.alibaba.nacos.naming.BaseTest)

Example 15 with Instance

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);
}
Also used : Instance(com.alibaba.nacos.naming.core.Instance) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) ArrayList(java.util.ArrayList) Service(com.alibaba.nacos.naming.core.Service) Cluster(com.alibaba.nacos.naming.core.Cluster) Test(org.junit.Test) BaseTest(com.alibaba.nacos.naming.BaseTest)

Aggregations

Instance (com.alibaba.nacos.naming.core.Instance)26 Test (org.junit.Test)13 Instances (com.alibaba.nacos.naming.core.Instances)6 HashMap (java.util.HashMap)6 BaseTest (com.alibaba.nacos.naming.BaseTest)5 Cluster (com.alibaba.nacos.naming.core.Cluster)5 Service (com.alibaba.nacos.naming.core.Service)5 ArrayList (java.util.ArrayList)5 Datum (com.alibaba.nacos.naming.consistency.Datum)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)4 LinkedList (java.util.LinkedList)3 InstanceOperationInfo (com.alibaba.nacos.naming.pojo.InstanceOperationInfo)2 Function (java.util.function.Function)2 Before (org.junit.Before)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)1 AbstractHealthChecker (com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker)1 Http (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http)1