SpringBoot之如何正確、安全的關(guān)閉服務(wù)
SpringBoot正確安全的關(guān)閉服務(wù)
我們利用遠(yuǎn)程關(guān)閉功能可以實現(xiàn)優(yōu)雅地關(guān)閉指定地服務(wù)。
正文
本文依然使用v1.5.8.RELEASE ,講地是利用actuator的Endpoints實現(xiàn)關(guān)閉服務(wù)
首先準(zhǔn)備一個eureka服務(wù),然后啟動他。
然后準(zhǔn)備一個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 #禁用密碼驗證 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)配好,這時可以啟動服務(wù)了,將他注冊在eureka上面,這時我們可以看到下面
然后在終端執(zhí)行 curl -X POST 127.0.0.1:8762/shutdown ,可以看到message:Shutting down,bye...說明成功關(guān)閉了服務(wù)
下面筆者要教給大家一種高級使用的方法,做了一個安全的認(rèn)證,上面關(guān)閉服務(wù)的缺點大家顯而易見,知道服務(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 #禁用密碼驗證 endpoints.shutdown.sensitive=true #驗證用戶名 security.user.name=admin #驗證密碼 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>
大功告成,是不是很簡單,下面啟動你的客戶端服務(wù),這里我就不貼一些多余的圖片了,成功注冊到eureka上面了,和上面的圖一樣。
接下來使用終端訪問 curl -X POST -u admin:admin 127.0.0.1:8081/custompath
看見了你的服務(wù)又和你say byebye了吧!
這個命令 curl -X POST -u admin:admin 127.0.0.1:8081/custompath 每一個位置對應(yīng)的參數(shù)值大家可以看application.properties文件分別對應(yīng)了哪些配置就明白了。
SpringBoot2.0.4關(guān)閉程序,我走過的那些坑
首次接觸springboot項目,在本地測試的時候,發(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 # 禁用密碼驗證 endpoints.shutdown.sensitive=false
第三步:http://IP:端口號/actuator/shutdown或者h(yuǎn)ttp://IP:端口號/shutdown
結(jié)果:
404!?。。。。?!
為什么總是404?
后來幡然醒悟,別人都是springboot 1.X,而我的是2.X。(springboot變化好大o(╥﹏╥)o)
接著,我繼續(xù)查2.0以上版本怎么解決,結(jié)果大多數(shù)是在啟動類加一推代碼……可能是我不會用吧,反正沒成功。繼續(xù)找……
后來看到大多數(shù)人又說,下面的方式配置:
management: endpoints: web: exposure: include: "*"
然后看日志,發(fā)現(xiàn)所有的端點都打開了,就shutdown沒打開o(╥﹏╥)o
實在找不到相關(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)寫成了這樣:
##錯誤寫法!?。。。。。。。。。。。。。。?! management: endpoints: web: exposure: include: "*" shutdown: enabled: true
注意哈,這是錯誤寫法,我把endpoints當(dāng)成了endpoint?。?!他們可是不一樣的啊!
最終寫法:
management: endpoints: web: exposure: include: shutdown #注意下面這個位置??! endpoint: shutdown: enabled: true
注:include后面可以添加你想用到的端點 。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java后端+前端使用WebSocket實現(xiàn)消息推送的詳細(xì)流程
后端向前端推送消息就需要長連接,首先想到的就是websocket,下面這篇文章主要給大家介紹了關(guān)于java后端+前端使用WebSocket實現(xiàn)消息推送的詳細(xì)流程,需要的朋友可以參考下2022-10-10深入解析堆排序的算法思想及Java代碼的實現(xiàn)演示
堆排序基于二叉堆結(jié)構(gòu)即完全二叉樹,可利用最大堆和最小堆的組建方式來進(jìn)行排序,這里就來深入解析堆排序的算法思想及Java代碼的實現(xiàn)演示2016-06-06springboot?publish?event?事件機(jī)制demo分享
這篇文章主要介紹了springboot?publish?event?事件機(jī)制demo,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10解決SpringCloud下spring-boot-maven-plugin插件的打包問題
這篇文章主要介紹了SpringCloud下spring-boot-maven-plugin插件的打包問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03