- 1. 前言
- 2. 创建Nacos配置
- 3. 创建项目 nacos-config
- 3.1 pom.xml
- 3.2 bootstrap.properties
- 3.3 Application启动类
- 3.4 Cotrolller 接口类
- 4. 启动测试
- 5. 总结& 使用注意事项
前两章 Nacos(三):Nacos集成OpenFegin 、Nacos(二):Spring Cloud项目中Nacos作为注册中心 描述了 Nacos作为注册中心的使用方式,本章使用Nacos 作为配置中心。
demo项目git地址: https://gitee.com/DianHaiShiYuDeMing/nacos-demo
2. 创建Nacos配置- 启动Nacos
- 创建配置文件
3.2 bootstrap.properties4.0.0 com.example nacos-demo 1.0.0 com.example.consumer nacos-config 0.0.1-SNAPSHOT nacos-config Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.boot spring-boot-maven-plugin
# 配置文件的优先级
#bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml
server.port=8010
#spring.application.name=nacos-config
##不配置的话 走 spring.application.name
#spring.cloud.nacos.config.prefix=nacos-config
###nacos配置
#nacos地址
spring.cloud.nacos.config.server-addr=192.168.104.110:16848
#账号&密码
spring.cloud.nacos.config.username=evone
spring.cloud.nacos.config.password=evone123456
#名称空间
spring.cloud.nacos.config.namespace=evone
#data id 不配置 默认 ${spring.application.name}-${spring.profiles.active}-${spring.cloud.nacos.config.file-extension}
# ${spring.profiles.active} 没有配置 就不会拼接到里边
spring.cloud.nacos.config.name=nacos-config.properties
#group分组
spring.cloud.nacos.config.group=evone
#properties 配置类型
spring.cloud.nacos.config.file-extension=properties
#spring.profiles.active=dev
3.3 Application启动类
package com.example.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
3.4 Cotrolller 接口类
package com.example.config.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
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//使用注解NacosValue 可以不使用这个注解
public class NacosConfigDemoController {
//${test.name:test} 中: 后边的test 为默认 值
//@NacosValue(value = "${test.name:aa}",autoRefreshed = true)
@Value(value = "${test.name:aa}")
private String testName;
//${test.name:test} 中: 后边的test 为默认 值
//@NacosValue(value = "${test.age:11}",autoRefreshed = true)
@Value(value = "${test.age}")
private int testAge;
//${test.name:test} 中: 后边的test 为默认 值
//@NacosValue(value = "${test.boolean:false}",autoRefreshed = true)
@Value(value = "${test.boolean:false}")
private boolean testBoolean;
@GetMapping("/testName")
public String testName() {
return testName;
}
@GetMapping("/testAge")
public int testAge() {
return testAge;
}
@GetMapping("/testBoolean")
public boolean testBoolean() {
return testBoolean;
}
}
4. 启动测试
http://localhost:8010/testName
http://localhost:8010/testAge
http://localhost:8010/testBoolean
-
配置动态变更
如果只是用@Value 不会动态变更
使用@Value+@RefreshScope value 会动态变更
使用@NacosValue 与设置 autoRefreshed =true value 会动态变更 -
NacosConfig配置文件的优先级
bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml -
建议配置,看着清晰 data Id不配置的话 默认为
${spring.application.name}-${spring.profiles.active}-${spring.cloud.nacos.config.file-extension}



