SpringBoot Actuator潜在的OOM问题的解决

目录
  • 背景&问题
  • 问题原因及解决
    • 默认埋点是如何生效的
    • http.client.requests 中的 uri
  • 解决
  • 会不会 OOM

此问题背景产生于近期需要上线的一个功能的埋点;主要表现就是在应用启动之后的一段时间内,内存使用一直呈现递增趋势。

下图为场景复线后,本地通过 jconsole 查看到的内部使用走势图。

实际环境受限于配置,内存不会膨胀

背景&问题

应用 a 使用 rest template 通过 http 方式调用 应用 b,应用项目中开启了 actuator,api 使用的是 micrometer;在 client 调用时,actuator 会产生一个 name 为 http.client.requests 的 metrics,此 metric 的 tag 中包含点目标的 uri。

应用 b 提供的接口大致如下:

@RequestMapping("test_query_params")
public String test_query_params(@RequestParam String value) {
    return value;
}

@RequestMapping("test_path_params/{value}")
public String test_path_params(@PathVariable String value) {
    return value;
}

http://localhost:8080/api/test/test_query_params?value=

http://localhost:8080/api/test/test_path_params/{value}_

期望在 metric 的收集结果中应该包括两个 metrics,主要区别是 tag 中的 uri 不同,一个是 api/test/test_query_params, 另一个是 api/test/test_path_params/{value};实际上从拿到的 metrics 数据来看,差异很大,这里以 pathvariable 的 metric 为例,数据如下:

tag: "uri",
values: [
"/api/test/test_path_params/glmapper58",
"/api/test/test_path_params/glmapper59",
"/api/test/test_path_params/glmapper54",
"/api/test/test_path_params/glmapper55",
"/api/test/test_path_params/glmapper56",
"/api/test/test_path_params/glmapper57",
"/api/test/test_path_params/glmapper50",
"/api/test/test_path_params/glmapper51",
"/api/test/test_path_params/glmapper52",
"/api/test/test_path_params/glmapper53",
"/api/test/test_path_params/glmapper47",
"/api/test/test_path_params/glmapper48",
"/api/test/test_path_params/glmapper49",
"/api/test/test_path_params/glmapper43",
"/api/test/test_path_params/glmapper44",
"/api/test/test_path_params/glmapper45",
"/api/test/test_path_params/glmapper46",
"/api/test/test_path_params/glmapper40",
"/api/test/test_path_params/glmapper41",
"/api/test/test_path_params/glmapper42",
"/api/test/test_path_params/glmapper36",
"/api/test/test_path_params/glmapper37",
"/api/test/test_path_params/glmapper38",
"/api/test/test_path_params/glmapper39",
"/api/test/test_path_params/glmapper32",
"/api/test/test_path_params/glmapper33",
"/api/test/test_path_params/glmapper34",
"/api/test/test_path_params/glmapper35",
"/api/test/test_path_params/glmapper30",
"/api/test/test_path_params/glmapper31",
"/api/test/test_path_params/glmapper25",
"/api/test/test_path_params/glmapper26",
....
]

可以非常明显的看到,这里将{value} 参数作为了 uri 组件部分,并且体现在 tag 中,并不是期望的 api/test/test_path_params/{value}。

问题原因及解决

两个问题,1、这个埋点是怎么生效的,先搞清楚这个问题,才能顺藤摸瓜。2、怎么解决。

默认埋点是如何生效的

因为是通过 resttemplate 进行调用访问,那么埋点肯定也是基于对 resttemplate 的代理;按照这个思路,笔者找到了 org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer 这个类。RestTemplateCustomizer 就是对 resttemplate 进行定制的,MetricsRestTemplateCustomizer 通过名字也能得知期作用是为了给 resttemplate 增加 metric 能力。

再来讨论 RestTemplateCustomizer,当使用RestTemplateBuilder构建RestTemplate时,可以通过RestTemplateCustomizer进行更高级的定制,所有RestTemplateCustomizer beans 将自动添加到自动配置的RestTemplateBuilder。也就是说如果 想 MetricsRestTemplateCustomizer 生效,那么构建 resttemplate 必须通过 RestTemplateBuilder 方式构建,而不是直接 new。

