springboot多环境配置文件及jar包部署

1、src/main/resources文件夹结构

springboot多环境配置文件及jar包部署

 2、如何打包src/main/resources和src/main/webapp
<resource>
	        	<directory>src/main/resources</directory>
	            <includes>
	             	<include>*</include>
	            </includes>
	            <excludes>
                    <exclude>envs/**</exclude>
                </excludes>
	            <filtering>true</filtering>
	        </resource>
            
            <resource>
            	<!-- 根据环境打包,该文件会被打包到src/main/resources,${env}是下面profiles标签里配置的 -->
                <directory>src/main/resources/envs/${env}</directory>
                <includes>
                    <include>*</include>
                </includes>
            </resource>
    <!-- 打可执行war包时去掉 -->
        <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/**</include>
                </includes>
            </resource>
3、pom中的配置文件

maven的全局配置文件和用户配置文件也可以配置profiles

<profiles>
	
		<profile>
			<id>as-dev</id>
			<properties>
				<port>8081</port>
				<ctx>/dev</ctx>
				<env>as-dev</env>
			</properties>
			
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		
		</profile>
	
		<profile>
			<id>as-test</id>
			<properties>
				<port>8082</port>
				<ctx>/test</ctx>
				<env>as-test</env>
			</properties>
			
		
		
		</profile>
	</profiles>
4、@@表达式 

 

spring.profiles.active=@env@
server.port=@port@
server.servlet.context-path=@ctx@

 

5、springboot多配置优先级

第一次总结:

springboot 2.0:
    spring.config.addtional-location:
        属性互补优先级:(命令行active | 内部active)>spring.config.location
    spring.config.location:
        属性互补优先级:(spring.config.location | 命令行active | 内部active)

springboot 1.0:
    spring.config.location:
        属性互补优先级:(命令行active | 内部active)>spring.config.location

说明:|代表只有一个生效,从左到右优先级从高到低

第二次总结:

1、spring.config.location或spring.config.additional-location的两种写法
    classpath:/application-prod.yml
    file:../config/application-prod.yml

2、spring.config.location完全替代默认的application.yml,spring.config.location指定的文件里面就算有spring.profiles.active也不会生效

3、spring.config.additional-location不会替代默认的application.yml,但如果spring.config.additional-location指定的配置文件里面指定了spring.profiles.active,
    则会替代默认的application.yml中指定的spring.profiles.active,且最终优先级以spring.profiles.active最高
 

 

 

6、命令多样化

1、java -jar  many-env-test-1.0.jar>log.txt  --spring.config.location=application-other.properties,

不能写成-Dspring.config.location

可以写成-Dspring.profiles.active等,比如-Dname="xiao ming" ,value有空格用双引号

-D 在虚拟机的系统属性中设置属性名/值对,运行在此虚拟机之上的应用程序可用System.getProperty(“propertyName”)得到value的值
2、>/dev/null 2>&1    shell命令重定向绑定
3、nohup java -jar many-env-test-1.0.jar >/dev/null 2>&1 &,nohup和&一起使用
4、java -jar -Xms512m -Xmx1024m many-env-test-1.0.jar>log.txt,还可以指定JVM启动参数

 

7、实战脚本

  • 集成start、stop、status
  • 日志备份

 

#!/bin/bash
# The following are the input parameters, you can adjust according to the actual situation
jarname=../apps/eureka-server-1.0.jar
logpath=../logs/
logname=eureka-server.log
active=
port=8092
vmoptions="-Xms1024m -Xmx2048m -XX:MetaspaceSize=1024m -XX:MaxMetaspaceSize=1024m"

# The following code is reliable, do not modify it, otherwise it is very likely to go wrong
if [ -z $1 ];then
echo command error,please specify start or stop or status!
exit
fi
if [ $1 == start ];then
# Determine whether the log path does not exist
if [ ! -d "$logpath" ];then
mkdir $logpath
fi
# Determine whether the log file does not exists
if [ ! -f "$logpath$logname" ];then
nohup java -jar $vmoptions $jarname --spring.profiles.active=$active >$logpath$logname 2>&1 &
else
# It is similar to yyyyMMddHHmm
tmp=$(date "+%Y%m%d%H%M")
# Backup log file
mv $logpath$logname $logpath$logname.$tmp
nohup java -jar $vmoptions $jarname --spring.profiles.active=$active >$logpath$logname 2>&1 &
fi
elif [ $1 == stop ];then
line=$(lsof -i:$port|grep LISTEN|head -n 1)
pidarr=$(lsof -ti:$port)
for pid in ${pidarr[@]}
do
res=$(echo $line | grep "$pid")
if [ "$res" != "" ];then
kill -9 $pid
exit
fi
done
elif [ $1 == status ];then
netstat -anp|grep $port
else
echo command error,please specify start or stop or status!
fi

 

 

8、打成可执行war包

pom文件中的packaging应改为war,并去掉2中打包src/main/webapp的部分

和jar包一样,war包也是通过java -jar启动

 

上一篇:标签下划线的切换,默认选中第一个,点击的时候切换


下一篇:C++ 线程类析构死锁问题