作者:废物大师兄

本文探讨 Nacos 作为配置中心,如何实现不同环境(开发、测试、灰度、正式)的配置管理问题。

maven项目怎么读取配置文件 maven配置profiles_jar

就像Maven用groupId、artifactId、version三者来定位jar包在仓库中的位置一样,Nacos也提供了 Namespace (命名空间) 、Data ID (配置集ID)、 Group (组) 来确定一个配置文件(或者叫配置集)。

由此,实现多环境配置的方案也有三种:

1、用命名空间(namespace)来区分不同的环境,一个命名空间对应一个环境;

2、用配置组(group)来区分不同的环境,命名空间用默认的public即可,一个组对应一种环境;

3、用配置集ID(Data ID)名称来区分不同的环境,命名空间和组用默认的即可,通过文件命名来区分;

接下来,逐个来看

http://{host}:{port}/nacos

http://{host}:{port}/nacos/index.html

默认用户名密码都是nacos



maven项目怎么读取配置文件 maven配置profiles_java_02

为了方便演示,这里建了一个名为example的Spring Boot项目:



maven项目怎么读取配置文件 maven配置profiles_jar_03

pom.xml

1 <?xml  version="1.0" encoding="UTF-8"?>
 2 "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     4.0.0
 5     
 6         org.springframework.boot
 7         spring-boot-starter-parent
 8         2.3.6.RELEASE
 9          
10     
11     com.example
12     example
13     0.0.1-SNAPSHOT
14     example
15 
16     
17         1.8
18         2.2.3.RELEASE
19     
20 
21     
22         
23             org.springframework.boot
24             spring-boot-starter-web
25         
26         
27             com.alibaba.cloud
28             spring-cloud-starter-alibaba-nacos-config
29         
30     
31 
32     
33         
34             
35                 com.alibaba.cloud
36                 spring-cloud-alibaba-dependencies
37                 ${spring-cloud-alibaba.version}
38                 <type>pomtype>
39                 import
40             
41         
42     
43 
44     
45         
46             
47                 org.springframework.boot
48                 spring-boot-maven-plugin
49             
50         
51     
52 
53

bootstrap.yml

spring:
  application:
    name: example
  cloud:
    nacos:
      config:
        server-addr: 192.168.100.10:8848
        file-extension: yaml

Spring Boot 基础不介绍了,不会的推荐看下这个教程:

https://github.com/javastacks/spring-boot-best-practice

HelloController.java

package com.example.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;

/**
 * @author ChengJianSheng
 * @data 2020/11/19
 */
@RestController
@RequestMapping("/hello")
@RefreshScope
public class HelloController {

    @Value("${greet.hello}")
    private String greet;

    @GetMapping("/sayHi")
    public String sayHi() {
        return greet;
    }
}

1、利用 Data ID 命名 来区分环境

利用Data ID命名来区分环境,命名空间和组默认即可。

在 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 的拼接格式变成 {file-extension}
  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。

maven项目怎么读取配置文件 maven配置profiles_maven profiles配置_04

maven项目怎么读取配置文件 maven配置profiles_maven profiles配置_05

maven项目怎么读取配置文件 maven配置profiles_jar_06

maven项目怎么读取配置文件 maven配置profiles_maven profiles配置_07

maven项目怎么读取配置文件 maven配置profiles_spring_08

用命令行启动也是一样的:



maven项目怎么读取配置文件 maven配置profiles_jar_09

例如:

java -Dspring.profiles.active=test -jar example-0.0.1-SNAPSHOT.jar



maven项目怎么读取配置文件 maven配置profiles_maven项目怎么读取配置文件_10

2、利用 Group 来区分环境

项目不变,我们把spring.application.name改成example2

命名空间用默认的public



maven项目怎么读取配置文件 maven配置profiles_maven项目怎么读取配置文件_11

maven项目怎么读取配置文件 maven配置profiles_jar_12

maven项目怎么读取配置文件 maven配置profiles_java_13

java -Dspring.cloud.nacos.config.group=DEV_GROUP -jar example-0.0.1-SNAPSHOT.jar



maven项目怎么读取配置文件 maven配置profiles_maven项目怎么读取配置文件_14

java -Dspring.cloud.nacos.config.group=TEST_GROUP -jar example-0.0.1-SNAPSHOT.jar



maven项目怎么读取配置文件 maven配置profiles_spring_15

java -Dspring.profiles.active=test -Dspring.cloud.nacos.config.group=TEST_GROUP -jar example-0.0.1-SNAPSHOT.jar

如果是这样的话,这个时候,Data ID 命名就应该是 example2-test.yaml

3、利用 Namespace 区分环境



maven项目怎么读取配置文件 maven配置profiles_jar_16

创建命名空间的时候,如果不指定ID,则自动生成的id就是这样的uuid字符串,我们还是自己指定一个有意义的ID吧



maven项目怎么读取配置文件 maven配置profiles_spring_17

maven项目怎么读取配置文件 maven配置profiles_java_18

maven项目怎么读取配置文件 maven配置profiles_maven profiles配置_19

maven项目怎么读取配置文件 maven配置profiles_maven profiles配置_20

maven项目怎么读取配置文件 maven配置profiles_maven profiles配置_21

maven项目怎么读取配置文件 maven配置profiles_maven项目怎么读取配置文件_22

maven项目怎么读取配置文件 maven配置profiles_spring_23

java -Dspring.cloud.nacos.config.namespace=ns_dev -jar example-0.0.1-SNAPSHOT.jar



maven项目怎么读取配置文件 maven配置profiles_maven项目怎么读取配置文件_24

java -Dspring.profiles.active=dev -Dspring.cloud.nacos.config.namespace=ns_dev -jar example-0.0.1-SNAPSHOT.jar



maven项目怎么读取配置文件 maven配置profiles_maven项目怎么读取配置文件_25

java -Dspring.cloud.nacos.config.namespace=ns_test -jar example-0.0.1-SNAPSHOT.jar



maven项目怎么读取配置文件 maven配置profiles_jar_26

java -Dspring.profiles.active=test \
     -Dspring.cloud.nacos.config.namespace=ns_test \
     -Dspring.cloud.nacos.config.group=TEST_GROUP \
     -jar example-0.0.1-SNAPSHOT.jar



maven项目怎么读取配置文件 maven配置profiles_maven项目怎么读取配置文件_27

4、小结

第一种,用 Data ID 区分环境,虽然简单,但是每个项目要创建4个配置文件,随着项目的增多,都在一个命名空间下回显得很混乱,查找起来也不是很方便,而且不利于做权限控制

第二种,用Group区分,问题也是一样的