nacos
nacos copied to clipboard
想将client中线程池设置为可配置的
版本(version):1.4.0
this.executorService = Executors .newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { ...... });
目前ClientWorker中的executorService,默认线程数是核数,我们不需要这么多线程,想改为可配置的:例如:
在application.properties中添加:nacos.executorService.threadCount=2,则上面池中使用两个线程。
问题:如果不修改与nacos-spring有关的应用,只修改nacos项目,怎么在ClientWorker中能读到应用配置文件中的值呢?
由于nacos-spring-boot的配置项是固定的,你必须修改源码才能使用nacos.executorService.threadCount=2控制;
或者可以使用 System.setProperty("nacos.executorService.threadCoun", "2");
![]()
由于nacos-spring-boot的配置项是固定的,你必须修改源码才能使用nacos.executorService.threadCount=2控制; 或者可以使用 System.setProperty("nacos.executorService.threadCoun", "2");
多谢
其实应该支持所有可配置参数通过配置项和环境变量进行配置,只是目前我们还需要梳理和重构。待2.0稳定之后会对其进行重构。
一、线程池 1)nacos-grpc-client-executor线程:com.alibaba.nacos.common.remote.client.grpc.GrpcClient#connectToServer
int threadNumber = ThreadUtils.getSuitableThreadCount(8);
grpcExecutor = new ThreadPoolExecutor(threadNumber, threadNumber, 10L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(10000),
new ThreadFactoryBuilder().daemon(true).nameFormat("nacos-grpc-client-executor-%d")
.build());
grpcExecutor.allowCoreThreadTimeOut(true);
2)com.alibaba.nacos.client.Worker线程:com.alibaba.nacos.client.config.impl.ClientWorker#ClientWorker
ScheduledExecutorService executorService = Executors
.newScheduledThreadPool(Math.max(count, MIN_THREAD_NUM), r -> {
Thread t = new Thread(r);
t.setName("com.alibaba.nacos.client.Worker");
t.setDaemon(true);
return t;
});
版本信息:<nacos-client.version>2.0.1</nacos-client.version>
二、背景诉求 目前nacos client里是通过 ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE)的方式取的线程数,对于我们冗余浪费比较严重。我们的cpu是32核的,每次nacos-client new出来的线程有几百个,请问下client里的上面2处的线程池数量可以支持可配置化么?@KomachiSion
I have the same request. My server has 64 cores and 128 memory. The CPU was abnormally high before, and the memory file was analyzed. The number of threads of Nacos-client and grpc-service is several K. Analysis of the source code found that each thread pool is the number of virtual cores * 2, but in fact, the number of threads is not used in business, causing some unnecessary thread waste. I had to revise the source code and now it looks like the resource overhead has been reduced by almost a third.
一、线程池 1)nacos-grpc-client-executor线程:com.alibaba.nacos.common.remote.client.grpc.GrpcClient#connectToServer
int threadNumber = ThreadUtils.getSuitableThreadCount(8); grpcExecutor = new ThreadPoolExecutor(threadNumber, threadNumber, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10000), new ThreadFactoryBuilder().daemon(true).nameFormat("nacos-grpc-client-executor-%d") .build()); grpcExecutor.allowCoreThreadTimeOut(true);
2)com.alibaba.nacos.client.Worker线程:com.alibaba.nacos.client.config.impl.ClientWorker#ClientWorker
ScheduledExecutorService executorService = Executors .newScheduledThreadPool(Math.max(count, MIN_THREAD_NUM), r -> { Thread t = new Thread(r); t.setName("com.alibaba.nacos.client.Worker"); t.setDaemon(true); return t; });
版本信息:<nacos-client.version>2.0.1</nacos-client.version>
二、背景诉求 目前nacos client里是通过 ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE)的方式取的线程数,对于我们冗余浪费比较严重。我们的cpu是32核的,每次nacos-client new出来的线程有几百个,请问下client里的上面2处的线程池数量可以支持可配置化么?@KomachiSion
是的,我也遇到该问题了。
一、线程池 1)nacos-grpc-client-executor线程:com.alibaba.nacos.common.remote.client.grpc.GrpcClient#connectToServer
int threadNumber = ThreadUtils.getSuitableThreadCount(8); grpcExecutor = new ThreadPoolExecutor(threadNumber, threadNumber, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(10000), new ThreadFactoryBuilder().daemon(true).nameFormat("nacos-grpc-client-executor-%d") .build()); grpcExecutor.allowCoreThreadTimeOut(true);
2)com.alibaba.nacos.client.Worker线程:com.alibaba.nacos.client.config.impl.ClientWorker#ClientWorker
ScheduledExecutorService executorService = Executors .newScheduledThreadPool(Math.max(count, MIN_THREAD_NUM), r -> { Thread t = new Thread(r); t.setName("com.alibaba.nacos.client.Worker"); t.setDaemon(true); return t; });
版本信息:<nacos-client.version>2.0.1</nacos-client.version> 二、背景诉求 目前nacos client里是通过 ThreadUtils.getSuitableThreadCount(THREAD_MULTIPLE)的方式取的线程数,对于我们冗余浪费比较严重。我们的cpu是32核的,每次nacos-client new出来的线程有几百个,请问下client里的上面2处的线程池数量可以支持可配置化么?@KomachiSion
是的,我也遇到该问题了。 我们的解决方案是:获取到当前机器的cpu数量和16 取最小值。这样避免线程浪费。这样操作下来,我们的nacos 资源消耗直接降低一半 哈哈。
-Dnacos.remote.client.grpc.pool.max.size -Dnacos.remote.client.grpc.pool.core.size
哪个版本开始支持的忘了, 最新版本那肯定是支持的