Skip to content

Commit

Permalink
dynamic config optimization
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <1449308021@qq.com>
  • Loading branch information
daizhenyu committed Jul 26, 2024
1 parent 31b0b97 commit 3502eb0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,12 @@ private Properties createProperties(String connectString, int sessionTimeout, St
LOGGER.log(Level.SEVERE, "Nacos username, password or privateKey is Empty");
return properties;
}
Optional<String> userNameOptinal = AesUtil.decrypt(CONFIG.getPrivateKey(), userName);
Optional<String> passWordOptinal = AesUtil.decrypt(CONFIG.getPrivateKey(), password);
if (!userNameOptinal.isPresent() || !passWordOptinal.isPresent()) {
LOGGER.log(Level.SEVERE, "Nacos username and password parsing failed");
if (!passWordOptinal.isPresent()) {
LOGGER.log(Level.SEVERE, "Nacos password parsing failed");
return properties;
}
properties.setProperty(PropertyKeyConst.USERNAME, userNameOptinal.get());
properties.setProperty(PropertyKeyConst.USERNAME, userName);
properties.setProperty(PropertyKeyConst.PASSWORD, passWordOptinal.get());
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.sermant.implement.service.dynamicconfig.ConfigClient;
import io.sermant.implement.service.dynamicconfig.common.DynamicConstants;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand Down Expand Up @@ -74,6 +73,8 @@ public class NacosClient implements ConfigClient {

private static final long DEFAULT_REQUEST_TIMEOUT = 6000L;

private static final int SUCCESS_CODE = 200;

/**
* Nacos refresh window for http request security authentication token
*/
Expand Down Expand Up @@ -296,12 +297,12 @@ private String buildUrl(String key, String group, String namespace, boolean exac
private String getToken() {
if ((System.currentTimeMillis() - lastRefreshTime) >= TimeUnit.SECONDS
.toMillis(tokenTtl - TOKEN_REFRESH_WINDOW)) {
lastRefreshTime = System.currentTimeMillis();
HttpLoginProcessor httpLoginProcessor = new HttpLoginProcessor(
NamingHttpClientManager.getInstance().getNacosRestTemplate());
LoginIdentityContext loginIdentityContext = httpLoginProcessor.getResponse(properties);
lastToken = loginIdentityContext.getParameter(KEY_ACCESS_TOKEN);
tokenTtl = Long.parseLong(loginIdentityContext.getParameter(KEY_TOKEN_TTL));
lastRefreshTime = System.currentTimeMillis();
}
return lastToken;
}
Expand All @@ -314,7 +315,6 @@ private String getToken() {
* @throws IOException IO exception during service invocation process
*/
private String doRequest(String url) throws IOException {
String result;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
int timeOut = Integer.parseInt(properties.getProperty(PropertyKeyConst.CONFIG_LONG_POLL_TIMEOUT));
RequestConfig requestConfig = RequestConfig.custom()
Expand All @@ -325,10 +325,12 @@ private String doRequest(String url) throws IOException {
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
if (response.getStatusLine().getStatusCode() == SUCCESS_CODE) {
return EntityUtils.toString(response.getEntity());
}
LOGGER.error("Http Request error, the message is: {}", EntityUtils.toString(response.getEntity()));
return "";
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,13 @@ private Properties getProperties() {
LOGGER.log(Level.SEVERE, "Nacos username, password or privateKey is Empty");
return properties;
}
Optional<String> userName = AesUtil.decrypt(CONFIG.getPrivateKey(), CONFIG.getUserName());
Optional<String> passWord = AesUtil.decrypt(CONFIG.getPrivateKey(), CONFIG.getPassword());
if (!userName.isPresent() || !passWord.isPresent()) {
LOGGER.log(Level.SEVERE, "Nacos username and password parsing failed");
if (!passWord.isPresent()) {
LOGGER.log(Level.SEVERE, "Nacos password parsing failed");
return properties;
}
properties.setProperty(KEY_SERVER, CONFIG.getServerAddress());
properties.setProperty(PropertyKeyConst.USERNAME, userName.get());
properties.setProperty(PropertyKeyConst.USERNAME, CONFIG.getUserName());
properties.setProperty(PropertyKeyConst.PASSWORD, passWord.get());
return properties;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ public void startMonitorServer() {
String key = monitorServiceConfig.getKey();
if (StringUtils.isNoneBlank(key) && StringUtils.isNoneBlank(monitorServiceConfig.getUserName())
&& StringUtils.isNoneBlank(monitorServiceConfig.getPassword())) {
Optional<String> userNameOptional = AesUtil.decrypt(key, monitorServiceConfig.getUserName());
Optional<String> passwordOptional = AesUtil.decrypt(key, monitorServiceConfig.getPassword());
if (userNameOptional.isPresent() && passwordOptional.isPresent()) {
builder.withAuthenticator(new MonitorAuthenticator("", userNameOptional.get(),
if (passwordOptional.isPresent()) {
builder.withAuthenticator(new MonitorAuthenticator("", monitorServiceConfig.getUserName(),
passwordOptional.get()));
}
}
Expand Down

0 comments on commit 3502eb0

Please sign in to comment.