網站首頁 編程語言 正文
需求:
最近項目部署,但是每個地方都有個別地方配置的差異,我們每次打包后再進行修改極度不方便,這里將有關的配置都抽取出來,放在jar包外進行配置,這樣以后更新時只需要將jar包更換然后重啟即可,配置讀取外部的固定配置文件。
SpringBoot 配置
springboot默認配置的加載位置和優先級順序
springboot啟動時會掃描以下位置的application.properties和application.yml文件作為默認的配置文件。
- file: ./config/
- file: ./
- classpath: /config/
- classpath: /
所有的配置文件都會被加載,但高優先級的配置會覆蓋低優先的配置,上面的優先級為由高到低排列,./config/ 的優先級最高,我們創建項目時默認的放在resources目錄下的優先級是最低的。 classpath可以理解為我們項目內部的沒打包時的resources文件夾下的,file理解為和jar包同級的目錄(如果在linux中啟動項目時寫的啟動路徑的相對位置和file的文件不是同級時外部配置文件會出現讀取失敗,可以翻到文章最下面)。
配置文件中帶spring.profile的文件會優先被加載
啟動項目時指定配置文件的位置
java -jar xxxx.jar -Dspring.config.location=/home/test/application.yml
啟動時加載/home/test文件夾下的application.yml作為配置文件。這里還可以指定端口、生效的配置文件、日志打印等
java -jar xxxx.jar -Dspring.profiles.active=prod -Dspring.config.location=/home/test/application.yml
-Dserver.port=8081 > logs/xxxlog.log 2>&1 &
?
- application配置
- 非默認配置文件,這里我是config.properties
可以通過注解_@PropertySource_ 來指定配置文件的位置,value中提供兩種方式 classpath 和 file
@Configuration
@ConfigurationProperties(prefix = "config", ignoreUnknownFields = false)
@PropertySource(name = "config.properties"
,value = {"classpath:/config.properties","file:./config/config.properties"}
,ignoreResourceNotFound = true
,encoding = "UTF-8")
value是一個數組,可以放多個配置文件,從左到右進行加載,后面的會覆蓋掉前面的配置,ignoreResourceNotFound = true主要用來忽略文件不存在的情況,如果第一個目錄下沒有找到對應的文件內容,就向后繼續找不加這個配置第一個文件找不到時會報錯。ConfigurationProperties 是用來減少下面@Value注解的參數,如果不加這個注解,配置項中配置的是 config.username = 'liuch’
這時你的@Value("${config.maintType}")
需要寫完整,如果配置了以后@Value("${maintType}")
即可。
部署時配置讀取失敗
?
注意: 在linux啟動項目時,config文件夾要和啟動根目錄的相對路徑是同一級,否則讀取不到配置,需要在啟動項目時指定配置文件位置。
例: 現在jar和config放在 /home/liuch/app/ 文件夾下,我們啟動時需要先進入到 /home/liuch/app 目錄,然后再nohup java -jar xxx.jar >/dev/null 2>&1 &
,直接使用命令nohup java -jar /home/liuch/app/xxx.jar >/dev/null 2>&1 &
啟動時不會去讀取config文件夾下的配置文件,需要使用-Dspring.config.location=
指定配置文件位置,此時我們的非默認配置文件是讀取不到的,我們可以在application.yml
文件中配置需要加載的非默認配置文件位置,
server:
port: 8088
config:
path: /home/liuch/config.properties
@PropertySource(value = {"file:${config.path}"}, encoding="utf-8")
下面的@Value的用法和之前一樣。
?
附:springboot項目啟動時的參數
/usr/local/java/jdk1.8.0_131/bin/java -jar -server -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/data/spb_zcmweb/8103/dump/heap/
-Djava.io.tmpdir=/data/liuch/tmp/
-Dserver.port=8103
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=5103
-Dcom.sun.management.jmxremote.rmi.port=6103
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.access.file=/usr/local/java/jdk1.8.0_131/jre/lib/management/jmxremote.access
-Xmx2G -Xms2G -XX:+DisableExplicitGC -verbose:gc -Xloggc:/data/liuch/log/gc.%t.log -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCTaskTimeStamps -XX:+PrintGCDetails -XX:+PrintGCDateStamps
-Dserver.connection-timeout=60000
-Dserver.tomcat.accept-count=1000
-Dserver.tomcat.max-threads=300
-Dserver.tomcat.min-spare-threads=65
-Dserver.tomcat.accesslog.enabled=false
-Dserver.tomcat.accesslog.directory=/data/liuch/log/
-Dserver.tomcat.accesslog.prefix=access_log
-Dserver.tomcat.accesslog.pattern=combine
-Dserver.tomcat.accesslog.suffix=.log
-Dserver.tomcat.accesslog.rotate=true
-Dserver.tomcat.accesslog.rename-on-rotate=true
-Dserver.tomcat.accesslog.request-attributes-enabled=true
-Dserver.tomcat.accesslog.buffered=true
-XX:NewRatio=4 -XX:SurvivorRatio=30 -XX:TargetSurvivorRatio=90 -XX:MaxTenuringThreshold=8 -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -XX:ParallelGCThreads=24 -XX:ConcGCThreads=24 -XX:-UseGCOverheadLimit -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=1 -XX:+CMSParallelRemarkEnabled -XX:+CMSScavengeBeforeRemark -XX:+ParallelRefProcEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSMaxAbortablePrecleanTime=6000 -XX:CompileThreshold=10 -XX:MaxInlineSize=1024 -Dsun.net.client.defaultConnectTimeout=60000
-Dsun.net.client.defaultReadTimeout=60000
-Dnetworkaddress.cache.ttl=300 -Dsun.net.inetaddr.ttl=300
-Djsse.enableCBCProtection=false
-Djava.security.egd=file:/dev/./urandom
-Dfile.encoding=UTF-8
-Dlog.path=/data/liuch/log/
-Dspring.profiles.active=online
/data/liuch/deploy/xxxx.jar zcmweb
原文鏈接:https://blog.csdn.net/chang100111/article/details/122191892
相關推薦
- 2022-04-17 iOS喚起記住密碼的數字鍵盤,輸入的時候發生閃爍的問題
- 2022-05-01 基于Python的Houdini插件開發過程詳情_python
- 2022-11-28 深入了解C++函數重載解析策略_C 語言
- 2023-11-22 Linux/Ubuntu下如何刪除文件或者目錄
- 2023-11-22 fatal: unable to access ‘https://github.com/xxxxx/
- 2022-07-06 C#多線程之線程池ThreadPool詳解_C#教程
- 2022-07-07 Python實現8個概率分布公式的方法詳解_python
- 2022-03-15 linux系統中計劃任務介紹_Linux
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支