parent
5200e1a4ea
commit
c958eda4a7
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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…
Reference in new issue