Search in sources :

Example 6 with Query

use of com.alibaba.nacos.common.http.param.Query in project nacos by alibaba.

the class NacosRestTemplate_ITCase method test_url_get.

@Test
public void test_url_get() throws Exception {
    String url = IP + INSTANCE_PATH + "/instance/list";
    Query query = Query.newInstance().addParam("serviceName", "app-test");
    HttpRestResult<Map> restResult = nacosRestTemplate.get(url, Header.newInstance(), query, Map.class);
    Assert.assertTrue(restResult.ok());
    Assert.assertEquals(restResult.getData().get("name"), "DEFAULT_GROUP@@app-test");
    System.out.println(restResult.getData());
}
Also used : Query(com.alibaba.nacos.common.http.param.Query) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with Query

use of com.alibaba.nacos.common.http.param.Query in project nacos by alibaba.

the class NacosAsyncRestTemplate_ITCase method test_url_delete.

@Test
public void test_url_delete() throws Exception {
    String url = IP + CONFIG_INSTANCE_PATH + "/instance";
    Query query = Query.newInstance().addParam("ip", "11.11.11.11").addParam("port", "8080").addParam("serviceName", "app-test");
    CallbackMap<String> callbackMap = new CallbackMap<>();
    nacosRestTemplate.delete(url, Header.newInstance(), query, String.class, callbackMap);
    Thread.sleep(2000);
    HttpRestResult<String> restResult = callbackMap.getRestResult();
    System.out.println(restResult.getData());
    System.out.println(restResult.getHeader());
    Assert.assertTrue(restResult.ok());
}
Also used : Query(com.alibaba.nacos.common.http.param.Query) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 8 with Query

use of com.alibaba.nacos.common.http.param.Query in project nacos by alibaba.

the class ServerListManager method getServerListFromEndpoint.

private List<String> getServerListFromEndpoint() {
    try {
        String urlString = HTTP_PREFIX + endpoint + "/nacos/serverlist";
        Header header = NamingHttpUtil.builderHeader();
        Query query = StringUtils.isNotBlank(namespace) ? Query.newInstance().addParam("namespace", namespace) : Query.EMPTY;
        HttpRestResult<String> restResult = nacosRestTemplate.get(urlString, header, query, String.class);
        if (!restResult.ok()) {
            throw new IOException("Error while requesting: " + urlString + "'. Server returned: " + restResult.getCode());
        }
        String content = restResult.getData();
        List<String> list = new ArrayList<String>();
        for (String line : IoUtils.readLines(new StringReader(content))) {
            if (!line.trim().isEmpty()) {
                list.add(line.trim());
            }
        }
        return list;
    } catch (Exception e) {
        NAMING_LOGGER.error("[SERVER-LIST] failed to update server list.", e);
    }
    return null;
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) Query(com.alibaba.nacos.common.http.param.Query) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) NacosLoadException(com.alibaba.nacos.api.exception.runtime.NacosLoadException) NacosException(com.alibaba.nacos.api.exception.NacosException) IOException(java.io.IOException)

Example 9 with Query

use of com.alibaba.nacos.common.http.param.Query in project nacos by alibaba.

the class ClientMetricsController method metric.

/**
 * get client metric.
 *
 * @param ip client ip .
 * @return ResponseEntity
 */
@GetMapping("/cluster")
public ResponseEntity metric(@RequestParam("ip") String ip, @RequestParam(value = "dataId", required = false) String dataId, @RequestParam(value = "group", required = false) String group, @RequestParam(value = "tenant", required = false) String tenant) {
    Loggers.CORE.info("Get cluster config metrics received, ip={},dataId={},group={},tenant={}", ip, dataId, group, tenant);
    Map<String, Object> responseMap = new HashMap<>(3);
    Collection<Member> members = serverMemberManager.allMembers();
    final NacosAsyncRestTemplate nacosAsyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CLUSTER);
    CountDownLatch latch = new CountDownLatch(members.size());
    for (Member member : members) {
        String url = HttpUtils.buildUrl(false, member.getAddress(), EnvUtil.getContextPath(), Constants.METRICS_CONTROLLER_PATH, "current");
        Query query = Query.newInstance().addParam("ip", ip).addParam("dataId", dataId).addParam("group", group).addParam("tenant", tenant);
        nacosAsyncRestTemplate.get(url, Header.EMPTY, query, new GenericType<Map>() {
        }.getType(), new Callback<Map>() {

            @Override
            public void onReceive(RestResult<Map> result) {
                if (result.ok()) {
                    responseMap.putAll(result.getData());
                }
                latch.countDown();
            }

            @Override
            public void onError(Throwable throwable) {
                Loggers.CORE.error("Get config metrics error from member address={}, ip={},dataId={},group={},tenant={},error={}", member.getAddress(), ip, dataId, group, tenant, throwable);
                latch.countDown();
            }

            @Override
            public void onCancel() {
                latch.countDown();
            }
        });
    }
    try {
        latch.await(3L, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return ResponseEntity.ok().body(responseMap);
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) Query(com.alibaba.nacos.common.http.param.Query) HashMap(java.util.HashMap) NacosAsyncRestTemplate(com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) HashMap(java.util.HashMap) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 10 with Query

use of com.alibaba.nacos.common.http.param.Query in project nacos by alibaba.

the class NacosRestTemplate_ITCase method test_url_get_return_restResult.

@Test
public void test_url_get_return_restResult() throws Exception {
    String url = IP + CONFIG_PATH + "/configs";
    Query query = Query.newInstance().addParam("beta", true).addParam("dataId", "test-1").addParam("group", "DEFAULT_GROUP");
    HttpRestResult<ConfigInfo4Beta> restResult = nacosRestTemplate.get(url, Header.newInstance(), query, new TypeReference<RestResult<ConfigInfo4Beta>>() {
    }.getType());
    Assert.assertTrue(restResult.ok());
    System.out.println(restResult.getData());
    System.out.println(restResult.getHeader());
}
Also used : Query(com.alibaba.nacos.common.http.param.Query) ConfigInfo4Beta(com.alibaba.nacos.config.server.model.ConfigInfo4Beta) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Query (com.alibaba.nacos.common.http.param.Query)13 Test (org.junit.Test)7 Header (com.alibaba.nacos.common.http.param.Header)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 NacosException (com.alibaba.nacos.api.exception.NacosException)3 HttpClientConfig (com.alibaba.nacos.common.http.HttpClientConfig)3 GenericType (com.alibaba.nacos.core.utils.GenericType)3 BaseClusterTest (com.alibaba.nacos.test.base.BaseClusterTest)2 ConnectException (java.net.ConnectException)2 SocketTimeoutException (java.net.SocketTimeoutException)2 NacosLoadException (com.alibaba.nacos.api.exception.runtime.NacosLoadException)1 NacosAsyncRestTemplate (com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate)1 ConfigInfo4Beta (com.alibaba.nacos.config.server.model.ConfigInfo4Beta)1 Member (com.alibaba.nacos.core.cluster.Member)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1