所有分类
  • 所有分类
  • 未分类

SpringBoot-配置文件的active/include-多环境/公共配置

 原文网址:SpringBoot–配置文件–active/include–多环境/公共配置_IT利刃出鞘的博客-CSDN博客

简介

说明

本文介绍SpringBoot如何切换多环境(开发、测试、生产)以及如何引入公共的配置文件。

application.yml的spring.profiles.active用于切换多环境(选择目前激活的是哪个环境),spring.profiles.include用于引入公共的配置文件。

application.yml简单配置示例

# 服务器配置
server:
  port: 8082

# spring配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/DatebaseName
    username: root
    password: 123
    driverClassName: com.mysql.jdbc.Driver

多环境(active)

简介

我们一般将开发(dev),测试(test),生产(prod)的配置写到不同的配置文件里边,运行时通过spring.profiles.active来选择使用哪个配置。

java命令指定参数

法1:使用-D(推荐)

  • java -Dspring.profiles.active=dev -jar test-1.0.0-SNAPSHOT.jar
  • -Dxxx=yyy必须在-jar之前
  • 此法增加的参数被设置到应用的系统属性中,可通过System.getProperty(“server.port”)获取

法2:使用–

  • java -jar test-1.0.0-SNAPSHOT.jar –spring.profiles.active=dev
  • –xxx=yyy必须在-jar之后
  • 此法增加的参数属于命令行参数,会作为SpringBoot启动的main方法的String[] args参数。
  • 有时本方法在Windows下无效。

spring.profiles.active 和 spring.profiles.include 区别

spring.profiles.active 和 spring.profiles.include 有什么区别呢?笔者认为主要是语意上的区别,实际使用效果基本相同。active 是与环境有关的,include 是与环境无关的。

实际使用,只有下边这一处区别:

The properties from spring.profile.include override default properties. The properties from active profiles override spring.profile.include and default properties.

即:spring.profile.include的属性会覆盖默认属性,spring.profiles.active会覆盖spring.profile.include和默认属性。

方案1:多个配置文件

application.yml

spring:
  profiles:
  #激活开发环境
    active: dev

spring:
  application:
    name: order

application-dev.yml 

#开发环境配置
spring:
  profiles: dev
server:
  port: 8080

application-prod.yml 

#生产环境配置
spring:
  profiles: prod
server:
  port: 8082

方案2:使用—

例如:

application.yml

spring:
  profiles:
  #激活开发环境
    active: dev

spring:
  application:
    name: order
---
#开发环境配置
spring:
  profiles: dev
server:
  port: 8080
---
#生产环境配置
spring:
  profiles: prod
server:
  port: 8082

新版本(SpringBoot2.4.2及之后)写法: 

spring:
  profiles:
  #激活开发环境
    active: dev

spring:
  application:
    name: order
---
#开发环境配置
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 8080
---
#生产环境配置
spring:
  config:
    activate:
      on-profile: prod
server:
  port: 8082

拆出(include)

简介

我们可以将一些公共的配置单独拿出来,然后其他文件都把这个配置给包含进去。

方案1:使用多个配置文件

下边这些文件,放到有效位置即可。

application.yml

spring:
  profiles:
    #导入其他配置(本处以eureka,feign为例)
    include: eureka,feign

spring:
  application:
    name: order

application-eureka.yml 

eureka:
  client:
    service-Url:
      defaultZone: http://localhost:7001/eureka

application-feign.yml 

feign:
  hystrix:
    enabled: true

方案2:使用—

例如:

application.yml

spring:
  profiles:
    #导入其他配置(本处以eureka,feign为例)
    include: eureka,feign

spring:
  application:
    name: order
---
#eureka配置
spring:
  profiles: eureka
eureka:
  client:
    service-Url:
      defaultZone: http://localhost:7001/eureka
---
#feign配置
spring:
  profiles: feign
feign:
  hystrix:
    enabled: true

新版本(SpringBoot2.4.2及之后)写法: 

spring:
  profiles:
    #导入其他配置(本处以eureka,feign为例)
    include: eureka,feign

spring:
  application:
    name: order
---
#eureka配置
spring:
  config:
    activate:
      on-profile: eureka
eureka:
  client:
    service-Url:
      defaultZone: http://localhost:7001/eureka
---
#feign配置
spring:
  config:
    activate:
      on-profile: feign
feign:
  hystrix:
    enabled: true
0

评论0

请先

显示验证码
没有账号?注册  忘记密码?

社交账号快速登录