You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

如何统计Spring Boot应用每秒请求数?生产环境如何监控该指标?

Hey there! Let's break down how to track your Spring Boot app's requests per second (QPS) and set up production-ready monitoring for it, plus a quick note on those frustrating failed requests.

1. Calculating Requests Per Second (QPS)

You've got a few straightforward ways to get this metric, depending on your needs:

Using Spring Boot Actuator's Built-in Metrics

Spring Boot Actuator comes with out-of-the-box metrics for HTTP requests, including total counts and response times. Here's how to leverage it:

  • First, add the Actuator dependency to your pom.xml (or build.gradle):
    <!-- Maven -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
  • Enable the metrics endpoint in your application.yml or application.properties:
    management:
      endpoints:
        web:
          exposure:
            include: metrics
      metrics:
        tags:
          application: your-app-name
    
  • Now hit http://your-app-url/actuator/metrics/http.server.requests — you'll get a JSON response with a count field (total requests) and duration stats. To calculate QPS, track the count over time (e.g., subtract the count from 10 seconds ago from the current count, then divide by 10).

Custom Counter with Micrometer

If you want a dedicated metric for your specific REST endpoint, use Micrometer (Spring Boot's default metrics library) to create a custom counter:

import io.micrometer.core.annotation.Counted;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class YourRestController {

    // This annotation auto-increments a counter for every request to this endpoint
    @Counted(value = "your.api.requests", description = "Total requests to my single REST endpoint")
    @GetMapping("/your-endpoint")
    public String handleRequest() {
        // Your business logic here
        return "Success";
    }
}

Access this metric via http://your-app-url/actuator/metrics/your.api.requests and calculate QPS the same way as above.

Quick Log-Based Count (For Temporary Debugging)

If you just need a quick check without full metrics setup, add a filter that increments an atomic counter on each request, then log the count every second:

import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class RequestCountFilter implements Filter {
    private final AtomicInteger requestCount = new AtomicInteger(0);

    @Override
    public void init(FilterConfig filterConfig) {}

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        requestCount.incrementAndGet();
        chain.doFilter(request, response);
    }

    // Log QPS every second
    @org.springframework.scheduling.annotation.Scheduled(fixedRate = 1000)
    public void logQps() {
        int count = requestCount.getAndSet(0);
        System.out.println("Current QPS: " + count);
    }
}

Note: This is great for quick debugging, but skip it in production — it adds unnecessary overhead and doesn't scale.

2. Production-Ready Monitoring

For production, you'll want a system that tracks QPS in real-time, stores historical data, and alerts you when things go sideways (like those 1-in-12 failed requests). Here's the standard setup:

Spring Boot Actuator + Prometheus + Grafana

This is the most popular open-source stack:

  1. Add Prometheus dependency to export metrics in a format Prometheus can scrape:
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    
  2. Configure Prometheus to pull metrics from your app (add this to prometheus.yml):
    scrape_configs:
      - job_name: 'your-spring-boot-app'
        metrics_path: '/actuator/prometheus'
        static_configs:
          - targets: ['your-app-host:port']
    
  3. Set up Grafana to connect to Prometheus as a data source. Import pre-built Spring Boot dashboards (search for "Spring Boot Prometheus" in Grafana's dashboard library) — these will show you QPS, error rates, response times, and more. You can specifically track failed requests by filtering metrics tagged with status=5xx or status=4xx.

SaaS Monitoring Platforms

If you don't want to manage your own infrastructure, use tools like Datadog, New Relic, or Dynatrace. They integrate seamlessly with Spring Boot/Micrometer:

  • Add the platform's Micrometer dependency to your project
  • Configure your API key in application.yml
  • The platform will automatically collect and visualize QPS, error rates, and other key metrics, plus send alerts when thresholds are breached (like a sudden spike in failed requests).

A Quick Note on Those Failed Requests

Since you're seeing 1 out of 12 requests fail, pair QPS monitoring with error rate tracking. The same metrics setup will highlight failed requests via HTTP status codes. Common causes for this kind of failure under high load include:

  • Thread pool exhaustion (check tomcat.threads.* metrics)
  • Database connection pool limits (check hikaricp.connections.* metrics)
  • Timeouts in downstream services

内容的提问来源于stack exchange,提问作者Bravo

火山引擎 最新活动