Springboot整合Nacos(动态改变数据库连接参数)
这个是整合nacos的教程,我在网上找相关文档,都不满意,就自己试了一下,然后分享出来。
1 安装Nacos。
安装很简单,这里就不说啦。直接说整合
2 Springboot中引入Nacos
<span class="token comment"><!--nacos--></span>
<span class="token tag"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token punctuation"></</span>groupId<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-config<span class="token tag"><span class="token punctuation"></</span>artifactId<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>version<span class="token punctuation">></span></span>2.2.1.RELEASE<span class="token tag"><span class="token punctuation"></</span>version<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"></</span>dependency<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>com.alibaba.cloud<span class="token tag"><span class="token punctuation"></</span>groupId<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token punctuation"></</span>artifactId<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>version<span class="token punctuation">></span></span>2.2.1.RELEASE<span class="token tag"><span class="token punctuation"></</span>version<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"></</span>dependency<span class="token punctuation">></span></span>
注意:springboot的版本要跟nacos保持一致,不然有问题,或者注册不上。
<span class="token tag"><span class="token punctuation"><</span>parent<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org.springframework.boot<span class="token tag"><span class="token punctuation"></</span>groupId<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring-boot-starter-parent<span class="token tag"><span class="token punctuation"></</span>artifactId<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"><</span>version<span class="token punctuation">></span></span>2.2.1.RELEASE<span class="token tag"><span class="token punctuation"></</span>version<span class="token punctuation">></span></span>
<span class="token tag"><span class="token punctuation"></</span>parent<span class="token punctuation">></span></span>
3 配置项目中的bootstrap.yml文件
注意
只能是bootstrap.yml文件,application文件是整合不上的。
bootstrap.yml 先于 application.yml 加载
bootstrap.yml(bootstrap.properties)用来在程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等
application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。
配置文件prefix是nacos服务器中DataID的前缀,若无此项配置,默认{spring.application.name}。项目启动时根据此配置文件拼接nacos配置中心的DataID的名称来查找配置文件
bootstrap.yml
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · springboot使用lock4j实现并发控制
- · springboot全局增加sentinel
- · Springboot整合Swagger常用注解
- · swagger隐藏authentication参数
- · Spring Security 中的自定义PreAuthorize 注解
- · JACKSON和FASTJSON处理返回JSON数据中为NULL字段不显示
- · Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
- · mac os m1可用的NACOS
- · 将数据从mysql迁移到clickhouse
- · springboot登录失败3次后需要验证码的设计及实现
- · Mybatis —— 解决单引号带来的sql注入问题
- · springboot访问静态资源404 —-idea设置问题