feat:NacosGetProperty

master
Giorno 3 years ago
parent 5200e1a4ea
commit c958eda4a7

@ -103,6 +103,11 @@
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
</dependency>
<!--nacos-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
</dependency>
</dependencies>

@ -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);
}

@ -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;
}
}

@ -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

@ -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
Loading…
Cancel
Save