Nacos 配置中心 动态刷新
修改nacos的的配置,微服务相应的数据也会改变
一、引入依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
注意:引入依赖后,不使用配置中心,启动服务会报错,可以先使用下面的yaml 配置 过渡一下
spring: cloud: nacos: config: import-check: enabled: false
二、配置yaml文件
spring: cloud: nacos: serverAddr: 127.0.0.1:8848 #如果用的云上托管版本,输入可访问的Nacos Server地址即可 config: import: nacos:service-product.properties?refreshEnabled=true
三、创建数据集
1、打开nacos网址
http://localhost:8848/nacos
2、创建配置
3、Data ID 要与 yaml 文件中的 config 一致
4、配置内容,配置格式:Properties
5、发布
四、配置数据集
创建类 properties.ProductProperties
package com.wt.product.properties; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Description: TODO * @Author: 1872428 * @Date: 2025/6/7 11:48 * @Version: 1.0 **/ @Component @ConfigurationProperties(prefix = "product") @Data public class ProductProperties { private String timeout; private String autoConfirm; }
五、使用
@Autowired private ProductProperties productProperties;