这个问题回答见: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的实现方法如下
代码实现:git.snowballfinance.com/lib/xueqiu-…
@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, ``0``, ``5``, TimeUnit.SECONDS);`` ``} } |
|---|
效果:matrix.snowballfinance.com/d/ie-LFzyZp…