1、添加依赖
<?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">
<parent>
<artifactId>lemon-alibaba</artifactId>
<groupId>com.lemon.alibaba</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lemon-nacos-config</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 配置中心配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- spring 高版本默认不加载bootstrap配置,加入这个配置依赖就可以了-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
</project>
2、添加配置文件:
application.yml
server:
port: 11000
spring:
profiles:
active: prod
application:
name: nacos-config
bootrstrap.yml
spring:
cloud:
nacos:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
config:
file-extension: yml #z指定配置的扩展名,默认是properties格式的方式配置
namespace: public
shared-configs: #配置多个配置中心文件的配置,以上namespace等只针对默认的配置进行限制,这个节点的配置则不受上述配置的限制
- data-id: test.yml #可以有多个配置,越往下的配置会覆盖掉上面的配置,这里的dataId需要天下为DataId.后缀的方式才可以
refresh: true
3、配置中心配置:在nacos的配置界面进行配置
添加配置:
dataId:规则:服务名称-模式.扩展名 ${application.name}-${profile}.扩展名
例如: nacos-config-dev.yml
nacos-config为服务的名称:spring.application.name 的值
dev 为配置模式的值: spring.profiles.active 的值
yml 为配置文件的值:spring.cloud.config.file-extension的值
4、配置唯一的确认: 命名空间+ 组名+ dataId来确认
配置中心配置数据之后可以在项目中实时的更新和刷新。
5、配置中心配置的数据在项目中读取的方式:
1、测试是否可以读取数据:在启动类中
package com.lemon.nacos.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class NacosConfigApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosConfigApplication.class,args);
String username = applicationContext.getEnvironment().getProperty("user.name");
System.out.println(username);
}
}
2、项目中实际使用:
在需要读取的类上添加注解@RefreshScope(用于在nacos配置中心更改了配置之后,将更新后的值实时刷新),其他值的读取可以和原先读取配置文件的读取方式一样去读取
package com.lemon.nacos.config.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("nacos-config")
@RefreshScope
public class IndexController {
@Value("${user.name}")
public String userName;
@Value("${user.age}")
public int userAge;
@GetMapping("/index")
public String index(){
return userName + "======="+String.valueOf(userAge);
}
}