gRPC服务器是否为每个请求启动一个新线程?

gRPC服务器是否为每个请求启动一个新线程?

问题描述:

我尝试对gRPC Java服务器进行性能分析.而且我主要看到以下线程池集.

I tried profiling a gRPC java server. And i see the below set of thread pools majorly.

  • grpc-default-executor线程:为每个传入请求创建1个.
  • grpc-default-worker-ELG线程:可能是侦听传入的gRPC请求&分配给上述"grpc-default-executor"线程.

总体而言,gRPC是Java服务器,Netty样式还是Jetty/Tomcat样式?还是可以配置为同时运行?

Overall, is gRPC java server, Netty style or Jetty/Tomcat style? Or it can configured to run as both ways?

gRPC Java服务器暴露于Jetty/Tomcat风格,但它是异步的.也就是说,在普通的Servlet中,每个请求都会消耗一个线程,直到完成为止.虽然较新的Servlet版本使您可以从专用线程中分离出来,然后继续异步工作(释放线程以供其他使用),这种情况并不常见.在gRPC中,您可以*使用任何一种样式.请注意,默认情况下,gRPC使用cachedThreadPool重用线程.在服务器端,最好通过ServerBuilder.executor()用您自己的(通常为固定大小的)池替换默认执行程序.

gRPC Java server is exposed closer to Jetty/Tomcat style, except that it is asynchronous. That is, in normal Servlets each request consumes a thread until it is complete. While newer Servlet versions let you detach from the dedicated thread and continue work asynchronously (freeing the thread for other use) that is more uncommon. In gRPC you are free to work in either style. Note that gRPC uses a cachedThreadPool by default to reuse threads; on server-side it's a good idea to replace the default executor with your own, generally fixed-size, pool via ServerBuilder.executor().

内部,gRPC Java使用Netty样式.这意味着完全不阻塞.您可以使用ServerBuilder.directExecutor()在Netty线程上运行.尽管在这种情况下,您可能需要指定NettyServerBuilder.bossEventLoopGroup()workerEventLoopGroup()和兼容性channelType().

Internally gRPC Java uses the Netty-style. That means fully non-blocking. You may use ServerBuilder.directExecutor() to run on the Netty threads. Although in that case you may want to specify the NettyServerBuilder.bossEventLoopGroup(), workerEventLoopGroup(), and for compatibility channelType().