詳解Spring Boot 部署與服務(wù)配置
spring Boot 其默認(rèn)是集成web容器的,啟動(dòng)方式由像普通Java程序一樣,main函數(shù)入口啟動(dòng)。其內(nèi)置Tomcat容器或Jetty容器,具體由配置來(lái)決定(默認(rèn)Tomcat)。當(dāng)然你也可以將項(xiàng)目打包成war包,放到獨(dú)立的web容器中(Tomcat、weblogic等等),當(dāng)然在此之前你要對(duì)程序入口做簡(jiǎn)單調(diào)整。
項(xiàng)目構(gòu)建我們使用Maven或Gradle,這將使項(xiàng)目依賴、jar包管理、以及打包部署變的非常方便。
一、內(nèi)嵌 Server 配置
Spring Boot將容器內(nèi)置后,它通過(guò)配置文件的方式類(lèi)修改相關(guān)server配置。
先看一下下面的圖,為關(guān)于server的配置列項(xiàng):
其中常用的配置只有少數(shù)幾個(gè),已經(jīng)用紫色標(biāo)記起來(lái)。紅框圈起來(lái)的部分,看名稱分類(lèi)就可以明白其作用。
對(duì)server的幾個(gè)常用的配置做個(gè)簡(jiǎn)單說(shuō)明:
# 項(xiàng)目contextPath,一般在正式發(fā)布版本中,我們不配置 server.context-path=/myspringboot # 錯(cuò)誤頁(yè),指定發(fā)生錯(cuò)誤時(shí),跳轉(zhuǎn)的URL。請(qǐng)查看BasicErrorController源碼便知 server.error.path=/error # 服務(wù)端口 server.port=9090 # session最大超時(shí)時(shí)間(分鐘),默認(rèn)為30 server.session-timeout=60 # 該服務(wù)綁定IP地址,啟動(dòng)服務(wù)器時(shí)如本機(jī)不是該IP地址則拋出異常啟動(dòng)失敗,只有特殊需求的情況下才配置 # server.address=192.168.16.11
Tomcat
Tomcat為Spring Boot的默認(rèn)容器,下面是幾個(gè)常用配置:
# tomcat最大線程數(shù),默認(rèn)為200 server.tomcat.max-threads=800 # tomcat的URI編碼 server.tomcat.uri-encoding=UTF-8 # 存放Tomcat的日志、Dump等文件的臨時(shí)文件夾,默認(rèn)為系統(tǒng)的tmp文件夾(如:C:\Users\Shanhy\AppData\Local\Temp) server.tomcat.basedir=H:/springboot-tomcat-tmp # 打開(kāi)Tomcat的Access日志,并可以設(shè)置日志格式的方法: #server.tomcat.access-log-enabled=true #server.tomcat.access-log-pattern= # accesslog目錄,默認(rèn)在basedir/logs #server.tomcat.accesslog.directory= # 日志文件目錄 logging.path=H:/springboot-tomcat-tmp # 日志文件名稱,默認(rèn)為spring.log logging.file=myapp.log
Jetty
如果你要選擇Jetty,也非常簡(jiǎn)單,就是把pom中的tomcat依賴排除,并加入Jetty容器的依賴,如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependencies>
打包
打包方法:
CMD進(jìn)入項(xiàng)目目錄,使用 mvn clean package 命令打包,以我的項(xiàng)目工程為例:
E:\spring-boot-sample>mvn clean package
可以追加參數(shù) -Dmaven.test.skip=true 跳過(guò)測(cè)試。
打包后的文件存放于項(xiàng)目下的target目錄中,如:spring-boot-sample-0.0.1-SNAPSHOT.jar
如果pom配置的是war包,則為spring-boot-sample-0.0.1-SNAPSHOT.war
二、部署到JavaEE容器
修改啟動(dòng)類(lèi),繼承 SpringBootServletInitializer 并重寫(xiě) configure 方法
public class SpringBootSampleApplication extends SpringBootServletInitializer{ private static final Logger logger = LoggerFactory.getLogger(SpringBootSampleApplication.class); @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(this.getClass()); } }
修改pom文件中jar 為 war
<!-- <packaging>jar</packaging> --> <packaging>war</packaging>
修改pom,排除tomcat插件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
打包部署到容器
使用命令 mvn clean package 打包后,同一般J2EE項(xiàng)目一樣部署到web容器。
三、使用Profile區(qū)分環(huán)境
spring boot 可以在 “配置文件”、“Java代碼類(lèi)”、“日志配置” 中來(lái)配置profile區(qū)分不同環(huán)境執(zhí)行不同的結(jié)果
1、配置文件
使用配置文件application.yml 和 application.properties 有所區(qū)別
以application.properties 為例,通過(guò)文件名來(lái)區(qū)分環(huán)境 application-{profile}.properties
application.properties
app.name=MyApp server.port=8080 spring.profiles.active=dev
application-dev.properties
server.port=8081
application-stg.properties
server.port=8082
在啟動(dòng)程序的時(shí)候通過(guò)添加 –spring.profiles.active={profile} 來(lái)指定具體使用的配置
例如我們執(zhí)行 java -jar demo.jar –spring.profiles.active=dev 那么上面3個(gè)文件中的內(nèi)容將被如何應(yīng)用?
Spring Boot 會(huì)先加載默認(rèn)的配置文件,然后使用具體指定的profile中的配置去覆蓋默認(rèn)配置。
app.name 只存在于默認(rèn)配置文件 application.properties 中,因?yàn)橹付ōh(huán)境中不存在同樣的配置,所以該值不會(huì)被覆蓋
server.port 默認(rèn)為8080,但是我們指定了環(huán)境后,將會(huì)被覆蓋。如果指定stg環(huán)境,server.port 則為 8082
spring.profiles.active 默認(rèn)指定dev環(huán)境,如果我們?cè)谶\(yùn)行時(shí)指定 –spring.profiles.active=stg 那么將應(yīng)用stg環(huán)境,最終 server.port 的值為8082
2、Java類(lèi)中@Profile注解
下面2個(gè)不同的類(lèi)實(shí)現(xiàn)了同一個(gè)接口,@Profile注解指定了具體環(huán)境
// 接口定義 public interface SendMessage { // 發(fā)送短信方法定義 public void send(); } // Dev 環(huán)境實(shí)現(xiàn)類(lèi) @Component @Profile("dev") public class DevSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Dev Send()<<<<<<<<"); } } // Stg環(huán)境實(shí)現(xiàn)類(lèi) @Component @Profile("stg") public class StgSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Stg Send()<<<<<<<<"); } } // 啟動(dòng)類(lèi) @SpringBootApplication public class ProfiledemoApplication { @Value("${app.name}") private String name; @Autowired private SendMessage sendMessage; @PostConstruct public void init(){ sendMessage.send();// 會(huì)根據(jù)profile指定的環(huán)境實(shí)例化對(duì)應(yīng)的類(lèi) } }
3、logback-spring.xml也支持有節(jié)點(diǎn)來(lái)支持區(qū)分
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/base.xml" /> <logger name="org.springframework.web" level="INFO"/> <springProfile name="default"> <logger name="org.springboot.sample" level="TRACE" /> </springProfile> <springProfile name="dev"> <logger name="org.springboot.sample" level="DEBUG" /> </springProfile> <springProfile name="staging"> <logger name="org.springboot.sample" level="INFO" /> </springProfile> </configuration>
再說(shuō)一遍文件名不要用logback.xml 請(qǐng)使用logback-spring.xml
四、指定外部的配置文件
有些系統(tǒng),關(guān)于一些數(shù)據(jù)庫(kù)或其他第三方賬戶等信息,由于安全問(wèn)題,其配置并不會(huì)提前配置在項(xiàng)目中暴露給開(kāi)發(fā)人員。
對(duì)于這種情況,我們?cè)谶\(yùn)行程序的時(shí)候,可以通過(guò)參數(shù)指定一個(gè)外部配置文件。
以 demo.jar 為例,方法如下:
java -jar demo.jar --spring.config.location=/opt/config/application.properties
其中文件名隨便定義,無(wú)固定要求。
五、創(chuàng)建一個(gè)Linux 應(yīng)用的sh腳本
下面幾個(gè)腳本僅供參考,請(qǐng)根據(jù)自己需要做調(diào)整
start.sh
#!/bin/sh rm -f tpid nohup java -jar myapp.jar --spring.config.location=application.yml > /dev/null 2>&1 & echo $! > tpid echo Start Success!
stop.sh
#!/bin/sh APP_NAME=myapp tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Stop Process...' kill -15 $tpid fi sleep 5 tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpid else echo 'Stop Success!' fi
check.sh
#!/bin/sh APP_NAME=myapp tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'App is running.' else echo 'App is NOT running.' fi
kill.sh
#!/bin/sh APP_NAME=myapp tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'` if [ ${tpid} ]; then echo 'Kill Process!' kill -9 $tpid fi
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 獲取原始請(qǐng)求域名實(shí)現(xiàn)示例
這篇文章主要為大家介紹了Java 獲取原始請(qǐng)求域名實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12MyBatisPlus條件構(gòu)造器的實(shí)現(xiàn)示例
本文主要介紹了MyBatisPlus條件構(gòu)造器的實(shí)現(xiàn)示例,主要包括了QueryWrapper,UpdateWrapper,LambdaQueryWrapper,LambdaUpdateWrapper這四種,具有一定的參考價(jià)值,感興趣的可以了解下2023-12-12Intellij IDEA解析jacoco結(jié)果文件的方法
這篇文章主要介紹了Intellij IDEA解析jacoco結(jié)果文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09手寫(xiě)redis@Cacheable注解?支持過(guò)期時(shí)間設(shè)置方式
這篇文章主要介紹了手寫(xiě)redis@Cacheable注解?支持過(guò)期時(shí)間設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01java實(shí)現(xiàn)區(qū)域內(nèi)屏幕截圖示例
這篇文章主要介紹了java截圖示例,需要的朋友可以參考下2014-04-04