在以往的分布式开发当中,各个服务节点的监控必不可少。监控包含有很多方面,比如说:内存占用情况,节点是否健康等。在spring-boot会给我们提供相关资源监控叫做spring-boot-actuator通过执行器可以帮我管理和监控生产环境下的应用服务。

 一。添加SpringBoot执行器的依赖(版本2.0.0.RELEASE)

添加gradle配置依赖:

dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
}

 二。关于SpringBoot的端点

    端点是一个提供给我们监控应用程序的功能点,SpringBoot提供了一系列内置端点叫我们使用,举个例子:health端点为我们提供了一个对我们基础程序的一个健康状态的监控

每个端点都可以打开或关闭,绝大多数的应用都可以通过http请求进行访问,springboot包含很多内置内置端点。tips:参考官网的

ID Description Enabled by default

auditevents

Exposes audit events information for the current application.

Yes

beans

Displays a complete list of all the Spring beans in your application.

Yes

conditions

Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.

Yes

configprops

Displays a collated list of all @ConfigurationProperties.

Yes

env

Exposes properties from Spring’s ConfigurableEnvironment.

Yes

flyway

Shows any Flyway database migrations that have been applied.

Yes

health

Shows application health information.

Yes

httptrace

Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges).

Yes

info

Displays arbitrary application info.

Yes

loggers

Shows and modifies the configuration of loggers in the application.

Yes

liquibase

Shows any Liquibase database migrations that have been applied.

Yes

metrics

Shows ‘metrics’ information for the current application.

Yes

mappings

Displays a collated list of all @RequestMapping paths.

Yes

scheduledtasks

Displays the scheduled tasks in your application.

Yes

sessions

Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications.

Yes

shutdown

Lets the application be gracefully shutdown.

No

threaddump

Performs a thread dump.

Yes

  

有几点要补充说明一下:

  1). 我们访问的端点监控的地址规范是:/actuator/{ID}的方式访问,比如说:health端点默认被映射的路径就是/actuator/health

2) 并不是所有端点都可以通过http请求访问,以下表格列举了各个端点的状态值:

ID JMX Web

auditevents

Yes

No

beans

Yes

No

conditions

Yes

No

configprops

Yes

No

env

Yes

No

flyway

Yes

No

health

Yes

Yes

heapdump

N/A

No

httptrace

Yes

No

info

Yes

Yes

jolokia

N/A

No

logfile

N/A

No

loggers

Yes

No

liquibase

Yes

No

metrics

Yes

No

mappings

Yes

No

prometheus

N/A

No

scheduledtasks

Yes

No

sessions

Yes

No

shutdown

Yes

No

threaddump

Yes

No

  

    我们可以发现默认情况下只有health与info端点才能通过http请求访问,当然我们可以在属性文件中配置 management.endpoints.web.exposure.include 来开放可以访问的端点:
    

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

3)当我们访问端点时,响应的数据会缓存一定的时间,我们可以通过这个属性进行配置:

  

management.endpoint.<id>.cache.time-to-live=10s

4) 我们也可以自己定义监视路径,默认情况是:/actuator/xxx,通过如下属性可以设置:

management.endpoints.web.base-path=/

5) 关于保护敏感端点,首先我们要添加对spring-security的依赖,并设置进行安全验证的用户名,密码以及角色,如果不使用spring-security就要慎重考虑暴露端口的端点了

三。关于Health端点

  1. health端点用于检测我们运行程序的健康状态,当程序宕机时,可以提供给开发人员相关的提示信息
  2. 默认情况下该端点只是显示简略的监控信息,不过我们可以通过management.endpoint.health.show-details属性来让其显示详细的监控信息
  3. 端点有如下几种状态: UP DOWN UNKNOW-SERVICE 从字面上我们很好理解
  4. 实现自定义健康监控:
package com.bdqn.lyrk.springboot.study.monitor;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component; @Component
public class SelfMonitor implements HealthIndicator {
@Override
public Health health() {
return Health.up().withDetail("health","next").build();
}
}

    例子很简单,主要是实现HealthIndicator接口,当我们访问:http://localhost:8080/actuator/health时,我们可以看到相关监控信息,如下图:

    

