Hystrix thread pool monitoring optimization

pull/284/head
shiming-stars-lk 2 years ago committed by shining-stars-lk
parent 36e7f64926
commit c1e5b4a617

@ -27,8 +27,8 @@ import org.springframework.context.annotation.Configuration;
public class ThreadPoolAdapterExtraAutoConfiguration { public class ThreadPoolAdapterExtraAutoConfiguration {
@Bean @Bean
public ThreadPoolAdapterExtra threadPoolAdapterExtra() { public ThreadPoolAdapterScheduler threadPoolAdapterExtra() {
return new ThreadPoolAdapterExtra(); return new ThreadPoolAdapterScheduler();
} }
} }

@ -1,29 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.hippo4j.adapter.base;
import java.util.Map;
/**
* Thread Pool Adapter Extra Handle
**/
@FunctionalInterface
public interface ThreadPoolAdapterExtraHandle {
void execute(Map<String, ThreadPoolAdapter> map);
}

@ -19,24 +19,21 @@ package cn.hippo4j.adapter.base;
import com.google.common.util.concurrent.ThreadFactoryBuilder; import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* thread pool adapter extra. * thread pool adapter schedule.
*/ */
@Slf4j @Slf4j
public class ThreadPoolAdapterExtra { public class ThreadPoolAdapterScheduler {
private static final int TASK_INTERVAL_SECONDS = 2;
private final ScheduledExecutorService scheduler; private final ScheduledExecutorService scheduler;
public ThreadPoolAdapterExtra() { public ThreadPoolAdapterScheduler() {
scheduler = new ScheduledThreadPoolExecutor(2, scheduler = new ScheduledThreadPoolExecutor(2,
new ThreadFactoryBuilder() new ThreadFactoryBuilder()
.setNameFormat("threadPoolAdapter") .setNameFormat("threadPoolAdapter")
@ -47,4 +44,8 @@ public class ThreadPoolAdapterExtra {
public ScheduledExecutorService getScheduler() { public ScheduledExecutorService getScheduler() {
return scheduler; return scheduler;
} }
public int getTaskIntervalSeconds(){
return TASK_INTERVAL_SECONDS;
}
} }

@ -18,13 +18,11 @@
package cn.hippo4j.adapter.hystrix; package cn.hippo4j.adapter.hystrix;
import cn.hippo4j.adapter.base.ThreadPoolAdapter; import cn.hippo4j.adapter.base.ThreadPoolAdapter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtra; import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter; import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterState; import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.CollectionUtil;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.netflix.hystrix.HystrixThreadPool; import com.netflix.hystrix.HystrixThreadPool;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent;
@ -32,13 +30,10 @@ import org.springframework.context.ApplicationListener;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -54,15 +49,13 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
private static final String THREAD_POOLS_FIELD = "threadPools"; private static final String THREAD_POOLS_FIELD = "threadPools";
private static final int TASK_INTERVAL_SECONDS = 2;
private final Map<String, ThreadPoolExecutor> HYSTRIX_CONSUME_EXECUTOR = Maps.newHashMap(); private final Map<String, ThreadPoolExecutor> HYSTRIX_CONSUME_EXECUTOR = Maps.newHashMap();
private ThreadPoolAdapterExtra threadPoolAdapterExtra; private ThreadPoolAdapterScheduler threadPoolAdapterScheduler;
public HystrixThreadPoolAdapter(ThreadPoolAdapterExtra threadPoolAdapterExtra) { public HystrixThreadPoolAdapter(ThreadPoolAdapterScheduler threadPoolAdapterScheduler) {
this.threadPoolAdapterExtra = threadPoolAdapterExtra; this.threadPoolAdapterScheduler = threadPoolAdapterScheduler;
} }
@ -113,9 +106,10 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
@Override @Override
public void onApplicationEvent(ApplicationStartedEvent event) { public void onApplicationEvent(ApplicationStartedEvent event) {
ScheduledExecutorService scheduler = threadPoolAdapterExtra.getScheduler(); ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler); int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds();
scheduler.schedule(hystrixThreadPoolRefreshTask, TASK_INTERVAL_SECONDS, TimeUnit.SECONDS); HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler,taskIntervalSeconds);
scheduler.schedule(hystrixThreadPoolRefreshTask, taskIntervalSeconds, TimeUnit.SECONDS);
} }
public void hystrixThreadPoolRefresh() { public void hystrixThreadPoolRefresh() {
@ -151,8 +145,11 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
private ScheduledExecutorService scheduler; private ScheduledExecutorService scheduler;
public HystrixThreadPoolRefreshTask(ScheduledExecutorService scheduler) { private int taskIntervalSeconds;
public HystrixThreadPoolRefreshTask(ScheduledExecutorService scheduler, int taskIntervalSeconds) {
this.scheduler = scheduler; this.scheduler = scheduler;
this.taskIntervalSeconds = taskIntervalSeconds;
} }
@Override @Override
@ -161,7 +158,7 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
hystrixThreadPoolRefresh(); hystrixThreadPoolRefresh();
} finally { } finally {
if (!scheduler.isShutdown()) { if (!scheduler.isShutdown()) {
scheduler.schedule(this, TASK_INTERVAL_SECONDS, TimeUnit.MILLISECONDS); scheduler.schedule(this, taskIntervalSeconds, TimeUnit.MILLISECONDS);
} }
} }
} }

@ -17,7 +17,7 @@
package cn.hippo4j.springboot.starter.adapter.hystrix; package cn.hippo4j.springboot.starter.adapter.hystrix;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtra; import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtraAutoConfiguration; import cn.hippo4j.adapter.base.ThreadPoolAdapterExtraAutoConfiguration;
import cn.hippo4j.adapter.hystrix.HystrixThreadPoolAdapter; import cn.hippo4j.adapter.hystrix.HystrixThreadPoolAdapter;
import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.config.ApplicationContextHolder;
@ -43,7 +43,7 @@ public class HystrixAdapterAutoConfiguration {
} }
@Bean @Bean
public HystrixThreadPoolAdapter hystrixThreadPoolAdapter(ThreadPoolAdapterExtra threadPoolAdapterExtra) { public HystrixThreadPoolAdapter hystrixThreadPoolAdapter(ThreadPoolAdapterScheduler threadPoolAdapterScheduler) {
return new HystrixThreadPoolAdapter(threadPoolAdapterExtra); return new HystrixThreadPoolAdapter(threadPoolAdapterScheduler);
} }
} }

