Java 净请求超时
我正在尝试编写一个HTTP服务,该服务将从HTTP获取数据,并使用Netty将其放入Kafka中。我需要在一个m5.large EC2实例上处理20K RPS,这似乎是可行的 代码很简单:Java 净请求超时,java,apache-kafka,netty,kafka-producer-api,Java,Apache Kafka,Netty,Kafka Producer Api,我正在尝试编写一个HTTP服务,该服务将从HTTP获取数据,并使用Netty将其放入Kafka中。我需要在一个m5.large EC2实例上处理20K RPS,这似乎是可行的 代码很简单: Server.java RequestHandler.java 公共类RequestHandler扩展了SimpleChannelInboundHandler{ 私人最终生产者; 公开招标人(最终制作人){ this.producer=生产者; } @凌驾 公共无效通道读取完成(最终通道HandlerCont
Server.java
RequestHandler.java
公共类RequestHandler扩展了SimpleChannelInboundHandler{
私人最终生产者;
公开招标人(最终制作人){
this.producer=生产者;
}
@凌驾
公共无效通道读取完成(最终通道HandlerContext ctx){
ctx.flush();
}
@凌驾
受保护的无效channelRead0(ChannelHandlerContext ctx,FullHttpMessage msg){
最终DefaultFullHttpResponse响应=新的DefaultFullHttpResponse(HTTP_1_1,OK);
最终生产记录=新生产记录(
“测试”,
UUID.randomUUID().toString(),
msg.content().nioBuffer()
);
制作人。发送(记录);
if(HttpUtil.isKeepAlive(msg)){
response.headers().set(连接,HttpHeaderValues.KEEP_活动);
}
write(response).addListener(ChannelFutureListener.CLOSE);
}
}
代码取自官方文件。但是,有时我会收到请求'Post BidRequest'失败:j.u.c.TimeoutException:负载测试中60000 ms后请求超时异常
据我所知,这意味着在我的负载测试实例和服务实例之间建立了连接,但完成连接花费了超过60秒的时间。这个简单程序的哪个部分可以阻止这么长时间
我已经调整了卡夫卡制作人:减少了超时时间。我知道send
可能会被阻塞,所以我增加了发送缓冲区,但没有帮助。
我还为服务用户增加了ulimits
。
我运行的是OpenJDK版本1.8.0_171和securerandom。source
设置为文件:/dev/urandom
,因此对randomUUID
的调用不应该被阻止。你说得对,里面没有任何东西应该被阻止。发送到Kafka的调用是异步的。我浏览了你的代码,从我所看到的一切看起来都很好
我要检查几件事:
- 确保AWS中的安全组定义允许Kafka服务器和应用程序与Zookeeper对话。如果这是一个测试/POC,您应该只允许所有三个实例/集群之间的所有通信。60秒的超时使我怀疑网络超时,这可能意味着无法访问某些服务
- 你有没有试过一个更简单的测试,试着不依赖卡夫卡?也许这有助于缩小问题的范围
Kafka和ZK都托管在另一台机器上。安全组允许我的应用程序与卡夫卡对话。我还使用JVisualVM跟踪了该应用程序,所有worker踏板几乎100%的时间都在工作,没有任何阻塞(绿色实线)。CPU和RAM为最大值的25%。我开始认为这些反应只是在电线上的某个地方丢失了,但我不知道如何追踪。虚拟机网络是万兆。
public class Server {
public static void main(final String[] args) throws Exception {
final EventLoopGroup bossGroup = new EpollEventLoopGroup();
final EventLoopGroup workerGroup = new EpollEventLoopGroup();
try {
final ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap
.group(bossGroup, workerGroup)
.channel(EpollServerSocketChannel.class)
.childHandler(new RequestChannelInitializer(createProducer()))
.childOption(ChannelOption.SO_KEEPALIVE, true);
bootstrap.bind(PORT).sync().channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
private static Producer<String, ByteBuffer> createProducer() {
final Properties properties = new Properties();
properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_HOST);
properties.put(ProducerConfig.CLIENT_ID_CONFIG, "KafkaBidRequestProducer");
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteBufferSerializer.class.getName());
properties.put(ProducerConfig.RETRIES_CONFIG, 0);
properties.put(ProducerConfig.REQUEST_TIMEOUT_MS_CONFIG, 10000);
properties.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10000);
properties.put(ProducerConfig.SEND_BUFFER_CONFIG, 33554432);
return new KafkaProducer<>(properties);
}
}
public class RequestChannelInitializer extends io.netty.channel.ChannelInitializer<SocketChannel> {
private final Producer<String, ByteBuffer> producer;
public BidRequestChannelInitializer(final Producer<String, ByteBuffer> producer) {
this.producer = producer;
}
@Override
public void initChannel(final SocketChannel ch) {
ch.pipeline().addLast(new HttpServerCodec());
ch.pipeline().addLast(new HttpObjectAggregator(1048576));
ch.pipeline().addLast(new RequestHandler(producer));
}
}
public class RequestHandler extends SimpleChannelInboundHandler<FullHttpMessage> {
private final Producer<String, ByteBuffer> producer;
public BidRequestHandler(final Producer<String, ByteBuffer> producer) {
this.producer = producer;
}
@Override
public void channelReadComplete(final ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpMessage msg) {
final DefaultFullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK);
final ProducerRecord<String, ByteBuffer> record = new ProducerRecord<>(
"test",
UUID.randomUUID().toString(),
msg.content().nioBuffer()
);
producer.send(record);
if (HttpUtil.isKeepAlive(msg)) {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
}
}