轉:https://blog.csdn.net/daguairen/article/details/79236885
springboot區分開發、測試、生產多環境的應用配置(一)
Spring可使用Profile區分程序在不同環境下執行情況,包含配置、加載Bean、依賴等。
Spring的Profile一般項目包含:dev(開發), stg(測試), prd(生產環境)。由spring.profiles.active屬性絕定啟用的profile。
SpringBoot的配置文件默認為 application.properties(或yaml,此外僅心properties配置為說明)。不同Profile下的配置文件由application-{profile}.properties管理,同時獨立的 Profile配置文件會覆蓋默認文件下的屬性。
多環境應用配置
將默認不變的配置,設置在application.properties
文件中。
新建開發環境下的屬性文件application-dev.properties
,將開發中的配置,設置在該文件中。‘
新建測試環境下的屬性文件application-stg.properties
,將測試中的配置,設置在該文件中。
新建生產環境下的屬性文件application-prd.properties
,將生產中的配置,設置在該文件中。
最后在application.properties
文件中配置:
## 開發/測試/生產環境分別對應dev/test/prod,可以自由定義 spring.profiles.active=dev
以上設置了應用配置文件使用application-dev.properties
。
示例:開發和生產redis分離例子
----------------- application.properties文件 ----------------------------
## 開發/測試/生產環境分別對應dev/test/prod,可以自由定義
spring.profiles.active=dev
------------ application-dev.properties文件 -------------
# Redis服務器地址
spring.redis.host=127.0.0.1
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.password=runoob
------------- application-prd.properties文件 --------------
# Redis服務器地址
spring.redis.host=127.168.123.112
# Redis服務器連接端口
spring.redis.port=6379
# Redis服務器連接密碼(默認為空)
spring.redis.password=
------------- redis.properties文件 ------------------------
############## redis start #################
# Redis數據庫索引(默認為0)
spring.redis.database=0
# Redis服務器地址
spring.redis.host=@spring.redis.host@
# Redis服務器連接端口
spring.redis.port=@spring.redis.port@
# Redis服務器連接密碼(默認為空)
spring.redis.password=@spring.redis.password@
# 連接池最大連接數(使用負值表示沒有限制)
spring.redis.pool.max-active=1000
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.pool.max-wait=200
# 連接池中的最大空閑連接
spring.redis.pool.max-idle=50
# 連接池中的最小空閑連接
spring.redis.pool.min-idle=10
# 連接超時時間(毫秒)
spring.redis.timeout=0
################ redis end ##################