动态加载可以用作数据源的切换,动态线程池
例如:
当使用这个Nacos依赖后
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
需要新建一个bootstrap.yml
spring:
application:
name: jc-club-oss
profiles:
active: dev
cloud:
nacos:
config:
server-addr: 117.72.14.166:8848
prefix: ${spring.application.name}
group: DEFAULT_GROUP
namespace:
file-extension: yaml
discovery:
enabled: true
server-addr: 117.72.14.166:8848
需要指定一些内容,同时可以将一些需要动态变化的配置放入nacos中
放入后,需要添加注解@RefreshScope
@Configuration
@RefreshScope
public class StorageConfig {
@Value("${storage.service.type}")
private String storageType;
@Bean
@RefreshScope
public StorageAdapter storageService() {
if ("minio".equals(storageType)) {
return new MinioStorageAdapter();
} else if ("aliyun".equals(storageType)) {
return new AliStorageAdapter();
} else {
throw new IllegalArgumentException("未找到对应的文件存储处理器");
}
}
}