SpringCloud入门之Spring Boot多环境配置切换指南

在 spring boot 中,有两种配置文件,一种是application.properties,另一种是application.yml,两种都可以配置spring boot 项目中的一些变量的定义,参数的设置等。

其中yml格式文件编写规范参考文章手把手学系列之YAML格式文件规范学习

一般在一个项目中,总是会有好多个环境。比如:

开发环境 -> 测试环境 -> 预发布环境 -> 生产环境

每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一点不同,配置读取可是一个让人有点伤脑筋的问题。

Spring Boot提供了一种优先级配置读取的机制来帮助我们从这种困境中走出来。

常规情况下,我们都知道Spring Boot的配置会从application.properties中读取。实际上,从resource目录下的application.properties文件读取是Spring Boot配置链中的一环而已。

根据Spring Boot的文档,配置使用的优先级从高到低的顺序,具体如下所示:

1. 命令行参数。
2. 通过 System.getProperties() 获取的 Java 系统参数。
3. 操作系统环境变量。
4. 从 java:comp/env 得到的 JNDI 属性。
5. 通过 RandomValuePropertySource 生成的“random.*”属性。
6. 应用 Jar 文件之外的属性文件(application.properties)。
7. 应用 Jar 文件内部的属性文件(application.properties)。
8. 在应用配置 Java 类(包含“@Configuration”注解的 Java 类)中通过“@PropertySource”注解声明的属性文件。
9. 通过“SpringApplication.setDefaultProperties”声明的默认属性。

这意味着,如果Spring Boot在优先级更高的位置找到了配置,那么它就会无视低级的配置。

方法一、不在配置文件写上配置节定义,而是通过执行时定位不同配置单独文件来区分。

比如,我在application.properties目录中,写入本地的MySQL的配置:

db.jdbc.driver=com.mysql.jdbc.Driver
db.jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
db.jdbc.username=username
db.jdbc.password=password

在自己项目调试的阶段,项目总是会使用本地的MySQL数据库。而一旦打包之后,在外部声明一个test_evn.properties.

启动Jar包的时候, 指定一个外部配置文件:

java -jar demo.jar --spring.config.location=/path/test_evn.properties

这样一来,我们在开发者的机器上总是使用自己的配置,而一到对应的环境,就会使用高级的位置所做的配置。

方法二、在配置文件写上配置节定义,在执行时传递配置节需激活定义名称来区分。

1、在application.properties ,写法如下:

spring.profiles.active=dev
spring.datasource.data-username=root
spring.datasource.data-password=root

2、在yml 文件中配置的话,写法如下:

  

spring:
profiles:
active: prod
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test
username: root
password: root


启动Jar包的时候:Java -jar xxxxxx.jar spring.profiles.actiove=prod 也可以这样启动设置配置文件,但是这只是用于开发和测试


下面提供一个在同一文件中定义多个配置节的详细参考用例:
spring:
application:
name: config-server  #一般情况下这部分应放在bootstrap.yml或bootstrap.properties文件中
---
#服务配置
spring:
profiles: config-server1
#应用配置
cloud:
config:
label: master #git label(提交id,分支名称或tag)
server:
git:
password: '' #登录密码
searchPaths: common-config #搜寻目录,可以配置多个,用逗号分割
uri: #仓库地址
username: '' #登录用户

#svn:
#uri: http://192.168.0.6/svn/repo/config-repo
#username: username
#password: password
#default-label: trunk
#profiles:
#active: subversion
server:
port: '8888' 
#eureka服务注册地址
eureka: 
client: 
service-url: 
defaultZone: http://server1:8761/eureka/,http://server1:8762/eureka/,http://server1:8763/eureka/ 
#instance: 
#prefer-ip-address: true
---
#服务配置
spring:
profiles: config-server2
#应用配置
cloud:
config:
label: master #git label(提交id,分支名称或tag)
server:
git:
password: '' #登录密码
searchPaths: common-config #搜寻目录,可以配置多个,用逗号分割
uri: *** #仓库地址
username: '' #登录用户

#svn:
#uri: http://192.168.0.6/svn/repo/config-repo
#username: username
#password: password
#default-label: trunk
#profiles:
#active: subversion
server:
port: '8889' 
#eureka服务注册地址
eureka: 
client: 
service-url: 
defaultZone: http://server1:8761/eureka/,http://server1:8762/eureka/,http://server1:8763/eureka/ 
#instance: 
#prefer-ip-address: true

启动Jar包的时候:

Java -jar xxxxxx.jar  --spring.profiles.active=config-server1 

Java -jar xxxxxx.jar  --spring.profiles.active=config-server2

在代码中读取这些配置也是非常方便的,在代码的逻辑中,其实是无需去关心这个配置是从什么地方来的,只用关注能获取什么配置就够了。

public class ApplicationConfigure {

    @Value("${db.jdbc.driver}")
    private String jdbcDriver;
    @Value("${db.jdbc.url}")
    private String jdbcUrl;
    @Value("${db.jdbc.username}")
    private String jdbcUsername;
    @Value("${db.jdbc.password}")
    private String jdbcPassword;
    
    // mysql config class
    // ..... 
    
}

有时候我们在项目启动的时候,总是需要先启动一些初始化的类,以前比较常见的做法是写再static块中,Spring Boot提供了一个CommandLineRunner接口,实现这个接口的类总是会被优先启动,并优先执行CommandLineRunner接口中提供的run()方法。

public class ApplicationConfigure implements CommandLineRunner  {

    @Value("${db.jdbc.driver}")
    private String jdbcDriver;
    @Value("${db.jdbc.url}")
    private String jdbcUrl;
    @Value("${db.jdbc.username}")
    private String jdbcUsername;
    @Value("${db.jdbc.password}")
    private String jdbcPassword;
    
    // mysql config class
    // ..... 
    @Override
    public void run(String... strings) throws Exception {
        // 预先加载的一些方法,类,属性。
    }
}
原文地址:https://www.cnblogs.com/BlogNetSpace/p/8444285.html