How to write a high performance Netty Client

前端 未结 2 1614
不思量自难忘°
不思量自难忘° 2020-12-12 16:30

I want an extremely efficient TCP client to send google protocol buffer messages. I have been using the Netty library to develop a server/client.

In tests the serve

2条回答
  •  青春惊慌失措
    2020-12-12 17:10

    1) If the client is only interested in sending, not in receiving, you can always disable reading from channel like below

    channel.setReadable(false);
    

    2) You can increase the throughput very easily by having multiple client channels per client, and also it can scale too.

    3) and you can do following tweaks to improve the performance in general (for read/ write)

    • Its better to have a SEDA like pipline by adding a EXecutionHandler with OrderdMemoryAwareThreadPoolExecutor, (with min, max channel memory with optimal value)

      bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
          @Override
          public ChannelPipeline getPipeline() throws Exception {
              return Channels.pipeline(
                      executionHandler1,//sharable
                      new MessageDecoderHandler(),
                      new MessageEncoderHandler(),
                      executionHandler2,//sharable
                      new BusinessLogicHandler1(),
                      new BusinessLogicHandler2());
          }
      });
      
    • Setting the writeBufferHighWaterMark of the channel to optimal value (Make sure that setting a big value will not create congestion)

      bootstrap.setOption("writeBufferHighWaterMark", 10 * 64 * 1024);

    • Setting the SO_READ, SO_WRITE buffer size

      bootstrap.setOption("sendBufferSize", 1048576); bootstrap.setOption("receiveBufferSize", 1048576);

    • Enabling the TCP No delay

      bootstrap.setOption("tcpNoDelay", true);

提交回复
热议问题