From 666cda6e93c6bacead68da51ec973162a4823e10 Mon Sep 17 00:00:00 2001 From: Giorno Date: Thu, 28 Jul 2022 17:41:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0getProperty=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../support/service/impl/ConfigServiceImpl.java | 2 +- .../com/java3y/austin/support/utils/NacosUtils.java | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/austin-support/src/main/java/com/java3y/austin/support/service/impl/ConfigServiceImpl.java b/austin-support/src/main/java/com/java3y/austin/support/service/impl/ConfigServiceImpl.java index 61ec9b9..c14a948 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/service/impl/ConfigServiceImpl.java +++ b/austin-support/src/main/java/com/java3y/austin/support/service/impl/ConfigServiceImpl.java @@ -47,7 +47,7 @@ public class ConfigServiceImpl implements ConfigService { Config config = com.ctrip.framework.apollo.ConfigService.getConfig(namespaces.split(StrUtil.COMMA)[0]); return config.getProperty(key, defaultValue); } else if (enableNacos) { - return nacosUtils.getProperty(key); + return nacosUtils.getProperty(key, defaultValue); } else { return props.getProperty(key, defaultValue); } diff --git a/austin-support/src/main/java/com/java3y/austin/support/utils/NacosUtils.java b/austin-support/src/main/java/com/java3y/austin/support/utils/NacosUtils.java index e6d8202..dc6a54a 100644 --- a/austin-support/src/main/java/com/java3y/austin/support/utils/NacosUtils.java +++ b/austin-support/src/main/java/com/java3y/austin/support/utils/NacosUtils.java @@ -1,9 +1,11 @@ package com.java3y.austin.support.utils; +import cn.hutool.core.util.StrUtil; import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.exception.NacosException; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; @@ -31,16 +33,17 @@ public class NacosUtils { private final Properties request = new Properties(); private final Properties properties = new Properties(); - public String getProperty(String key) { + public String getProperty(String key, String defaultValue) { try { String property = this.getContext(); if (StringUtils.hasText(property)) { properties.load(new StringReader(property)); } } 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() { @@ -51,7 +54,7 @@ public class NacosUtils { context = NacosFactory.createConfigService(request) .getConfig(nacosDataId, nacosGroup, 5000); } catch (NacosException e) { - log.error("Nacos error:{}", e.getMessage()); + log.error("Nacos error:{}", ExceptionUtils.getStackTrace(e)); } return context; }