From 4c0b7a45123c9c57ef0bce720cc0885c68b413db Mon Sep 17 00:00:00 2001 From: caojiajun Date: Mon, 6 Nov 2023 19:24:27 +0800 Subject: [PATCH] update console api of redis proxy (#159) --- .../proxy/console/ConsoleServiceAdaptor.java | 86 ++++++++++--------- update-en.md | 2 +- update-zh.md | 2 +- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/console/ConsoleServiceAdaptor.java b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/console/ConsoleServiceAdaptor.java index bfd6a686d..09a4a9f5e 100644 --- a/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/console/ConsoleServiceAdaptor.java +++ b/camellia-redis-proxy/camellia-redis-proxy-core/src/main/java/com/netease/nim/camellia/redis/proxy/console/ConsoleServiceAdaptor.java @@ -17,6 +17,7 @@ import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,14 +30,6 @@ public class ConsoleServiceAdaptor implements ConsoleService { private static final Logger logger = LoggerFactory.getLogger(ConsoleServiceAdaptor.class); - private int serverPort; - public ConsoleServiceAdaptor(int serverPort) { - this.serverPort = serverPort; - } - - public ConsoleServiceAdaptor() { - } - @Override public ConsoleResult status() { ServerStatus.Status status = ServerStatus.getStatus(); @@ -81,30 +74,38 @@ public ConsoleResult offline() { @Override public ConsoleResult check() { - int port = serverPort; - if (port <= 0) { - port = GlobalRedisProxyEnv.getPort(); + List ports = new ArrayList<>(); + if (GlobalRedisProxyEnv.getPort() > 0) { + ports.add(GlobalRedisProxyEnv.getPort()); } - if (port <= 0) { + if (GlobalRedisProxyEnv.getTlsPort() > 0) { + ports.add(GlobalRedisProxyEnv.getTlsPort()); + } + if (GlobalRedisProxyEnv.getCport() > 0) { + ports.add(GlobalRedisProxyEnv.getCport()); + } + if (ports.isEmpty()) { return ConsoleResult.success(); } - Socket socket = new Socket(); - try { - socket.connect(new InetSocketAddress("127.0.0.1", port), 200); - if (logger.isDebugEnabled()) { - logger.debug("check serverPort = " + port + " success"); - } - return ConsoleResult.success("check serverPort = " + port + " success"); - } catch (IOException e) { - logger.error("check serverPort = " + port + " fail"); - return ConsoleResult.error("check serverPort = " + port + " error"); - } finally { + for (Integer port : ports) { + Socket socket = new Socket(); try { - socket.close(); + socket.connect(new InetSocketAddress("127.0.0.1", port), 200); + if (logger.isDebugEnabled()) { + logger.debug("check serverPort = " + port + " success"); + } } catch (IOException e) { - logger.error("close error", e); + logger.error("check serverPort = " + port + " fail"); + return ConsoleResult.error("check serverPort = " + port + " error"); + } finally { + try { + socket.close(); + } catch (IOException e) { + logger.error("close error", e); + } } } + return ConsoleResult.success(); } @Override @@ -175,23 +176,28 @@ public ConsoleResult detect(Map> params) { @Override public ConsoleResult shutdownUpstreamClient(Map> params) { - List list = params.get("resource"); - if (list == null) { - return ConsoleResult.error("resource param missing"); + if (logger.isDebugEnabled()) { + logger.debug("shutdownUpstreamClient, params = {}", params); } - if (list.isEmpty()) { - return ConsoleResult.error("illegal resource param"); + String url = null; + for (Map.Entry> entry : params.entrySet()) { + if (entry.getKey().equalsIgnoreCase("url")) { + url = entry.getValue().get(0); + } } - String resource = list.get(0); - IUpstreamClient upstreamClient = GlobalRedisProxyEnv.getClientTemplateFactory().getEnv().getClientFactory().remove(resource); - if (upstreamClient == null) { - return ConsoleResult.error("upstream client not found, resource = " + resource); + if (url != null) { + try { + IUpstreamClient upstreamClient = GlobalRedisProxyEnv.getClientTemplateFactory().getEnv().getClientFactory().remove(url); + if (upstreamClient == null) { + return ConsoleResult.error("upstream client not found, url = " + url); + } + upstreamClient.shutdown(); + return ConsoleResult.success(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + return ConsoleResult.error("internal error"); + } } - upstreamClient.shutdown(); - return ConsoleResult.success(); - } - - public void setServerPort(int serverPort) { - this.serverPort = serverPort; + return ConsoleResult.error("param wrong"); } } diff --git a/update-en.md b/update-en.md index 837b490a9..55d3e2159 100644 --- a/update-en.md +++ b/update-en.md @@ -5,7 +5,7 @@ * provide camellia-redis-proxy-nacos-bootstrap, so Operations Engineer can use nacos to manager camellia-redis-proxy cluster without java development * provide camellia-redis-proxy-etcd-bootstrap, so Operations Engineer can use etcd to manager camellia-redis-proxy cluster without java development * camelia-feign, add util GlobalCamelliaFeignEnv, so you can calc load balance code before request when use hash policy -* camellia-redis-proxy console add api `/shutdownUpstreamClient?resource=redis://@127.0.0.1:6379`, so you can shutdown upstream client +* camellia-redis-proxy console add api `/shutdownUpstreamClient?url=redis://@127.0.0.1:6379`, so you can shutdown upstream client * camellia-redis-proxy support shard-pubsub, support `SSUBSCRIBE/SUNSUBSCRIBE/SPUBLISH` commands * camellia-redis-proxy support `FUNCTION/TFUNCTION` commands * camellia-redis-proxy support `SCRIPT KILL` command diff --git a/update-zh.md b/update-zh.md index 3dd31fd1a..afc3460c9 100644 --- a/update-zh.md +++ b/update-zh.md @@ -5,7 +5,7 @@ * 新增camellia-redis-proxy-nacos-bootstrap,从而方便运维人员不需要进行java开发即可使用nacos管理proxy集群 * 新增camellia-redis-proxy-etcd-bootstrap,从而方便运维人员不需要进行java开发即可使用etcd管理proxy集群 * camelia-feign,新增GlobalCamelliaFeignEnv类,从而在使用哈希策略时,可以提前进行负载均衡计算,从而可以在调用前就知道调用的服务实例分布情况,方便进行请求合并 -* camellia-redis-proxy的console新增`/shutdownUpstreamClient?resource=redis://@127.0.0.1:6379`接口,用于卸载到某个redis集群的连接 +* camellia-redis-proxy的console新增`/shutdownUpstreamClient?url=redis://@127.0.0.1:6379`接口,用于卸载到某个redis集群的连接 * camellia-redis-proxy支持shard-pubsub,也就是支持`SSUBSCRIBE/SUNSUBSCRIBE/SPUBLISH`命令 * camellia-redis-proxy支持`FUNCTION/TFUNCTION`相关命令 * camellia-redis-proxy支持`SCRIPT KILL`命令