springboot集成nacos配置中心直接代替application.yml

nacos配置中心可以使用@nacosValue的方式,使用的依赖是:nacos-config-spring-boot-starter。但是本文讲的是spring-cloud-starter-alibaba-nacos-config,可以直接使用nacos中的配置文件而不是application.yml,属性值依然用@value来获取

1.pom.xml添加依赖

<!-- 因为需要springcloud的上下文来引入nacos中的配置所以要添加cloud相关,版本可以不与下面的一模一样但要匹配boot版本 -->
 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>0.2.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>
  <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>`
<dependency>
	    <groupId>com.alibaba.nacos</groupId>
	    <artifactId>nacos-client</artifactId>
	    <version>1.1.3</version>
	</dependency> 
``

2.bootstrap.properties文件配置

spring.application.name=nacosConfig
spring.cloud.nacos.config.server-addr=192.168.0.29:8848
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.data-id=nacosConfig
spring.cloud.nacos.config.namespace=9e414f5e-d48a-47ba-92a9-c22d1072b88c
spring.main.allow-bean-definition-overriding=true

3.此时,一定要记住,nacos配置文件的名称为nacosConfig.yaml,不是nacosConfig

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值