詳解spring-bootactuator(監(jiān)控)配置和使用

在生產(chǎn)環(huán)境中,需要實時或定期監(jiān)控服務(wù)的可用性。spring-boot 的actuator(監(jiān)控)功能提供了很多監(jiān)控所需的接口。簡單的配置和使用如下:

創(chuàng)新互聯(lián)公司自2013年起,先為梁溪等服務(wù)建站,梁溪等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為梁溪企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

1、引入依賴:

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-actuator</artifactId> 
</dependency> 

如果使用http調(diào)用的方式,還需要這個依賴:

<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指標(biāo):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、自定義指標(biāo):

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接口調(diào)用的時候,返回多一個屬性:"mySpringBootApplication":{"status":"UP","hello":"world"} 
   */ 
  @Override 
  public Health health() { 
    return Health.up().withDetail("hello", "world").build(); 
  } 
} 

/health 運(yùn)行結(jié)果(注意第二個指標(biāo)):

{"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的結(jié)果如下:

{"app":{"name":"my-spring-boot","description":"Test Project for Spring Boot","version":"1.0","spring-boot-version":"1.3.6.RELEASE"}}

官網(wǎng):http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready

源代碼參考:https://github.com/xujijun/my-spring-boot

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:詳解spring-bootactuator(監(jiān)控)配置和使用
網(wǎng)頁路徑:http://muchs.cn/article42/pdgdhc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、外貿(mào)建站、域名注冊動態(tài)網(wǎng)站、移動網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司