Spring Boot监控-Actuator原创
金蝶云社区-艾贺521
艾贺521
10人赞赏了该文章 523次浏览 未经作者许可,禁止转载编辑于2019年01月23日 08:21:10
封面

 

Spring Boot上手简单,用起来方便,假设你已经知道Spring Boot是如何使用的,这里我们将一些Spring Boot在生产环境上的监控特性。

 

使用

只需要引入actuator的starter即可,可通过下面maven配置:

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

 

Endpoint

Actutor差价提供了一系列Http请求,可以发送相应的请求获取应用程序的相关信息,这些请求都是Get请求并且不用带参数的。这些信息对于用户来说是敏感信息,一般要经过安全认证的。

列举几个常用的开箱即用的endpoints:

•         /health 展示应用的健康信息,仅仅一个简单的status返回。{"status":"UP"},默认是公开的

•         /info 获取应用的基本信息

•         /metrics 展示当前应用的指标,默认是敏感信息,需要认证

•         /trace 获取请求的调用信息,需认证

•         /beans 获取Spring Bean的基本信息,需认证

•         /dump 获取当前线程的信息,需认证

•         /env 获取环境变量信息,需认证

•         /mappings 获取请求映射信息,需认证

 

1.       假设在开发环境下,我们想要直接访问:

# 关闭安全认证
management.security.enabled=false

image-20190122110622328.png

 

 

image-20190122110649182.png

 

 

1.       想要关闭所有的endpoints

endpoints.enabled=false

 

2.       只想开启某一个endpoints,比如metrics

      endpoints.enabled=false
endpoints.metrics.enabled=true

3.       修改endpoints的名称。

endpoints.metrics.id=perf

image-20190122110905712.png

 

 

图形化Endpoints

每次都通过url来监控会有些麻烦,大家一般都会开发自己的组件来统一提供监控信息,但是除了第三方的组件之外,Spring还内置了一个图形化的获取监控组件,只需要我们添加依赖即可:

       <!--开启actutors监控-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-hateoas</artifactId>
       </dependency>
       <!--提供浏览器访问界面-->
       <dependency>
           <groupId>org.webjars</groupId>
           <artifactId>hal-browser</artifactId>
       </dependency>

 

添加上面依赖之后,访问http://localhost:8080/actuator,可以看到如下的效果,是不是比一个个的访问简单很多了。

image-20190122112558805.png

 

 

image-20190122112624417.png

 

应用基本信息/info

刚才我们提到了默认有个info的endpoints,但是首次访问的时候里面是空的内容,什么都没有,因为通过这个endpoints获取的都是我们自定义的信息,那么如何定义呢,很简单,在application.properties中

info.name=aihe
info.age=23
info.project=demo
info.place=Xiamen

image-20190122112119868.png

 

 

image-20190122111934423.png

 

 

 

自定义Endpoint

除了使用已有的Endpint之外,我们还可以创建一个新的自定义Endpoint,只需要实现Endpoint接口即可。如果不想完全的自定义Endpoint,可以继承AbstractEndpoint,仅仅实现必要的方法即可。

@Component
publicclassCustomEndpoint implementsEndpoint<List<String>> {

    @Override
    publicStringgetId() {
        return"customEndpoint";
    }

    @Override
    publicbooleanisEnabled() {
        returntrue;
    }

    @Override
    publicbooleanisSensitive() {
        returntrue;
    }

    @Override
    publicList<String> invoke() {
        // Custom logic to build the output
        List<String> messages = newArrayList<String>();
        messages.add("自定义Endpoint");
        messages.add("额外的信息");
        messages.add("除了List之外,输出自己想要输出的对象都可以");
        returnmessages;
    }
}

 

image-20190123081519228.png

 

最后效果如图,这样我们就创建了一个全新的Endpoint

 

最后

这里主要讲了一些Spring Boot的生产级监控的特性,在用的时候注意要加上认证,性能监控室所有应用中不可少的一部分,希望能帮到大家。

参考

•         Spring Boot Actuator: Production-ready features

•         Spring Boot Actuator

 

注:

本文独家发布自金蝶云社区


赞 10