Redis连接池不够用?

这个问题回答见:lettuce 共享连接

 

xueqiu-toolbox-spring提供了spring中Executor类型bean的默认监控:集成监控Executor的线程池参数

<!-- 版本从50开始:0.0.50 -->
<dependency>
    <groupId>com.xueqiu.infra.toolbox</groupId>
    <artifactId>xueqiu-toolbox-spring</artifactId>
</dependency>

 

 

从这个问题主要是引出监控的事情,要是想要对一个线程池进行matrix监控,那么当前比较low的实现方法如下

代码实现:http://git.snowballfinance.com/lib/xueqiu-push/blob/prod/xueqiu-push-server/src/main/java/com/xueqiu/infra/push/server/prometheus/ThreadPoolMonitor.java

@Service

public class ThreadPoolMonitor implements InitializingBean {

 

    @Resource(name = "consumerExecutor")

    ThreadPoolTaskExecutor consumerExecutor;

    @Resource

    ConnectionPool connectionPool;

 

    private static final Iterable<Tag> consumerExecutorTAG = Collections.singletonList(Tag.of("thread.pool.name""consumerExecutor"));

    private static final Iterable<Tag> okHttpExecutorTAG = Collections.singletonList(Tag.of("thread.pool.name""okHttpExecutor"));

    private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();

 

    private Runnable monitor = () -> {

        //这里需要捕获异常,尽管实际上不会产生异常,但是必须预防异常导致调度线程池线程失效的问题

        try {

            Metrics.gauge("thread.pool.core.size", consumerExecutorTAG, consumerExecutor, ThreadPoolTaskExecutor::getCorePoolSize);

            Metrics.gauge("thread.pool.largest.size", consumerExecutorTAG, consumerExecutor, e -> e.getThreadPoolExecutor().getLargestPoolSize());

            Metrics.gauge("thread.pool.max.size", consumerExecutorTAG, consumerExecutor, ThreadPoolTaskExecutor::getMaxPoolSize);

            Metrics.gauge("thread.pool.active.size", consumerExecutorTAG, consumerExecutor, ThreadPoolTaskExecutor::getActiveCount);

            Metrics.gauge("thread.pool.thread.count", consumerExecutorTAG, consumerExecutor, ThreadPoolTaskExecutor::getPoolSize);

            Metrics.gauge("thread.pool.queue.size", consumerExecutorTAG, consumerExecutor, e -> e.getThreadPoolExecutor().getQueue().size());

 

 

            Metrics.gauge("thread.pool.connectionCount", okHttpExecutorTAG, connectionPool, ConnectionPool::connectionCount);

            Metrics.gauge("thread.pool.idleConnectionCount", okHttpExecutorTAG, connectionPool, ConnectionPool::idleConnectionCount);

        catch (Exception e) {

            //ignore

        }

    };

 

    @Override

    public void afterPropertiesSet() throws Exception {

        // 每5秒执行一次

        scheduledExecutor.scheduleWithFixedDelay(monitor, 05, TimeUnit.SECONDS);

    }

 

}

效果:http://matrix.snowballfinance.com/d/ie-LFzyZp/xueqiu-push-detail?orgId=1&refresh=5s

 

GitHub 加速计划 / th / ThreadPool
7.74 K
2.22 K
下载
A simple C++11 Thread Pool implementation
最近提交(Master分支:2 个月前 )
9a42ec13 - 9 年前
fcc91415 - 9 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