• Tomcat的:
  • Dubbo的:
  • grpc官方默认的:
  • xueqiu-spring的:

Tomcat的:

https://tomcat.apache.org/tomcat-9.0-doc/config/http.html

name

描述

maxThreads: 最大并发数,默认设置 200,一般建议在 500 ~ 800,根据硬件设施和业务来判断 The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used.
minSpareThreads: Tomcat 初始化时创建的线程数,默认设置 10 The minimum number of threads always kept running. This includes both active and idle threads. If not specified, the default of 10 is used. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used.
acceptCount: 最大的等待队列数,超过则拒绝请求,默认设置 100 The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.

Dubbo的:

https://dubbo.apache.org/docs/v2.7/user/examples/thread-model/

dubbo的做法是包罗万象的意思,线程池可以有多种的实现方式,并且可以控制哪些任务是通过线程池来处理,哪些任务直接在IO线程上处理

en

cn

eg:(这个例如是为了便于理解概念)

If events handing can be executed quickly without sending new request like marking in memory. Events should be handled by I/O thread since it reduces thread dispatching. 如果事件处理的逻辑能迅速完成,并且不会发起新的 IO 请求,比如只是在内存中记个标识,则直接在 IO 线程上处理更快,因为减少了线程池调度。 就好比你的服务是个redis(这样就没有必要使用线程池)

If event handling will be executed slowly or needs to send new I/O request like querying from database,

events should be handled in thread pool. Otherwise, I/O thread will be blocked and then will be not able to receive requests.

但如果事件处理逻辑较慢,或者需要发起新的 IO 请求,比如需要查询数据库,则必须派发到线程池,否则 IO 线程阻塞,将导致不能接收其它请求。 就好比你的服务是个经典的CURD操作(这个就很有必要使用线程池)

If events are handled by I/O thread, and send new I/O requests during the handling like sending a l login request during connect event,

it will alert with “Potentially leading to deadlock”, but deadlock will not happen actually.

如果用 IO 线程处理事件,又在事件处理过程中发起新的 IO 请求,比如在连接事件中发起登录请求,会报“可能引发死锁”异常,但不会真死锁。 --

https://github.com/apache/dubbo/blob/2d9583adf2/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java

默认值

name

描述

默认值

DEFAULT_CORE_THREADS 核心线程数 0
DEFAULT_THREADS 最大线程池大小 200
DEFAULT_QUEUES 线程池等待队列大小 0

grpc官方默认的:

没有看到相关的文档 直接贴代码吧:private static final ObjectPool<? extends Executor> DEFAULT_EXECUTOR_POOL = SharedResourcePool.forResource(GrpcUtil.SHARED_CHANNEL_EXECUTOR);

/**

 * Creates a thread pool that creates new threads as needed, but

 * will reuse previously constructed threads when they are

 * available, and uses the provided

 * ThreadFactory to create new threads when needed.

 * @param threadFactory the factory to use when creating new threads

 * @return the newly created thread pool

 * @throws NullPointerException if threadFactory is null

 */

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {

    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

                                  60L, TimeUnit.SECONDS,

                                  new SynchronousQueue(),

                                  threadFactory);

}

xueqiu-spring的:

Executor线程池可以自定义,也可以在使用xueqiu-spring的默认实现

1.自定义是依赖spring的IOC注入实现,要求实例bean的name为grpcExecutor

后续针对这种场景做了针对gRPC线程池的监控:集成监控Executor的线程池参数

example:

是个fixedthreadPool,机器硬件资源(CPU和内存)充足可以一直挂起这多的线程为grpc服务

@Bean(name = "grpcExecutor")

@Order(Ordered.HIGHEST_PRECEDENCE)

public static ExecutorService newFixedThreadPool() {

   AtomicInteger executorCounter = new AtomicInteger();

   ThreadFactory factory = (target) -> {

      Thread thread = new Thread(target, String.format("my-grpc-executor-%d", executorCounter.getAndIncrement()));

      thread.setDaemon(true);

      return thread;

   };

   return new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors() * 2, Runtime.getRuntime().availableProcessors() * 2,

         0L, TimeUnit.MILLISECONDS,

         new LinkedBlockingQueue(), factory);

}

各种线程池对应的场景

threadPool

场景

FixedThreadPool 可用于Web服务瞬时削峰,但需注意长时间持续高峰情况造成的队列阻塞。
CachedThreadPool 快速处理大量耗时较短的任务,如Netty的NIO接受请求时,可使用CachedThreadPool。
SingleThreadExecutor 单线程的队列模型(没有特殊应用场景,不推荐)
ScheduledThreadPool 延时执行线程队列(没有特殊应用场景,不推荐)
自定义 使用者可以根据自己的场景定制,好比拒绝策略(xueqiu-spring的拒绝策略就是告警日志,没有抛出异常)

2.xueqiu-spring默认实现

public static final String DEFAULT_REGION = "default";

public static final int DEFAULT_TIMEOUT = 5;

public static final int DEFAULT_INFLIGHT = 0;

public static final int DEFAULT_THREADS_MAX = 200;

public static final int DEFAULT_THREADS_CORE = Runtime.getRuntime().availableProcessors() * 2;

public static final int DEFAULT_PORT = 6565;

public static final int DEFAULT_MAX_INBOUND_MESSAGE_SIZE = 1024 1024 4;

……

