Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encode配置加载 #340

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ public static class Config {

private String password;

private String encode;

public String getUsername() {
return username;
}
Expand Down Expand Up @@ -501,6 +503,14 @@ public void setEnableRemoteSyncConfig(boolean enableRemoteSyncConfig) {
this.enableRemoteSyncConfig = enableRemoteSyncConfig;
}

public String getEncode() {
return encode;
}

public void setEncode(String encode) {
this.encode = encode;
}

@Override
public String toString() {
final StringBuffer sb = new StringBuffer("Config{");
Expand All @@ -514,6 +524,7 @@ public String toString() {
sb.append(", dataIds='").append(dataIds).append('\'');
sb.append(", group='").append(group).append('\'');
sb.append(", type=").append(type);
sb.append(", encode='").append(encode).append('\'');
sb.append(", maxRetry='").append(maxRetry).append('\'');
sb.append(", configLongPollTimeout='").append(configLongPollTimeout)
.append('\'');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public Properties buildGlobalNacosProperties(ConfigurableEnvironment environment
nacosConfigProperties.getMaxRetry(),
nacosConfigProperties.getContextPath(),
nacosConfigProperties.isEnableRemoteSyncConfig(),
nacosConfigProperties.getUsername(), nacosConfigProperties.getPassword());
nacosConfigProperties.getUsername(), nacosConfigProperties.getPassword(),
nacosConfigProperties.getEncode());
}

private Properties buildSubNacosProperties(ConfigurableEnvironment environment, Properties globalProperties,
Expand All @@ -102,7 +103,7 @@ private Properties buildSubNacosProperties(ConfigurableEnvironment environment,
config.getSecretKey(), config.getAccessKey(), config.getRamRoleName(),
config.getConfigLongPollTimeout(), config.getConfigRetryTime(),
config.getMaxRetry(),null, config.isEnableRemoteSyncConfig(),
config.getUsername(), config.getPassword());
config.getUsername(), config.getPassword(), config.getEncode());
NacosPropertiesBuilder.merge(sub, globalProperties);
return sub;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.alibaba.boot.nacos.config.util;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Properties;

Expand All @@ -31,11 +33,20 @@
*/
public class NacosPropertiesBuilder {

public static String unifiedCodingName(String encode) {
Charset charset = StandardCharsets.UTF_8;
if(encode == null){
return charset.name();
}
charset = Charset.forName(encode);
return charset.name();
}

public static Properties buildNacosProperties(Environment environment,
String serverAddr, String namespaceId, String endpoint, String secretKey,
String accessKey, String ramRoleName, String configLongPollTimeout,
String configRetryTimeout, String maxRetry,String contextPath, boolean enableRemoteSyncConfig,
String username, String password) {
String username, String password, String encode) {
Properties properties = new Properties();
processPropertiesData(properties,environment,serverAddr,PropertyKeyConst.SERVER_ADDR);
processPropertiesData(properties,environment,namespaceId,PropertyKeyConst.NAMESPACE);
Expand All @@ -49,12 +60,13 @@ public static Properties buildNacosProperties(Environment environment,
processPropertiesData(properties,environment,maxRetry,PropertyKeyConst.MAX_RETRY);
processPropertiesData(properties,environment,username,PropertyKeyConst.USERNAME);
processPropertiesData(properties,environment,password,PropertyKeyConst.PASSWORD);

processPropertiesData(properties,environment,unifiedCodingName(encode),PropertyKeyConst.ENCODE);

properties.put(PropertyKeyConst.ENABLE_REMOTE_SYNC_CONFIG,
String.valueOf(enableRemoteSyncConfig));
return properties;
}

private static void processPropertiesData(Properties properties,Environment environment,String keyword,String key) {
if (StringUtils.isNotBlank(keyword)) {
properties.put(key ,environment.resolvePlaceholders(keyword));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void setup() {
nacosConfigProperties.setGroup("group01");
nacosConfigProperties.setAutoRefresh(true);
nacosConfigProperties.setEndpoint("localhost");
nacosConfigProperties.setEncode("utf8");
globalProperties = new Properties();
globalProperties.setProperty("maxRetry","3");
globalProperties.setProperty("content","key=01");
Expand All @@ -104,7 +105,7 @@ public void buildGlobalNacosProperties() {
Properties properties = nacosConfigLoader.buildGlobalNacosProperties(environment, nacosConfigProperties);
LOGGER.info("buildGlobalNacosProperties properties : {}", properties);
Assert.assertNotNull(properties);
Assert.assertEquals(properties.size(), 6);
Assert.assertEquals(properties.size(), 7);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public void testBuildNacosProperties() {
String enableRemoteSyncConfig = "enableRemoteSyncConfig";
String username = "nacos";
String password = "password";
String encode = "utf8";
Properties properties = NacosPropertiesBuilder.buildNacosProperties(environment, serverAddr, namespaceId, secretKey,
"ak", ramRoleName, configLongPollTimeout, configRetryTimeout, maxRetry, null,enableRemoteSyncConfig, true,
username, password);
Assert.assertEquals(properties.size(), 12);
username, password, encode);
Assert.assertEquals(properties.size(), 13);
Assert.assertEquals(properties.get("serverAddr"), "localhost");
}

Expand Down