[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
This commit is contained in:
brotherlu-xcq 2021-06-09 09:46:07 +08:00 committed by GitHub
parent ac3280aa5b
commit 1daa598902
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 241 additions and 194 deletions

View File

@ -60,6 +60,47 @@ public class CacheData {
private static final Logger LOGGER = LogUtils.logger(CacheData.class); 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<ManagerListenerWrap> 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() { public boolean isInitializing() {
return 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. * 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 * 3.last listener is remove,set to false;need to check
* *
* @return * @return the flag if sync with server
*/ */
public boolean isSyncWithServer() { public boolean isSyncWithServer() {
return isSyncWithServer; return isSyncWithServer;
@ -374,49 +415,6 @@ public class CacheData {
this.md5 = getMd5String(content); 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<ManagerListenerWrap> 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() { public String getEncryptedDataKey() {
return encryptedDataKey; return encryptedDataKey;
} }

View File

@ -96,6 +96,40 @@ public class ClientWorker implements Closeable {
private static final Logger LOGGER = LogUtils.logger(ClientWorker.class); 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<Map<String, CacheData>> cacheMap = new AtomicReference<Map<String, CacheData>>(
new HashMap<String, CacheData>());
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. * Add listeners for data.
* *
@ -537,26 +571,6 @@ public class ClientWorker implements Closeable {
this.isHealthServer = isHealthServer; this.isHealthServer = isHealthServer;
} }
/**
* groupKey -> cacheData.
*/
private final AtomicReference<Map<String, CacheData>> cacheMap = new AtomicReference<Map<String, CacheData>>(
new HashMap<String, CacheData>());
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 { public class ConfigRpcTransportClient extends ConfigTransportClient {
private final BlockingQueue<Object> listenExecutebell = new ArrayBlockingQueue<Object>(1); private final BlockingQueue<Object> listenExecutebell = new ArrayBlockingQueue<Object>(1);
@ -961,7 +975,7 @@ public class ClientWorker implements Closeable {
public ConfigResponse queryConfig(String dataId, String group, String tenant, long readTimeouts, boolean notify) public ConfigResponse queryConfig(String dataId, String group, String tenant, long readTimeouts, boolean notify)
throws NacosException { throws NacosException {
ConfigQueryRequest request = ConfigQueryRequest.build(dataId, group, tenant); ConfigQueryRequest request = ConfigQueryRequest.build(dataId, group, tenant);
request.putHeader("notify", String.valueOf(notify)); request.putHeader(NOTIFY_HEADER, String.valueOf(notify));
RpcClient rpcClient = getOneRunningClient(); RpcClient rpcClient = getOneRunningClient();
if (notify) { if (notify) {
CacheData cacheData = cacheMap.get().get(GroupKey.getKeyTenant(dataId, group, tenant)); CacheData cacheData = cacheMap.get().get(GroupKey.getKeyTenant(dataId, group, tenant));
@ -1053,7 +1067,7 @@ public class ClientWorker implements Closeable {
String group = ((ConfigRemoveRequest) request).getGroup(); String group = ((ConfigRemoveRequest) request).getGroup();
return getResource(tenant, group); return getResource(tenant, group);
} }
return ""; return DEFAULT_RESOURCE;
} }
private String getResource(String tenant, String group) { private String getResource(String tenant, String group) {
@ -1066,7 +1080,7 @@ public class ClientWorker implements Closeable {
if (StringUtils.isNotBlank(tenant)) { if (StringUtils.isNotBlank(tenant)) {
return tenant; return tenant;
} }
return ""; return DEFAULT_RESOURCE;
} }
RpcClient getOneRunningClient() throws NacosException { RpcClient getOneRunningClient() throws NacosException {
@ -1080,11 +1094,11 @@ public class ClientWorker implements Closeable {
try { try {
ConfigPublishRequest request = new ConfigPublishRequest(dataId, group, tenant, content); ConfigPublishRequest request = new ConfigPublishRequest(dataId, group, tenant, content);
request.setCasMd5(casMd5); request.setCasMd5(casMd5);
request.putAdditionalParam("tag", tag); request.putAdditionalParam(TAG_PARAM, tag);
request.putAdditionalParam("appName", appName); request.putAdditionalParam(APP_NAME_PARAM, appName);
request.putAdditionalParam("betaIps", betaIps); request.putAdditionalParam(BETAIPS_PARAM, betaIps);
request.putAdditionalParam("type", type); request.putAdditionalParam(TYPE_PARAM, type);
request.putAdditionalParam("encryptedDataKey", encryptedDataKey); request.putAdditionalParam(ENCRYPTED_DATA_KEY_PARAM, encryptedDataKey);
ConfigPublishResponse response = (ConfigPublishResponse) requestProxy(getOneRunningClient(), request); ConfigPublishResponse response = (ConfigPublishResponse) requestProxy(getOneRunningClient(), request);
if (!response.isSuccess()) { if (!response.isSuccess()) {
LOGGER.warn("[{}] [publish-single] fail, dataId={}, group={}, tenant={}, code={}, msg={}", LOGGER.warn("[{}] [publish-single] fail, dataId={}, group={}, tenant={}, code={}, msg={}",

View File

@ -53,6 +53,14 @@ public abstract class ConfigTransportClient {
private static final Logger LOGGER = LogUtils.logger(ConfigTransportClient.class); 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 encode;
String tenant; String tenant;
@ -108,11 +116,11 @@ public abstract class ConfigTransportClient {
StsCredential stsCredential = getStsCredential(); StsCredential stsCredential = getStsCredential();
accessKey = stsCredential.accessKeyId; accessKey = stsCredential.accessKeyId;
secretKey = stsCredential.accessKeySecret; secretKey = stsCredential.accessKeySecret;
spasHeaders.put("Spas-SecurityToken", stsCredential.securityToken); spasHeaders.put(SECURITY_TOKEN_HEADER, stsCredential.securityToken);
} }
if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotBlank(secretKey)) { if (StringUtils.isNotEmpty(accessKey) && StringUtils.isNotBlank(secretKey)) {
spasHeaders.put("Spas-AccessKey", accessKey); spasHeaders.put(ACCESS_KEY_HEADER, accessKey);
} }
return spasHeaders; return spasHeaders;
} }
@ -145,7 +153,7 @@ public abstract class ConfigTransportClient {
headers.put(Constants.CLIENT_APPNAME_HEADER, ParamUtil.getAppName()); headers.put(Constants.CLIENT_APPNAME_HEADER, ParamUtil.getAppName());
headers.put(Constants.CLIENT_REQUEST_TS_HEADER, ts); headers.put(Constants.CLIENT_REQUEST_TS_HEADER, ts);
headers.put(Constants.CLIENT_REQUEST_TOKEN_HEADER, token); 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); headers.put(Constants.CHARSET_KEY, encode);
return headers; return headers;
} }
@ -165,9 +173,8 @@ public abstract class ConfigTransportClient {
} }
} }
String stsResponse = getStsResponse(); String stsResponse = getStsResponse();
StsCredential stsCredentialTmp = JacksonUtils.toObj(stsResponse, new TypeReference<StsCredential>() { stsCredential = JacksonUtils.toObj(stsResponse, new TypeReference<StsCredential>() {
}); });
stsCredential = stsCredentialTmp;
LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}",
stsCredential.getCode(), stsCredential.getAccessKeyId(), stsCredential.getLastUpdated(), stsCredential.getCode(), stsCredential.getAccessKeyId(), stsCredential.getLastUpdated(),
stsCredential.getExpiration()); stsCredential.getExpiration());

View File

@ -42,6 +42,8 @@ public class Limiter {
private static final Cache<String, RateLimiter> CACHE = CacheBuilder.newBuilder().initialCapacity(CAPACITY_SIZE) private static final Cache<String, RateLimiter> CACHE = CacheBuilder.newBuilder().initialCapacity(CAPACITY_SIZE)
.expireAfterAccess(1, TimeUnit.MINUTES).build(); .expireAfterAccess(1, TimeUnit.MINUTES).build();
private static final String LIMIT_TIME_PROPERTY = "limitTime";
/** /**
* qps 5. * qps 5.
*/ */
@ -49,7 +51,7 @@ public class Limiter {
static { static {
try { try {
String limitTimeStr = System.getProperty("limitTime", String.valueOf(limit)); String limitTimeStr = System.getProperty(LIMIT_TIME_PROPERTY, String.valueOf(limit));
limit = Double.parseDouble(limitTimeStr); limit = Double.parseDouble(limitTimeStr);
LOGGER.info("limitTime:{}", limit); LOGGER.info("limitTime:{}", limit);
} catch (Exception e) { } catch (Exception e) {

View File

@ -39,6 +39,34 @@ public class LocalConfigInfoProcessor {
private static final Logger LOGGER = LogUtils.logger(LocalConfigInfoProcessor.class); 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) { public static String getFailover(String serverName, String dataId, String group, String tenant) {
File localPath = getFailoverFile(serverName, dataId, group, tenant); File localPath = getFailoverFile(serverName, dataId, group, tenant);
if (!localPath.exists() || !localPath.isFile()) { 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) { public static String getSnapshot(String name, String dataId, String group, String tenant) {
if (!SnapShotSwitch.getIsSnapShot()) { if (!SnapShotSwitch.getIsSnapShot()) {
@ -81,17 +109,8 @@ public class LocalConfigInfoProcessor {
if (JvmUtil.isMultiInstance()) { if (JvmUtil.isMultiInstance()) {
return ConcurrentDiskUtil.getFileContent(file, Constants.ENCODE); return ConcurrentDiskUtil.getFileContent(file, Constants.ENCODE);
} else { } else {
InputStream is = null; try (InputStream is = new FileInputStream(file)) {
try {
is = new FileInputStream(file);
return IoUtils.toString(is, Constants.ENCODE); 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() { public static void cleanAllSnapshot() {
try { try {
@ -148,7 +167,7 @@ public class LocalConfigInfoProcessor {
return; return;
} }
for (File file : files) { for (File file : files) {
if (file.getName().endsWith("_nacos")) { if (file.getName().endsWith(SUFFIX)) {
IoUtils.cleanDirectory(file); IoUtils.cleanDirectory(file);
} }
} }
@ -163,53 +182,37 @@ public class LocalConfigInfoProcessor {
* @param envName env name * @param envName env name
*/ */
public static void cleanEnvSnapshot(String envName) { public static void cleanEnvSnapshot(String envName) {
File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + "_nacos"); File tmp = new File(LOCAL_SNAPSHOT_PATH, envName + SUFFIX);
tmp = new File(tmp, "snapshot"); tmp = new File(tmp, ENV_CHILD);
try { try {
IoUtils.cleanDirectory(tmp); IoUtils.cleanDirectory(tmp);
LOGGER.info("success delete " + envName + "-snapshot"); LOGGER.info("success delete {}-snapshot", envName);
} catch (IOException e) { } catch (IOException e) {
LOGGER.info("fail delete " + envName + "-snapshot, " + e.toString()); LOGGER.warn("fail delete {}-snapshot, exception: ", envName, e);
e.printStackTrace();
} }
} }
static File getFailoverFile(String serverName, String dataId, String group, String tenant) { static File getFailoverFile(String serverName, String dataId, String group, String tenant) {
File tmp = new File(LOCAL_SNAPSHOT_PATH, serverName + "_nacos"); File tmp = new File(LOCAL_SNAPSHOT_PATH, serverName + SUFFIX);
tmp = new File(tmp, "data"); tmp = new File(tmp, FAILOVER_FILE_CHILD_1);
if (StringUtils.isBlank(tenant)) { if (StringUtils.isBlank(tenant)) {
tmp = new File(tmp, "config-data"); tmp = new File(tmp, FAILOVER_FILE_CHILD_2);
} else { } else {
tmp = new File(tmp, "config-data-tenant"); tmp = new File(tmp, FAILOVER_FILE_CHILD_3);
tmp = new File(tmp, tenant); tmp = new File(tmp, tenant);
} }
return new File(new File(tmp, group), dataId); return new File(new File(tmp, group), dataId);
} }
static File getSnapshotFile(String envName, String dataId, String group, String tenant) { 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)) { if (StringUtils.isBlank(tenant)) {
tmp = new File(tmp, "snapshot"); tmp = new File(tmp, SNAPSHOT_FILE_CHILD_1);
} else { } else {
tmp = new File(tmp, "snapshot-tenant"); tmp = new File(tmp, SNAPSHOT_FILE_CHILD_2);
tmp = new File(tmp, tenant); tmp = new File(tmp, tenant);
} }
return new File(new File(tmp, group), dataId); 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);
}
} }

View File

@ -29,7 +29,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
/** /**
* 密文数据密钥EncryptedDataKey的本地快照容灾目录相关. * Encrypted data key (EncryptedDataKey) local snapshot, disaster recovery directory related.
* *
* @author luyanbo(RobberPhex) * @author luyanbo(RobberPhex)
*/ */
@ -37,8 +37,22 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor {
private static final Logger LOGGER = LogUtils.logger(LocalEncryptedDataKeyProcessor.class); 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";
/** /**
* 获取容灾配置的 EncryptedDataKeyNULL表示没有本地文件或抛出异常. * 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) { public static String getEncryptDataKeyFailover(String envName, String dataId, String group, String tenant) {
File file = getEncryptDataKeyFailoverFile(envName, dataId, group, tenant); File file = getEncryptDataKeyFailoverFile(envName, dataId, group, tenant);
@ -55,7 +69,7 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor {
} }
/** /**
* 获取本地缓存文件的 EncryptedDataKeyNULL表示没有本地文件或抛出异常. * 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) { public static String getEncryptDataKeySnapshot(String envName, String dataId, String group, String tenant) {
if (!SnapShotSwitch.getIsSnapShot()) { 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, public static void saveEncryptDataKeySnapshot(String envName, String dataId, String group, String tenant,
String encryptDataKey) { String encryptDataKey) {
@ -110,13 +124,13 @@ public class LocalEncryptedDataKeyProcessor extends LocalConfigInfoProcessor {
} }
private static File getEncryptDataKeyFailoverFile(String envName, String dataId, String group, String tenant) { private static File getEncryptDataKeyFailoverFile(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);
tmp = new File(tmp, "encrypted-data-key"); tmp = new File(tmp, FAILOVER_CHILD_1);
if (StringUtils.isBlank(tenant)) { if (StringUtils.isBlank(tenant)) {
tmp = new File(tmp, "failover"); tmp = new File(tmp, FAILOVER_CHILD_2);
} else { } else {
tmp = new File(tmp, "failover-tenant"); tmp = new File(tmp, FAILOVER_CHILD_3);
tmp = new File(tmp, tenant); 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) { private static File getEncryptDataKeySnapshotFile(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);
tmp = new File(tmp, "encrypted-data-key"); tmp = new File(tmp, SNAPSHOT_CHILD_1);
if (StringUtils.isBlank(tenant)) { if (StringUtils.isBlank(tenant)) {
tmp = new File(tmp, "snapshot"); tmp = new File(tmp, SNAPSHOT_CHILD_2);
} else { } else {
tmp = new File(tmp, "snapshot-tenant"); tmp = new File(tmp, SNAPSHOT_CHILD_3);
tmp = new File(tmp, tenant); tmp = new File(tmp, tenant);
} }

View File

@ -31,8 +31,10 @@ import java.util.Properties;
*/ */
public class PropertiesChangeParser extends AbstractConfigChangeParser { public class PropertiesChangeParser extends AbstractConfigChangeParser {
private static final String CONFIG_TYPE = "properties";
public PropertiesChangeParser() { public PropertiesChangeParser() {
super("properties"); super(CONFIG_TYPE);
} }
@Override @Override

View File

@ -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<String> serverUrls = new ArrayList<String>();
private volatile String currentServerAddr;
private Iterator<String> iterator;
public String serverPort = ParamUtil.getDefaultServerPort();
public String addressServerUrl;
private String serverAddrsStr;
public ServerListManager() { public ServerListManager() {
this.isFixed = false; this.isFixed = false;
this.isStarted = false; this.isStarted = false;
@ -418,8 +464,7 @@ public class ServerListManager implements Closeable {
return currentServerAddr; return currentServerAddr;
} }
try { try {
String next = iterator.next(); return iterator.next();
return next;
} catch (Exception e) { } catch (Exception e) {
//No nothing. //No nothing.
} }
@ -460,52 +505,6 @@ public class ServerListManager implements Closeable {
return tenant; 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<String> serverUrls = new ArrayList<String>();
private volatile String currentServerAddr;
private Iterator<String> iterator;
public String serverPort = ParamUtil.getDefaultServerPort();
public String addressServerUrl;
private String serverAddrsStr;
/** /**
* Sort the address list, with the same room priority. * Sort the address list, with the same room priority.
*/ */

View File

@ -28,24 +28,34 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* 适配spas接口. * adapt spas interface.
* *
* @author Nacos * @author Nacos
*/ */
public class SpasAdapter { 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<String, String> getSignHeaders(String resource, String secretKey) { public static Map<String, String> getSignHeaders(String resource, String secretKey) {
Map<String, String> header = new HashMap<String, String>(2); Map<String, String> header = new HashMap<String, String>(2);
String timeStamp = String.valueOf(System.currentTimeMillis()); String timeStamp = String.valueOf(System.currentTimeMillis());
header.put("Timestamp", timeStamp); header.put(TIMESTAMP_HEADER, timeStamp);
if (secretKey != null) { if (secretKey != null) {
String signature = ""; String signature;
if (StringUtils.isBlank(resource)) { if (StringUtils.isBlank(resource)) {
signature = signWithHmacSha1Encrypt(timeStamp, secretKey); signature = signWithHmacSha1Encrypt(timeStamp, secretKey);
} else { } else {
signature = signWithHmacSha1Encrypt(resource + "+" + timeStamp, secretKey); signature = signWithHmacSha1Encrypt(resource + "+" + timeStamp, secretKey);
} }
header.put("Spas-Signature", signature); header.put(SIGNATURE_HEADER, signature);
} }
return header; return header;
} }
@ -104,22 +114,18 @@ public class SpasAdapter {
public static String signWithHmacSha1Encrypt(String encryptText, String encryptKey) { public static String signWithHmacSha1Encrypt(String encryptText, String encryptKey) {
try { try {
byte[] data = encryptKey.getBytes(Constants.ENCODE); byte[] data = encryptKey.getBytes(Constants.ENCODE);
// 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称 // 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, "HmacSHA1"); SecretKey secretKey = new SecretKeySpec(data, SHA_ENCRYPT);
// 生成一个指定 Mac 算法 Mac 对象 // Generate a Mac object specifying Mac algorithm
Mac mac = Mac.getInstance("HmacSHA1"); Mac mac = Mac.getInstance(SHA_ENCRYPT);
// 用给定密钥初始化 Mac 对象 // Initialize the Mac object with the given key
mac.init(secretKey); mac.init(secretKey);
byte[] text = encryptText.getBytes(Constants.ENCODE); byte[] text = encryptText.getBytes(Constants.ENCODE);
byte[] textFinal = mac.doFinal(text); 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); return new String(Base64.encodeBase64(textFinal), Constants.ENCODE);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("signWithhmacSHA1Encrypt fail", e); throw new RuntimeException("signWithhmacSHA1Encrypt fail", e);
} }
} }
private static final String GROUP_KEY = "group";
public static final String TENANT_KEY = "tenant";
} }

View File

@ -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 INVALID_CONSTRUCTOR_ERROR_INFO = "could not determine a constructor for the tag";
private static final String CONFIG_TYPE = "yaml";
public YmlChangeParser() { public YmlChangeParser() {
super("yaml"); super(CONFIG_TYPE);
} }
@Override @Override