SpringBoot profles配置多环境

SpringBoot profles配置多环境
23/100
发布文章
u014427391

软件环境简介
这里介绍一下SpringBoot提供的profiles属性加上maven配置一下多环境,在实践生产中,正规一点的可能有开发环境、测试环境、预发布环境、生产环境等等,而这些环境的参数肯定都不一样,换环境的时候,经常需要修改参数,参数一多,维护起来很麻烦,所以SpringBoot提供了通过profiles配置来达到多环境配置,不需要项目一上生产环境还是预发布就改一堆配置文件。

软件环境:

  • application-dev(开发环境)
  • application-test(测试环境)
  • application-uat(预发布)
  • application-prod(生产环境)

配置文件格式可以为preperties或者yml,因为yml写起来比较简介,所以本博客介绍一yml的配置文件,介绍一下配置方式:

yml配置profiles
先介绍一下通过SpringBoot配置文件的这种方式,这里需要新建如图yml配置文件:

在这里插入图片描述

  • application-dev.yml(开发环境)
  • application-test.yml(测试环境)
  • application-uat.yml(预发布)
  • application-prod.yml(生产环境)

配置比较容易,需要在application.yml配置:

spring:
  profiles:
    active: dev

给出我的application.yml配置,可以参考:

server:
  context-path: /jeeplatform
  error:
    whitelabel:
      enabled: true

spring:
  profiles:
    active: dev

开发环境apprlication-dev.yml配置,根据自己需要配置:

server:
  port: 8080

spring:

  datasource:

    # 主数据源
    shop:
      url: jdbc:mysql://127.0.0.1:3306/jeeplatform?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
      username: root
      password: root

    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    # 连接池设置
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 20
      # 配置获取连接等待超时的时间
      max-wait: 60000
      # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一个连接在池中最小生存的时间,单位是毫秒
      min-evictable-idle-time-millis: 300000
      # Oracle请使用select 1 from dual
      validation-query: SELECT 'x'
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      # 打开PSCache,并且指定每个连接上PSCache的大小
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
      filters: stat,wall,slf4j
      # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
      connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      # 合并多个DruidDataSource的监控数据
      use-global-data-source-stat: true

  # JPA配置
  jpa:
    database: mysql
    hibernate:
      show_sql: true
      format_sql: true
      ddl-auto: none
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

  # MVC配置
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp

  #Jedis配置
  jedis :
    pool :
      host : 127.0.0.1
      port : 6379
      password : redispassword
      timeout : 0
      config :
        maxTotal : 100
        maxIdle : 10
        maxWaitMillis : 100000

测试环境、预发布环境、生产环境的配置类似,根据需要配置

如果是jar,可以直接运行命令:

java -jar [jar名称].jar --spring.profiles.active=[环境参数(dev|test|uat|prod)]

eg:

java -jar myproject.jar --spring.profiles.active = dev

拓展,配置maven

这是另外的拓展,其实也是基于前面的配置,配置多环境信息在maven的pom文件里,主要目的是maven打包的时候,可以通过传环境参数对应package

配置,需要在maven的pom文件添加配置信息,这里设置默认开发环境,设置<activeByDefault>为true

<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
                <project.packaging>jar</project.packaging>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
                <project.packaging>jar</project.packaging>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <activatedProperties>uat</activatedProperties>
                <project.packaging>jar</project.packaging>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
                <project.packaging>jar</project.packaging>
            </properties>
        </profile>
    </profiles>

可以修改一下application.yml:直接读maven配置文件:

server:
  context-path: /jeeplatform
  error:
    whitelabel:
      enabled: true

spring:
  profiles:
    active: @activatedProperties@
  application:
    name: @project.name@_${spring.profiles.active}

maven根据环境package

maven clean package -Pdev | -Ptest | -Puat | -Pprod

eg:直接package开发环境的

maven clean package -Pdev 

软件环境简介
这里介绍一下SpringBoot提供的profiles属性加上maven配置一下多环境,在实践生产中,正规一点的可能有开发环境、测试环境、预发布环境、生产环境等等,而这些环境的参数肯定都不一样,换环境的时候,经常需要修改参数,参数一多,维护起来很麻烦,所以SpringBoot提供了通过profiles配置来达到多环境配置,不需要项目一上生产环境还是预发布就改一堆配置文件。

软件环境:

application-dev(开发环境)
application-test(测试环境)
application-uat(预发布)
application-prod(生产环境)
配置文件格式可以为preperties或者yml,因为yml写起来比较简介,所以本博客介绍一yml的配置文件,介绍一下配置方式:

yml配置profiles
先介绍一下通过SpringBoot配置文件的这种方式,这里需要新建如图yml配置文件:

在这里插入图片描述

application-dev.yml(开发环境)
application-test.yml(测试环境)
application-uat.yml(预发布)
application-prod.yml(生产环境)
配置比较容易,需要在application.yml配置:

spring:
profiles:
active: dev

给出我的application.yml配置,可以参考:

server:
context-path: /jeeplatform
error:
whitelabel:
enabled: true

spring:
profiles:
active: dev

开发环境apprlication-dev.yml配置,根据自己需要配置:

server:
port: 8080

spring:

datasource:

# 主数据源
shop:
  url: jdbc:mysql://127.0.0.1:3306/jeeplatform?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false
  username: root
  password: root

driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource

# 连接池设置
druid:
  initial-size: 5
  min-idle: 5
  max-active: 20
  # 配置获取连接等待超时的时间
  max-wait: 60000
  # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  time-between-eviction-runs-millis: 60000
  # 配置一个连接在池中最小生存的时间,单位是毫秒
  min-evictable-idle-time-millis: 300000
  # Oracle请使用select 1 from dual
  validation-query: SELECT 'x'
  test-while-idle: true
  test-on-borrow: false
  test-on-return: false
  # 打开PSCache,并且指定每个连接上PSCache的大小
  pool-prepared-statements: true
  max-pool-prepared-statement-per-connection-size: 20
  # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  filters: stat,wall,slf4j
  # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  # 合并多个DruidDataSource的监控数据
  use-global-data-source-stat: true

# JPA配置
jpa:
database: mysql
hibernate:
show_sql: true
format_sql: true
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

# MVC配置
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp

#Jedis配置
jedis :
pool :
host : 127.0.0.1
port : 6379
password : redispassword
timeout : 0
config :
maxTotal : 100
maxIdle : 10
maxWaitMillis : 100000
测试环境、预发布环境、生产环境的配置类似,根据需要配置

如果是jar,可以直接运行命令:

java -jar [jar名称].jar --spring.profiles.active=[环境参数(dev|test|uat|prod)]
eg:

java -jar myproject.jar --spring.profiles.active = dev
拓展,配置maven

这是另外的拓展,其实也是基于前面的配置,配置多环境信息在maven的pom文件里,主要目的是maven打包的时候,可以通过传环境参数对应package

配置,需要在maven的pom文件添加配置信息,这里设置默认开发环境,设置

可以修改一下application.yml:直接读maven配置文件:

server:
context-path: /jeeplatform
error:
whitelabel:
enabled: true

spring:
profiles:
active: @activatedProperties@
application:
name: @project.name@_${spring.profiles.active}
maven根据环境package

maven clean package -Pdev | -Ptest | -Puat | -Pprod
eg:直接package开发环境的

maven clean package -Pdev
Markdown 已选中 3409 字数 200 行数 当前行 200, 当前列 0 HTML 3270 字数 145 段落

猜你喜欢

转载自www.cnblogs.com/mzq123/p/10807200.html