1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

format code and change logger to log

This commit is contained in:
flystar32 2019-02-21 11:37:17 +08:00
parent e6c7b10b79
commit f96869e1d2
5 changed files with 141 additions and 136 deletions

View File

@ -27,29 +27,29 @@ import java.util.Map;
*/ */
public class AcmPropertySource extends MapPropertySource { public class AcmPropertySource extends MapPropertySource {
private final String dataId; private final String dataId;
private final Date timestamp; private final Date timestamp;
private final boolean groupLevel; private final boolean groupLevel;
AcmPropertySource(String dataId, Map<String, Object> source, Date timestamp, AcmPropertySource(String dataId, Map<String, Object> source, Date timestamp,
boolean groupLevel) { boolean groupLevel) {
super(dataId, source); super(dataId, source);
this.dataId = dataId; this.dataId = dataId;
this.timestamp = timestamp; this.timestamp = timestamp;
this.groupLevel = groupLevel; this.groupLevel = groupLevel;
} }
public String getDataId() { public String getDataId() {
return dataId; return dataId;
} }
public Date getTimestamp() { public Date getTimestamp() {
return timestamp; return timestamp;
} }
public boolean isGroupLevel() { public boolean isGroupLevel() {
return groupLevel; return groupLevel;
} }
} }

View File

@ -33,64 +33,68 @@ import java.util.*;
*/ */
class AcmPropertySourceBuilder { class AcmPropertySourceBuilder {
private Logger logger = LoggerFactory.getLogger(AcmPropertySourceBuilder.class); private Logger log = LoggerFactory.getLogger(AcmPropertySourceBuilder.class);
/** /**
* 传入 ACM DataId groupID获取到解析后的 AcmProperty 对象 * 传入 ACM DataId groupID获取到解析后的 AcmProperty 对象
* *
* @param dataId * @param dataId
* @param diamondGroup * @param diamondGroup
* @param groupLevel * @param groupLevel
* @return * @return
*/ */
AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) { AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) {
Properties properties = loadDiamondData(dataId, diamondGroup); Properties properties = loadDiamondData(dataId, diamondGroup);
if (properties == null) { if (properties == null) {
return null; return null;
} }
return new AcmPropertySource(dataId, toMap(properties), new Date(), groupLevel); return new AcmPropertySource(dataId, toMap(properties), new Date(), groupLevel);
} }
private Properties loadDiamondData(String dataId, String diamondGroup) { private Properties loadDiamondData(String dataId, String diamondGroup) {
try { try {
String data = ConfigService.getConfig(dataId, diamondGroup, 3000L); String data = ConfigService.getConfig(dataId, diamondGroup, 3000L);
if (StringUtils.isEmpty(data)) { if (StringUtils.isEmpty(data)) {
return null; return null;
} }
if (dataId.endsWith(".properties")) { if (dataId.endsWith(".properties")) {
Properties properties = new Properties(); Properties properties = new Properties();
logger.info(String.format("Loading acm data, dataId: '%s', group: '%s'", log.info(String.format("Loading acm data, dataId: '%s', group: '%s'",
dataId, diamondGroup)); dataId, diamondGroup));
properties.load(new StringReader(data)); properties.load(new StringReader(data));
return properties; return properties;
} else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) { }
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) {
yamlFactory.setResources(new ByteArrayResource(data.getBytes())); YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
return yamlFactory.getObject(); yamlFactory.setResources(new ByteArrayResource(data.getBytes()));
} return yamlFactory.getObject();
} catch (Exception e) { }
if (e instanceof ConfigException) { }
logger.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e); catch (Exception e) {
} else { if (e instanceof ConfigException) {
logger.error("DIAMOND-100500:" + dataId, e); log.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e);
} }
} else {
return null; log.error("DIAMOND-100500:" + dataId, e);
} }
}
return null;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> toMap(Properties properties) { private Map<String, Object> toMap(Properties properties) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
Enumeration<String> keys = (Enumeration<String>)properties.propertyNames(); Enumeration<String> keys = (Enumeration<String>) properties.propertyNames();
while (keys.hasMoreElements()) { while (keys.hasMoreElements()) {
String key = keys.nextElement(); String key = keys.nextElement();
Object value = properties.getProperty(key); Object value = properties.getProperty(key);
if (value != null) { if (value != null) {
result.put(key, ((String)value).trim()); result.put(key, ((String) value).trim());
} else { }
result.put(key, null); else {
} result.put(key, null);
} }
return result; }
} return result;
}
} }

View File

