1.maven pom依赖
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>1.1.1</version>
</dependency>
2.配置nacos初始化配置
@Configuration
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "http://127.0.0.1:8848", username="nacos",password = "nacos"))
@NacosPropertySources({
@NacosPropertySource(dataId = "example", autoRefreshed = true,properties =@NacosProperties(namespace = "") ),
@NacosPropertySource(dataId = "arthas",groupId = "sms-admin",autoRefreshed = true,properties =@NacosProperties(namespace = "015ae736-f567-4633-b6ce-62d2adf0970a") ),
@NacosPropertySource(dataId = "arthas",groupId = "sms-channel",autoRefreshed = true,properties =@NacosProperties(namespace = "f114336d-d549-44bc-bd57-4f41ba5a8347") )
})
public class NacosConfig {
}
3.@NacosValue读取
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;
@NacosValue(value = "${testnumber:-1}", autoRefreshed = true)
private Integer testNumber;
4.@NacosConfigurationProperties
@Component
@NacosConfigurationProperties(prefix = "arthas",dataId = "arthas",groupId = "sms-admin",autoRefreshed = true,properties =@NacosProperties(namespace = "015ae736-f567-4633-b6ce-62d2adf0970a"),type = ConfigType.PROPERTIES)
public class DemoProperties {
private String home;
private String agentid;
private String configMap;
}
5.参数发布,动态刷新
@NacosInjected
private ConfigService configService;
@PostMapping
@ResponseBody
public ResponseEntity<String> publish(@RequestParam String dataId,
@RequestParam(defaultValue = "DEFAULT_GROUP") String group,
@RequestParam String content) throws NacosException {
boolean result = configService.publishConfig(dataId, group, content);
if (result) {
return new ResponseEntity<String>("Success", HttpStatus.OK);
}
return new ResponseEntity<String>("Fail", HttpStatus.INTERNAL_SERVER_ERROR);
}
6.本地冷启动
1)在本地缓存目录,手动根据namespace group dateid 的要求,写入指定数据,在程序中能够读取
2)用于一些特殊网络限制冷启动的情况
3)默认{home}/nacos/config/fixed-{host}_{port}-{namespace}_nacos/snapshot-tenant/{namespace}/{group}
/Users/xxx/nacos/config/fixed-127.0.0.1_8848-015ae736-f567-4633-b6ce-62d2adf0970a_nacos/snapshot-tenant/015ae736-f567-4633-b6ce-62d2adf0970a/sms-channel
Featured Tags
工具类
XML
JAVA
Java
Mybatis
反射
代理模式
日志
工厂方法模式
装饰器模式
结果集映射
Web
RESTful
WebAPI规范
SQL
Redis
Jira
SynapseRT
有限状态机 State Machine
statemachine
SpringBoot
IDEA
Tomcat
Vue
Docker
Springboot
Apache Ignite
项目发布与调试
白帽子讲web安全
黑名单
通用化
企业中台实践
MacOS
docker
minio
springboot
分布式存储
Postgres
数据备份
数据恢复
Elasticsearch
K8S
抖音小程序
开发实践
SpringCloud
PostgreSQL
Skywalking
Nacos
Kubernetes
Secret
ConfigMap
Git
GitOps
云原生
RocketMQ
ActiveMQ-Artemis
ClickHouse
Kamailio
SIP
sipp
Homer
heplify
heplify-server
HEP
MySQL
OpenSIPS
sip
DeepSeek
Manticore Search