From c958eda4a7f62df17d12f36e9c169d4d6e6ee940 Mon Sep 17 00:00:00 2001 From: Giorno Date: Thu, 28 Jul 2022 14:45:44 +0800 Subject: [PATCH] feat:NacosGetProperty --- austin-support/pom.xml | 5 ++ .../service/impl/ConfigServiceImpl.java | 11 ++-- .../austin/support/utils/NacosUtils.java | 58 +++++++++++++++++++ .../src/main/resources/application.properties | 7 ++- austin-web/src/main/resources/bootstrap.yml | 18 ------ 5 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 austin-support/src/main/java/com/java3y/austin/support/utils/NacosUtils.java delete mode 100644 austin-web/src/main/resources/bootstrap.yml diff --git a/austin-support/pom.xml b/austin-support/pom.xml index 29a3619..f6dfb2b 100644 --- a/austin-support/pom.xml +++ b/austin-support/pom.xml @@ -103,6 +103,11 @@ org.springframework.amqp spring-rabbit + + + com.alibaba.boot + nacos-config-spring-boot-starter + 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 509cac0..61ec9b9 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 @@ -4,11 +4,11 @@ import cn.hutool.core.util.StrUtil; import cn.hutool.setting.dialect.Props; import com.ctrip.framework.apollo.Config; import com.java3y.austin.support.service.ConfigService; +import com.java3y.austin.support.utils.NacosUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.stereotype.Service; -import javax.annotation.Resource; import java.nio.charset.StandardCharsets; @@ -37,8 +37,9 @@ public class ConfigServiceImpl implements ConfigService { */ @Value("${austin.nacos.enabled}") private Boolean enableNacos; - @Resource - private ConfigurableApplicationContext configurableApplicationContext; + @Autowired + private NacosUtils nacosUtils; + @Override public String getProperty(String key, String defaultValue) { @@ -46,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 configurableApplicationContext.getEnvironment().getProperty(key, defaultValue); + return nacosUtils.getProperty(key); } 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 new file mode 100644 index 0000000..e6d8202 --- /dev/null +++ b/austin-support/src/main/java/com/java3y/austin/support/utils/NacosUtils.java @@ -0,0 +1,58 @@ +package com.java3y.austin.support.utils; + +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.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import java.io.StringReader; +import java.util.Properties; + +/** + * @program: austin + * @description: + * @author: Giorno + * @create: 2022-07-28 + **/ +@Slf4j +@Component +public class NacosUtils { + @Value("${austin.nacos.server}") + private String nacosServer; + @Value("${austin.nacos.group}") + private String nacosGroup; + @Value("${austin.nacos.dataId}") + private String nacosDataId; + @Value("${austin.nacos.namespace}") + private String nacosNamespace; + private final Properties request = new Properties(); + private final Properties properties = new Properties(); + + public String getProperty(String key) { + try { + String property = this.getContext(); + if (StringUtils.hasText(property)) { + properties.load(new StringReader(property)); + } + } catch (Exception e) { + log.error("Nacos error:{}", e.getMessage()); + } + return properties.getProperty(key); + } + + private String getContext() { + String context = null; + try { + request.put(PropertyKeyConst.SERVER_ADDR, nacosServer); + request.put(PropertyKeyConst.NAMESPACE, nacosNamespace); + context = NacosFactory.createConfigService(request) + .getConfig(nacosDataId, nacosGroup, 5000); + } catch (NacosException e) { + log.error("Nacos error:{}", e.getMessage()); + } + return context; + } +} diff --git a/austin-web/src/main/resources/application.properties b/austin-web/src/main/resources/application.properties index cc02ddb..0d9d68b 100644 --- a/austin-web/src/main/resources/application.properties +++ b/austin-web/src/main/resources/application.properties @@ -129,8 +129,11 @@ apollo.bootstrap.enabled=${apollo.enabled} apollo.bootstrap.namespaces=boss.austin,dynamic-tp-apollo-dtp.yml ##################### nacos ##################### -austin.nacos.enabled=false - +austin.nacos.enabled=true +austin.nacos.server= +austin.nacos.dataId= +austin.nacos.group= +austin.nacos.namespace= ##################### httpUtils properties ##################### ok.http.connect-timeout=30 diff --git a/austin-web/src/main/resources/bootstrap.yml b/austin-web/src/main/resources/bootstrap.yml deleted file mode 100644 index 26ffd13..0000000 --- a/austin-web/src/main/resources/bootstrap.yml +++ /dev/null @@ -1,18 +0,0 @@ -spring: - application: - name: - profiles: - active: - cloud: - nacos: - # 配置中心 - config: - server-addr: - username: - password: - namespace: ${spring.profiles.active} - file-extension: yaml - extension-configs: - - dataId: ${spring.profiles.active}.yaml - group: ${spring.profiles.active} - refresh: true \ No newline at end of file