如何记录Spring WebClient的线程池指标?

时间:2019-03-12 03:40:38

标签: java spring threadpool webclient metrics

有没有一种方法可以获取线程池指标,以便将其记录(导出)到Spring WebClient的石墨仪表板。

有一种方法可以通过配置PoolingHttpClientConnectionManager对Rest Template做类似的事情。

 public Map<String, Metric> getMetrics() {
    final Map<String, Metric> gauges = new HashMap<>();

    for (Entry<String, PoolingHttpClientConnectionManager> entry : connectionManagerMap
        .entrySet()) {
      gauges.put(entry.getKey() + ".leasedConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getLeased());
      gauges.put(entry.getKey() + ".maxConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getMax());
      gauges.put(entry.getKey() + ".pendingConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getPending());
      gauges.put(entry.getKey() + ".availableConnections",
          (Gauge<Integer>) () -> entry.getValue().getTotalStats().getAvailable());
    }

    return gauges;
  }

对于WebClient,我不确定如何获取HttpThreadPool,我以以下方式创建了WebClient对象。

WebClient webClient = WebClient.builder()
            .baseUrl(downstream.getServiceLocation())
            .clientConnector(clientHttpConnector)
            .exchangeStrategies(exchangeStrategies)
            .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
            .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML_VALUE)
            .build();

**and clientHttpConnector was created:**

public ClientHttpConnector lbosClientHttpConnector() {
        return new ReactorClientHttpConnector(options -> {
            options.option(ChannelOption.SO_TIMEOUT, applicationConfiguration.getDownstreams().getHttpClient().getSocketTimeout());
            options.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, applicationConfiguration.getDownstreams().getHttpClient().getKeepAliveTimeoutInMs());
            options.poolResources(PoolResources.fixed("HTTP_CONNECTION_POOL", applicationConfiguration.getDownstreams().getHttpClient().getMaxConnections()));
        });
    }

请提供解决方案或指导我解决问题。

0 个答案:

没有答案