第11章 Spring Boot使用Actuator

 

  在生產環境中,須要實時或按期監控服務的可用性,spring-Boot的Actuator 功能提供了不少監控所需的接口。node

Actuator是Spring Boot提供的對應用系統的自省和監控的集成功能,能夠對應用系統進行配置查看、健康檢查、相關功能統計等,通常運維人員使用多些。web

11.1 使用該功能步驟

咱們這裏監控03-springboot-web程序spring

11.1.1        在項目的pom.xml中添加以下依賴

<!--Spring Boot Actuator依賴-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

11.1.2        在application.properties或application.yml配置文件中指定監控的HTTP端口及路徑

我這裏沒有進行配置json

  • #服務運行的端口

  server.port=8080瀏覽器

  server.servlet.context-path=/03-springboot-webspringboot

  • #actuator監控的端口(端口可配可不配,若是不配置,則使用和server.port相同的端口)

  management.server.port=8100app

  • #actuator監控的訪問上下文根路徑(路徑可配可不配,若是不配置,則使用和server.context-path相同的路徑)

  management.server.servlet.context-path=/03-springboot-web運維

 

11.1.3        在application.properties或application.yml配置文件中設置開啓全部的端口

#默認只開啓了health和info,設置爲*,則包含全部的web入口端點 management.endpoints.web.exposure.include=*

11.1.4        啓動MySQL

11.1.5        啓動Redis

 

11.1.7       啓動03-springboot-web

 

(1)   瀏覽器訪問http://localhost:8080/actuator/health

 

(2)瀏覽器訪問http://localhost:8080/actuator/info

默認沒有內容spring-boot

 

 

須要本身在application.properties配置文件中添加信息,須要以info開頭,後面的內容能夠本身設定,通常配置項目的版權等信息,例如url

 

#配置項目版權相關信息 info.contact.email=bjpowernode@163.com info.contact.phone=010-84846003

配置完畢後,重啓項目再進行訪問

 

 

 

11.2 Actuator 提供的主要功能

 

HTTP方法

路徑

描述

GET

/configprops

查看配置屬性,包括默認配置
http://www.haojson.com對json進行格式化

GET

/beans

查看Spring容器目前初始化的bean及其關係列表

GET

/env

查看全部環境變量

GET

/mappings

查看全部url映射

GET

/health

查看應用健康指標

GET

       /info

查看應用信息

GET

/metrics

查看應用基本指標

GET

/metrics/{name}

查看具體指標

JMX

/shutdown

關閉應用

 

 

shutdown的使用

注意:/shutdown不能直接在瀏覽器中訪問

  • 先在application.properties文件中配置
# 啓用關閉springboot的服務 management.endpoint.shutdown.enabled=true
  • 雙擊打開jconsole

 

 

  • 選擇Spring Boot進程,進行鏈接管理

 

  • 點擊shutdown,可關閉Spring Boot服務
相關文章
相關標籤/搜索