此项目是一个Demo项目,主要用于进行相关技术的集成练习。
对项目进行初始化构建
主要框架 :
SpringBoot:2.6.3
SpringCloud:2021.0.1
SpringCloud Alibaba:2021.0.1.1
出现的问题一
项目构建完成后,进行启动测试,提示出现 The bean 'inetUtilsProperties', defined in class path resource [org/springframework/cloud/commons/util/UtilAutoConfiguration.class], could not be registered. 错误
解决方法是通过在配置文件进行设置
spring:
main:
allow-bean-definition-overriding: true
问题二
项目设想是将大部分的配置信息部署在Nacos的配置中心,实现动态的配置,无需重新启动项目。在此过程中,将原有的applicaiton.yml 注释,新增 bootstrap.yml,发现项目并未正常读取bootstrap.yml文件。
将查阅资料,发现在SpringBoot 2.4 版本开始,配置文件的加载方式进行了重构,在此之后的版本可通过添加对应的bootstrap依赖解决。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
问题三
若是想在对在Nacos配置中心设置的静态数据进行试试监听,并且动态修改对应的数据。
解决方法可以使用@RefreshScope结合@Value实现
@RestController
@RefreshScope
@RequestMapping("/user")
public class UserController {
@Value("${test.string}")
private String testString;
@GetMapping("/test")
public String test(){
return testString;
}
}