@ -38,40 +38,40 @@ import java.util.Map;
@Endpoint(id = "acm") @Endpoint(id = "acm")
public class AcmEndpoint { public class AcmEndpoint {
private final AcmProperties properties; private final AcmProperties properties;
private final AcmRefreshHistory refreshHistory; private final AcmRefreshHistory refreshHistory;
private final AcmPropertySourceRepository propertySourceRepository; private final AcmPropertySourceRepository propertySourceRepository;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory, public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory,
AcmPropertySourceRepository propertySourceRepository) { AcmPropertySourceRepository propertySourceRepository) {
this.properties = properties; this.properties = properties;
this.refreshHistory = refreshHistory; this.refreshHistory = refreshHistory;
this.propertySourceRepository = propertySourceRepository; this.propertySourceRepository = propertySourceRepository;
} }
@ReadOperation @ReadOperation
public Map<String, Object> invoke() { public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("config", properties); result.put("config", properties);
Map<String, Object> runtime = new HashMap<>(); Map<String, Object> runtime = new HashMap<>();
List<AcmPropertySource> all = propertySourceRepository.getAll(); List<AcmPropertySource> all = propertySourceRepository.getAll();
List<Map<String, Object>> sources = new ArrayList<>(); List<Map<String, Object>> sources = new ArrayList<>();
for (AcmPropertySource ps : all) { for (AcmPropertySource ps : all) {
Map<String, Object> source = new HashMap<>(); Map<String, Object> source = new HashMap<>();
source.put("dataId", ps.getDataId()); source.put("dataId", ps.getDataId());
source.put("lastSynced", dateFormat.format(ps.getTimestamp())); source.put("lastSynced", dateFormat.format(ps.getTimestamp()));
sources.add(source); sources.add(source);
} }
runtime.put("sources", sources); runtime.put("sources", sources);
runtime.put("refreshHistory", refreshHistory.getRecords()); runtime.put("refreshHistory", refreshHistory.getRecords());
result.put("runtime", runtime); result.put("runtime", runtime);
return result; return result;
} }
} }

View File

@ -33,39 +33,40 @@ import java.util.List;
*/ */
public class AcmHealthIndicator extends AbstractHealthIndicator { public class AcmHealthIndicator extends AbstractHealthIndicator {
private final AcmProperties acmProperties; private final AcmProperties acmProperties;
private final AcmPropertySourceRepository acmPropertySourceRepository; private final AcmPropertySourceRepository acmPropertySourceRepository;
private final List<String> dataIds; private final List<String> dataIds;
public AcmHealthIndicator(AcmProperties acmProperties, public AcmHealthIndicator(AcmProperties acmProperties,
AcmPropertySourceRepository acmPropertySourceRepository) { AcmPropertySourceRepository acmPropertySourceRepository) {
this.acmProperties = acmProperties; this.acmProperties = acmProperties;
this.acmPropertySourceRepository = acmPropertySourceRepository; this.acmPropertySourceRepository = acmPropertySourceRepository;
this.dataIds = new ArrayList<>(); this.dataIds = new ArrayList<>();
for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository
.getAll()) { .getAll()) {
this.dataIds.add(acmPropertySource.getDataId()); this.dataIds.add(acmPropertySource.getDataId());
} }
} }
@Override @Override
protected void doHealthCheck(Health.Builder builder) throws Exception { protected void doHealthCheck(Health.Builder builder) throws Exception {
for (String dataId : dataIds) { for (String dataId : dataIds) {
try { try {
String config = ConfigService.getConfig(dataId, acmProperties.getGroup(), String config = ConfigService.getConfig(dataId, acmProperties.getGroup(),
acmProperties.getTimeout()); acmProperties.getTimeout());
if (StringUtils.isEmpty(config)) { if (StringUtils.isEmpty(config)) {
builder.down().withDetail(String.format("dataId: '%s', group: '%s'", builder.down().withDetail(String.format("dataId: '%s', group: '%s'",
dataId, acmProperties.getGroup()), "config is empty"); dataId, acmProperties.getGroup()), "config is empty");
} }
} catch (Exception e) { }
builder.down().withDetail(String.format("dataId: '%s', group: '%s'", catch (Exception e) {
dataId, acmProperties.getGroup()), e.getMessage()); builder.down().withDetail(String.format("dataId: '%s', group: '%s'",
} dataId, acmProperties.getGroup()), e.getMessage());
} }
builder.up().withDetail("dataIds", dataIds); }
} builder.up().withDetail("dataIds", dataIds);
}
} }

View File

@ -49,7 +49,7 @@ import com.alibaba.edas.acm.listener.ConfigChangeListener;
public class AcmContextRefresher public class AcmContextRefresher
implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware { implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware {
private Logger logger = LoggerFactory.getLogger(AcmContextRefresher.class); private Logger log = LoggerFactory.getLogger(AcmContextRefresher.class);
private final ContextRefresher contextRefresher; private final ContextRefresher contextRefresher;
@ -103,7 +103,7 @@ public class AcmContextRefresher
} }
catch (NoSuchAlgorithmException catch (NoSuchAlgorithmException
| UnsupportedEncodingException e) { | UnsupportedEncodingException e) {
logger.warn("unable to get md5 for dataId: " + dataId, e); log.warn("unable to get md5 for dataId: " + dataId, e);
} }
} }
refreshHistory.add(dataId, md5); refreshHistory.add(dataId, md5);