Nacos Config的配置中心
1.创建一个新的文件
2.导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.muke</groupId>
<artifactId>configdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<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>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
3.创建启动类
package com.muke;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class TestController {
@Value("${testStr}")
private String testStr;
@GetMapping("/test")
public String test(){
return testStr;
}
}
4.在Nacos中新增配置
记得勾选yaml
5.调用配置
使用bootstrap.yaml
spring:
application:
name: configdemo
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
6.启动
7.测试
8.增加动态配置注解@RefreshScope
/**
* @author ziya
* @date 2021/3/2 12:19 上午
*/
@RestController
@RefreshScope
public class TestController {
@Value("${testStr}")
private String testStr;
@GetMapping("/test")
public String test(){
return testStr;
}
}
成功