在没学过服务注册和配置中心技术前,每次修改SpringBoot配置文件的信息,都要重新部署服务,及其的不方便。自从学了nacos配置中心之后,我把项目中的配置文件信息全部放到nacos上面进行管理,来方便的动态修改项目的配置信息,且不用重新部署服务。
依赖版本自己选择
<!-- 服务注册/发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
引入之后,在nacos配置管理中添加了配置文件信息
本地项目文件读取nacos配置文件信息
跟着文档配置,基本一样,以为能启动成功。结果启动之后报了错误,这里报错很明显了,就是没有配置数据库连接,但是nacos我明明写好了,所以这里只能是bootstrap.properties根本没有读取到nacos配置中心的配置文件
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
经过一系列排查和查阅一些资料后发现,从Spring Boot 2.4版本开始,配置文件加载方式进行了重构,这才导致读取不到nacos配置中心文件的问题,还需要增加依赖
解
决
方
法
:
\textcolor{red}{解决方法:}
解决方法:
<!-- 版本跟nacos保持一致 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>