溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何解決SpringBoot?Actuator潛在的OOM問題

發(fā)布時(shí)間:2021-11-30 12:56:51 來源:億速云 閱讀:246 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹如何解決SpringBoot Actuator潛在的OOM問題,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

    此問題背景產(chǎn)生于近期需要上線的一個(gè)功能的埋點(diǎn);主要表現(xiàn)就是在應(yīng)用啟動(dòng)之后的一段時(shí)間內(nèi),內(nèi)存使用一直呈現(xiàn)遞增趨勢(shì)。

    下圖為場(chǎng)景復(fù)線后,本地通過 jconsole 查看到的內(nèi)部使用走勢(shì)圖。

    如何解決SpringBoot?Actuator潛在的OOM問題

    實(shí)際環(huán)境受限于配置,內(nèi)存不會(huì)膨脹

    背景&問題

    應(yīng)用 a 使用 rest template 通過 http 方式調(diào)用 應(yīng)用 b,應(yīng)用項(xiàng)目中開啟了 actuator,api 使用的是 micrometer;在 client 調(diào)用時(shí),actuator 會(huì)產(chǎn)生一個(gè) name 為 http.client.requests 的 metrics,此 metric 的 tag 中包含點(diǎn)目標(biāo)的 uri。

    應(yīng)用 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 的收集結(jié)果中應(yīng)該包括兩個(gè) metrics,主要區(qū)別是 tag 中的 uri 不同,一個(gè)是 api/test/test_query_params, 另一個(gè)是 api/test/test_path_params/{value};實(shí)際上從拿到的 metrics 數(shù)據(jù)來看,差異很大,這里以 pathvariable 的 metric 為例,數(shù)據(jù)如下:

    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} 參數(shù)作為了 uri 組件部分,并且體現(xiàn)在 tag 中,并不是期望的 api/test/test_path_params/{value}。

    問題原因及解決

    兩個(gè)問題,1、這個(gè)埋點(diǎn)是怎么生效的,先搞清楚這個(gè)問題,才能順藤摸瓜。2、怎么解決。

    默認(rèn)埋點(diǎn)是如何生效的

    因?yàn)槭峭ㄟ^ resttemplate 進(jìn)行調(diào)用訪問,那么埋點(diǎn)肯定也是基于對(duì) resttemplate 的代理;按照這個(gè)思路,筆者找到了 org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer 這個(gè)類。RestTemplateCustomizer 就是對(duì) resttemplate 進(jìn)行定制的,MetricsRestTemplateCustomizer 通過名字也能得知期作用是為了給 resttemplate 增加 metric 能力。

    再來討論 RestTemplateCustomizer,當(dāng)使用RestTemplateBuilder構(gòu)建RestTemplate時(shí),可以通過RestTemplateCustomizer進(jìn)行更高級(jí)的定制,所有RestTemplateCustomizer beans 將自動(dòng)添加到自動(dòng)配置的RestTemplateBuilder。也就是說如果 想 MetricsRestTemplateCustomizer 生效,那么構(gòu)建 resttemplate 必須通過 RestTemplateBuilder 方式構(gòu)建,而不是直接 new。

    http.client.requests 中的 uri

    塞 tag 的代碼在org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTags 類中,作用時(shí)機(jī)是在 MetricsClientHttpRequestInterceptor 攔截器中。當(dāng)調(diào)用執(zhí)行完成后,會(huì)將當(dāng)次請(qǐng)求 metric 記錄下來,在這里就會(huì)使用到 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。

    通過斷點(diǎn),可以看到,這里 request.getURI() 拿到的是帶有參數(shù)的完整請(qǐng)求鏈接。

    如何解決SpringBoot?Actuator潛在的OOM問題

    這些 tag 的組裝最終在 DefaultRestTemplateExchangeTagsProvider 中完成,并返回一個(gè) 列表。

    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");
    }

    解決

    這里先來看下官方對(duì)于 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();

    返回請(qǐng)求的 URI,這里包括了任何的查詢參數(shù)。那么是不是拿到不用參數(shù)的 path 就行呢?

    如何解決SpringBoot?Actuator潛在的OOM問題

    這里嘗試通過 request.getURI().getPath() 拿到了預(yù)期的 path(@pathvariable 拿到的是模板)。

    再回到 DefaultRestTemplateExchangeTagsProvider,所有的 tag 都是在這里完成組裝,這個(gè)類明顯是一個(gè)默認(rèn)的實(shí)現(xiàn)(Spring 體系下基本只要是Defaultxxx 的,一般都能擴(kuò)展 ),查看它的接口類 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 的,所以這里通過自定義一個(gè) RestTemplateExchangeTagsProvider,來替換DefaultRestTemplateExchangeTagsProvider,以達(dá)到我們的目標(biāo),大致代碼如下:

    @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));
        }

    會(huì)不會(huì) OOM

    理論上,應(yīng)該參數(shù)不同,在使用默認(rèn) DefaultRestTemplateExchangeTagsProvider 的情況下,meter 會(huì)隨著 tags 的不同迅速膨脹,在 micrometer 中,這些數(shù)據(jù)是存在 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<>();

    一般情況下不會(huì),這里是因?yàn)?spring boot actuator 自己提供了保護(hù)機(jī)制,對(duì)于默認(rèn)情況,tags 在同一個(gè) metric 下,最多只有 100 個(gè)

    /**
    * 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;

    如果你想使得這個(gè)數(shù)更大一些,可以通過如下配置配置

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

    如果配置值過大,會(huì)存在潛在的 oom 風(fēng)險(xiǎn)。

    以上是“如何解決SpringBoot Actuator潛在的OOM問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI