From 1daa598902684293431f080c4dfc306ea717238f Mon Sep 17 00:00:00 2001 From: brotherlu-xcq <1285823170@qq.com> Date: Wed, 9 Jun 2021 09:46:07 +0800 Subject: [PATCH] [FOR 5771 config impl] improve the code quality of nacos-client. (#5980) * [code quality] [nacos-client] [common/filter/http] simple the judge logic, constant export, chinese javadoc translate * [code quality] [nacos-client] [impl] remove unused import * [code quality] [nacos-client] [impl] fix CI problem --- .../nacos/client/config/impl/CacheData.java | 86 ++++++++--------- .../client/config/impl/ClientWorker.java | 70 ++++++++------ .../config/impl/ConfigTransportClient.java | 17 +++- .../nacos/client/config/impl/Limiter.java | 4 +- .../config/impl/LocalConfigInfoProcessor.java | 83 ++++++++-------- .../impl/LocalEncryptedDataKeyProcessor.java | 38 +++++--- .../config/impl/PropertiesChangeParser.java | 4 +- .../client/config/impl/ServerListManager.java | 95 +++++++++---------- .../nacos/client/config/impl/SpasAdapter.java | 34 ++++--- .../client/config/impl/YmlChangeParser.java | 4 +- 10 files changed, 241 insertions(+), 194 deletions(-) diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java index db31ad1aa..b4621ecc4 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/CacheData.java @@ -60,6 +60,47 @@ public class CacheData { private static final Logger LOGGER = LogUtils.logger(CacheData.class); + private final String name; + + private final ConfigFilterChainManager configFilterChainManager; + + public final String dataId; + + public final String group; + + public final String tenant; + + private final CopyOnWriteArrayList listeners; + + private volatile String md5; + + /** + * whether use local config. + */ + private volatile boolean isUseLocalConfig = false; + + /** + * last modify time. + */ + private volatile long localConfigLastModified; + + private volatile String content; + + private volatile String encryptedDataKey; + + private volatile long lastModifiedTs; + + private int taskId; + + private volatile boolean isInitializing = true; + + /** + * if is cache data md5 sync with the server. + */ + private volatile boolean isSyncWithServer = false; + + private String type; + public boolean isInitializing() { return isInitializing; } @@ -332,7 +373,7 @@ public class CacheData { * 1.first add listener.default is false;need to check. 2.receive config change notify,set false;need to check. * 3.last listener is remove,set to false;need to check * - * @return + * @return the flag if sync with server */ public boolean isSyncWithServer() { return isSyncWithServer; @@ -374,49 +415,6 @@ public class CacheData { this.md5 = getMd5String(content); } - // ================== - - private final String name; - - private final ConfigFilterChainManager configFilterChainManager; - - public final String dataId; - - public final String group; - - public final String tenant; - - private final CopyOnWriteArrayList listeners; - - private volatile String md5; - - /** - * whether use local config. - */ - private volatile boolean isUseLocalConfig = false; - - /** - * last modify time. - */ - private volatile long localConfigLastModified; - - private volatile String content; - - private volatile String encryptedDataKey; - - private volatile long lastModifiedTs; - - private int taskId; - - private volatile boolean isInitializing = true; - - /** - * if is cache data md5 sync with the server. - */ - private volatile boolean isSyncWithServer = false; - - private String type; - public String getEncryptedDataKey() { return encryptedDataKey; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java index 54575d2c1..2a85094c9 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ClientWorker.java @@ -96,6 +96,40 @@ public class ClientWorker implements Closeable { private static final Logger LOGGER = LogUtils.logger(ClientWorker.class); + private static final String NOTIFY_HEADER = "notify"; + + private static final String TAG_PARAM = "tag"; + + private static final String APP_NAME_PARAM = "appName"; + + private static final String BETAIPS_PARAM = "betaIps"; + + private static final String TYPE_PARAM = "type"; + + private static final String ENCRYPTED_DATA_KEY_PARAM = "encryptedDataKey"; + + private static final String DEFAULT_RESOURCE = ""; + + /** + * groupKey -> cacheData. + */ + private final AtomicReference> cacheMap = new AtomicReference>( + new HashMap()); + + private final ConfigFilterChainManager configFilterChainManager; + + private boolean isHealthServer = true; + + private String uuid = UUID.randomUUID().toString(); + + private long timeout; + + private ConfigTransportClient agent; + + private int taskPenaltyTime; + + private boolean enableRemoteSyncConfig = false; + /** * Add listeners for data. * @@ -537,26 +571,6 @@ public class ClientWorker implements Closeable { this.isHealthServer = isHealthServer; } - /** - * groupKey -> cacheData. - */ - private final AtomicReference> cacheMap = new AtomicReference>( - new HashMap()); - - private final ConfigFilterChainManager configFilterChainManager; - - private boolean isHealthServer = true; - - private String uuid = UUID.randomUUID().toString(); - - private long timeout; - - private ConfigTransportClient agent; - - private int taskPenaltyTime; - - private boolean enableRemoteSyncConfig = false; - public class ConfigRpcTransportClient extends ConfigTransportClient { private final BlockingQueue listenExecutebell = new ArrayBlockingQueue(1); @@ -961,7 +975,7 @@ public class ClientWorker implements Closeable { public ConfigResponse queryConfig(String dataId, String group, String tenant, long readTimeouts, boolean notify) throws NacosException { ConfigQueryRequest request = ConfigQueryRequest.build(dataId, group, tenant); - request.putHeader("notify", String.valueOf(notify)); + request.putHeader(NOTIFY_HEADER, String.valueOf(notify)); RpcClient rpcClient = getOneRunningClient(); if (notify) { CacheData cacheData = cacheMap.get().get(GroupKey.getKeyTenant(dataId, group, tenant)); @@ -1053,7 +1067,7 @@ public class ClientWorker implements Closeable { String group = ((ConfigRemoveRequest) request).getGroup(); return getResource(tenant, group); } - return ""; + return DEFAULT_RESOURCE; } private String getResource(String tenant, String group) { @@ -1066,7 +1080,7 @@ public class ClientWorker implements Closeable { if (StringUtils.isNotBlank(tenant)) { return tenant; } - return ""; + return DEFAULT_RESOURCE; } RpcClient getOneRunningClient() throws NacosException { @@ -1080,11 +1094,11 @@ public class ClientWorker implements Closeable { try { ConfigPublishRequest request = new ConfigPublishRequest(dataId, group, tenant, content); request.setCasMd5(casMd5); - request.putAdditionalParam("tag", tag); - request.putAdditionalParam("appName", appName); - request.putAdditionalParam("betaIps", betaIps); - request.putAdditionalParam("type", type); - request.putAdditionalParam("encryptedDataKey", encryptedDataKey); + request.putAdditionalParam(TAG_PARAM, tag); + request.putAdditionalParam(APP_NAME_PARAM, appName); + request.putAdditionalParam(BETAIPS_PARAM, betaIps); + request.putAdditionalParam(TYPE_PARAM, type); + request.putAdditionalParam(ENCRYPTED_DATA_KEY_PARAM, encryptedDataKey); ConfigPublishResponse response = (ConfigPublishResponse) requestProxy(getOneRunningClient(), request); if (!response.isSuccess()) { LOGGER.warn("[{}] [publish-single] fail, dataId={}, group={}, tenant={}, code={}, msg={}", diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java index 3146e4710..3cdf9c6a7 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ConfigTransportClient.java @@ -53,6 +53,14 @@ public abstract class ConfigTransportClient { private static final Logger LOGGER = LogUtils.logger(ConfigTransportClient.class); + private static final String SECURITY_TOKEN_HEADER = "Spas-SecurityToken"; + + private static final String ACCESS_KEY_HEADER = "Spas-AccessKey"; + + private static final String CONFIG_INFO_HEADER = "exConfigInfo"; + + private static final String DEFAULT_CONFIG_INFO = "true"; + String encode; String tenant; @@ -108,11 +116,11 @@ public abstract class ConfigTransportClient { StsCredential stsCredential = getStsCredential(); accessKey = stsCredential.accessKeyId; secretKey = stsCredential.accessKeySecret; - spasHeaders.put("Spas-SecurityToken", stsCredential.securityToken); + spasHeaders.put(SECURITY_TOKEN_HEADER, stsCredential.securityToken); } if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotBlank(secretKey)) { - spasHeaders.put("Spas-AccessKey", accessKey); + spasHeaders.put(ACCESS_KEY_HEADER, accessKey); } return spasHeaders; } @@ -145,7 +153,7 @@ public abstract class ConfigTransportClient { headers.put(Constants.CLIENT_APPNAME_HEADER, ParamUtil.getAppName()); headers.put(Constants.CLIENT_REQUEST_TS_HEADER, ts); headers.put(Constants.CLIENT_REQUEST_TOKEN_HEADER, token); - headers.put("exConfigInfo", "true"); + headers.put(CONFIG_INFO_HEADER, DEFAULT_CONFIG_INFO); headers.put(Constants.CHARSET_KEY, encode); return headers; } @@ -165,9 +173,8 @@ public abstract class ConfigTransportClient { } } String stsResponse = getStsResponse(); - StsCredential stsCredentialTmp = JacksonUtils.toObj(stsResponse, new TypeReference() { + stsCredential = JacksonUtils.toObj(stsResponse, new TypeReference() { }); - stsCredential = stsCredentialTmp; LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", stsCredential.getCode(), stsCredential.getAccessKeyId(), stsCredential.getLastUpdated(), stsCredential.getExpiration()); diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java index 345e2d7de..5ca4e0d7e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/Limiter.java @@ -42,6 +42,8 @@ public class Limiter { private static final Cache CACHE = CacheBuilder.newBuilder().initialCapacity(CAPACITY_SIZE) .expireAfterAccess(1, TimeUnit.MINUTES).build(); + private static final String LIMIT_TIME_PROPERTY = "limitTime"; + /** * qps 5. */ @@ -49,7 +51,7 @@ public class Limiter { static { try { - String limitTimeStr = System.getProperty("limitTime", String.valueOf(limit)); + String limitTimeStr = System.getProperty(LIMIT_TIME_PROPERTY, String.valueOf(limit)); limit = Double.parseDouble(limitTimeStr); LOGGER.info("limitTime:{}", limit); } catch (Exception e) { diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java index e618eb3f2..cbe935577 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalConfigInfoProcessor.java @@ -39,6 +39,34 @@ public class LocalConfigInfoProcessor { private static final Logger LOGGER = LogUtils.logger(LocalConfigInfoProcessor.class); + public static final String LOCAL_FILEROOT_PATH; + + public static final String LOCAL_SNAPSHOT_PATH; + + private static final String SUFFIX = "_nacos"; + + private static final String ENV_CHILD = "snapshot"; + + private static final String FAILOVER_FILE_CHILD_1 = "data"; + + private static final String FAILOVER_FILE_CHILD_2 = "config-data"; + + private static final String FAILOVER_FILE_CHILD_3 = "config-data-tenant"; + + private static final String SNAPSHOT_FILE_CHILD_1 = "snapshot"; + + private static final String SNAPSHOT_FILE_CHILD_2 = "snapshot-tenant"; + + static { + LOCAL_FILEROOT_PATH = + System.getProperty("JM.LOG.PATH", System.getProperty("user.home")) + File.separator + "nacos" + + File.separator + "config"; + LOCAL_SNAPSHOT_PATH = + System.getProperty("JM.SNAPSHOT.PATH", System.getProperty("user.home")) + File.separator + "nacos" + + File.separator + "config"; + LOGGER.info("LOCAL_SNAPSHOT_PATH:{}", LOCAL_SNAPSHOT_PATH); + } + public static String getFailover(String serverName, String dataId, String group, String tenant) { File localPath = getFailoverFile(serverName, dataId, group, tenant); if (!localPath.exists() || !localPath.isFile()) { @@ -54,7 +82,7 @@ public class LocalConfigInfoProcessor { } /** - * 获取本地缓存文件内容。NULL表示没有本地文件或抛出异常. + * get snapshot file content. NULL means no local file or throw exception. */ public static String getSnapshot(String name, String dataId, String group, String tenant) { if (!SnapShotSwitch.getIsSnapShot()) { @@ -81,17 +109,8 @@ public class LocalConfigInfoProcessor { if (JvmUtil.isMultiInstance()) { return ConcurrentDiskUtil.getFileContent(file, Constants.ENCODE); } else { - InputStream is = null; - try { - is = new FileInputStream(file); + try (InputStream is = new FileInputStream(file)) { return IoUtils.toString(is, Constants.ENCODE); - } finally { - try { - if (null != is) { - is.close(); - } - } catch (IOException ignored) { - } } } } @@ -138,7 +157,7 @@ public class LocalConfigInfoProcessor { } /** - * 清除snapshot目录下所有缓存文件. + * clear the cache files under snapshot directory. */ public static void cleanAllSnapshot() { try { @@ -148,7 +167,7 @@ public class LocalConfigInfoProcessor { return; } for (File file : files) { - if (file.getName().endsWith("_nacos")) { + if (file.getName().endsWith(SUFFIX)) { IoUtils.cleanDirectory(file); } } @@ -163,53 +182,37 @@ public class LocalConfigInfoProcessor { * @param envName env name */ public static void cleanEnvSnapshot(String envName) { - File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + "_nacos"); - tmp = new File(tmp, "snapshot"); + File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + SUFFIX); + tmp = new File(tmp, ENV_CHILD); try { IoUtils.cleanDirectory(tmp); - LOGGER.info("success delete " + envName + "-snapshot"); + LOGGER.info("success delete {}-snapshot", envName); } catch (IOException e) { - LOGGER.info("fail delete " + envName + "-snapshot, " + e.toString()); - e.printStackTrace(); + LOGGER.warn("fail delete {}-snapshot, exception: ", envName, e); } } static File getFailoverFile(String serverName, String dataId, String group, String tenant) { - File tmp = new File(LOCAL_SNAPSHOT_PATH, serverName + "_nacos"); - tmp = new File(tmp, "data"); + File tmp = new File(LOCAL_SNAPSHOT_PATH, serverName + SUFFIX); + tmp = new File(tmp, FAILOVER_FILE_CHILD_1); if (StringUtils.isBlank(tenant)) { - tmp = new File(tmp, "config-data"); + tmp = new File(tmp, FAILOVER_FILE_CHILD_2); } else { - tmp = new File(tmp, "config-data-tenant"); + tmp = new File(tmp, FAILOVER_FILE_CHILD_3); tmp = new File(tmp, tenant); } return new File(new File(tmp, group), dataId); } static File getSnapshotFile(String envName, String dataId, String group, String tenant) { - File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + "_nacos"); + File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + SUFFIX); if (StringUtils.isBlank(tenant)) { - tmp = new File(tmp, "snapshot"); + tmp = new File(tmp, SNAPSHOT_FILE_CHILD_1); } else { - tmp = new File(tmp, "snapshot-tenant"); + tmp = new File(tmp, SNAPSHOT_FILE_CHILD_2); tmp = new File(tmp, tenant); } return new File(new File(tmp, group), dataId); } - - public static final String LOCAL_FILEROOT_PATH; - - public static final String LOCAL_SNAPSHOT_PATH; - - static { - LOCAL_FILEROOT_PATH = - System.getProperty("JM.LOG.PATH", System.getProperty("user.home")) + File.separator + "nacos" - + File.separator + "config"; - LOCAL_SNAPSHOT_PATH = - System.getProperty("JM.SNAPSHOT.PATH", System.getProperty("user.home")) + File.separator + "nacos" - + File.separator + "config"; - LOGGER.info("LOCAL_SNAPSHOT_PATH:{}", LOCAL_SNAPSHOT_PATH); - } - } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalEncryptedDataKeyProcessor.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalEncryptedDataKeyProcessor.java index b79558331..51808ed07 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalEncryptedDataKeyProcessor.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/LocalEncryptedDataKeyProcessor.java @@ -29,7 +29,7 @@ import java.io.File; import java.io.IOException; /** - * 密文数据密钥(EncryptedDataKey)的本地快照、容灾目录相关. + * Encrypted data key (EncryptedDataKey) local snapshot, disaster recovery directory related. * * @author luyanbo(RobberPhex) */ @@ -37,8 +37,22 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor { private static final Logger LOGGER = LogUtils.logger(LocalEncryptedDataKeyProcessor.class); + private static final String FAILOVER_CHILD_1 = "encrypted-data-key"; + + private static final String FAILOVER_CHILD_2 = "failover"; + + private static final String FAILOVER_CHILD_3 = "failover-tenant"; + + private static final String SNAPSHOT_CHILD_1 = "encrypted-data-key"; + + private static final String SNAPSHOT_CHILD_2 = "snapshot"; + + private static final String SNAPSHOT_CHILD_3 = "snapshot-tenant"; + + private static final String SUFFIX = "_nacos"; + /** - * 获取容灾配置的 EncryptedDataKey。NULL表示没有本地文件或抛出异常. + * Obtain the EncryptedDataKey of the disaster recovery configuration. NULL means there is no local file or an exception is thrown. */ public static String getEncryptDataKeyFailover(String envName, String dataId, String group, String tenant) { File file = getEncryptDataKeyFailoverFile(envName, dataId, group, tenant); @@ -55,7 +69,7 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor { } /** - * 获取本地缓存文件的 EncryptedDataKey。NULL表示没有本地文件或抛出异常. + * Get the EncryptedDataKey of the locally cached file. NULL means there is no local file or an exception is thrown. */ public static String getEncryptDataKeySnapshot(String envName, String dataId, String group, String tenant) { if (!SnapShotSwitch.getIsSnapShot()) { @@ -75,7 +89,7 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor { } /** - * 保存 encryptDataKey 的snapshot。如果内容为NULL,则删除snapshot. + * Save the snapshot of encryptDataKey. If the content is NULL, delete the snapshot. */ public static void saveEncryptDataKeySnapshot(String envName, String dataId, String group, String tenant, String encryptDataKey) { @@ -110,13 +124,13 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor { } private static File getEncryptDataKeyFailoverFile(String envName, String dataId, String group, String tenant) { - File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + "_nacos"); - tmp = new File(tmp, "encrypted-data-key"); + File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + SUFFIX); + tmp = new File(tmp, FAILOVER_CHILD_1); if (StringUtils.isBlank(tenant)) { - tmp = new File(tmp, "failover"); + tmp = new File(tmp, FAILOVER_CHILD_2); } else { - tmp = new File(tmp, "failover-tenant"); + tmp = new File(tmp, FAILOVER_CHILD_3); tmp = new File(tmp, tenant); } @@ -124,13 +138,13 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor { } private static File getEncryptDataKeySnapshotFile(String envName, String dataId, String group, String tenant) { - File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + "_nacos"); - tmp = new File(tmp, "encrypted-data-key"); + File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + SUFFIX); + tmp = new File(tmp, SNAPSHOT_CHILD_1); if (StringUtils.isBlank(tenant)) { - tmp = new File(tmp, "snapshot"); + tmp = new File(tmp, SNAPSHOT_CHILD_2); } else { - tmp = new File(tmp, "snapshot-tenant"); + tmp = new File(tmp, SNAPSHOT_CHILD_3); tmp = new File(tmp, tenant); } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java index da10ffe8c..34439269f 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/PropertiesChangeParser.java @@ -31,8 +31,10 @@ import java.util.Properties; */ public class PropertiesChangeParser extends AbstractConfigChangeParser { + private static final String CONFIG_TYPE = "properties"; + public PropertiesChangeParser() { - super("properties"); + super(CONFIG_TYPE); } @Override diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java index 1f21322e4..6fa13de3b 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/ServerListManager.java @@ -75,6 +75,52 @@ public class ServerListManager implements Closeable { } }); + /** + * The name of the different environment. + */ + private final String name; + + private String namespace = ""; + + private String tenant = ""; + + public static final String DEFAULT_NAME = "default"; + + public static final String CUSTOM_NAME = "custom"; + + public static final String FIXED_NAME = "fixed"; + + private final int initServerlistRetryTimes = 5; + + /** + * Connection timeout and socket timeout with other servers. + */ + static final int TIMEOUT = 5000; + + final boolean isFixed; + + boolean isStarted = false; + + private String endpoint; + + private int endpointPort = 8080; + + private String contentPath = ParamUtil.getDefaultContextPath(); + + private String serverListName = ParamUtil.getDefaultNodesPath(); + + volatile List serverUrls = new ArrayList(); + + private volatile String currentServerAddr; + + private Iterator iterator; + + public String serverPort = ParamUtil.getDefaultServerPort(); + + public String addressServerUrl; + + private String serverAddrsStr; + public ServerListManager() { this.isFixed = false; this.isStarted = false; @@ -418,8 +464,7 @@ public class ServerListManager implements Closeable { return currentServerAddr; } try { - String next = iterator.next(); - return next; + return iterator.next(); } catch (Exception e) { //No nothing. } @@ -460,52 +505,6 @@ public class ServerListManager implements Closeable { return tenant; } - /** - * The name of the different environment. - */ - private final String name; - - private String namespace = ""; - - private String tenant = ""; - - public static final String DEFAULT_NAME = "default"; - - public static final String CUSTOM_NAME = "custom"; - - public static final String FIXED_NAME = "fixed"; - - private final int initServerlistRetryTimes = 5; - - /** - * Connection timeout and socket timeout with other servers. - */ - static final int TIMEOUT = 5000; - - final boolean isFixed; - - boolean isStarted = false; - - private String endpoint; - - private int endpointPort = 8080; - - private String contentPath = ParamUtil.getDefaultContextPath(); - - private String serverListName = ParamUtil.getDefaultNodesPath(); - - volatile List serverUrls = new ArrayList(); - - private volatile String currentServerAddr; - - private Iterator iterator; - - public String serverPort = ParamUtil.getDefaultServerPort(); - - public String addressServerUrl; - - private String serverAddrsStr; - /** * Sort the address list, with the same room priority. */ diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java index cfa2d8613..a0bad9fbd 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/SpasAdapter.java @@ -28,24 +28,34 @@ import java.util.HashMap; import java.util.Map; /** - * 适配spas接口. + * adapt spas interface. * * @author Nacos */ public class SpasAdapter { + private static final String TIMESTAMP_HEADER = "Timestamp"; + + private static final String SIGNATURE_HEADER = "Spas-Signature"; + + private static final String GROUP_KEY = "group"; + + public static final String TENANT_KEY = "tenant"; + + private static final String SHA_ENCRYPT = "HmacSHA1"; + public static Map getSignHeaders(String resource, String secretKey) { Map header = new HashMap(2); String timeStamp = String.valueOf(System.currentTimeMillis()); - header.put("Timestamp", timeStamp); + header.put(TIMESTAMP_HEADER, timeStamp); if (secretKey != null) { - String signature = ""; + String signature; if (StringUtils.isBlank(resource)) { signature = signWithHmacSha1Encrypt(timeStamp, secretKey); } else { signature = signWithHmacSha1Encrypt(resource + "+" + timeStamp, secretKey); } - header.put("Spas-Signature", signature); + header.put(SIGNATURE_HEADER, signature); } return header; } @@ -104,22 +114,18 @@ public class SpasAdapter { public static String signWithHmacSha1Encrypt(String encryptText, String encryptKey) { try { byte[] data = encryptKey.getBytes(Constants.ENCODE); - // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称 - SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1"); - // 生成一个指定 Mac 算法 的 Mac 对象 - Mac mac = Mac.getInstance("HmacSHA1"); - // 用给定密钥初始化 Mac 对象 + // Construct a key according to the given byte array, and the second parameter specifies the name of a key algorithm + SecretKey secretKey = new SecretKeySpec(data, SHA_ENCRYPT); + // Generate a Mac object specifying Mac algorithm + Mac mac = Mac.getInstance(SHA_ENCRYPT); + // Initialize the Mac object with the given key mac.init(secretKey); byte[] text = encryptText.getBytes(Constants.ENCODE); byte[] textFinal = mac.doFinal(text); - // 完成 Mac 操作, base64编码,将byte数组转换为字符串 + // Complete Mac operation, base64 encoding, convert byte array to string return new String(Base64.encodeBase64(textFinal), Constants.ENCODE); } catch (Exception e) { throw new RuntimeException("signWithhmacSHA1Encrypt fail", e); } } - - private static final String GROUP_KEY = "group"; - - public static final String TENANT_KEY = "tenant"; } diff --git a/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java b/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java index b56ac3caf..acb66d67e 100644 --- a/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java +++ b/client/src/main/java/com/alibaba/nacos/client/config/impl/YmlChangeParser.java @@ -39,8 +39,10 @@ public class YmlChangeParser extends AbstractConfigChangeParser { private static final String INVALID_CONSTRUCTOR_ERROR_INFO = "could not determine a constructor for the tag"; + private static final String CONFIG_TYPE = "yaml"; + public YmlChangeParser() { - super("yaml"); + super(CONFIG_TYPE); } @Override