SpringBoot学习之SpringBoot执行器的更多相关文章

  1. SpringBoot学习(八)-->SpringBoot之过滤器、监听器

    本文将直接使用@WebFilter和@WebListener的方式,完成一个Filter 和一个 Listener. 过滤器(Filter)和 监听器(Listener)的注册方法和 Servlet ...

  2. SpringBoot学习(七)-->SpringBoot在web开发中的配置

    SpringBoot在web开发中的配置 Web开发的自动配置类:在Maven Dependencies-->spring-boot-1.5.2.RELEASE.jar-->org.spr ...

  3. SpringBoot学习(六)-->SpringBoot的自动配置的原理

    Spring Boot的自动配置的原理 Spring Boot在进行SpringApplication对象实例化时会加载META-INF/spring.factories文件,将该配置文件中的配置载入 ...

  4. SpringBoot学习(五)-->SpringBoot的核心

    SpringBoot的核心 1.入口类和@SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的J ...

  5. SpringBoot学习(四)-->SpringBoot快速入门,开山篇

    Spring Boot简介 Spring Boot的目的在于创建和启动新的基于Spring框架的项目.Spring Boot会选择最适合的Spring子项目和第三方开源库进行整合.大部分Spring ...

  6. SpringBoot学习<二>——SpringBoot的默认配置文件application和多环境配置

    一.SpringBoot的默认文件appliction 上一篇文章已经说明,springboot启动会内嵌tomcat,端口也是默认的8080,如果我们想要改变端口如果做呢? 在springboot项 ...

  7. SpringBoot学习(一):SpringBoot入门

    1.Spring Boot 简介 1) 简化Spring应用开发的一个框架: 2) 整个Spring技术栈的一个大整合: 3) J2EE开发的一站式解决方案: 2.微服务 2014,martin fo ...

  8. SpringBoot学习- 5、整合Redis

    SpringBoot学习足迹 SpringBoot项目中访问Redis主要有两种方式:JedisPool和RedisTemplate,本文使用JedisPool 1.pom.xml添加dependen ...

  9. springboot 学习资源推荐

    springboot 是什么?对于构建生产就绪的Spring应用程序有一个看法. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.(这是springboot的官方介绍) 我们为什么要学 ...

随机推荐

  1. AWK读书笔记

    1.awk 'parttern {action}' filename 从文件中逐行读取并匹配parttern,若匹配成功执行action否则读取下一行. parttern和action都可选,若省略p ...

  2. linux系统增加开机启动服务/应用

    操作 在/etc/init.d下新建示例脚本文件(customize.sh),该脚本会启动zookeeper服务.内容如下: #!/bin/sh /usr/local/zookeeper-/bin/z ...

  3. MySQL ID排序乱了的解决办法

    可能在整理表中数据的时候删除了某一行数据,导致ID空缺,下面是我用到的解决办法:(请先备份,MySQL备份方法见 MySQL->MySQL备份) 使用ALTER DROP删除原有的ID字段: A ...

  4. HTTP协议扫盲(二)HTTP协议的请求方法、请求头和响应头

    一.HTTP请求方法 Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET,POST,PUT,DELETE. 一个URL地址用于描述一个网络上的资源,而HTTP中的GET, POST ...

  5. css(1-1)样式表

    CSS Id 和 Class id 和 class 选择器 如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. id ...

  6. ShellCode瘦身的艺术0_HASH

    写在前面的话: 前面几篇文章,我们介绍了如何获取kernerl32.dll导出函数地址的方法: 并在此基础上,编写了ShellCode,实现了动态加载DLL以及解析API地址: 但是,似乎还称不上Pe ...

  7. SpringMVC(二):RequestMapping修饰类、指定请求方式、请求参数或请求头、支持Ant路径

    @RequestMapping用来映射请求:RequestMapping可以修饰方法外,还可以修饰类 1)SpringMVC使用@RequestMapping注解为控制指定可以处理哪些URL请求: 2 ...

  8. 初学Java Web(7)——文件的上传和下载

    文件上传 文件上传前的准备 在表单中必须有一个上传的控件 <input type="file" name="testImg"/> 因为 GET 方式 ...

  9. C# 6.0中你不知道的新特性

    为什么写? 今天去上班的公交上,有朋友在张队(张善友)的微信群里,发了一个介绍C# 6.0新特性的视频,视频7分钟,加上本人英语实在太low,整体看下来是一脸懵逼的. 下班回到家里,打开这个视频,把视 ...

  10. logback生成多个不同的日志文件

    用logback生成日志文件做日志分析,日志写到多个文件中 http://stackoverflow.com/questions/2488558/logback-to-log-different-me ...