詳解spring-boot actuator(監(jiān)控)配置和使用
在生產環(huán)境中,需要實時或定期監(jiān)控服務的可用性。spring-boot 的actuator(監(jiān)控)功能提供了很多監(jiān)控所需的接口。簡單的配置和使用如下:
1、引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
如果使用http調用的方式,還需要這個依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2、配置:
application.yml中指定監(jiān)控的HTTP端口(如果不指定,則使用和server相同的端口);指定去掉某項的檢查(比如不監(jiān)控health.mail):
server: port: 8082 management: port: 54001 health: mail: enabled: false
3、使用:
查看health指標:http://localhost:54001/health
{"status":"UP","diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}
4、自定義指標:
4.1 /health:在某個類中implements HealthIndicator接口,然后實現(xiàn)其中的health()方法即可:
代碼:
@SpringBootApplication @EnableScheduling public class MySpringBootApplication implements HealthIndicator{ private static Logger logger = LoggerFactory.getLogger(MySpringBootApplication.class); public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class, args); logger.info("My Spring Boot Application Started"); } /** * 在/health接口調用的時候,返回多一個屬性:"mySpringBootApplication":{"status":"UP","hello":"world"} */ @Override public Health health() { return Health.up().withDetail("hello", "world").build(); } }
/health 運行結果(注意第二個指標):
{"status":"UP","mySpringBootApplication":{"status":"UP","hello":"world"},"diskSpace":{"status":"UP","total":120031539200,"free":33554337792,"threshold":10485760},"db":{"status":"UP","dataSource1":{"status":"UP","database":"MySQL","hello":1},"dataSource2":{"status":"UP","database":"MySQL","hello":1}}}
4.2 /info:配置如下,可以直接給一個字符串,也可以從pom.xml配置中獲取
info: app: name: "@project.name@" #從pom.xml中獲取 description: "@project.description@" version: "@project.version@" spring-boot-version: "@project.parent.version@"
/info的結果如下:
{"app":{"name":"my-spring-boot","description":"Test Project for Spring Boot","version":"1.0","spring-boot-version":"1.3.6.RELEASE"}}
官網:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
源代碼參考:https://github.com/xujijun/my-spring-boot
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java過濾器與監(jiān)聽器間區(qū)別與聯(lián)系
監(jiān)聽器是一個接口內容由我們實現(xiàn),會在特定時間被調用,監(jiān)聽器用于監(jiān)聽web應用中三大域對象(request,session,application),信息的創(chuàng)建,銷毀,增加,修改,刪除等動作的發(fā)生,然后做出相應的響應處理2023-01-01Spring?Boot項目中使用OpenAI-Java的示例詳解
Spring?Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程,這篇文章主要介紹了Spring?Boot項目中使用OpenAI-Java的示例詳解,需要的朋友可以參考下2023-04-04springboot打包實現(xiàn)項目JAR包和依賴JAR包分離
這篇文章主要介紹了springboot打包實現(xiàn)項目JAR包和依賴JAR包分離,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02利用Java的Struts框架實現(xiàn)電子郵件發(fā)送功能
這篇文章主要介紹了利用Java的Struts框架實現(xiàn)電子郵件發(fā)送功能,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12MyBatis中的ResultMap的association和collection標簽詳解
這篇文章主要介紹了MyBatis中的ResultMap的association和collection標簽詳解,主要包括association標簽常用參數(shù)及id & result標簽參數(shù)詳解,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10