Springboot工程多环境配置

Profile

         随着系统的研发部署,工程会面临越来越多的不通环境的配置要求。最为常见的是dev(开发)、test(测试)、pro(生产)环境的区分,而profile文件的配置使得工程在面向不同环境时,可以灵活配置打包部署。

工程配置

通常profile配置有两种方式,以Springboot工程举例。

  • 在pom.xml文件中已经指定了配置文件目录,如:
    在这里插入图片描述
<bulid>
       <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/dev/**</exclude>
                    <exclude>**/pro/**</exclude>
                    <exclude>**/test/**</exclude>
                    <exclude>**/pre/**</exclude>
                </excludes>
            </resource>

            <resource>
                <directory>src/main/resources/${profile.path}</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
</build>
<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.path>config/dev</profile.path>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profile.path>config/pro</profile.path>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profile.path>config/test</profile.path>
            </properties>
        </profile>
        <profile>
            <id>pre</id>
            <properties>
                <profile.path>config/pre</profile.path>
            </properties>
        </profile>
    </profiles>

在maven打包时可以通过-P dev/test/pro 命令打出含有不同配置文件的jar包。

  • 除此之外,我们也可以通过application.yml中设置属性进行指定
spring:
    profiles:
        active: test

使用该方法时,上述pom.xml里面的配置就不需要了,但是需要将不同环境的系统配置文件重新按规则命名
application-{profile}.properties,如application-test.properties
但是值得注意的是,该方法在打出的包里包含所有环境的配置文件,这样安全性有所欠缺,下为jar包内目录:
在这里插入图片描述
示例文件:
application.yml

server:
    port: 52001
    context-path: /ldemo_device
    tomcat:
        accesslog: 
            enabled: true 
            directory: logs
            pattern: common
            prefix: access_lucas_device
        basedir: .
        maxConnections: -1
        maxThreads: 1000
        maxHttpPostSize: -1
        acceptCount: -1
spring:
    profiles:
        active: test

applicaction-dev.yml

spring:
    #datasource
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://xxx.xxx.xx.xx:3306/device?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
        username: dev
        password: dev
        driver-class-name: com.mysql.jdbc.Driver
        druid:
            initialSize: 5
            minIdle: 500
            maxActive: 1000
            maxWait: 60000
            timeBetweenEvictionRunsMillis: 60000
            minEvictableIdleTimeMillis: 300000
            validationQuery: SELECT 1
            testWhileIdle: true
            testOnBorrow: false
            testOnReturn: false

applicaction-test.yml

spring:
    #datasource
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://xxx.xxx.xx.xx:3306/device?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
        username: test
        password: test
        driver-class-name: com.mysql.jdbc.Driver
        druid:
            initialSize: 5
            minIdle: 500
            maxActive: 1000
            maxWait: 60000
            timeBetweenEvictionRunsMillis: 60000
            minEvictableIdleTimeMillis: 300000
            validationQuery: SELECT 1
            testWhileIdle: true
            testOnBorrow: false
            testOnReturn: false

          如遇到Springboot工程启动时打印日志No active profile set, falling back to default profiles: default,该日志级别是INFO,并不会对系统运行产生影响。产生该日志的原因是spring容器未找到系统profile配置。即使使用pom配置的方法,工程内没有application-{profile}.yml文件,但在application.yml中指定profiles:dev也不会对工程产生影响,启动日志会打印
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值