feat:添加getProperty默认值

pull/13/head
Giorno 2 years ago
parent 5a52aa1480
commit 666cda6e93

@ -47,7 +47,7 @@ public class ConfigServiceImpl implements ConfigService {
Config config = com.ctrip.framework.apollo.ConfigService.getConfig(namespaces.split(StrUtil.COMMA)[0]); Config config = com.ctrip.framework.apollo.ConfigService.getConfig(namespaces.split(StrUtil.COMMA)[0]);
return config.getProperty(key, defaultValue); return config.getProperty(key, defaultValue);
} else if (enableNacos) { } else if (enableNacos) {
return nacosUtils.getProperty(key); return nacosUtils.getProperty(key, defaultValue);
} else { } else {
return props.getProperty(key, defaultValue); return props.getProperty(key, defaultValue);
} }

@ -1,9 +1,11 @@
package com.java3y.austin.support.utils; package com.java3y.austin.support.utils;
import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -31,16 +33,17 @@ public class NacosUtils {
private final Properties request = new Properties(); private final Properties request = new Properties();
private final Properties properties = new Properties(); private final Properties properties = new Properties();
public String getProperty(String key) { public String getProperty(String key, String defaultValue) {
try { try {
String property = this.getContext(); String property = this.getContext();
if (StringUtils.hasText(property)) { if (StringUtils.hasText(property)) {
properties.load(new StringReader(property)); properties.load(new StringReader(property));
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Nacos error:{}", e.getMessage()); log.error("Nacos error:{}", ExceptionUtils.getStackTrace(e));
} }
return properties.getProperty(key); String property = properties.getProperty(key);
return StrUtil.isBlank(property) ? defaultValue : property;
} }
private String getContext() { private String getContext() {
@ -51,7 +54,7 @@ public class NacosUtils {
context = NacosFactory.createConfigService(request) context = NacosFactory.createConfigService(request)
.getConfig(nacosDataId, nacosGroup, 5000); .getConfig(nacosDataId, nacosGroup, 5000);
} catch (NacosException e) { } catch (NacosException e) {
log.error("Nacos error:{}", e.getMessage()); log.error("Nacos error:{}", ExceptionUtils.getStackTrace(e));
} }
return context; return context;
} }

Loading…
Cancel
Save