美文网首页程序员开发
利用Actuator构建Spring Boot的监控

利用Actuator构建Spring Boot的监控

作者: whthomas | 来源:发表于2016-07-14 20:54 被阅读1922次

    当我们的服务发布的时候,需要一些能够监控应用依赖服务的服务,而且这些服务需要能够以合适的接口开放给我们。

    Spring Boot提供了一个Actuator组件帮助我们构建这个体系。

    加入这个监控非常简单,在pom.xml添加一个dependency

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

    这里以mongo服务做一个例子,在包中加入一个mongo监控类:

    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失去响应 告警!!!!");
    
                throw e;
            }
    
            builder.up().withDetail("mongoClient",client.getAddress());
        }
    }
    

    以这个MongoMonitor为例,一般加入Actuator的监控类需要继承AbstractHealthIndicator这个抽象类,然后在doHealthCheck()方法中加入监测服务健康的方法。

    builder.up()方法表示服务总是在健康状态。

    然后在Spring Boot的容器中注册这个健康检测类:

    @Bean
    HealthIndicator mongoHealthIndicator(MongoClient mongoClientMain){
      return new MongoMonitor(mongoClientMain);
    }
    

    启动这个服务之后,如果是web应用可以通过url看到这个服务的健康状况:

    ➜  ~ 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"
    }
    

    依赖服务挂掉的时候:

    ➜  ~ 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"
    }
    

    如果我们启用的不是一个web应用,那么我们也可以通过jmx的方式来监测这个应用的健康,使用jdk自带的jconsole连接到对应的应用,可以在mbean中看到当前应用的健康情况:

    通过jmx查看应用依赖服务的健康状况

    以上就是一个简单的Actuator健康监测应用了。

    相关文章

      网友评论

        本文标题:利用Actuator构建Spring Boot的监控

        本文链接:https://www.haomeiwen.com/subject/dktyjttx.html