Merge remote-tracking branch 'origin/develop' into develop

1.3.1
chen.ma 3 years ago
commit c0217be55b

@ -0,0 +1,33 @@
/*
* 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;
/**
* Provide registration for each adaptation
*/
public interface ThreadPoolAdapterRegisterAction {
/**
* adapterRegister
* @param threadPoolAdapterMap
* @return
*/
void adapterRegister(Map<String, ThreadPoolAdapter> threadPoolAdapterMap);
}

@ -29,7 +29,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;
@Slf4j @Slf4j
public class ThreadPoolAdapterScheduler { public class ThreadPoolAdapterScheduler {
private static final int TASK_INTERVAL_SECONDS = 2; private static final int TASK_INTERVAL_SECONDS = 10;
private final ScheduledExecutorService scheduler; private final ScheduledExecutorService scheduler;

@ -17,10 +17,8 @@
package cn.hippo4j.adapter.hystrix; package cn.hippo4j.adapter.hystrix;
import cn.hippo4j.adapter.base.ThreadPoolAdapter; import cn.hippo4j.adapter.base.*;
import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler; import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.adapter.base.ThreadPoolAdapterParameter;
import cn.hippo4j.adapter.base.ThreadPoolAdapterState;
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.netflix.hystrix.HystrixThreadPool; import com.netflix.hystrix.HystrixThreadPool;
@ -79,7 +77,7 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
@Override @Override
public List<ThreadPoolAdapterState> getThreadPoolStates() { public List<ThreadPoolAdapterState> getThreadPoolStates() {
List<ThreadPoolAdapterState> threadPoolAdapterStates = new ArrayList<>(); List<ThreadPoolAdapterState> threadPoolAdapterStates = new ArrayList<>();
HYSTRIX_CONSUME_EXECUTOR.forEach((kel, val) -> threadPoolAdapterStates.add(getThreadPoolState(String.valueOf(val)))); HYSTRIX_CONSUME_EXECUTOR.forEach((kel, val) -> threadPoolAdapterStates.add(getThreadPoolState(kel)));
return threadPoolAdapterStates; return threadPoolAdapterStates;
} }
@ -104,6 +102,16 @@ public class HystrixThreadPoolAdapter implements ThreadPoolAdapter, ApplicationL
@Override @Override
public void onApplicationEvent(ApplicationStartedEvent event) { public void onApplicationEvent(ApplicationStartedEvent event) {
// Periodically update the Hystrix thread pool
HystrixThreadPoolRefresh();
// Periodically refresh registration
ThreadPoolAdapterRegisterAction threadPoolAdapterRegisterAction = ApplicationContextHolder.getBean(ThreadPoolAdapterRegisterAction.class);
Map<String, ? extends HystrixThreadPoolAdapter> beansOfType = ApplicationContextHolder.getBeansOfType(this.getClass());
Map<String, ThreadPoolAdapter> map = Maps.newHashMap(beansOfType);
threadPoolAdapterRegisterAction.adapterRegister(map);
}
public void HystrixThreadPoolRefresh() {
ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler(); ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds(); int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds();
HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler, taskIntervalSeconds); HystrixThreadPoolRefreshTask hystrixThreadPoolRefreshTask = new HystrixThreadPoolRefreshTask(scheduler, taskIntervalSeconds);

@ -17,10 +17,7 @@
package cn.hippo4j.springboot.starter.core; package cn.hippo4j.springboot.starter.core;
import cn.hippo4j.adapter.base.ThreadPoolAdapter; import cn.hippo4j.adapter.base.*;
import cn.hippo4j.adapter.base.ThreadPoolAdapterCacheConfig;
import cn.hippo4j.adapter.base.ThreadPoolAdapterScheduler;
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;
import cn.hippo4j.common.web.base.Result; import cn.hippo4j.common.web.base.Result;
@ -51,8 +48,7 @@ import static cn.hippo4j.common.constant.Constants.REGISTER_ADAPTER_PATH;
*/ */
@Slf4j @Slf4j
@AllArgsConstructor @AllArgsConstructor
@RequiredArgsConstructor public class ThreadPoolAdapterRegister implements ApplicationRunner, ThreadPoolAdapterRegisterAction {
public class ThreadPoolAdapterRegister implements ApplicationRunner {
private final HttpAgent httpAgent; private final HttpAgent httpAgent;
@ -64,23 +60,18 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
private final ThreadPoolAdapterScheduler threadPoolAdapterScheduler; private final ThreadPoolAdapterScheduler threadPoolAdapterScheduler;
private List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler(); register();
int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds();
ThreadPoolAdapterRegisterTask threadPoolAdapterRegisterTask = new ThreadPoolAdapterRegisterTask(scheduler, taskIntervalSeconds);
scheduler.schedule(threadPoolAdapterRegisterTask, threadPoolAdapterScheduler.getTaskIntervalSeconds(), TimeUnit.SECONDS);
} }
public List<ThreadPoolAdapterCacheConfig> getThreadPoolAdapterCacheConfigs() { public List<ThreadPoolAdapterCacheConfig> getThreadPoolAdapterCacheConfigs(Map<String, ThreadPoolAdapter> threadPoolAdapterMap) {
Map<String, ThreadPoolAdapter> threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class);
List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList(); List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
threadPoolAdapterMap.forEach((key, val) -> { for (Map.Entry<String, ThreadPoolAdapter> threadPoolAdapterEntry : threadPoolAdapterMap.entrySet()) {
ThreadPoolAdapter val = threadPoolAdapterEntry.getValue();
List<ThreadPoolAdapterState> threadPoolStates = val.getThreadPoolStates(); List<ThreadPoolAdapterState> threadPoolStates = val.getThreadPoolStates();
if (CollectionUtil.isEmpty(threadPoolStates)) { if (CollectionUtil.isEmpty(threadPoolStates) || threadPoolStates.size() == 0) {
return; continue;
} }
ThreadPoolAdapterCacheConfig cacheConfig = new ThreadPoolAdapterCacheConfig(); ThreadPoolAdapterCacheConfig cacheConfig = new ThreadPoolAdapterCacheConfig();
cacheConfig.setMark(val.mark()); cacheConfig.setMark(val.mark());
@ -91,12 +82,12 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
cacheConfig.setClientAddress(clientAddress); cacheConfig.setClientAddress(clientAddress);
cacheConfig.setThreadPoolAdapterStates(threadPoolStates); cacheConfig.setThreadPoolAdapterStates(threadPoolStates);
cacheConfigList.add(cacheConfig); cacheConfigList.add(cacheConfig);
}); }
return cacheConfigList; return cacheConfigList;
} }
public void doRegister(List<ThreadPoolAdapterCacheConfig> cacheConfigList) { public void doRegister(List<ThreadPoolAdapterCacheConfig> cacheConfigList) {
if (CollectionUtil.isNotEmpty(cacheConfigList)) { if (CollectionUtil.isNotEmpty(cacheConfigList) && cacheConfigList.size() > 0) {
try { try {
Result result = httpAgent.httpPost(REGISTER_ADAPTER_PATH, cacheConfigList); Result result = httpAgent.httpPost(REGISTER_ADAPTER_PATH, cacheConfigList);
if (!result.isSuccess()) { if (!result.isSuccess()) {
@ -109,25 +100,40 @@ public class ThreadPoolAdapterRegister implements ApplicationRunner {
} }
public void register() { public void register() {
List<ThreadPoolAdapterCacheConfig> threadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(); Map<String, ThreadPoolAdapter> threadPoolAdapterMap = ApplicationContextHolder.getBeansOfType(ThreadPoolAdapter.class);
List<ThreadPoolAdapterCacheConfig> threadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(threadPoolAdapterMap);
doRegister(threadPoolAdapterCacheConfigs); doRegister(threadPoolAdapterCacheConfigs);
} }
@Override
public void adapterRegister(Map<String, ThreadPoolAdapter> threadPoolAdapterMap) {
ScheduledExecutorService scheduler = threadPoolAdapterScheduler.getScheduler();
int taskIntervalSeconds = threadPoolAdapterScheduler.getTaskIntervalSeconds();
ThreadPoolAdapterRegisterTask threadPoolAdapterRegisterTask = new ThreadPoolAdapterRegisterTask(scheduler, taskIntervalSeconds, threadPoolAdapterMap);
scheduler.schedule(threadPoolAdapterRegisterTask, threadPoolAdapterScheduler.getTaskIntervalSeconds(), TimeUnit.SECONDS);
}
class ThreadPoolAdapterRegisterTask implements Runnable { class ThreadPoolAdapterRegisterTask implements Runnable {
private ScheduledExecutorService scheduler; private ScheduledExecutorService scheduler;
private int taskIntervalSeconds; private int taskIntervalSeconds;
public ThreadPoolAdapterRegisterTask(ScheduledExecutorService scheduler, int taskIntervalSeconds) { Map<String, ThreadPoolAdapter> threadPoolAdapterMap;
private List<ThreadPoolAdapterCacheConfig> cacheConfigList = Lists.newArrayList();
public ThreadPoolAdapterRegisterTask(ScheduledExecutorService scheduler, int taskIntervalSeconds,
Map<String, ThreadPoolAdapter> threadPoolAdapterMap) {
this.scheduler = scheduler; this.scheduler = scheduler;
this.taskIntervalSeconds = taskIntervalSeconds; this.taskIntervalSeconds = taskIntervalSeconds;
this.threadPoolAdapterMap = threadPoolAdapterMap;
} }
@Override @Override
public void run() { public void run() {
try { try {
List<ThreadPoolAdapterCacheConfig> newThreadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(); List<ThreadPoolAdapterCacheConfig> newThreadPoolAdapterCacheConfigs = getThreadPoolAdapterCacheConfigs(threadPoolAdapterMap);
boolean registerFlag = compareThreadPoolAdapterCacheConfigs(newThreadPoolAdapterCacheConfigs, cacheConfigList); boolean registerFlag = compareThreadPoolAdapterCacheConfigs(newThreadPoolAdapterCacheConfigs, cacheConfigList);
cacheConfigList = newThreadPoolAdapterCacheConfigs; cacheConfigList = newThreadPoolAdapterCacheConfigs;
if (registerFlag) { if (registerFlag) {

Loading…
Cancel
Save