我想将我的dropwizard传入请求限制为3。 因此,如果服务器正在为3个请求提供服务,我想发送一个503:"服务器容量达到"回应代码。
我知道Dropwizard已配置最大排队请求:
MaxQueuedRequests
默认为1024。 但是,我不知道如何使用此参数。 当我做这个3时,我的服务器不会启动。
如果有人可以帮助提供有关用于实现此目的的优选设计模式的更多信息吗?
我正在考虑的一种方法是:使用全局(或静态)变量来保持计数器。 对于每个请求,我都会递增计数器。 当计数器> 3,我回答Code 503。
希望听到更多关于此的想法。
编辑:
所以我能够在@pandaadb回答之后取得进展。 但是,我打了另一个路障:( 这就是我的配置:
server:
minThreads: 2
maxQueuedRequests: 3
type: simple
applicationContextPath: /
adminContextPath: /admin
connector:
type: http
port: 9999
有了这个,我不断得到以下例外:
WARN [2016-06-23 08:38:52,354] io.dropwizard.setup.AdminEnvironment:
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! THIS APPLICATION HAS NO HEALTHCHECKS. THIS MEANS YOU WILL NEVER KNOW !
! IF IT DIES IN PRODUCTION, WHICH MEANS YOU WILL NEVER KNOW IF YOU'RE !
! LETTING YOUR USERS DOWN. YOU SHOULD ADD A HEALTHCHECK FOR EACH OF YOUR !
! APPLICATION'S DEPENDENCIES WHICH FULLY (BUT LIGHTLY) TESTS IT. !
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INFO [2016-06-23 08:38:52,358] org.eclipse.jetty.server.handler.ContextHandler: Started i.d.j.MutableServletContextHandler@9d200de{/admin,null,AVAILABLE}
WARN [2016-06-23 08:38:52,363] org.eclipse.jetty.util.thread.QueuedThreadPool: dw{STARTED,2<=3<=1024,i=0,q=3} rejected org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
WARN [2016-06-23 08:38:52,367] org.eclipse.jetty.util.component.AbstractLifeCycle: FAILED org.eclipse.jetty.server.ServerConnector$ServerConnectorManager@289ba785: java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
! java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
! at org.eclipse.jetty.util.thread.QueuedThreadPool.execute(QueuedThreadPool.java:362) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.execute(SelectorManager.java:160) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.doStart(SelectorManager.java:258) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:256) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.Server.doStart(Server.java:366) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:43) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.Cli.run(Cli.java:70) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.Application.run(Application.java:73) [dropwizard-core-0.8.2.jar:0.8.2]
! at net.lacework.eventgen.EventGeneratorMain.main(EventGeneratorMain.java:16) [classes/:na]
! at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
! at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]
! at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]
! at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]
! at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
WARN [2016-06-23 08:38:52,368] org.eclipse.jetty.util.component.AbstractLifeCycle: FAILED query@3e8f7922{HTTP/1.1}{0.0.0.0:9999}: java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
! java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
! at org.eclipse.jetty.util.thread.QueuedThreadPool.execute(QueuedThreadPool.java:362) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.execute(SelectorManager.java:160) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.doStart(SelectorManager.java:258) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:256) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.Server.doStart(Server.java:366) [jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) [jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:43) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.Cli.run(Cli.java:70) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.Application.run(Application.java:73) [dropwizard-core-0.8.2.jar:0.8.2]
! at net.lacework.eventgen.EventGeneratorMain.main(EventGeneratorMain.java:16) [classes/:na]
! at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
! at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]
! at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]
! at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]
! at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
WARN [2016-06-23 08:38:52,368] org.eclipse.jetty.util.component.AbstractLifeCycle: FAILED org.eclipse.jetty.server.Server@70242f38: java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
! java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
! at org.eclipse.jetty.util.thread.QueuedThreadPool.execute(QueuedThreadPool.java:362) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.execute(SelectorManager.java:160) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.doStart(SelectorManager.java:258) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:256) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.Server.doStart(Server.java:366) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:43) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.Cli.run(Cli.java:70) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.Application.run(Application.java:73) [dropwizard-core-0.8.2.jar:0.8.2]
! at net.lacework.eventgen.EventGeneratorMain.main(EventGeneratorMain.java:16) [classes/:na]
! at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_77]
! at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_77]
! at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_77]
! at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_77]
! at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
ERROR [2016-06-23 08:38:52,368] io.dropwizard.cli.ServerCommand: Unable to start server, shutting down
! java.util.concurrent.RejectedExecutionException: org.eclipse.jetty.util.thread.NonBlockingThread@109d724c
! at org.eclipse.jetty.util.thread.QueuedThreadPool.execute(QueuedThreadPool.java:362) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.execute(SelectorManager.java:160) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.io.SelectorManager.doStart(SelectorManager.java:258) ~[jetty-io-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:106) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:256) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.server.Server.doStart(Server.java:366) ~[jetty-server-9.2.9.v20150224.jar:9.2.9.v20150224]
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68) ~[jetty-util-9.2.9.v20150224.jar:9.2.9.v20150224]
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:43) ~[dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.cli.Cli.run(Cli.java:70) [dropwizard-core-0.8.2.jar:0.8.2]
! at io.dropwizard.Application.run(Application.java:73) [dropwizard-core-0.8.2.jar:0.8.2]
理想情况下,我还想尝试将我的服务器请求限制为1.这样它一次只能处理1个请求。 另外,我不知道进入WAIT的新请求。相反,他们服务器应该使用一些响应代码拒绝它们。
我不确定dropwizard是否已内置此功能。
答案 0 :(得分:2)
我认为您的问题是您只更改了一个值,而不是其他值。并且DW验证没有捕捉到这种情况。
如果您不更改容量,则设置MaxQueuedRequests不允许3。看这里:
/**
* Create a bounded {@link BlockingArrayQueue} that grows by the given parameter.
*
* @param capacity
* the initial capacity
* @param growBy
* the growth factor
* @param maxCapacity
* the maximum capacity
*/
public BlockingArrayQueue(int capacity, int growBy, int maxCapacity)
{
if (capacity > maxCapacity)
throw new IllegalArgumentException();
_elements = new Object[capacity];
_growCapacity = growBy;
_maxCapacity = maxCapacity;
}
这是由服务器工厂调用的:
protected ThreadPool createThreadPool(MetricRegistry metricRegistry) {
final BlockingQueue<Runnable> queue = new BlockingArrayQueue<>(minThreads, maxThreads, maxQueuedRequests);
final InstrumentedQueuedThreadPool threadPool =
new InstrumentedQueuedThreadPool(metricRegistry, maxThreads, minThreads,
(int) idleThreadTimeout.toMilliseconds(), queue);
threadPool.setName("dw");
return threadPool;
}
意味着你的minThreads需要3或更少才能使用,否则你的容量大于maxCapacity,其中:
容量:minThreads maxCapacity:maxQueuedRequests
编辑:在获得Op的更多信息之后,这是解决方案:
server:
minThreads: 2
maxQueuedRequests: 3
rootPath: /api/*
requestLog:
appenders: []
applicationConnectors:
- type: http
port: 9085
acceptorThreads: 3
selectorThreads: 3
你的错误并不是真正的错误 - 我相信它是Jetty中的一个错误(或者说是在启动时未经过验证的错误配置)。
问题是jetty开始了一些acceptorthreads / selectorthreads。这些是count()&gt;但是线程是由服务器生命周期启动的,所以jetty会将它们炸成自己的线程池。这没有问题,因为它们一旦被执行就会消失(您可以通过设置断点并慢慢地逐步完成启动来测试它 - 您的服务器将启动就好了)。
解决方案是限制接受器/选择器线程(请做一些实验/研究以了解事情的作用并正确配置它们)。 因此,通过限制这些线程,启动不会使您的码头过载,一切都很好。
(我没有用这个相当奇怪的设置做任何有关性能/稳定性的实验)。
FWIW - 我解决这个问题的方式:
实施请求过滤器,用于计算传入/传出请求。一旦请求进入并且计数器是> X,立即拒绝请求指示高负载和稍后重试的响应(因此您的客户端知道该怎么做)。上面的配置可能会导致从码头抛出500,这将使您的客户认为您的服务器已关闭。
此致
阿图尔