Nacos使用(三):Spring Cloud集成Nacos配置中心动态配置

7 篇文章 1 订阅
3 篇文章 0 订阅

上一篇教程介绍了Spring Boot集成Nacos配置中心,感兴趣的可以查看

看本文需要对Spring Cloud有初步了解

先上maven依赖pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.3</version>
	</parent>

	<groupId>com.test</groupId>
	<artifactId>test</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>test</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
		    <groupId>com.alibaba.cloud</groupId>
		    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
        <!-- 引导 必须有此依赖,否则无法读取bootstrap引导配置 -->
		<dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-bootstrap</artifactId>
		</dependency>
	</dependencies>
	
	<dependencyManagement>
		<dependencies>
			<dependency>
			    <groupId>com.alibaba.cloud</groupId>
			    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
			    <version>2021.0.1.0</version>
			    <type>pom</type>
			    <scope>import</scope>
			</dependency>
	        <dependency>
	            <groupId>org.springframework.cloud</groupId>
	            <artifactId>spring-cloud-dependencies</artifactId>
	            <version>2021.0.1</version>
	            <type>pom</type>
	            <scope>import</scope>
	        </dependency>
		</dependencies>
	</dependencyManagement>
</project>

引导配置bootstrap.properties

spring.application.name=test

#-------------------------------Nacos配置中心 start-------------------------------
#Nacos配置服务地址
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#配置文件类型,可选yaml
spring.cloud.nacos.config.file-extension=properties
#-------------------------------Nacos配置中心 end----------------------------------

项目入口App.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * 
 * @author longmap
 *
 */
@SpringBootApplication
@ComponentScan(value = {"com.test.**"})
public class App {
    public static void main( String[] args ) {
    	SpringApplication.run(App.class, args);
    }
}

接口示例代码TestController.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestController {
	@Value(value = "${test:123}")
	private String test;
	
	@RequestMapping(value = "/get", method = { RequestMethod.GET })
	public String get() {
		return test;
	}
	
}

说明:

说明:之所以需要配置 spring.application.name ,是因为它是构成 Nacos 配置管理 dataId字段的一部分。

在 Nacos Spring Cloud 中,dataId 的完整格式如下:

${prefix}-${spring.profiles.active}.${file-extension}
  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
  • spring.profiles.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。 注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。

注意:

1.配置文件相比Spring Boot前面多了spring.cloud.,比如spring.cloud.nacos.config.server-addr=127.0.0.1:8848

2.有注入在线配置的类通过SpringCloud原生注解@RefreshScope实现配置自动刷新

3.配置注入注解从@NacosValue变为@Value

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值