need
Recently, the project has been deployed, but each place has different configurations in some places. It is extremely inconvenient for us to modify them after each package. Here we extract all the relevant configurations and place them outside the jar package for configuration. In this way, when updating in the future, you only need to replace the jar package and restart it, and the configuration can be used to read the external fixed configuration file.
SpringBoot Configuration
Springboot default configuration loading position and priority order
Springboot starts to scan the following locations and files as the default configuration files.
- file: ./config/
- file: ./
- classpath: /config/
- classpath: /
All configuration files will be loaded, but high-priority configurations will override low-priority configurations. The above priority is arranged from high to low, and the ./config/ has the highest priority. When we create the project, the default priority is placed in the resources directory.
classpath can be understood as the resource folder in our project when it is not packaged, and the file is understood as the directory at the same level as the jar package (if the relative position of the startup path written when starting the project in Linux and the file of the file is not at the same level, the external configuration file will fail to read, you can turn to the bottom of the article).
Files in the configuration file will be loaded first
Specify the location of the configuration file when starting the project
java -jar -=/home/test/
Load as configuration file under the /home/test folder at startup.
You can also specify ports, effective configuration files, log printing, etc.
java -jar -=prod -=/home/test/ -=8081 > logs/ 2>&1 &
Application Configuration
- Non-default configuration file, here I am
You can specify the location of the configuration file by annotating _@PropertySource_. There are two ways to provide classpath and file in value.
@Configuration @ConfigurationProperties(prefix = "config", ignoreUnknownFields = false) @PropertySource(name = "" ,value = {"classpath:/","file:./config/"} ,ignoreResourceNotFound = true ,encoding = "UTF-8")
value is an array that can place multiple configuration files, load from left to right, and the following will overwrite the previous configuration. ignoreResourceNotFound = true is mainly used to ignore the situation where the file does not exist. If the corresponding file content is not found in the first directory, continue to find the configuration backwards and the first file cannot be found.
ConfigurationProperties is used to reduce the parameters of the @Value annotation below. If this annotation is not added, the configuration item is configured with = 'liuch'
At this time your @Value("" ) needs to be written in full. If it is configured, @Value("{}") needs to be written in full. If it is configured, @Value("config.maintType")Need to write it in full, if configured@Value("{maintType}")
Configuration read failed during deployment
Note: When starting a project in Linux, the config folder must be at the same level as the relative path of the startup root directory. Otherwise, the configuration cannot be read, and you need to specify the configuration file location when starting the project.
example:
Now jar and config are placed in the /home/liuch/app/ folder. When we start, we need to first enter the /home/liuch/app directory, and then
nohup java -jar >/dev/null 2>&1 &, directly use the command nohup java -jar /home/liuch/app/ >/dev/null 2>&1 & will not read the configuration file under the config folder when starting. You need to use -= to specify the configuration file location. At this time, our non-default configuration file cannot be read. We can configure the non-default configuration file location in the file that needs to be loaded.
server: port: 8088 config: path: /home/liuch/
@PropertySource(value = {"file:${}"}, encoding="utf-8")
The usage of @Value below is the same as before.
Attached:
Parameters when springboot project starts
/usr/local/java/jdk1.8.0_131/bin/java -jar -server -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/data/spb_zcmweb/8103/dump/heap/ -=/data/liuch/tmp/ -=8103 - -=5103 -=6103 -=false -=false -=/usr/local/java/jdk1.8.0_131/jre/lib/management/ -Xmx2G -Xms2G -XX:+DisableExplicitGC -verbose:gc -Xloggc:/data/liuch/log/gc.% -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCTaskTimeStamps -XX:+PrintGCDetails -XX:+PrintGCDateStamps --timeout=60000 --count=1000 --threads=300 --spare-threads=65 -=false -=/data/liuch/log/ -=access_log -=combine -=.log -=true --on-rotate=true --attributes-enabled=true -=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 -=60000 -=60000 -=300 -=300 -=false -=file:/dev/./urandom -=UTF-8 -=/data/liuch/log/ -=online /data/liuch/deploy/ zcmweb
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.