分类

链接

2022 年 3 月
 123456
78910111213
14151617181920
21222324252627
28293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > Others > 正文
共享办公室出租
Springboot整合Nacos(动态改变数据库连接参数)
Others 暂无评论 阅读(176)

这个是整合nacos的教程,我在网上找相关文档,都不满意,就自己试了一下,然后分享出来。

1 安装Nacos

安装很简单,这里就不说啦。直接说整合

2 Springboot中引入Nacos

<span class="token comment">&lt;!--nacos--&gt;</span>
        <span class="token tag"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token punctuation">&lt;/</span>groupId<span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-nacos-config<span class="token tag"><span class="token punctuation">&lt;/</span>artifactId<span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>2.2.1.RELEASE<span class="token tag"><span class="token punctuation">&lt;/</span>version<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;/</span>dependency<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token punctuation">&lt;/</span>groupId<span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token punctuation">&lt;/</span>artifactId<span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>2.2.1.RELEASE<span class="token tag"><span class="token punctuation">&lt;/</span>version<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;/</span>dependency<span class="token punctuation">&gt;</span></span>

注意:springboot的版本要跟nacos保持一致,不然有问题,或者注册不上。

<span class="token tag"><span class="token punctuation">&lt;</span>parent<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token punctuation">&lt;/</span>groupId<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring-boot-starter-parent<span class="token tag"><span class="token punctuation">&lt;/</span>artifactId<span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>2.2.1.RELEASE<span class="token tag"><span class="token punctuation">&lt;/</span>version<span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token punctuation">&lt;/</span>parent<span class="token punctuation">&gt;</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的名称来查找配置文件

 

nacos配置
bootstrap.yml

注: prefix可配置为service name

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:Springboot整合Nacos(动态改变数据库连接参数) | Bruce's Blog

发表评论

留言无头像?