spring boot,作為spring框架對“約定優先于配置(convention over configuration)”理念的最佳實踐的產物,它能幫助我們很快捷的創建出獨立運行、產品級別的基于spring框架的應用,大部分spring boot應用只需要非常少的配置就可以快速運行起來,是一個與微服務(microservices)相當契合的微框架。
下面主要有兩種方式進行spring boot的關閉:通過http發送shutdown信號,或者通過service stop的方式。
一、通過http發送shutdown信號關閉應用
該方式主要依賴spring boot actuator的endpoint特性,具體步驟如下:
1、在pom.xml中引入actuator依賴
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator</artifactid> </dependency> |
2、開啟shutdown endpoint
spring boot actuator的shutdown endpoint默認是關閉的,因此在application.properties中開啟shutdown endpoint:
1
2
3
4
|
#啟用shutdown endpoints.shutdown.enabled= true #禁用密碼驗證 endpoints.shutdown.sensitive= false |
指定路徑、ip、端口
1
2
3
4
5
6
|
#指定shutdown endpoint的路徑 endpoints.shutdown.path=/custompath #也可以統一指定所有endpoints的路徑`management.context-path=/manage` #指定管理端口和ip management.port= 8081 management.address= 127.0 . 0.1 |
3、發送shutdown信號
shutdown的默認url為host:port/shutdown,當需要停止服務時,向服務器post該請求即可,如:
1
|
curl -x post host:port/shutdown |
將得到形如{"message":"shutting down, bye..."}的響應
4、安全設置
可以看出,使用該方法可以非常方便的進行遠程操作,但是需要注意的是,正式使用時,必須對該請求進行必要的安全設置,比如借助spring-boot-starter-security進行身份認證:
pom.xml添加security依賴
1
2
3
4
|
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-security</artifactid> </dependency> |
開啟安全驗證
在application.properties中變更配置
1
2
3
4
5
6
7
8
|
#開啟shutdown的安全驗證 endpoints.shutdown.sensitive= true #驗證用戶名 security.user.name=admin #驗證密碼 security.user.password=secret #角色 management.security.role=superuser |
注意:如果引入了security框架后,按照上面的配置,那么全部請求都會要求輸入賬號密碼才能訪問。
二、部署為unix/linux service
該方式主要借助官方的spring-boot-maven-plugin創建"fully executable" jar ,這中jar包內置一個shell腳本,可以方便的將該應用設置為unix/linux的系統服務(init.d service),官方對該功能在centos和ubuntu進行了測試,對于os x和freebsd,可能需要自定義。具體步驟如下:
1、在pom.xml中引入插件:
1
2
3
4
5
6
7
|
<plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> <configuration> <executable> true </executable> </configuration> </plugin> |
注意:標紅部分的意思是是否是可以執行的。
2、賦予可執行權限:
1
|
chmod u+x app.jar |
說明:到了這一步之后基本可以在命令行運行,先打包出jar包,然后啟動,比如./app.jar start即可啟動。
3、設置為系統服務
將你的應用打成jar包,部署到服務器,假設部署路徑為/var/app,包名為app.jar,通過如下方式將應該設置為一個系統服務:
1
|
sudo ln -s /var/app/app.jar /etc/init.d/app |
4. 以系統服務的方式管理
接下來,就可以使用我們熟悉的service foo start|stop|restart來對應用進行啟停等管理了
1
|
sudo service app start|stop |
命令將得到形如started|stopped [pid]的結果反饋
默認pid文件路徑: /var/run/appname/appname.pid
默認日志文件路徑: /var/log/appname.log
這可能是我們更熟悉也更常用的管理方式。
提示:上面的的日志和存放pid的文件根據不同的系統可能出現的位置不一樣。
5、自定義參數
在這種方式下,我們還可以使用自定義的.conf文件來變更默認配置,方法如下:
1)在jar包相同路徑下創建一個.conf文件,名稱應該與.jar的名稱相同,如appname.conf
2)在其中配置相關變量,如:
1
2
3
|
java_home=/usr/local/jdk java_opts=-xmx1024m log_folder=/custom/log |
6、安全設置
- 作為應用服務,安全性是一個不能忽略的問題,如下一些操作可以作為部分基礎設置參考:
- 為服務創建一個獨立的用戶,同時最好將該用戶的shell綁定為/usr/sbin/nologin
- 賦予最小范圍權限:chmod 500 app.jar
- 阻止修改:sudo chattr +i app.jar
- 對.conf文件做類似的工作:chmod 400 app.conf,sudo chown root:root app.conf
references:
1.installing spring boot applications
2.endpoints
3.securing sensitive endpoints
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/lobo/p/5657684.html