@ -18,7 +18,7 @@
package cn.hippo4j.springboot.starter.config; package cn.hippo4j.springboot.starter.config;
import cn.hippo4j.adapter.base.ThreadPoolAdapterBeanContainer; import cn.hippo4j.adapter.base.ThreadPoolAdapterBeanContainer;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtra; import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
import cn.hippo4j.common.api.ThreadDetailState; import cn.hippo4j.common.api.ThreadDetailState;
import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.config.UtilAutoConfiguration; import cn.hippo4j.core.config.UtilAutoConfiguration;
@ -166,7 +166,7 @@ public class DynamicThreadPoolAutoConfiguration {
@Bean @Bean
@SuppressWarnings("all") @SuppressWarnings("all")
public ThreadPoolAdapterRegister threadPoolAdapterRegister(HttpAgent httpAgent, InetUtils hippo4JInetUtils, ThreadPoolAdapterExtra threadPoolAdapterExtra) { public ThreadPoolAdapterRegister threadPoolAdapterRegister(HttpAgent httpAgent, InetUtils hippo4JInetUtils, ThreadPoolAdapterScheduler threadPoolAdapterScheduler) {
return new ThreadPoolAdapterRegister(httpAgent, properties, environment, hippo4JInetUtils, threadPoolAdapterExtra); return new ThreadPoolAdapterRegister(httpAgent, properties, environment, hippo4JInetUtils, threadPoolAdapterScheduler);
} }
} }

@ -19,7 +19,7 @@ package cn.hippo4j.springboot.starter.core;
import cn.hippo4j.adapter.base.ThreadPoolAdapter; import cn.hippo4j.adapter.base.ThreadPoolAdapter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterCacheConfig; import cn.hippo4j.adapter.base.ThreadPoolAdapterCacheConfig;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtra; import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
import cn.hippo4j.adapter.base.ThreadPoolAdapterState; import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.CollectionUtil; import cn.hippo4j.common.toolkit.CollectionUtil;
@ -62,7 +62,7 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
private final InetUtils hippo4JInetUtils; private final InetUtils hippo4JInetUtils;
private final ThreadPoolAdapterExtra threadPoolAdapterExtra; private final ThreadPoolAdapterScheduler threadPoolAdapterScheduler;
private List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList(); private List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
@ -73,7 +73,7 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
ScheduledExecutorService scheduler = threadPoolAdapterExtra.getScheduler(); ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
ThreadPoolAdapterRegisterTask threadPoolAdapterRegisterTask = new ThreadPoolAdapterRegisterTask(scheduler); ThreadPoolAdapterRegisterTask threadPoolAdapterRegisterTask = new ThreadPoolAdapterRegisterTask(scheduler);

Loading…
Cancel
Save