`
dwj147258
  • 浏览: 192056 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

强大的 actuator 服务监控与管理

 
阅读更多

1.

 SpringBoot是为了简化Spring应用的创建、运行、调试、部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程

actuatorspring boot项目中非常强大一个功能,有助于对应用程序进行监视和管理,通过restful api请求来监管、审计、收集应用的运行情况,针对微服务而言它是必不可少的一个环节…

Endpoints

actuator的核心部分,它用来监视应用程序及交互,spring-boot-actuator中已经内置了非常多的Endpoints(health、info、beans、httptrace、shutdown等等),同时也允许我们自己扩展自己的端点

Spring Boot 2.0中的端点和之前的版本有较大不同,使用时需注意。另外端点的监控机制也有很大不同,启用了不代表可以直接访问,还需要将其暴露出来,传统的management.security管理已被标记为不推荐。

皮一下很开心

内置Endpoints

id desc Sensitive
auditevents 显示当前应用程序的审计事件信息 Yes
beans 显示应用Spring Beans的完整列表 Yes
caches 显示可用缓存信息 Yes
conditions 显示自动装配类的状态及及应用信息 Yes
configprops 显示所有 @ConfigurationProperties 列表 Yes
env 显示 ConfigurableEnvironment 中的属性 Yes
flyway 显示 Flyway 数据库迁移信息 Yes
health 显示应用的健康信息(未认证只显示status,认证显示全部信息详情) No
info 显示任意的应用信息(在资源文件写info.xxx即可) No
liquibase 展示Liquibase 数据库迁移 Yes
metrics 展示当前应用的 metrics 信息 Yes
mappings 显示所有 @RequestMapping 路径集列表 Yes
scheduledtasks 显示应用程序中的计划任务 Yes
sessions 允许从Spring会话支持的会话存储中检索和删除用户会话。 Yes
shutdown 允许应用以优雅的方式关闭(默认情况下不启用) Yes
threaddump 执行一个线程dump Yes
httptrace 显示HTTP跟踪信息(默认显示最后100个HTTP请求 - 响应交换) Yes

导入依赖

pom.xml中添加spring-boot-starter-actuator的依赖

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意事项

如果要访问info接口想获取maven中的属性内容请记得添加如下内容

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

属性配置

application.properties文件中配置actuator的相关配置,其中info开头的属性,就是访问info端点中显示的相关内容,值得注意的是Spring Boot2.x中,默认只开放了info、health两个端点,剩余的需要自己通过配置management.endpoints.web.exposure.include属性来加载(有include自然就有exclude,不做详细概述了)。如果想单独操作某个端点可以使用management.endpoint.端点.enabled属性进行启用或禁用

# 描述信息
info.blog-url=http://winterchen.com
info.author=Luis
info.version=@project.version@

# 加载所有的端点/默认只加载了 info / health
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# 可以关闭制定的端点
management.endpoint.shutdown.enabled=false

# 路径映射,将 health 路径映射成 rest_health 那么在访问 health 路径将为404,因为原路径已经变成 rest_health 了,一般情况下不建议使用
# management.endpoints.web.path-mapping.health=rest_health
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

简单测试

启动项目,访问http://localhost:8080/actuator/info看到如下内容代表配置成功

{
"blog-url":"http://winterchen.com",
"author":"Luis",
"version":"0.0.1-SNAPSHOT"
}
  • 1
  • 2
  • 3
  • 4
  • 5

自定义 - 重点

上面讲了很多都是配置相关,以及自带的一些端点,在实际应用中有时候默认并不能满足我们的要求,比如Spring Boot默认的健康端点就很有可能不能满足

默认装配 HealthIndicators

下列是依赖spring-boot-xxx-starter后相关HealthIndicator的实现(通过management.health.defaults.enabled属性可以禁用它们),但想要获取一些额外的信息时,自定义的作用就体现出来了…

名称 描述
CassandraHealthIndicator 检查Cassandra数据库是否启动。
DiskSpaceHealthIndicator 检查磁盘空间不足。
DataSourceHealthIndicator 检查是否可以获得连接DataSource
ElasticsearchHealthIndicator 检查Elasticsearch集群是否启动。
InfluxDbHealthIndicator 检查InfluxDB服务器是否启动。
JmsHealthIndicator 检查JMS代理是否启动。
MailHealthIndicator 检查邮件服务器是否启动。
MongoHealthIndicator 检查Mongo数据库是否启动。
Neo4jHealthIndicator 检查Neo4j服务器是否启动。
RabbitHealthIndicator 检查Rabbit服务器是否启动。
RedisHealthIndicator 检查Redis服务器是否启动。
SolrHealthIndicator 检查Solr服务器是否已启动。

健康端点(第一种方式)

实现HealthIndicator接口,根据自己的需要判断返回的状态是UP还是DOWN,功能简单。

package com.winterchen.health;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

/**
 * 自定义健康端点
 * Created by Donghua.Chen on 2018/7/12.
 */
@Component("my1")
public class MyHealthIndicator implements HealthIndicator {

    private static final String VERSION = "v1.0.0";

    @Override
    public Health health() {
        int code = check();
        if (code != 0) {
            Health.down().withDetail("code", code).withDetail("version", VERSION).build();
        }
        return Health.up().withDetail("code", code)
                .withDetail("version", VERSION).up().build();
    }
    private int check() {
        return 0;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

简单测试

启动项目,访问http://localhost:8080/actuator/health看到如下内容代表配置成功

{
    "status": "UP",
    "details": {
        "my1": {
            "status": "UP",
            "details": {
                "code": 0,
                "version": "v1.0.0"
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 121123069952,
                "free": 35826044928,
                "threshold": 10485760
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

健康端点(第二种方式)

继承AbstractHealthIndicator抽象类,重写doHealthCheck方法,功能比第一种要强大一点点,默认的DataSourceHealthIndicator 、 RedisHealthIndicator都是这种写法,内容回调中还做了异常的处理。

package com.winterchen.health;

import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;

/**
 * 自定义健康端点
 * <p>功能更加强大一点,DataSourceHealthIndicator / RedisHealthIndicator 都是这种写法</p>
 * Created by Donghua.Chen on 2018/7/12.
 */
@Component("my2")
public class MyAbstractHealthIndicator extends AbstractHealthIndicator {

    private static final String VERSION = "v1.0.0";

    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        int code = check();
        if (code != 0) {
            builder.down().withDetail("code", code).withDetail("version", VERSION).build();
        }
        builder.withDetail("code", code)
                .withDetail("version", VERSION).up().build();
    }

    private int check() {
        return 0;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

简单测试

启动项目,访问http://localhost:8080/actuator/health看到如下内容代表配置成功

{
  "status": "UP",
  "details": {
    "my2": {
      "status": "UP",
      "details": {
        "code": 0,
        "version": "v1.0.0"
      }
    },
    "my1": {...},
    "diskSpace": {...}
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

定义自己的端点

上面介绍的infohealth都是spring-boot-actuator内置的,真正要实现自己的端点还得通过@Endpoint、 @ReadOperation、@WriteOperation、@DeleteOperation

注解介绍

不同请求的操作,调用时缺少必需参数,或者使用无法转换为所需类型的参数,则不会调用操作方法,响应状态将为400(错误请求)

  • @Endpoint构建 rest api 的唯一路径
  • **@ReadOperation**GET请求,响应状态为 200 如果没有返回值响应 404(资源未找到)
  • **@WriteOperation**POST请求,响应状态为 200 如果没有返回值响应 204(无响应内容)
  • **@DeleteOperation**DELETE请求,响应状态为 200 如果没有返回值响应 204(无响应内容)
package com.winterchen.endpoint;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

import java.util.HashMap;
import java.util.Map;

/**
 * * <p>@Endpoint 是构建 rest 的唯一路径 </p>
 * 不同请求的操作,调用时缺少必需参数,或者使用无法转换为所需类型的参数,则不会调用操作方法,响应状态将为400(错误请求)
 * <P>@ReadOperation = GET 响应状态为 200 如果没有返回值响应 404(资源未找到) </P>
 * <P>@WriteOperation = POST 响应状态为 200 如果没有返回值响应 204(无响应内容) </P>
 * <P>@DeleteOperation = DELETE 响应状态为 200 如果没有返回值响应 204(无响应内容) </P>
 *
 * Created by Donghua.Chen on 2018/7/12.
 */
@Endpoint(id = "luis")
public class MyEndPoint {

    @ReadOperation
    public Map<String, String> hello() {
        Map<String, String> result = new HashMap<>();
        result.put("author", "Luis");
        result.put("age", "25");
        result.put("email", "1085143002@qq.com");
        return result;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

以为这就大功告成了吗,现实告诉我的是spring-boot默认是不认识这玩意的,得申明成一个Bean(请看主函数

皮一下很开心

主函数

package com.winterchen;

import com.winterchen.endpoint.MyEndPoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
public class SpringBootActuatorApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootActuatorApplication.class, args);
    }

    @Configuration
    static class MyEndpointConfiguration {
        @Bean
        @ConditionalOnMissingBean
        @ConditionalOnEnabledEndpoint
        public MyEndPoint myEndPoint() {
            return new MyEndPoint();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

测试

完成准备事项后,启动Chapter13Application访问http://localhost:8080/actuator/luis看到如下内容代表配置成功…

{
    "author": "Luis",
    "age": "25",
    "email": "1085143002@qq.com"
}
  • 1
  • 2
  • 3
  • 4
  • 5

总结

参考文档:https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#production-ready

目前很多大佬都写过关于SpringBoot的教程了,如有雷同,请多多包涵,本教程基于最新的spring-boot-starter-parent:2.0.2.RELEASE编写,包括新版本的特性都会一起介绍…

分享到:
评论

相关推荐

    SpringBoot2.0-Actuator监控参数说明

    SpringBoot2.0-Actuator是Spring Boot框架中的一个强大组件,主要负责提供应用程序的监控和管理能力。在Spring Boot应用中,Actuator通过暴露一系列的端点(endpoints),使得开发者可以方便地查看和操作应用程序的...

    11springboot+actuator监控1

    在描述中提到了一个博客文章——"springboot(十九):使用Spring Boot Actuator监控应用",这可能是一个详细的实践指南,指导读者如何在Spring Boot项目中启用和使用Actuator。 Actuator提供的主要功能包括: 1. **...

    SpringBoot2.0-Actuator监控参数说明 - 最全文档.docx

    ### Spring Boot 2.0 Actuator 监控参数说明 #### 一、Spring Boot 2.0 ...通过以上步骤,Spring Boot 2.0项目不仅能够充分利用Actuator模块的强大功能,还能够借助于Prometheus实现更加灵活和细致的监控需求。

    使用SpringBoot Actuator监控应用示例

    总的来说,SpringBoot Actuator是一个强大的工具,可以帮助开发者更好地管理和监控Spring Boot应用程序,提供丰富的监控数据和自定义选项,确保应用在各种环境下都能保持稳定运行。通过熟练掌握Actuator的使用,可以...

    使用 Spring Boot Actuator 监控应用.docx

    Spring Boot Actuator 作为一款强大的监控工具,提供了丰富的功能来帮助开发者和运维人员更好地管理和监控他们的微服务应用。 #### Actuator 的作用与原理 Spring Boot Actuator 是 Spring Boot 提供的一个用于...

    SpringBoot 监控管理模块actuator没有权限的问题解决方法

    SpringBoot 监控管理模块actuator 是一个功能强大且灵活的监控管理模块,它提供了一系列的监控和管理功能,例如自动配置报告、 Beans 列表、配置属性、环境变量、健康信息、metrics 等等。然而,在使用 SpringBoot ...

    Spring Cloud 学习案例,服务发现、服务治理、链路追踪、服务监控等

    例如,可能有 Eureka 的服务注册与发现示例、Ribbon 客户端负载均衡示例、Zuul API Gateway 路由与过滤器示例、Hystrix 断路器示例、Sleuth 链路追踪示例以及与 Prometheus 和 Grafana 集成的监控示例。 学习这些...

    第 5-2 课:使⽤ Spring Boot Actuator 监控应⽤1

    Spring Boot Actuator 是一个强大的工具,它为Spring Boot应用程序提供了丰富的监控和管理功能。这个组件允许开发者深入了解应用程序的运行状态,包括内部配置、性能指标、健康检查等关键信息。通过集成Actuator,...

    dubbo服务监控工具dubbo-admin

    4. 监控与管理:在界面上,可以查看服务详情、监控各项指标、进行调用测试和运维操作。 三、优化与扩展 1. 自定义监控:可以通过插件机制扩展监控项,比如添加自定义的日志监控或异常监控。 2. 整合其他工具:...

    配套linux监控+springboot服务监控json文件

    对于Springboot应用,可以使用Actuator模块暴露监控端点,让Prometheus能够抓取服务的运行状态信息。 Grafana则是一个流行的可视化平台,它允许用户从各种数据源(包括Prometheus)中拉取数据并创建自定义的仪表盘...

    springboot+thymeleaf+actuator的demo

    Spring Boot Actuator是一个强大的工具集,用于为生产级别的微服务提供健康检查、性能指标和管理端点。Actuator可以帮助我们监控应用程序的状态,包括内存使用情况、HTTP请求统计、环境信息等。这些信息对于诊断问题...

    spring boot starter actuator(健康监控)配置和使用教程

    spring boot starter actuator(健康监控)配置和使用教程 Spring Boot Starter Actuator 是 ...Spring Boot Starter Actuator 提供了一个强大且灵活的监控和管理解决方案,可以帮助开发者更好地监控和管理应用程序。

    基于java的数据监控与管理平台技术实现.zip

    在IT行业中,数据监控与管理平台是至关重要的组成部分,它帮助企业实时掌握系统运行状况,确保服务稳定,并优化业务性能。本篇文章将详细探讨基于Java的数据监控与管理平台的技术实现,帮助开发者了解如何构建这样一...

    Spring Boot Actuator监控的简单使用方法示例代码详解

    Spring Boot Actuator监控的简单使用方法示例代码详解 Spring Boot Actuator是一种监控工具,可以帮助开发者快速实现对Spring Boot应用程序的监控和管理。本文将详细介绍Spring Boot Actuator监控的简单使用方法,...

    springboot136人口老龄化社区服务与管理平台.zip

    SpringBoot可以构建一套强大的后台管理系统,包括用户管理、服务管理、数据分析等多个模块,帮助管理员进行有效监控和服务优化。例如,通过SpringData JPA实现数据库操作,提高数据管理效率;使用SpringBoot ...

    promethus监控系统设计与实现1

    【Prometheus监控系统设计与实现】是针对应用性能管理和监控的一种解决方案,尤其适用于C#毕业设计项目。Prometheus是一款强大的开源监控系统,以其无侵入性、灵活性和强大的数据模型而受到广泛欢迎。 **Prometheus...

    渗透测试之dirsearch半自动化测试与Actuator端点

    这种技术可以帮助安全专家发现可能被忽视的安全问题,例如未授权的后台管理接口、未加密的敏感文件或者未公开的服务。 在dirsearch的半自动化测试中,用户可以自定义词典、设置HTTP头部、代理、重试机制等参数,以...

    springboot项目整合.zip

    第十四篇:强大的 actuator 服务监控与管理] 第十五篇:actuator与spring-boot-admin 第十六篇:定时任务详解] 第十七篇:轻松搞定文件上传] 第十八篇:轻松搞定全局异常] 第十九篇:轻松搞定数据验证

    Spring Boot druid 以及相关sql、Spring监控配置demo

    Spring Boot 是一个基于Java的框架,它简化了创建独立、生产级别的基于Spring的应用程序的流程。...通过实践,你将能更深入地理解Spring Boot与Druid的集成,以及如何利用它们来提升应用的监控和管理能力。

Global site tag (gtag.js) - Google Analytics