一、什么是spring-boot-starter-actuator(doc)
springboot項目如何檢查配置與運行狀態呢?官方提供了一些接口可以查看springboot項目運行情況,只需要導入spring-boot-starter-actuator,項目集成:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
二、監控api
幾個api示例:
/autoconfig : 查看自動配置
/beans : 查看beans
為了安全起見,可以單獨設置監控的端口和訪問地址:
management.port=9000 # 通過9000端口訪問
management.address=127.0.0.1 # 只允許本機訪問
/shotdown : 關閉
打開shutdown功能:
endpoints:
shutdown:
enabled: true #啟用shutdown
sensitive: false #禁用密碼驗證
執行完畢,啟動的項目已被終止了。
安全設置,設置賬號密碼,只有擁有賬號密碼的才能shutdown
1、引用security
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2、開啟安全驗證
endpoints: shutdown: enabled: true #啟用shutdown sensitive: true #開啟shutdown的安全驗證 path: /testshutdown #指定shutdown endpoint的路徑 management: port: 9000 #指定管理端口 address: 127.0.0.1 #指定管理IP security: role: SUPERUSER #角色 security: user: name: admin #驗證用戶名 password: 123 #驗證密碼
3、postman請求shutdown