http.client.requests 中的 uri

塞 tag 的代码在org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTags 类中,作用时机是在 MetricsClientHttpRequestInterceptor 拦截器中。当调用执行完成后,会将当次请求 metric 记录下来,在这里就会使用到 RestTemplateExchangeTags 来填充 tags。 下面仅给出 uri 的部分代码

	/**
	 * Creates a {@code uri} {@code Tag} for the URI of the given {@code request}.
	 * @param request the request
	 * @return the uri tag
	 */
	public static Tag uri(HttpRequest request) {
		return Tag.of("uri", ensureLeadingSlash(stripUri(request.getURI().toString())));
	}

	/**
	 * Creates a {@code uri} {@code Tag} from the given {@code uriTemplate}.
	 * @param uriTemplate the template
	 * @return the uri tag
	 */
	public static Tag uri(String uriTemplate) {
		String uri = (StringUtils.hasText(uriTemplate) ? uriTemplate : "none");
		return Tag.of("uri", ensureLeadingSlash(stripUri(uri)));

其余的还有 status 和 clientName 等 tag name。

通过断点,可以看到,这里 request.getURI() 拿到的是带有参数的完整请求链接。

这些 tag 的组装最终在 DefaultRestTemplateExchangeTagsProvider 中完成,并返回一个 列表。

private Timer.Builder getTimeBuilder(HttpRequest request, ClientHttpResponse response) {
    return this.autoTimer.builder(this.metricName)
                // tagProvider 为 DefaultRestTemplateExchangeTagsProvider
				.tags(this.tagProvider.getTags(urlTemplate.get().poll(), request, response))
				.description("Timer of RestTemplate operation");
}

解决

这里先来看下官方对于 request.getURI  的解释

	/**
	 * Return the URI of the request (including a query string if any,
	 * but only if it is well-formed for a URI representation).
	 * @return the URI of the request (never {@code null})
	 */
	URI getURI();

返回请求的 URI,这里包括了任何的查询参数。那么是不是拿到不用参数的 path 就行呢?

这里尝试通过 request.getURI().getPath() 拿到了预期的 path(@pathvariable 拿到的是模板)。

再回到 DefaultRestTemplateExchangeTagsProvider,所有的 tag 都是在这里完成组装,这个类明显是一个默认的实现(Spring 体系下基本只要是Defaultxxx 的,一般都能扩展 ),查看它的接口类 RestTemplateExchangeTagsProvider 如下:

/**
 * Provides {@link Tag Tags} for an exchange performed by a {@link RestTemplate}.
 *
 * @author Jon Schneider
 * @author Andy Wilkinson
 * @since 2.0.0
 */
@FunctionalInterface
public interface RestTemplateExchangeTagsProvider {

	/**
	 * Provides the tags to be associated with metrics that are recorded for the given
	 * {@code request} and {@code response} exchange.
	 * @param urlTemplate the source URl template, if available
	 * @param request the request
	 * @param response the response (may be {@code null} if the exchange failed)
	 * @return the tags
	 */
	Iterable<Tag> getTags(String urlTemplate, HttpRequest request, ClientHttpResponse response);

}

RestTemplateExchangeTagsProvider 的作用就是为 resttemplate 提供 tag 的,所以这里通过自定义一个 RestTemplateExchangeTagsProvider,来替换DefaultRestTemplateExchangeTagsProvider,以达到我们的目标,大致代码如下:

@Override
 public Iterable<Tag> getTags(String urlTemplate, HttpRequest request, ClientHttpResponse response) {
    Tag uriTag;
    // 取 request.getURI().getPath() 作为 uri 的 value
    if (StringUtils.hasText(request.getURI().getPath())) {
      uriTag = Tag.of("uri", ensureLeadingSlash(stripUri(request.getURI().getPath())));
    } else {
      uriTag = (StringUtils.hasText(urlTemplate) ? RestTemplateExchangeTags.uri(urlTemplate)
                    : RestTemplateExchangeTags.uri(request));
    }
    return Arrays.asList(RestTemplateExchangeTags.method(request), uriTag,
                RestTemplateExchangeTags.status(response), RestTemplateExchangeTags.clientName(request));
    }

会不会 OOM

理论上,应该参数不同,在使用默认 DefaultRestTemplateExchangeTagsProvider 的情况下,meter 会随着 tags 的不同迅速膨胀,在 micrometer 中,这些数据是存在 map 中的

// Even though writes are guarded by meterMapLock, iterators across value space are supported
// Hence, we use CHM to support that iteration without ConcurrentModificationException risk
private final Map<Id, Meter> meterMap = new ConcurrentHashMap<>();

一般情况下不会,这里是因为 spring boot actuator 自己提供了保护机制,对于默认情况,tags 在同一个 metric 下,最多只有 100 个

/**
* Maximum number of unique URI tag values allowed. After the max number of
* tag values is reached, metrics with additional tag values are denied by
* filter.
*/
private int maxUriTags = 100;

如果你想使得这个数更大一些,可以通过如下配置配置

management.metrics.web.client.max-uri-tags=10000

如果配置值过大,会存在潜在的 oom 风险。

到此这篇关于SpringBoot Actuator潜在的OOM问题的解决的文章就介绍到这了,更多相关SpringBoot Actuator OOM内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们! 

(0)

相关推荐

  • SpringBoot actuator 健康检查不通过的解决方案

    SpringBoot actuator 健康检查不通过 今天遇到有个服务能够注册成功,但是健康检查不通过,通过浏览器访问健康检查的url,chrome的network一直显示pending,说明这个请求提交了,但是得不到返回,卡住了. 原来以为健康检查就是检查服务端口下的/health这个请求本身是否能正常返回,其实不是. 所谓健康检查是有很多检查项的,springboot中继承AbstractHealthIndicator的类,比如DataSourceHealthIndicator Redis

  • Springboot actuator应用后台监控实现

    一 前言 springboot 额外的特色是提供了后台应用监控,可以通过 HTTP 或者 JMX的方式管理监控应用,本文主讲HTTP方式:其主要的功能是监控应用的健康状态,查看环境变量等: 二 pom.xml springboot 2.1.1,主要引入 actuator 依赖,web依赖用于测试: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-

  • SpringBoot 指标监控actuator的专题

    目录 1.写在前面 2.SpringBoot Actuator 3.定制化Endpoint 3.1 定制health端点信息 3.2 定制info端点信息 1.写在前面 首先肯定要说一下SpringBoot的四大核心了: 自动装配:简单配置甚至零配置即可运行项目 起步依赖:场景启动器 Actuator:指标监控 命令行界面 :命令行 这篇文章呢,我来和大家聊聊指标监控这个东西. 2.SpringBoot Actuator 未来每一个微服务在云上部署以后,我们都需要对其进行监控.追踪.审计.控制等

  • springboot Actuator的指标监控可视化功能详解

    springboot为我们提供了丰富的指标监控功能SpringBoot Actuator SpringBoot Actuator是springboot为简化我们对微服务项目的监控功能抽取出来的模块,使得我们每个微服务快速引用即可获得生产界别的应用监控.审计等功能. 后序文章会更新使用 我们先来看看怎么可视化 我们可以通过github上的开源项目 这里 我们创建一个springboot项目 作为可视化的服务端 使用新功能首先都是引入依赖 需要web项目 <dependency> <grou

  • Springboot actuator生产就绪功能实现解析

    Spring Boot包含许多附加功能,可帮助您在将应用程序投入生产时对其进行监视和管理.可以选择使用HTTP端点或JMX管理和监视您的应用程序.审核,运行状况和指标收集可以自动应用于您的应用程序. Springboot Actuator,它提供了很多生产级的特性,比如说监控和度量spring boot应用程序.Actuator的这些特性可以通过众多的REST断点,远程shell和JMX获得. 只有基于Spring MVC的应用程序才可以通过HTTP终端来监控应用程序的运行指标. 使用Sprin

  • SpringBoot实现监控Actuator,关闭redis监测

    目录 SpringBoot监控Actuator,关闭redis监测 方法 springboot Actuator 查看配置明细 运行时度量 SpringBoot监控Actuator,关闭redis监测 方法 当我们导入了spring-boot-starter-actuator这个依赖后, SpringBoot会默认去监测一些信息.其中就包括redis. 会根据redis的默认初始配置, localhost:6379 尝试连接redis.如果我们没有用到redis, 启动就会报错 <depende

  • SpringBoot Actuator埋点和监控及简单使用

    目录 1. 数据埋点 2. Micrometer 2.1 简单使用 2.2 命名规范 3. SpringBoot Actuator 3.1 添加依赖 3.2 基础配置 3.3 查看可消费的端点 3.4 获取应用的基本信息 3.5 健康指标 3.6 指标端点 metrics 4. 实例 4.1 测试接口 4.2 消费指标端点 5. SpringBoot Admin 5.1 Admin 服务器端 5.1.1 启用功能.添加依赖 5.1.2 选择一个端口 5.1.3 访问 5.2 Client 客户端

  • SpringBoot Admin 如何实现Actuator端点可视化监控

    目录 SpringBoot Admin 实现Actuator端点可视化监控 简介 Spring Boot Admin Server Spring Boot Admin Client 启动客户端, 在管理端进行可视化端点监控 Spring Boot 监控信息可视化 一.设置Spring Boot Admin Server 二.注册客户端 SpringBoot Admin 实现Actuator端点可视化监控 简介 Actuator可视化监控SpringBoot Admin Note: SpringB

  • SpringBoot Actuator潜在的OOM问题的解决

    目录 背景&问题 问题原因及解决 默认埋点是如何生效的 http.client.requests 中的 uri 解决 会不会 OOM 此问题背景产生于近期需要上线的一个功能的埋点:主要表现就是在应用启动之后的一段时间内,内存使用一直呈现递增趋势. 下图为场景复线后,本地通过 jconsole 查看到的内部使用走势图. 实际环境受限于配置,内存不会膨胀 背景&问题 应用 a 使用 rest template 通过 http 方式调用 应用 b,应用项目中开启了 actuator,api 使用

  • SpringBoot Actuator未授权访问漏洞修复详解

    目录 1.写在前面 2.问题描述 3.安全问题 4.禁止方法 5.完全禁用Actuator 1.写在前面 目前SpringBoot得框架,越来越广泛,大多数中小型企业,在开发新项目得时候.后端语言使用java得情况下,首选都会使用到SpringBoot. 在很多得一些开源得框架中,例如: ruoyi若以,这些. 不知道是出于什么原因?我们都会在这些框架中得pom文件中找到SpringBoot Actuator的依赖. 嘿,这Actuator估计很多人都没有真真实实使用过,但是就会出现在pom文件

  • SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <

  • springboot 2.3之后消失的hibernate-validator解决方法

    项目升级到springboot2.3之后,参数校验的注解报错,发现spring-boot-starter-web的依赖项已经去除了依赖 点开spring-boot-starter-web源码看了下. <?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache

  • springboot多模块包扫描问题的解决方法

    问题描述: springboot建立多个模块,当一个模块需要使用另一个模块的服务时,需要注入另一个模块的组件,如下面图中例子: memberservice模块中的MemberServiceApiImpl类需要注入common模块中的RedisService组件,该怎么注入呢? 解决: 在memberservice模块的启动类上加上RedisService类所在包的全路径的组件扫描,就像这样: 注意启动类上方的注解@ComponentScan(basePackages={"com.whu.comm

  • 基于SpringBoot bootstrap.yml配置未生效的解决

    我就废话不多说了,大家还是直接看代码吧~ <!--需要引入该jar才能使bootstrap配置文件生效--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </dependency> 补充知识:SpringBoot不读取bootstrap.yml/properti

  • springboot拦截器无法注入redisTemplate的解决方法

    在工作中我们经常需要做登录拦截验证或者其他拦截认证功能,基于springboot项目下我们很容易想到结合redis做的分布式拦截,把用户登录或者需要验证的信息放到redis里面.但是在写拦截器的时候发现redisTemplate一直无法注入进来,最后查资料才发现springboot拦截器是在Bean实例化之前执行的,所以Bean实例无法注入. 先看下问题,新建一个拦截器,然后注入redisTemplate /** * @author: lockie * @Date: 2019/8/13 16:1

随机推荐