use of com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate 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());
}
}
use of com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate 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());
}
}
use of com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate 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());
}
}
use of com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate in project nacos by alibaba.
the class AbstractHttpClientFactory method createNacosAsyncRestTemplate.
@Override
public NacosAsyncRestTemplate createNacosAsyncRestTemplate() {
final HttpClientConfig originalRequestConfig = buildHttpClientConfig();
final DefaultConnectingIOReactor ioreactor = getIoReactor();
final RequestConfig defaultConfig = getRequestConfig();
return new NacosAsyncRestTemplate(assignLogger(), new DefaultAsyncHttpClientRequest(HttpAsyncClients.custom().addInterceptorLast(new RequestContent(true)).setDefaultIOReactorConfig(getIoReactorConfig()).setDefaultRequestConfig(defaultConfig).setMaxConnTotal(originalRequestConfig.getMaxConnTotal()).setMaxConnPerRoute(originalRequestConfig.getMaxConnPerRoute()).setUserAgent(originalRequestConfig.getUserAgent()).setConnectionManager(getConnectionManager(originalRequestConfig, ioreactor)).build(), ioreactor, defaultConfig));
}
use of com.alibaba.nacos.common.http.client.NacosAsyncRestTemplate in project nacos by alibaba.
the class HttpClientBeanHolder method shutdownNacosAsyncRest.
/**
* Shutdown async http client holder and remove template.
*
* @param className HttpClientFactory implement class name
* @throws Exception ex
*/
public static void shutdownNacosAsyncRest(String className) throws Exception {
final NacosAsyncRestTemplate nacosAsyncRestTemplate = SINGLETON_ASYNC_REST.get(className);
if (nacosAsyncRestTemplate != null) {
nacosAsyncRestTemplate.close();
SINGLETON_ASYNC_REST.remove(className);
}
}
Aggregations