【问题标题】:How to count number of requests in spring boot app?如何计算 Spring Boot 应用程序中的请求数?
【发布时间】:2020-05-12 06:13:09
【问题描述】:

我有一个 Spring Boot 应用程序,我想稍后将其指标发送到 gcp。但是现在我只需要找到一种方法来计算我的应用程序使用actuatormicrometer 处理的请求总数。有人知道我怎么能做这样的事情吗?提前致谢

【问题讨论】:

    标签: java spring-boot metrics actuator


    【解决方案1】:

    spring-actuator 示例:

    //add pom
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    

    使用执行器

    @Bean
    public SecurityWebFilterChain securityWebFilterChain(
      ServerHttpSecurity http) {
        return http.authorizeExchange()
          .pathMatchers("/actuator/**").permitAll()
          .anyExchange().authenticated()
          .and().build();
    }
    

    然后就可以从http://localhost:8080/metrics获取数据了

    official documentation查看更多信息

    【讨论】:

      【解决方案2】:

      看看Spring Boot Actuator - HttpTrace

      默认情况下,它在最新版本中未启用,但您可以通过实现 HttpTraceRepository 来启用。

      在此存储库中,您可以使用计数器来测量请求的数量,然后将其添加到 MetricRegistry。另一种选择是使用过滤器,然后在那里使用计数器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 2016-02-02
        • 1970-01-01
        • 2017-04-03
        • 2019-04-15
        • 2017-07-18
        • 2018-02-28
        相关资源
        最近更新 更多