【SpringBoot1.x】SpringBoot1.x 開發熱部署和監控管理
阿新 • • 發佈:2021-01-03
SpringBoot1.x 開發熱部署和監控管理
熱部署
在開發中我們修改一個 Java 檔案後想看到效果不得不重啟應用,這導致大量時間花費,我們希望不重啟應用的情況下,程式可以自動部署(熱部署)。
- 模板引擎
- 在 SpringBoot 中開發情況下禁用模板引擎的 cache
- 頁面模板改變,按 CTRL+F9 可以重新編譯當前頁面並生效
- Spring Loaded
- Spring 官方提供的熱部署程式,實現修改類檔案的熱部署
- 下載 Spring Loaded
- 新增執行時引數,javaagent:/parzulpan/dev/springloaded-xx.jar -noverify
- JRebel
- 收費的一個熱部署軟體
- 安裝外掛使用即可
- Spring Boot Devtools
-
推薦使用
-
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
-
按 CTRL+F9 生效
-
監控管理
通過引入 spring-boot-starter-actuator,可以使用 Spring Boot 為我們提供的準生產環境下的應用監控和管理功能。我們可以通過HTTP,JMX,SSH協議來進行操作,自動得到審計、健康及指標資訊等。
使用步驟:
-
引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
通過 http 方式訪問監控端點
-
可進行 shutdown(POST 提交,此端點預設關閉)
監控和管理端點說明:
autoconfig
所有自動配置資訊auditevents
審計事件beans
所有 Bean 的資訊configprops
所有配置屬性dump
執行緒狀態資訊env
當前環境資訊health
應用健康狀況info
當前應用資訊metrics
應用的各項指標mappings
應用 @RequestMapping 對映路徑shutdown
關閉當前應用(預設關閉)trace
追蹤資訊(最新的 http 請求)
定製端點資訊
- 定製端點一般通過
endpoints+端點名+屬性名
來設定,比如- 修改端點 id
endpoints.beans.id=mybeans
- 開啟遠端應用關閉功能
endpoints.shutdown.enabled=true
- 關閉端點
endpoints.beans.enabled=false
- 開啟所需端點
endpoints.enabled=false
endpoints.beans.enabled=true
- 修改端點 id
- 定製端點訪問根路徑
management.context-path=/manage
- 關閉 http 端點
management.port=-1
自定義 HealthIndicator
自定義步驟:
- 編寫一個指示器實現 HealthIndicator 介面
- 指示器的名字為 xxHealthIndicator
- 將其加入容器中
/**
* @Author : parzulpan
* @Time : 2021-01
* @Desc : 自定義健康指示器
*/
@Component
public class MyAppHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 自定義檢查方法
// Health.up().build() 代表健康
// Health.down().build() 代表不健康,還可以帶上資訊
return Health.down().withDetail("msg", "服務異常").build();
}
}