spring Boot 其默認是集成web容器的,啓動方式由像普通Java程序同樣,main函數入口啓動。其內置Tomcat容器或Jetty容器,具體由配置來決定(默認Tomcat)。固然你也能夠將項目打包成war包,放到獨立的web容器中(Tomcat、weblogic等等),固然在此以前你要對程序入口作簡單調整。java
項目構建咱們使用Maven或Gradle,這將使項目依賴、jar包管理、以及打包部署變的很是方便。mysql
Spring Boot將容器內置後,它經過配置文件的方式類修改相關server配置。
先看一下下面的圖,爲關於server的配置列項:
linux
其中經常使用的配置只有少數幾個,已經用紫色標記起來。紅框圈起來的部分,看名稱分類就能夠明白其做用。
對server的幾個經常使用的配置作個簡單說明:web
# 項目contextPath,通常在正式發佈版本中,咱們不配置 server.context-path=/myspringboot # 錯誤頁,指定發生錯誤時,跳轉的URL。請查看BasicErrorController源碼便知 server.error.path=/error # 服務端口 server.port=9090 # session最大超時時間(分鐘),默認爲30 server.session-timeout=60 # 該服務綁定IP地址,啓動服務器時如本機不是該IP地址則拋出異常啓動失敗,只有特殊需求的狀況下才配置 # server.address=192.168.16.11
Tomcat
Tomcat爲Spring Boot的默認容器,下面是幾個經常使用配置:spring
# tomcat最大線程數,默認爲200 server.tomcat.max-threads=800 # tomcat的URI編碼 server.tomcat.uri-encoding=UTF-8 # 存放Tomcat的日誌、Dump等文件的臨時文件夾,默認爲系統的tmp文件夾(如:C:\Users\Shanhy\AppData\Local\Temp) server.tomcat.basedir=H:/springboot-tomcat-tmp # 打開Tomcat的Access日誌,並能夠設置日誌格式的方法: #server.tomcat.access-log-enabled=true #server.tomcat.access-log-pattern= # accesslog目錄,默認在basedir/logs #server.tomcat.accesslog.directory= # 日誌文件目錄 logging.path=H:/springboot-tomcat-tmp # 日誌文件名稱,默認爲spring.log logging.file=myapp.log
Jetty
若是你要選擇Jetty,也很是簡單,就是把pom中的tomcat依賴排除,並加入Jetty容器的依賴,以下:sql
<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進入項目目錄,使用 mvn clean package 命令打包,以個人項目工程爲例:數據庫
E:\spring-boot-sample>mvn clean package
能夠追加參數 -Dmaven.test.skip=true 跳過測試。
打包後的文件存放於項目下的target目錄中,如:spring-boot-sample-0.0.1-SNAPSHOT.jar
若是pom配置的是war包,則爲spring-boot-sample-0.0.1-SNAPSHOT.wartomcat
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()); } }
<!-- <packaging>jar</packaging> --> <packaging>war</packaging>
<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>
spring boot 能夠在 「配置文件」、「Java代碼類」、「日誌配置」 中來配置profile區分不一樣環境執行不一樣的結果安全
一、配置文件
使用配置文件application.yml 和 application.properties 有所區別
以application.properties 爲例,經過文件名來區分環境 application-{profile}.properties
application.propertiesspringboot
app.name=MyApp server.port=8080 spring.profiles.active=dev
application-dev.properties
server.port=8081
application-stg.properties
server.port=8082
在啓動程序的時候經過添加 –spring.profiles.active={profile} 來指定具體使用的配置
例如咱們執行 java -jar demo.jar –spring.profiles.active=dev 那麼上面3個文件中的內容將被如何應用?
Spring Boot 會先加載默認的配置文件,而後使用具體指定的profile中的配置去覆蓋默認配置。
app.name 只存在於默認配置文件 application.properties 中,由於指定環境中不存在一樣的配置,因此該值不會被覆蓋
server.port 默認爲8080,可是咱們指定了環境後,將會被覆蓋。若是指定stg環境,server.port 則爲 8082
spring.profiles.active 默認指定dev環境,若是咱們在運行時指定 –spring.profiles.active=stg 那麼將應用stg環境,最終 server.port 的值爲8082
二、Java類中@Profile註解
下面2個不一樣的類實現了同一個接口,@Profile註解指定了具體環境
// 接口定義 public interface SendMessage { // 發送短信方法定義 public void send(); } // Dev 環境實現類 @Component @Profile("dev") public class DevSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Dev Send()<<<<<<<<"); } } // Stg環境實現類 @Component @Profile("stg") public class StgSendMessage implements SendMessage { @Override public void send() { System.out.println(">>>>>>>>Stg Send()<<<<<<<<"); } } // 啓動類 @SpringBootApplication public class ProfiledemoApplication { @Value("${app.name}") private String name; @Autowired private SendMessage sendMessage; @PostConstruct public void init(){ sendMessage.send();// 會根據profile指定的環境實例化對應的類 } }
三、logback-spring.xml也支持有節點來支持區分
<?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>
再說一遍文件名不要用logback.xml 請使用logback-spring.xml
有些系統,關於一些數據庫或其餘第三方帳戶等信息,因爲安全問題,其配置並不會提早配置在項目中暴露給開發人員。
對於這種狀況,咱們在運行程序的時候,能夠經過參數指定一個外部配置文件。
以 demo.jar 爲例,方法以下:
java -jar demo.jar --spring.config.location=/opt/config/application.properties
其中文件名隨便定義,無固定要求。
下面幾個腳本僅供參考,請根據本身須要作調整
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