nacos 配置中心
1. 配置pom
<?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>cloud2020</artifactId>
<groupId>com.pyh.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloudalibaba-config-nacos-client3377</artifactId>
<dependencies>
<!-- nacos config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba nacos discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.pyh.springcloud</groupId>
<artifactId>cloud-api-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2. 配置文件
2.1 bootstrap.yaml
server:
port: 3377
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: 192.168.226.128:8848 # nacos 服务中心地址
config:
server-addr: 192.168.226.128:8848 # nacos 配置中心地址
file-extension: yaml # 指定 yaml 格式的配置
# ${prefix}-${spring.profiles.active}.${file-extension}
# ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
# 根据官方定义,配置文件格式如上,所以得出配置文件名称为: nacos-config-client-dev.yaml
2.2 application.yaml
spring:
profiles:
active: dev
3. 主配置类
package com.pyh.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain3377 {
public static void main(String[] args) {
SpringApplication.run(NacosConfigClientMain3377.class,args);
}
}
4. controller
package com.pyh.springcloud.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.RestController;
@RestController
@RefreshScope // 支持 nacos 动态刷新功能
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo(){
return configInfo;
}
}
5. nacos UI.
官网:
https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html
${prefix}-${spring.profiles.active}.${file-extension}
5.1 配置nacos
打开nacos主页 : http://192.168.226.128:8848/nacos
添加新配置文件 nacos-config-client-dev.yaml
5.2 配置dataId
6. 测试
7. nacos 分类配置
7.1 DataID 方案
application.yaml
spring:
profiles:
active: test # 测试环境,配置是什么就加载什么
# active: dev # 开发环境
nacos 添加配置:
7.2 Group 方案
application.yaml
spring:
profiles:
active: info
bootstrap.yaml 添加参数
spring:
cloud:
nacos:
config:
group: TEST_GROUP # 默认 DEFAULT_GROUP # DEV_GROUP
7.3 namespace 命名空间方案
新建两个命名空间 test 和 dev
以 dev namespace 为例,添加三个不同group的 配置文件
yaml文件添加以下配置
spring:
cloud:
nacos:
config:
group: TEST_GROUP # 默认 DEFAULT_GROUP # DEV_GROUP
namespace: d6b14de0-82a2-4404-a7ad-e9bfc3f58128 # 不填写则默认default, 这个在nacos的配置中找
spring:
profiles:
# active: info
active: test # 测试环境,配置是什么就加载什么
# active: dev # 开发环境
配置好对应的 namespace / group / filename 之后,
从上面 application.yaml 和 bootstrap.yaml 可以得到, 会获取一个属性如下的文件
namespace : d6b14de0-82a2-4404-a7ad-e9bfc3f58128
group: TEST_GROUP
dataID(文件名称): nacos-config-client-test.yaml