Search in sources :

Example 1 with GenericType

use of com.alibaba.nacos.core.utils.GenericType in project nacos by alibaba.

the class NacosClusterController method leave.

/**
 * member leave.
 *
 * @param params member ip list, example [ip1:port1,ip2:port2,...]
 * @return {@link RestResult}
 * @throws Exception {@link Exception}
 */
@PostMapping("/server/leave")
public RestResult<String> leave(@RequestBody Collection<String> params) throws Exception {
    Collection<Member> memberList = MemberUtil.multiParse(params);
    memberManager.memberLeave(memberList);
    final NacosAsyncRestTemplate nacosAsyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CLUSTER);
    final GenericType<RestResult<String>> genericType = new GenericType<RestResult<String>>() {
    };
    final Collection<Member> notifyList = memberManager.allMembersWithoutSelf();
    notifyList.removeAll(memberList);
    CountDownLatch latch = new CountDownLatch(notifyList.size());
    for (Member member : notifyList) {
        final String url = HttpUtils.buildUrl(false, member.getAddress(), EnvUtil.getContextPath(), Commons.NACOS_CORE_CONTEXT, "/cluster/server/leave");
        nacosAsyncRestTemplate.post(url, Header.EMPTY, Query.EMPTY, params, genericType.getType(), new Callback<String>() {

            @Override
            public void onReceive(RestResult<String> result) {
                try {
                    if (result.ok()) {
                        LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "The node : [{}] success to process the request", member);
                        MemberUtil.onSuccess(memberManager, member);
                    } else {
                        Loggers.CLUSTER.warn("The node : [{}] failed to process the request, response is : {}", member, result);
                        MemberUtil.onFail(memberManager, member);
                    }
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
                try {
                    Loggers.CLUSTER.error("Failed to communicate with the node : {}", member);
                    MemberUtil.onFail(memberManager, member);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onCancel() {
            }
        });
    }
    try {
        latch.await(10_000, TimeUnit.MILLISECONDS);
        return RestResultUtils.success("ok");
    } catch (Throwable ex) {
        return RestResultUtils.failed(ex.getMessage());
    }
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) RestResult(com.alibaba.nacos.common.model.RestResult) NacosAsyncRestTemplate(com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 2 with GenericType

use of com.alibaba.nacos.core.utils.GenericType in project nacos by alibaba.

the class NacosClusterController method leave.

/**
 * member leave.
 *
 * @param params member ip list, example [ip1:port1,ip2:port2,...]
 * @return {@link RestResult}
 * @throws Exception {@link Exception}
 */
@PostMapping("/server/leave")
public RestResult<String> leave(@RequestBody Collection<String> params, @RequestParam(defaultValue = "true") Boolean notifyOtherMembers) throws Exception {
    Collection<Member> memberList = MemberUtil.multiParse(params);
    memberManager.memberLeave(memberList);
    // End conditions for cluster notifications, FIX https://github.com/alibaba/nacos/issues/6273
    if (Boolean.FALSE.equals(notifyOtherMembers)) {
        return RestResultUtils.success("ok");
    }
    final NacosAsyncRestTemplate nacosAsyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CLUSTER);
    final GenericType<RestResult<String>> genericType = new GenericType<RestResult<String>>() {
    };
    final Collection<Member> notifyList = memberManager.allMembersWithoutSelf();
    notifyList.removeAll(memberList);
    CountDownLatch latch = new CountDownLatch(notifyList.size());
    for (Member member : notifyList) {
        final String url = HttpUtils.buildUrl(false, member.getAddress(), EnvUtil.getContextPath(), Commons.NACOS_CORE_CONTEXT, "/cluster/server/leave?notifyOtherMembers=false");
        nacosAsyncRestTemplate.post(url, Header.EMPTY, Query.EMPTY, params, genericType.getType(), new Callback<String>() {

            @Override
            public void onReceive(RestResult<String> result) {
                try {
                    if (result.ok()) {
                        LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "The node : [{}] success to process the request", member);
                        MemberUtil.onSuccess(memberManager, member);
                    } else {
                        Loggers.CLUSTER.warn("The node : [{}] failed to process the request, response is : {}", member, result);
                        MemberUtil.onFail(memberManager, member);
                    }
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
                try {
                    Loggers.CLUSTER.error("Failed to communicate with the node : {}", member);
                    MemberUtil.onFail(memberManager, member);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onCancel() {
            }
        });
    }
    try {
        latch.await(10_000, TimeUnit.MILLISECONDS);
        return RestResultUtils.success("ok");
    } catch (Throwable ex) {
        return RestResultUtils.failed(ex.getMessage());
    }
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) RestResult(com.alibaba.nacos.common.model.RestResult) NacosAsyncRestTemplate(com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with GenericType

use of com.alibaba.nacos.core.utils.GenericType in project nacos by alibaba.

the class NacosClusterV2Controller method deleteNodes.

/**
 * member leave.
 *
 * @param addresses member ip list, example [ip1:port1,ip2:port2,...]
 * @return {@link RestResult}
 * @throws Exception throw {@link Exception}
 */
@DeleteMapping("/nodes")
public RestResult<Void> deleteNodes(@RequestParam("addresses") List<String> addresses) throws Exception {
    Collection<Member> memberList = MemberUtil.multiParse(addresses);
    memberManager.memberLeave(memberList);
    final NacosAsyncRestTemplate nacosAsyncRestTemplate = HttpClientBeanHolder.getNacosAsyncRestTemplate(Loggers.CLUSTER);
    final GenericType<RestResult<String>> genericType = new GenericType<RestResult<String>>() {
    };
    final Collection<Member> notifyList = memberManager.allMembersWithoutSelf();
    notifyList.removeAll(memberList);
    CountDownLatch latch = new CountDownLatch(notifyList.size());
    for (Member member : notifyList) {
        final String url = HttpUtils.buildUrl(false, member.getAddress(), EnvUtil.getContextPath(), Commons.NACOS_CORE_CONTEXT_V2, "/cluster/nodes");
        nacosAsyncRestTemplate.delete(url, Header.EMPTY, StringUtils.join(addresses, ","), genericType.getType(), new Callback<Void>() {

            @Override
            public void onReceive(RestResult<Void> result) {
                try {
                    if (result.ok()) {
                        LoggerUtils.printIfDebugEnabled(Loggers.CLUSTER, "The node : [{}] success to process the request", member);
                        MemberUtil.onSuccess(memberManager, member);
                    } else {
                        Loggers.CLUSTER.warn("The node : [{}] failed to process the request, response is : {}", member, result);
                        MemberUtil.onFail(memberManager, member);
                    }
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onError(Throwable throwable) {
                try {
                    Loggers.CLUSTER.error("Failed to communicate with the node : {}", member);
                    MemberUtil.onFail(memberManager, member);
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onCancel() {
            }
        });
    }
    try {
        latch.await(10_000, TimeUnit.MILLISECONDS);
        return RestResultUtils.success();
    } catch (Throwable ex) {
        return RestResultUtils.failed(ex.getMessage());
    }
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) RestResult(com.alibaba.nacos.common.model.RestResult) NacosAsyncRestTemplate(com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.alibaba.nacos.core.cluster.Member) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Example 4 with GenericType

use of com.alibaba.nacos.core.utils.GenericType in project nacos by alibaba.

the class ConfigDerbyRaft_DITCase method test_e_derby_ops.

@Test
public void test_e_derby_ops() throws Exception {
    String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby";
    Query query = Query.newInstance().addParam("sql", "select * from users");
    RestResult<List<Map<String, Object>>> result = NACOS_REST_TEMPLATE.get(url, Header.EMPTY, query, new GenericType<RestResult<List<Map<String, Object>>>>() {
    }.getType());
    System.out.println(result.getData());
    Assert.assertTrue(result.ok());
    List<Map<String, Object>> list = result.getData();
    Assert.assertEquals(1, list.size());
    Assert.assertEquals("nacos", list.get(0).get("USERNAME"));
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) Query(com.alibaba.nacos.common.http.param.Query) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test) BaseClusterTest(com.alibaba.nacos.test.base.BaseClusterTest)

Example 5 with GenericType

use of com.alibaba.nacos.core.utils.GenericType in project nacos by alibaba.

the class ConfigDerbyRaft_DITCase method test_g_derby_ops_no_select.

@Test
public void test_g_derby_ops_no_select() throws Exception {
    String url = "http://127.0.0.1:8848/nacos/v1/cs/ops/derby";
    Query query = Query.newInstance().addParam("sql", "update users set username='nacos'");
    RestResult<Object> result = NACOS_REST_TEMPLATE.get(url, Header.EMPTY, query, new GenericType<RestResult<Object>>() {
    }.getType());
    System.out.println(result);
    Assert.assertFalse(result.ok());
    Assert.assertEquals("Only query statements are allowed to be executed", result.getMessage());
}
Also used : GenericType(com.alibaba.nacos.core.utils.GenericType) Query(com.alibaba.nacos.common.http.param.Query) Test(org.junit.Test) BaseClusterTest(com.alibaba.nacos.test.base.BaseClusterTest)

Aggregations

GenericType (com.alibaba.nacos.core.utils.GenericType)7 NacosAsyncRestTemplate (com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate)4 Member (com.alibaba.nacos.core.cluster.Member)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Query (com.alibaba.nacos.common.http.param.Query)3 RestResult (com.alibaba.nacos.common.model.RestResult)3 BaseClusterTest (com.alibaba.nacos.test.base.BaseClusterTest)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)1 ConfigDumpEvent (com.alibaba.nacos.config.server.model.event.ConfigDumpEvent)1 ByteString (com.google.protobuf.ByteString)1 List (java.util.List)1 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1