天天看點

springboot多環境配置檔案及jar包部署1、src/main/resources檔案夾結構 2、如何打包src/main/resources和src/main/webapp3、pom中的配置檔案4、@@表達式 5、springboot多配置優先級6、指令多樣化8、打成可執行war包

1、src/main/resources檔案夾結構

springboot多環境配置檔案及jar包部署1、src/main/resources檔案夾結構 2、如何打包src/main/resources和src/main/webapp3、pom中的配置檔案4、@@表達式 5、springboot多配置優先級6、指令多樣化8、打成可執行war包

 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、@@表達式 

[email protected]@
[email protected]@
[email protected]@
           

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啟動

繼續閱讀