Spring Boot(Jetty)REST服务HTTP连接数限制配置失效求助
Hey there, let's figure out why your current config isn't limiting connections and how to fix it properly.
First, What Your Current Config Actually Does
Let's break down those parameters you set—they don't do what you think they do for connection limiting:
server.jetty.acceptors=1: This sets the number of threads Jetty uses to accept new incoming connections. It's about how fast Jetty can take new connections, not how many total connections are allowed. Even with 1 acceptor thread, it can still accept multiple connections over time (or even concurrently if the OS allows it).server.jetty.selectors=1: This is the number of NIO threads that handle IO operations for established connections. A single selector thread can manage multiple connections efficiently (that's the point of NIO), so this doesn't cap the total number of active connections either.server.connection-timeout=-1: This just disables idle connection timeout, so connections stay open indefinitely—this part works as you expect, but it doesn't limit how many can exist.
That's why your 3 clients could all connect and stay active: those parameters don't restrict the total number of concurrent connections.
How to Actually Limit Concurrent Connections
To cap the number of active connections in Jetty, you need to configure Jetty's maxConnections setting. Since Spring Boot doesn't expose this as a simple property, you'll need to create a custom configuration class to tweak the Jetty server directly:
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JettyConnectionLimitConfig { @Bean public WebServerFactoryCustomizer<JettyServletWebServerFactory> jettyConnectionCustomizer() { return factory -> { factory.addServerCustomizers(server -> { // Set maximum concurrent connections to 1 server.setMaxConnections(1); // Optional: If you also want to limit concurrent requests (not just connections) // You can configure the thread pool to restrict how many requests are processed at once // server.setThreadPool(new org.eclipse.jetty.util.thread.QueuedThreadPool(1, 1)); }); }; } }
A Couple of Notes
- When
maxConnectionsis hit: Jetty will typically reject new connection attempts (or queue them depending on your OS and Jetty's internal settings) until an existing connection is closed. - If you also want to limit how many requests are being processed at the same time (not just how many connections are open), uncomment the thread pool configuration line. This sets both the core and maximum thread count to 1, ensuring only one request is handled at a time.
After adding this configuration, restart your app and test again—you should see that only one client can maintain an active connection at a time.
内容的提问来源于stack exchange,提问作者Onki

