Springboot读取外部配置文件,项目部署时配置读取不到问题

需求:

最近项目部署,但是每个地方都有个别地方配置的差异,我们每次打包后再进行修改极度不方便,这里将有关的配置都抽取出来,放在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 &


  1. application配置
  2. 非默认配置文件,这里我是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文件夹要和启动根目录的相对路径是同一级,否则读取不到配置,需要在启动项目时指定配置文件位置。

扫描二维码关注公众号,回复: 16239647 查看本文章

例: 现在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 

猜你喜欢

转载自blog.csdn.net/chang100111/article/details/122191892