1、项目设置允许读取Json格式
@NacosPropertySource(dataId = "testReportConf", groupId = CommonConstant.PROJECT_NAME, autoRefreshed = true, type = ConfigType.JSON)
@NacosPropertySource(dataId = "application", groupId = "groupId", autoRefreshed = true)
@NacosPropertySource(dataId = "testReportConf", groupId = "groupId", autoRefreshed = true, type = ConfigType.JSON)
@EnableScheduling
@ComponentScan({"com.xm.autotestplatform", "com.xm"})
@SpringBootApplication
public class AutoTestPlatformApplication {
public static void main(String[] args) {
SpringApplication.run(AutoTestPlatformApplication.class, args);
}
}
2、编写一个监听器用于监听Nacos配置的更改
import com.alibaba.boot.nacos.config.properties.NacosConfigProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.google.gson.Gson;
import com.xm.autotestplatform.config.nacos.NaCosJsonData;
import com.xm.autotestplatform.config.nacos.NacosHelper;
import com.xm.autotestplatform.utils.ThreadPoolFactory;
import com.xm.constants.CommonConstant;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Properties;
import java.util.concurrent.Executor;
@Component
@Slf4j
@Data
public class TestReportConfListenerAgent implements InitializingBean {
@Resource
private NacosConfigProperties nacosConfigProperties;
public ConfigService configService;
@Override
public void afterPropertiesSet() {
configService = getConfigService();
this.listener();
}
public void listener() {
Listener listener = new Listener() {
@Override
public Executor getExecutor() {
return ThreadPoolFactory.getCommonPoll();
}
@SneakyThrows
@Override
public void receiveConfigInfo(String configInfo) {
log.info("configInfo:{}", configInfo);
String jsonConfig = configService.getConfig("testReportConf", "autotestplatform", 3000);
Gson gson = new Gson();
NaCosJsonData naCosJsonData = gson.fromJson(jsonConfig, NaCosJsonData.class);
NacosHelper.setNaCosJsonData(naCosJsonData);
}
};
try {
// 监听
configService.addListener("testReportConf", nacosConfigProperties.getGroup(), listener);
} catch (NacosException e) {
e.printStackTrace();
}
}
public void publishConfig(String msg) {
try {
configService.publishConfig(CommonConstant.AGENT_LOG_DATAID,nacosConfigProperties.getGroup(),msg);
configService.publishConfig("testReportConf",nacosConfigProperties.getGroup(),msg);
} catch (NacosException e) {
e.printStackTrace();
}
}
/**
* 获取ConfigService
*
* @return
*/
public ConfigService getConfigService() {
String serverAddr = nacosConfigProperties.getServerAddr();
String nameSpace = nacosConfigProperties.getNamespace();
String username = nacosConfigProperties.getUsername();
String password = nacosConfigProperties.getPassword();
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
properties.put(PropertyKeyConst.NAMESPACE, nameSpace);
properties.put(PropertyKeyConst.USERNAME, username);
properties.put(PropertyKeyConst.PASSWORD, password);
ConfigService configService = null;
try {
configService = NacosFactory.createConfigService(properties);
} catch (NacosException e) {
e.printStackTrace();
}
return configService;
}
}
3、上面 NacosHelper类如下 (作用是:监听器监听到值改变时唯一实例同步改变)
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
2024/3/21 下午1:52
**/
@Component
public class NacosHelper {
@Resource
private NaCosJsonData nacosJsonData;
private static NaCosJsonData staticNaCosJsonData;
@PostConstruct
public void init() {
staticNaCosJsonData = nacosJsonData;
}
public static void setNaCosJsonData(NaCosJsonData naCosJsonData) {
NacosHelper.staticNaCosJsonData = naCosJsonData;
}
public static NaCosJsonData getNaCosJsonData() {
return staticNaCosJsonData;
}
}
4、将页面上配置的Json值映射成对象(加注解 @ConfigurationProperties)
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties
@Data
public class NaCosJsonData {
private ExampleConfig example;
private String userName;
@Data
public static class ExampleConfig {
private String message;
private String timestamp;
private List<DataItem> arrayListName; // 映射data数组
}
}
package com.xm.autotestplatform.config.nacos;
import lombok.Data;
@Data
public class DataItem {
private String name;
private String value;
}
页面配置的Json值下图
![](https://i-blog.csdnimg.cn/blog_migrate/0a3f93bcad06e85b9e267f3934980fdb.png)