From 23bd92b36f8bb28d49e1f1359def3e2c3b321740 Mon Sep 17 00:00:00 2001 From: brotherlu-xcq <1285823170@qq.com> Date: Tue, 8 Jun 2021 09:53:45 +0800 Subject: [PATCH] [FOR 5771 config common/filter/http] improve the code quality of nacos-client. (#5979) * [code quality] [nacos-client] [common/filter/http] simple the judge logic, constant export, chinese javadoc translate * [code quality] [nacos-client] [common/filter/http] remove unused import --- .../client/config/common/ConfigConstants.java | 2 ++ .../nacos/client/config/common/GroupKey.java | 30 ++++++++++++------- .../filter/impl/ConfigFilterChainManager.java | 2 +- .../config/filter/impl/ConfigRequest.java | 5 ++-- .../client/config/http/MetricsHttpAgent.java | 21 ++++++------- .../client/config/http/ServerHttpAgent.java | 21 +++++++------ 6 files changed, 47 insertions(+), 34 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/common/ConfigConstants.java b/client/src/main/java/com/alibaba/nacos/client/config/common/ConfigConstants.java index 0a799ac1a..ab6e3d92a 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/common/ConfigConstants.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/common/ConfigConstants.java @@ -34,4 +34,6 @@ public class ConfigConstants { public static final String CONFIG_TYPE = "configType"; public static final String ENCRYPTED_DATA_KEY = "encryptedDataKey"; + + public static final String TYPE = "type"; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/common/GroupKey.java b/client/src/main/java/com/alibaba/nacos/client/config/common/GroupKey.java index bb606ed5b..b5f4b75f9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/common/GroupKey.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/common/GroupKey.java @@ -25,6 +25,16 @@ import com.alibaba.nacos.common.utils.StringUtils; */ public class GroupKey { + private static final char PLUS = '+'; + + private static final char PERCENT = '%'; + + private static final char TWO = '2'; + + private static final char B = 'B'; + + private static final char FIVE = '5'; + public static String getKey(String dataId, String group) { return getKey(dataId, group, ""); } @@ -46,10 +56,10 @@ public class GroupKey { } StringBuilder sb = new StringBuilder(); urlEncode(dataId, sb); - sb.append('+'); + sb.append(PLUS); urlEncode(group, sb); if (StringUtils.isNotEmpty(datumStr)) { - sb.append('+'); + sb.append(PLUS); urlEncode(datumStr, sb); } @@ -70,7 +80,7 @@ public class GroupKey { for (int i = 0; i < groupKey.length(); ++i) { char c = groupKey.charAt(i); - if ('+' == c) { + if (PLUS == c) { if (null == dataId) { dataId = sb.toString(); sb.setLength(0); @@ -80,13 +90,13 @@ public class GroupKey { } else { throw new IllegalArgumentException("invalid groupkey:" + groupKey); } - } else if ('%' == c) { + } else if (PERCENT == c) { char next = groupKey.charAt(++i); char nextnext = groupKey.charAt(++i); - if ('2' == next && 'B' == nextnext) { - sb.append('+'); - } else if ('2' == next && '5' == nextnext) { - sb.append('%'); + if (TWO == next && B == nextnext) { + sb.append(PLUS); + } else if (TWO == next && FIVE == nextnext) { + sb.append(PERCENT); } else { throw new IllegalArgumentException("invalid groupkey:" + groupKey); } @@ -116,9 +126,9 @@ public class GroupKey { static void urlEncode(String str, StringBuilder sb) { for (int idx = 0; idx < str.length(); ++idx) { char c = str.charAt(idx); - if ('+' == c) { + if (PLUS == c) { sb.append("%2B"); - } else if ('%' == c) { + } else if (PERCENT == c) { sb.append("%25"); } else { sb.append(c); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java index 6dc419d98..74c031243 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigFilterChainManager.java @@ -51,7 +51,7 @@ public class ConfigFilterChainManager implements IConfigFilterChain { * @return this */ public synchronized ConfigFilterChainManager addFilter(IConfigFilter filter) { - // 根据order大小顺序插入 + // ordered by order value int i = 0; while (i < this.filters.size()) { IConfigFilter currentValue = this.filters.get(i); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigRequest.java b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigRequest.java index e7aef6a9c..916f83e35 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigRequest.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/filter/impl/ConfigRequest.java @@ -26,6 +26,7 @@ import static com.alibaba.nacos.client.config.common.ConfigConstants.CONTENT; import static com.alibaba.nacos.client.config.common.ConfigConstants.DATA_ID; import static com.alibaba.nacos.client.config.common.ConfigConstants.GROUP; import static com.alibaba.nacos.client.config.common.ConfigConstants.TENANT; +import static com.alibaba.nacos.client.config.common.ConfigConstants.TYPE; /** * Config Request. @@ -71,11 +72,11 @@ public class ConfigRequest implements IConfigRequest { } public String getType() { - return (String) param.get("type"); + return (String) param.get(TYPE); } public void setType(String type) { - param.put("type", type); + param.put(TYPE, type); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java index ca228846a..af4d90172 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/MetricsHttpAgent.java @@ -21,7 +21,6 @@ import com.alibaba.nacos.client.monitor.MetricsMonitor; import com.alibaba.nacos.common.http.HttpRestResult; import io.prometheus.client.Histogram; -import java.io.IOException; import java.util.Map; /** @@ -31,6 +30,14 @@ import java.util.Map; */ public class MetricsHttpAgent implements HttpAgent { + private static final String GET = "GET"; + + private static final String POST = "POST"; + + private static final String DELETE = "DELETE"; + + private static final String DEFAULT_CODE = "NA"; + private final HttpAgent httpAgent; public MetricsHttpAgent(HttpAgent httpAgent) { @@ -45,12 +52,10 @@ public class MetricsHttpAgent implements HttpAgent { @Override public HttpRestResult httpGet(String path, Map headers, Map paramValues, String encode, long readTimeoutMs) throws Exception { - Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("GET", path, "NA"); + Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor(GET, path, DEFAULT_CODE); HttpRestResult result; try { result = httpAgent.httpGet(path, headers, paramValues, encode, readTimeoutMs); - } catch (IOException e) { - throw e; } finally { timer.observeDuration(); } @@ -61,12 +66,10 @@ public class MetricsHttpAgent implements HttpAgent { @Override public HttpRestResult httpPost(String path, Map headers, Map paramValues, String encode, long readTimeoutMs) throws Exception { - Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("POST", path, "NA"); + Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor(POST, path, DEFAULT_CODE); HttpRestResult result; try { result = httpAgent.httpPost(path, headers, paramValues, encode, readTimeoutMs); - } catch (IOException e) { - throw e; } finally { timer.observeDuration(); } @@ -77,12 +80,10 @@ public class MetricsHttpAgent implements HttpAgent { @Override public HttpRestResult httpDelete(String path, Map headers, Map paramValues, String encode, long readTimeoutMs) throws Exception { - Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor("DELETE", path, "NA"); + Histogram.Timer timer = MetricsMonitor.getConfigRequestMonitor(DELETE, path, DEFAULT_CODE); HttpRestResult result; try { result = httpAgent.httpDelete(path, headers, paramValues, encode, readTimeoutMs); - } catch (IOException e) { - throw e; } finally { timer.observeDuration(); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java index 36bd6132a..6eb4fefae 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/http/ServerHttpAgent.java @@ -49,6 +49,16 @@ public class ServerHttpAgent implements HttpAgent { private static final NacosRestTemplate NACOS_RESTTEMPLATE = ConfigHttpClientManager.getInstance() .getNacosRestTemplate(); + private String accessKey; + + private String secretKey; + + private String encode; + + private int maxRetry = 3; + + final ServerListManager serverListMgr; + @Override public HttpRestResult httpGet(String path, Map headers, Map paramValues, String encode, long readTimeoutMs) throws Exception { @@ -272,15 +282,4 @@ public class ServerHttpAgent implements HttpAgent { SpasAdapter.freeCredentialInstance(); LOGGER.info("{} do shutdown stop", className); } - - private String accessKey; - - private String secretKey; - - private String encode; - - private int maxRetry = 3; - - final ServerListManager serverListMgr; - }