當(dāng)我們的服務(wù)發(fā)布的時(shí)候,需要一些能夠監(jiān)控應(yīng)用依賴服務(wù)的服務(wù),而且這些服務(wù)需要能夠以合適的接口開(kāi)放給我們。
Spring Boot提供了一個(gè)Actuator組件幫助我們構(gòu)建這個(gè)體系。
加入這個(gè)監(jiān)控非常簡(jiǎn)單,在pom.xml添加一個(gè)dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
這里以mongo服務(wù)做一個(gè)例子,在包中加入一個(gè)mongo監(jiān)控類:
import org.bson.Document;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import com.mongodb.MongoClient;
public class MongoMonitor extends AbstractHealthIndicator {
private final MongoClient client;
public MongoMonitor(MongoClient client) {
this.client = client;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
try {
Document document = new Document();
document.put("buildInfo",1);
Document result = client.getDatabase("test").runCommand(document);
}catch (Exception e){
System.out.println("mongo失去響應(yīng) 告警!!!!");
throw e;
}
builder.up().withDetail("mongoClient",client.getAddress());
}
}
以這個(gè)MongoMonitor為例,一般加入Actuator的監(jiān)控類需要繼承AbstractHealthIndicator這個(gè)抽象類,然后在doHealthCheck()方法中加入監(jiān)測(cè)服務(wù)健康的方法。
builder.up()方法表示服務(wù)總是在健康狀態(tài)。
然后在Spring Boot的容器中注冊(cè)這個(gè)健康檢測(cè)類:
@Bean
HealthIndicator mongoHealthIndicator(MongoClient mongoClientMain){
return new MongoMonitor(mongoClientMain);
}
啟動(dòng)這個(gè)服務(wù)之后,如果是web應(yīng)用可以通過(guò)url看到這個(gè)服務(wù)的健康狀況:
? ~ http get http://localhost:8080/health
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Date: Thu, 14 Jul 2016 12:45:16 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
X-Application-Context: application
{
"diskSpace": {
"free": 124176261120,
"status": "UP",
"threshold": 10485760,
"total": 249821663232
},
"mongo": {
"mongoClient": {
"host": "127.0.0.1",
"port": 27017,
"socketAddress": "127.0.0.1:27017"
},
"status": "UP"
},
"status": "UP"
}
依賴服務(wù)掛掉的時(shí)候:
? ~ http get http://localhost:8080/health
HTTP/1.1 503 Service Unavailable
Connection: close
Content-Type: application/json;charset=UTF-8
Date: Thu, 14 Jul 2016 12:51:14 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
X-Application-Context: application
{
"diskSpace": {
"free": 124172673024,
"status": "UP",
"threshold": 10485760,
"total": 249821663232
},
"mongo": {
"error": "com.mongodb.MongoTimeoutException: Timed out after 1000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]",
"status": "DOWN"
},
"status": "DOWN"
}
如果我們啟用的不是一個(gè)web應(yīng)用,那么我們也可以通過(guò)jmx的方式來(lái)監(jiān)測(cè)這個(gè)應(yīng)用的健康,使用jdk自帶的jconsole連接到對(duì)應(yīng)的應(yīng)用,可以在mbean中看到當(dāng)前應(yīng)用的健康情況:

以上就是一個(gè)簡(jiǎn)單的Actuator健康監(jiān)測(cè)應(yīng)用了。