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 {
@Bean
public ThreadPoolAdapterExtra threadPoolAdapterExtra() {
return new ThreadPoolAdapterExtra();
public ThreadPoolAdapterScheduler 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 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.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* thread pool adapter extra.
* thread pool adapter schedule.
*/
@Slf4j
public class ThreadPoolAdapterExtra {
public class ThreadPoolAdapterScheduler {
private static final int TASK_INTERVAL_SECONDS = 2;
private final ScheduledExecutorService scheduler;
public ThreadPoolAdapterExtra() {
public ThreadPoolAdapterScheduler() {
scheduler = new ScheduledThreadPoolExecutor(2,
new ThreadFactoryBuilder()
.setNameFormat("threadPoolAdapter")
@ -47,4 +44,8 @@ public class ThreadPoolAdapterExtra {
public ScheduledExecutorService getScheduler() {
return scheduler;
}
public int getTaskIntervalSeconds(){
return TASK_INTERVAL_SECONDS;
}
}

@ -18,13 +18,11 @@
package cn.hippo4j.adapter.hystrix;
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.ThreadPoolAdapterState;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.CollectionUtil;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.netflix.hystrix.HystrixThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
@ -32,13 +30,10 @@ import org.springframework.context.ApplicationListener;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
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 int TASK_INTERVAL_SECONDS = 2;
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
public void onApplicationEvent(ApplicationStartedEvent event) {
ScheduledExecutorService scheduler = threadPoolAdapterExtra.getScheduler();
HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler);
scheduler.schedule(hystrixThreadPoolRefreshTask, TASK_INTERVAL_SECONDS, TimeUnit.SECONDS);
ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds();
HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler,taskIntervalSeconds);
scheduler.schedule(hystrixThreadPoolRefreshTask, taskIntervalSeconds, TimeUnit.SECONDS);
}
public void hystrixThreadPoolRefresh() {
@ -151,8 +145,11 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
private ScheduledExecutorService scheduler;
public HystrixThreadPoolRefreshTask(ScheduledExecutorService scheduler) {
private int taskIntervalSeconds;
public HystrixThreadPoolRefreshTask(ScheduledExecutorService scheduler, int taskIntervalSeconds) {
this.scheduler = scheduler;
this.taskIntervalSeconds = taskIntervalSeconds;
}
@Override
@ -161,7 +158,7 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
hystrixThreadPoolRefresh();
} finally {
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;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtra;
import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtraAutoConfiguration;
import cn.hippo4j.adapter.hystrix.HystrixThreadPoolAdapter;
import cn.hippo4j.common.config.ApplicationContextHolder;
@ -43,7 +43,7 @@ public class HystrixAdapterAutoConfiguration {
}
@Bean
public HystrixThreadPoolAdapter hystrixThreadPoolAdapter(ThreadPoolAdapterExtra threadPoolAdapterExtra) {
return new HystrixThreadPoolAdapter(threadPoolAdapterExtra);
public HystrixThreadPoolAdapter hystrixThreadPoolAdapter(ThreadPoolAdapterScheduler threadPoolAdapterScheduler) {
return new HystrixThreadPoolAdapter(threadPoolAdapterScheduler);
}
}

@ -18,7 +18,7 @@
package cn.hippo4j.springboot.starter.config;
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.config.ApplicationContextHolder;
import cn.hippo4j.core.config.UtilAutoConfiguration;
@ -166,7 +166,7 @@ public class DynamicThreadPoolAutoConfiguration {
@Bean
@SuppressWarnings("all")
public ThreadPoolAdapterRegister threadPoolAdapterRegister(HttpAgent httpAgent, InetUtils hippo4JInetUtils, ThreadPoolAdapterExtra threadPoolAdapterExtra) {
return new ThreadPoolAdapterRegister(httpAgent, properties, environment, hippo4JInetUtils, threadPoolAdapterExtra);
public ThreadPoolAdapterRegister threadPoolAdapterRegister(HttpAgent httpAgent, InetUtils hippo4JInetUtils, ThreadPoolAdapterScheduler threadPoolAdapterScheduler) {
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.ThreadPoolAdapterCacheConfig;
import cn.hippo4j.adapter.base.ThreadPoolAdapterExtra;
import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.toolkit.CollectionUtil;
@ -62,7 +62,7 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
private final InetUtils hippo4JInetUtils;
private final ThreadPoolAdapterExtra threadPoolAdapterExtra;
private final ThreadPoolAdapterScheduler threadPoolAdapterScheduler;
private List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
@ -73,7 +73,7 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
ScheduledExecutorService scheduler = threadPoolAdapterExtra.getScheduler();
ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
ThreadPoolAdapterRegisterTask threadPoolAdapterRegisterTask = new ThreadPoolAdapterRegisterTask(scheduler);

Loading…
Cancel
Save