mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
style(nacos): Standard code style.
This commit is contained in:
@@ -27,15 +27,17 @@ import org.springframework.context.ApplicationContextAware;
|
||||
*/
|
||||
public class NacosConfigManager implements ApplicationContextAware {
|
||||
|
||||
private ConfigService configService;
|
||||
private ConfigService configService;
|
||||
|
||||
public ConfigService getConfigService() {
|
||||
return configService;
|
||||
}
|
||||
public ConfigService getConfigService() {
|
||||
return configService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
NacosConfigProperties properties = applicationContext.getBean(NacosConfigProperties.class);
|
||||
configService = properties.configServiceInstance();
|
||||
}
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
NacosConfigProperties properties = applicationContext
|
||||
.getBean(NacosConfigProperties.class);
|
||||
configService = properties.configServiceInstance();
|
||||
}
|
||||
}
|
||||
|
@@ -64,17 +64,19 @@ public class NacosConfigProperties {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.overrideFromEnv();
|
||||
}
|
||||
|
||||
|
||||
private void overrideFromEnv() {
|
||||
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||
String serverAddr = environment.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
||||
if(StringUtils.isEmpty(serverAddr)) {
|
||||
serverAddr = environment.resolvePlaceholders("${spring.cloud.nacos.server-addr}");
|
||||
String serverAddr = environment
|
||||
.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
||||
if (StringUtils.isEmpty(serverAddr)) {
|
||||
serverAddr = environment
|
||||
.resolvePlaceholders("${spring.cloud.nacos.server-addr}");
|
||||
}
|
||||
this.setServerAddr(serverAddr);
|
||||
}
|
||||
@@ -110,25 +112,26 @@ public class NacosConfigProperties {
|
||||
private int timeout = 3000;
|
||||
|
||||
/**
|
||||
* nacos maximum number of tolerable server reconnection errors.
|
||||
*/
|
||||
* nacos maximum number of tolerable server reconnection errors.
|
||||
*/
|
||||
private String maxRetry;
|
||||
|
||||
/**
|
||||
* nacos get config long poll timeout.
|
||||
*/
|
||||
* nacos get config long poll timeout.
|
||||
*/
|
||||
private String configLongPollTimeout;
|
||||
|
||||
/**
|
||||
* nacos get config failure retry time.
|
||||
*/
|
||||
* nacos get config failure retry time.
|
||||
*/
|
||||
private String configRetryTime;
|
||||
|
||||
/**
|
||||
* If you want to pull it yourself when the program starts to get the configuration for the first time,
|
||||
* and the registered Listener is used for future configuration updates, you can keep the original
|
||||
* code unchanged, just add the system parameter: enableRemoteSyncConfig = "true" ( But there is network overhead);
|
||||
* therefore we recommend that you use {@link ConfigService#getConfigAndSignListener} directly.
|
||||
* If you want to pull it yourself when the program starts to get the configuration
|
||||
* for the first time, and the registered Listener is used for future configuration
|
||||
* updates, you can keep the original code unchanged, just add the system parameter:
|
||||
* enableRemoteSyncConfig = "true" ( But there is network overhead); therefore we
|
||||
* recommend that you use {@link ConfigService#getConfigAndSignListener} directly.
|
||||
*/
|
||||
private boolean enableRemoteSyncConfig = false;
|
||||
|
||||
@@ -163,9 +166,9 @@ public class NacosConfigProperties {
|
||||
*/
|
||||
private String clusterName;
|
||||
|
||||
/**
|
||||
* nacos config dataId name.
|
||||
*/
|
||||
/**
|
||||
* nacos config dataId name.
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
@@ -233,27 +236,27 @@ public class NacosConfigProperties {
|
||||
}
|
||||
|
||||
public void setMaxRetry(String maxRetry) {
|
||||
this.maxRetry = maxRetry;
|
||||
this.maxRetry = maxRetry;
|
||||
}
|
||||
|
||||
public String getConfigLongPollTimeout() {
|
||||
return configLongPollTimeout;
|
||||
return configLongPollTimeout;
|
||||
}
|
||||
|
||||
public void setConfigLongPollTimeout(String configLongPollTimeout) {
|
||||
this.configLongPollTimeout = configLongPollTimeout;
|
||||
this.configLongPollTimeout = configLongPollTimeout;
|
||||
}
|
||||
|
||||
public String getConfigRetryTime() {
|
||||
return configRetryTime;
|
||||
return configRetryTime;
|
||||
}
|
||||
|
||||
public void setConfigRetryTime(String configRetryTime) {
|
||||
this.configRetryTime = configRetryTime;
|
||||
this.configRetryTime = configRetryTime;
|
||||
}
|
||||
|
||||
public Boolean getEnableRemoteSyncConfig() {
|
||||
return enableRemoteSyncConfig;
|
||||
return enableRemoteSyncConfig;
|
||||
}
|
||||
|
||||
public void setEnableRemoteSyncConfig(Boolean enableRemoteSyncConfig) {
|
||||
@@ -416,9 +419,11 @@ public class NacosConfigProperties {
|
||||
properties.put(CONTEXT_PATH, Objects.toString(this.contextPath, ""));
|
||||
properties.put(CLUSTER_NAME, Objects.toString(this.clusterName, ""));
|
||||
properties.put(MAX_RETRY, Objects.toString(this.maxRetry, ""));
|
||||
properties.put(CONFIG_LONG_POLL_TIMEOUT, Objects.toString(this.configLongPollTimeout, ""));
|
||||
properties.put(CONFIG_LONG_POLL_TIMEOUT,
|
||||
Objects.toString(this.configLongPollTimeout, ""));
|
||||
properties.put(CONFIG_RETRY_TIME, Objects.toString(this.configRetryTime, ""));
|
||||
properties.put(ENABLE_REMOTE_SYNC_CONFIG, Objects.toString(this.enableRemoteSyncConfig, ""));
|
||||
properties.put(ENABLE_REMOTE_SYNC_CONFIG,
|
||||
Objects.toString(this.enableRemoteSyncConfig, ""));
|
||||
|
||||
String endpoint = Objects.toString(this.endpoint, "");
|
||||
if (endpoint.contains(":")) {
|
||||
|
@@ -57,7 +57,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private NacosConfigManager nacosConfigManager;
|
||||
|
||||
public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager, NacosConfigProperties nacosConfigProperties) {
|
||||
public NacosPropertySourceLocator(NacosConfigManager nacosConfigManager,
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
this.nacosConfigManager = nacosConfigManager;
|
||||
this.nacosConfigProperties = nacosConfigProperties;
|
||||
}
|
||||
@@ -199,19 +200,16 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
if (stringBuilder.length() > 0) {
|
||||
String result = stringBuilder.substring(0, stringBuilder.length() - 1);
|
||||
throw new IllegalStateException(String.format(
|
||||
"[%s] must end file extension with properties|yaml|yml",
|
||||
result));
|
||||
"[%s] must end file extension with properties|yaml|yml", result));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean canLoadFileExtension(String dataId) {
|
||||
return SUPPORT_FILE_EXTENSION.stream()
|
||||
.anyMatch((fileExtension) -> StringUtils.endsWithIgnoreCase(dataId,
|
||||
fileExtension));
|
||||
return SUPPORT_FILE_EXTENSION.stream().anyMatch(
|
||||
(fileExtension) -> StringUtils.endsWithIgnoreCase(dataId, fileExtension));
|
||||
}
|
||||
|
||||
private boolean checkDataIdIsRefreshable(String refreshDataIds,
|
||||
String sharedDataId) {
|
||||
private boolean checkDataIdIsRefreshable(String refreshDataIds, String sharedDataId) {
|
||||
if (StringUtils.isEmpty(refreshDataIds)) {
|
||||
return false;
|
||||
}
|
||||
|
@@ -42,8 +42,8 @@ public class NacosConfigEndpoint {
|
||||
|
||||
private final NacosRefreshHistory refreshHistory;
|
||||
|
||||
private ThreadLocal<DateFormat> dateFormat = ThreadLocal.withInitial(() ->
|
||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
private ThreadLocal<DateFormat> dateFormat = ThreadLocal
|
||||
.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
public NacosConfigEndpoint(NacosConfigProperties properties,
|
||||
NacosRefreshHistory refreshHistory) {
|
||||
|
@@ -20,18 +20,18 @@ import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = NacosConfigPropertiesServerAddressBothLevelTests.TestConfig.class, properties = {
|
||||
"spring.cloud.nacos.config.server-addr=321,321,321,321:8848",
|
||||
"spring.cloud.nacos.server-addr=123.123.123.123:8848"
|
||||
}, webEnvironment = RANDOM_PORT)
|
||||
"spring.cloud.nacos.server-addr=123.123.123.123:8848" }, webEnvironment = RANDOM_PORT)
|
||||
public class NacosConfigPropertiesServerAddressBothLevelTests {
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetServerAddr() {
|
||||
assertEquals("NacosConfigProperties server address was wrong","321,321,321,321:8848", properties.getServerAddr());
|
||||
assertEquals("NacosConfigProperties server address was wrong",
|
||||
"321,321,321,321:8848", properties.getServerAddr());
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
|
@@ -19,18 +19,18 @@ import com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration;
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = NacosConfigPropertiesServerAddressTopLevelTests.TestConfig.class, properties = {
|
||||
"spring.cloud.nacos.server-addr=123.123.123.123:8848"
|
||||
}, webEnvironment = RANDOM_PORT)
|
||||
"spring.cloud.nacos.server-addr=123.123.123.123:8848" }, webEnvironment = RANDOM_PORT)
|
||||
public class NacosConfigPropertiesServerAddressTopLevelTests {
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties properties;
|
||||
|
||||
|
||||
@Test
|
||||
public void testGetServerAddr() {
|
||||
assertEquals("NacosConfigProperties server address was wrong","123.123.123.123:8848", properties.getServerAddr());
|
||||
assertEquals("NacosConfigProperties server address was wrong",
|
||||
"123.123.123.123:8848", properties.getServerAddr());
|
||||
}
|
||||
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ NacosConfigEndpointAutoConfiguration.class,
|
||||
|
Reference in New Issue
Block a user