在生產(chǎn)環(huán)境中,需要實(shí)時(shí)或定期監(jiān)控服務(wù)的可用性。spring-boot 的actuator(監(jiān)控)功能提供了很多監(jiān)控所需的接口。簡(jiǎn)單的配置和使用如下:
創(chuàng)新互聯(lián)公司自2013年起,先為梁溪等服務(wù)建站,梁溪等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為梁溪企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
1、引入依賴(lài):
org.springframework.boot spring-boot-starter-actuator
如果使用http調(diào)用的方式,還需要這個(gè)依賴(lài):
org.springframework.boot spring-boot-starter-web
2、配置:
application.yml中指定監(jiān)控的HTTP端口(如果不指定,則使用和server相同的端口);指定去掉某項(xiàng)的檢查(比如不監(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:在某個(gè)類(lèi)中implements HealthIndicator接口,然后實(shí)現(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)用的時(shí)候,返回多一個(gè)屬性:"mySpringBootApplication":{"status":"UP","hello":"world"} */ @Override public Health health() { return Health.up().withDetail("hello", "world").build(); } }
/health 運(yùn)行結(jié)果(注意第二個(gè)指標(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:配置如下,可以直接給一個(gè)字符串,也可以從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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。