private static Executor createGrpcExecutor(GRpcServerProperties properties) {

   // GRPC自带的线程池是CacheThreadPool 可以适当调整下

   RejectedExecutionHandler handler = (target, executor) -> {

      LOGGER.warn("can't handle request: {} in executor: {}", target, executor);

   };

   AtomicInteger executorCounter = new AtomicInteger();

   ThreadFactory factory = (target) -> {

      Thread thread = new Thread(target, String.format("xueqiu-grpc-executor-%d", executorCounter.getAndIncrement()));

      thread.setDaemon(true);

      return thread;

   };

   ArrayBlockingQueue inflight = new ArrayBlockingQueue<>(properties.getInflight(), false);

   Executor executor = new ThreadPoolExecutor(properties.getThreadsCore(), properties.getThreadsMax(), 5, TimeUnit.MINUTES, inflight, factory, handler);

   return executor;

}

默认参数配置:

name

描述

默认值

DEFAULT_THREADS_CORE
默认核心线程数 CPU核数*2
DEFAULT_THREADS_MAX
默认最大线程数 200
DEFAULT_INFLIGHT
默认线程等待队列大小 0

关于RPC框架封装时ThreadPool的选型和设计相关推荐

  1. RPC框架:从原理到选型,一文带你搞懂RPC

    大家好,我是华仔,RPC系列的文章是我去年写的,当时写的比较散,现在重新进行整理.对于想学习RPC框架的同学,通过这篇文章,让你知其然并知其所以然,便于以后技术选型,下面是文章内容目录: RPC 什么 ...

  2. RPC框架:一文带你搞懂RPC

    RPC是什么(GPT答) ChatGPT回答: RPC(Remote Procedure Call)是一种分布式应用程序的编程模型,允许程序在不同的计算机上运行.它以一种透明的方式,将一个程序的函数调 ...

  3. 通俗的解释一下什么是 RPC 框架?

    在这篇文章中: RPC 功能目标 RPC 调用分类 RPC 结构拆解 RPC 组件职责 RPC 实现分析 导出远程接口 导入远程接口与客户端代理 协议编解码 传输服务 执行调用 RPC 异常处理 总结 ...

  4. 漫谈grpc 4:grpc和其他rpc框架的横向对比,到底好在哪里?

    1,什么是grpc gRpc 是一个高性能.开源和通用的 RPC 框架,面向移动和 HTTP/2 设计.目前提供 C.Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc- ...

  5. 布道微服务_10注册中心与RPC框架的选型

    文章目录 开源注册中心 Nacos 其他 关注的问题 高可用性 数据一致性 CAP 三者不能被同时满足 选型经验 RPC框架选型 gRPC Thrift 开源注册中心 Nacos 毫无疑问,现在就选N ...

  6. .net RPC框架选型(一)

    近期开始研究分布式架构,会涉及到一个最核心的组件:RPC(Remote Procedure Call Protocol).这个东西的稳定性与性能,直接决定了分布式架构系统的好坏.RPC技术,我们的产品 ...

  7. 什么是RPC?RPC框架dubbo的核心流程

    一.REST 与 RPC: 1.什么是 REST 和 RPC 协议: 在单体应用中,各模块间的调用是通过编程语言级别的方法函数来实现,但分布式系统运行在多台机器上,一般来说,每个服务实例都是一个进程, ...

  8. 如何手撸一个较为完整的RPC框架

    [文章作者/来源]一个没有追求的技术人/https://sourl.cn/sJ4Brp 缘 起 最近在公司分享了手撸RPC,因此做一个总结. 概 念 篇 RPC 是什么? RPC 称远程过程调用(Re ...

  9. Spark RPC框架源码分析(二)RPC运行时序

    前情提要: Spark RPC框架源码分析(一)简述 一. Spark RPC概述 上一篇我们已经说明了Spark RPC框架的一个简单例子,Spark RPC相关的两个编程模型,Actor模型和Re ...

最新文章

  1. java内存溢出的情况解决方法
  2. 为什么要学习DOS?
  3. CSS3 Animation动画的十二原则
  4. 20本最好的Linux免费书籍
  5. 6.变量的使用.rs
  6. Chrome remote debugging protocol在自动化测试中的应用和实践
  7. SpringBoot和Elasticsearch集成
  8. 种草笔记App放话:要让一万创作者月入过万
  9. ContentType明细对照表(文件类型相关的设置)
  10. mysql数据库主从出现1236错误
  11. C语言各种keyword
  12. arcgis多个数据融合python_使用Python在ArcGIS中添加多个字段名
  13. dlopen失败一例:路径字串多一个回车,导致文件找不到
  14. ffmpeg: error while loading shared libraries: libavdevice.so.53
  15. 服务器维修工程师个人简历,机械维修工程师个人简历模板精选
  16. 主动微波遥感和被动微波遥感
  17. 无法在驱动器0分区上安装windows解决方法
  18. 【毕业设计】机器学习车牌识别系统 - python opencv
  19. 龙尚3G模块在arm板上的应用
  20. ViewOverlay 浮层

热门文章

  1. 看懂logcat日志
  2. 如何引入iconfont中的单色图标和多色图标(超简单)
  3. WEB前端打印使用记录
  4. python 爬虫 表格,python爬虫爬取网页表格数据
  5. node+vue微信小程序的社区后勤报修系统
  6. 清理Win11磁盘的方法
  7. 榨干运营成本:一亿之后再省两亿
  8. 关于U盘烧录iso问题
  9. 什么是企业管理咨询服务?
  10. python学习记录——容器篇