SpringBoot之如何正確、安全的關(guān)閉服務(wù)
SpringBoot正確安全的關(guān)閉服務(wù)
我們利用遠(yuǎn)程關(guān)閉功能可以實(shí)現(xiàn)優(yōu)雅地關(guān)閉指定地服務(wù)。
正文
本文依然使用v1.5.8.RELEASE ,講地是利用actuator的Endpoints實(shí)現(xiàn)關(guān)閉服務(wù)
首先準(zhǔn)備一個(gè)eureka服務(wù),然后啟動(dòng)他。
然后準(zhǔn)備一個(gè)eureka客戶端服務(wù),客戶端的pom除了必要的springboot的web依賴還需要添加依賴如下
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
在eureka客戶端服務(wù)的application.properties文件開啟shutdown endpoint,SpringBoot的endpoints.shutdown.enabled默認(rèn)是關(guān)閉的。
eureka.client.service-url.defaultZone=http://admin:admin@localhost:1111/eureka/ server.port=8762 spring.application.name=eureka-client #啟用shutdown endpoints.shutdown.enabled=true #禁用密碼驗(yàn)證 endpoints.shutdown.sensitive=false #如果用的2.x版本的 就用注釋的那四行配置 #management.endpoints.shutdown.enabled=true #management.endpoints.health.enabled=true #management.endpoints.web.base-path=/ #management.endpoints.web.exposure.include=*
配置已經(jīng)配好,這時(shí)可以啟動(dòng)服務(wù)了,將他注冊(cè)在eureka上面,這時(shí)我們可以看到下面
然后在終端執(zhí)行 curl -X POST 127.0.0.1:8762/shutdown ,可以看到message:Shutting down,bye...說明成功關(guān)閉了服務(wù)
下面筆者要教給大家一種高級(jí)使用的方法,做了一個(gè)安全的認(rèn)證,上面關(guān)閉服務(wù)的缺點(diǎn)大家顯而易見,知道服務(wù)端口和ip的就能關(guān)閉,這種做法很不安全,接下來要在客戶端服務(wù)配置一下安全認(rèn)證。
首先在eureka客戶端服務(wù)的application.properties文件追加配置
eureka.client.service-url.defaultZone=http://admin:admin@localhost:1111/eureka/ server.port=8762 spring.application.name=eureka-client management.security.enabled=true #啟用shutdown endpoints.shutdown.enabled=true #禁用密碼驗(yàn)證 endpoints.shutdown.sensitive=true #驗(yàn)證用戶名 security.user.name=admin #驗(yàn)證密碼 security.user.password=admin #角色 management.security.role=SUPERUSER #指定shutdown endpoint的路徑 endpoints.shutdown.path=/custompath #也可以統(tǒng)一指定所有endpoints的路徑`management.context-path=/manage` #指定管理端口和IP management.port=8081 management.address=127.0.0.1
我們使用了security,就需要在pom添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
大功告成,是不是很簡單,下面啟動(dòng)你的客戶端服務(wù),這里我就不貼一些多余的圖片了,成功注冊(cè)到eureka上面了,和上面的圖一樣。
接下來使用終端訪問 curl -X POST -u admin:admin 127.0.0.1:8081/custompath
看見了你的服務(wù)又和你say byebye了吧!
這個(gè)命令 curl -X POST -u admin:admin 127.0.0.1:8081/custompath 每一個(gè)位置對(duì)應(yīng)的參數(shù)值大家可以看application.properties文件分別對(duì)應(yīng)了哪些配置就明白了。
SpringBoot2.0.4關(guān)閉程序,我走過的那些坑
首次接觸springboot項(xiàng)目,在本地測試的時(shí)候,發(fā)現(xiàn)不知道怎么關(guān)閉程序,雖然后來不得不用殺死進(jìn)程的方式解決,但總覺得這種方式太簡單粗暴。就準(zhǔn)備問問度娘別人都是怎么做的。
結(jié)果普遍答案是:
步驟:
第一步:引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
第二步:application.properties配置
# 啟用shutdown endpoints.shutdown.enabled=true # 禁用密碼驗(yàn)證 endpoints.shutdown.sensitive=false
第三步:http://IP:端口號(hào)/actuator/shutdown或者h(yuǎn)ttp://IP:端口號(hào)/shutdown
結(jié)果:
404?。。。。。?!
為什么總是404?
后來幡然醒悟,別人都是springboot 1.X,而我的是2.X。(springboot變化好大o(╥﹏╥)o)
接著,我繼續(xù)查2.0以上版本怎么解決,結(jié)果大多數(shù)是在啟動(dòng)類加一推代碼……可能是我不會(huì)用吧,反正沒成功。繼續(xù)找……
后來看到大多數(shù)人又說,下面的方式配置:
management: endpoints: web: exposure: include: "*"
然后看日志,發(fā)現(xiàn)所有的端點(diǎn)都打開了,就shutdown沒打開o(╥﹏╥)o
實(shí)在找不到相關(guān)博客了,就去官網(wǎng)找答案
官網(wǎng)鏈接https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
原來人家默認(rèn)是關(guān)著的,那就打開呀!于是我以為發(fā)現(xiàn)了新大陸,就去打開,據(jù)需看官網(wǎng),看到這樣一句。
management.endpoint.shutdown.enabled=true
添加上去,果然成功!
但是,過程中我曾經(jīng)寫成了這樣:
##錯(cuò)誤寫法?。。。。。。。。。。。。。。。?! management: endpoints: web: exposure: include: "*" shutdown: enabled: true
注意哈,這是錯(cuò)誤寫法,我把endpoints當(dāng)成了endpoint?。?!他們可是不一樣的??!
最終寫法:
management: endpoints: web: exposure: include: shutdown #注意下面這個(gè)位置!! endpoint: shutdown: enabled: true
注:include后面可以添加你想用到的端點(diǎn) 。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程
后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實(shí)現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下2022-10-10深入解析堆排序的算法思想及Java代碼的實(shí)現(xiàn)演示
堆排序基于二叉堆結(jié)構(gòu)即完全二叉樹,可利用最大堆和最小堆的組建方式來進(jìn)行排序,這里就來深入解析堆排序的算法思想及Java代碼的實(shí)現(xiàn)演示2016-06-06springboot接受前端請(qǐng)求的方法實(shí)現(xiàn)
本文主要介紹了springboot接受前端請(qǐng)求的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01WebUploader實(shí)現(xiàn)圖片上傳功能
這篇文章主要為大家詳細(xì)介紹了WebUploader實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03springboot?publish?event?事件機(jī)制demo分享
這篇文章主要介紹了springboot?publish?event?事件機(jī)制demo,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10解決SpringCloud下spring-boot-maven-plugin插件的打包問題
這篇文章主要介紹了SpringCloud下spring-boot-maven-plugin插件的打包問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03