Spring Boot 应用监控

简介: 当一个Spring Boot 应用运行的时候,开发者需要对Spring Boot应用进行实时监控,获得项目的报警需求,Spring Boot 提供了,actuator 来帮助开发者获取应用程序运行时的数据。

当一个Spring Boot 应用运行的时候,开发者需要对Spring Boot应用进行实时监控,获得项目的报警需求,Spring Boot 提供了,actuator 来帮助开发者获取应用程序运行时的数据。

端点配置

在Spring Boot 中添加端点配置相当的简单。
只需要添加 spring-boot-starter-actuator
添加相关的依赖

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>

常用的端点如下:

常用端点列举如下,可以一个个详细试一下:

/info        应用基本信息
/health       健康度信息
/metrics      运行指标
/env        环境变量信息
/loggers      日志相关
/dump       线程相关信息
/trace       请求调用轨迹

这些端点大都是默认开启的,如果想要开启一个端点,需要在配置文件中,配置以下内容。

endpoints:
  metrics:
    sensitive: false

此时sensitive 是关闭的。

举个例子:
这里举个例子,访问是否在线的接口

localhost:8080/actuator/health

此时浏览器的输出结果为:

端点响应缓存

对于一些不带参数的端点将会进行缓存。

management:
  endpoint:
    auditevents:
      cache:
        time-to-live: 100s

上方的配置说明了缓存达到100s

路径映射

可以对访问的路径进行映射。

management:
  endpoints:
    web:
      base-path: /
      path-mapping: 
        health: healthcheck

此时访问路径由原先的 localhost:8080/actuator/health 转变为 localhost:8080/healthcheck

CORS

进行跨域操作。
可以通过配置文件,快速的开启CORS支持。

management:
  endpoints:
    web:
      cors:
        allowed-origins: http://localhost:8091
        allowed-methods: *

在上方中,允许处理,来自http://localhost:8091 的任何请求,允许的方法任意。

配置信息可视化

添加相关的依赖。

        <!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-server -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.2.3</version>
        </dependency>

在启动类上增加相关的注解:

package com.example.demo;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer
public class DemoApplication {

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

}

配置完成以后,输入链接,进行访问。

http://localhost:8080/index.html

再次添加client端

<!-- https://mvnrepository.com/artifact/de.codecentric/spring-boot-admin-starter-client -->
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.2.3</version>
</dependency>

书写配置文件

spring:
  boot:
    admin:
      client:
        url: http://localhost:8080

此时查看admin


查看其健康度

微信公众号:

目录
相关文章
|
4天前
|
监控 Java 数据库连接
Spring Boot中的健康检查和监控
Spring Boot中的健康检查和监控
|
5天前
|
缓存 监控 Java
优化Spring Boot应用的数据库访问性能
优化Spring Boot应用的数据库访问性能
|
4天前
|
Prometheus 监控 Cloud Native
使用Spring Boot和Prometheus进行监控
使用Spring Boot和Prometheus进行监控
|
6天前
|
缓存 监控 Java
优化Spring Boot应用的数据库访问性能
优化Spring Boot应用的数据库访问性能
|
9天前
|
NoSQL Java MongoDB
Spring Boot与MongoDB的集成应用
Spring Boot与MongoDB的集成应用
|
4天前
|
NoSQL Java MongoDB
使用Spring Boot构建响应式应用
使用Spring Boot构建响应式应用
|
4天前
|
存储 NoSQL Java
使用Spring Boot和MongoDB构建NoSQL应用
使用Spring Boot和MongoDB构建NoSQL应用
|
5天前
|
缓存 监控 Java
如何优化Spring Boot应用的性能?
如何优化Spring Boot应用的性能?
|
6天前
|
Kubernetes Java Docker
使用Kubernetes部署Spring Boot应用的实践
使用Kubernetes部署Spring Boot应用的实践
|
6天前
|
监控 Java API
使用Spring Boot构建企业级应用的实践
使用Spring Boot构建企业级应用的实践