Optimize hippo4j core monitoring change logic (#221)

pull/233/head
chen.ma 2 years ago
parent fe0c5895f5
commit 6f4ce12302

@ -34,15 +34,21 @@ import cn.hippo4j.core.executor.state.ThreadPoolRunStateHandler;
import cn.hippo4j.core.springboot.starter.monitor.DynamicThreadPoolMonitorExecutor;
import cn.hippo4j.core.springboot.starter.monitor.LogMonitorHandler;
import cn.hippo4j.core.springboot.starter.monitor.MetricMonitorHandler;
import cn.hippo4j.core.springboot.starter.notify.CoreNotifyConfigBuilder;
import cn.hippo4j.core.springboot.starter.refresher.ApolloRefresherHandler;
import cn.hippo4j.core.springboot.starter.refresher.NacosCloudRefresherHandler;
import cn.hippo4j.core.springboot.starter.refresher.NacosRefresherHandler;
import cn.hippo4j.core.springboot.starter.support.DynamicThreadPoolPostProcessor;
import cn.hippo4j.core.springboot.starter.notify.CoreNotifyConfigBuilder;
import cn.hippo4j.core.springboot.starter.refresher.ZookeeperRefresherHandler;
import cn.hippo4j.core.springboot.starter.refresher.event.ExecutorsListener;
import cn.hippo4j.core.springboot.starter.refresher.event.PlatformsListener;
import cn.hippo4j.core.springboot.starter.refresher.event.WebExecutorListener;
import cn.hippo4j.core.springboot.starter.support.DynamicThreadPoolPostProcessor;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -124,29 +130,29 @@ public class DynamicThreadPoolCoreAutoConfiguration {
@ConditionalOnClass(name = NACOS_CONFIG_KEY)
@ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY)
@ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "nacos.data-id")
public NacosRefresherHandler nacosRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler) {
return new NacosRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public NacosRefresherHandler nacosRefresherHandler() {
return new NacosRefresherHandler(bootstrapCoreProperties);
}
@Bean
@ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY)
@ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "nacos.data-id")
public NacosCloudRefresherHandler nacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler) {
return new NacosCloudRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public NacosCloudRefresherHandler nacosCloudRefresherHandler() {
return new NacosCloudRefresherHandler(bootstrapCoreProperties);
}
@Bean
@ConditionalOnClass(name = APOLLO_CONFIG_KEY)
@ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "apollo.namespace")
public ApolloRefresherHandler apolloRefresher(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler) {
return new ApolloRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public ApolloRefresherHandler apolloRefresher() {
return new ApolloRefresherHandler(bootstrapCoreProperties);
}
@Bean
@ConditionalOnClass(name = ZK_CONFIG_KEY)
@ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "zookeeper.zk-connect-str")
public ZookeeperRefresherHandler zookeeperRefresher(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler) {
return new ZookeeperRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public ZookeeperRefresherHandler zookeeperRefresher() {
return new ZookeeperRefresherHandler(bootstrapCoreProperties);
}
@Bean
@ -163,4 +169,19 @@ public class DynamicThreadPoolCoreAutoConfiguration {
public MetricMonitorHandler hippo4jMetricMonitorHandler(ThreadPoolRunStateHandler threadPoolRunStateHandler) {
return new MetricMonitorHandler(threadPoolRunStateHandler);
}
@Bean
public ExecutorsListener hippo4jExecutorsListener(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler) {
return new ExecutorsListener(threadPoolNotifyAlarmHandler);
}
@Bean
public PlatformsListener hippo4jPlatformsListener() {
return new PlatformsListener();
}
@Bean
public WebExecutorListener hippo4jWebExecutorListener() {
return new WebExecutorListener();
}
}

@ -18,21 +18,17 @@
package cn.hippo4j.core.springboot.starter.refresher;
import cn.hippo4j.common.api.ThreadPoolDynamicRefresh;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.notify.ThreadPoolNotifyAlarm;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.executor.manage.GlobalNotifyAlarmManage;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.springboot.starter.event.ThreadPoolDynamicRefreshEvent;
import cn.hippo4j.core.springboot.starter.parser.ConfigParserHandler;
import cn.hippo4j.core.springboot.starter.refresher.event.Hippo4jCoreDynamicRefreshEvent;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ExecutorService;
@ -44,32 +40,23 @@ import java.util.concurrent.ExecutorService;
*/
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, InitializingBean, ApplicationContextAware {
private final ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler;
public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, InitializingBean {
protected final BootstrapCoreProperties bootstrapCoreProperties;
protected final ExecutorService dynamicRefreshExecutorService = ThreadPoolBuilder.builder().singlePool("client.dynamic.refresh").build();
private ApplicationContext applicationContext;
@Override
public void dynamicRefresh(String content) {
Map<Object, Object> configInfo;
try {
configInfo = ConfigParserHandler.getInstance().parseConfig(content, bootstrapCoreProperties.getConfigFileType());
} catch (IOException e) {
log.error("dynamic-thread-pool parse config file error, content: {}, fileType: {}",
content, bootstrapCoreProperties.getConfigFileType(), e);
} catch (Exception ex) {
log.error("dynamic-thread-pool parse config file error, content: {}, fileType: {}", content, bootstrapCoreProperties.getConfigFileType(), ex);
return;
}
BootstrapCoreProperties bindableCoreProperties = BootstrapCorePropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapCoreProperties);
// web pool
ThreadPoolDynamicRefreshEvent event = new ThreadPoolDynamicRefreshEvent(this);
event.setBootstrapCoreProperties(bindableCoreProperties);
applicationContext.publishEvent(event);
ApplicationContextHolder.getInstance().publishEvent(new Hippo4jCoreDynamicRefreshEvent(this, bindableCoreProperties));
}
/**
@ -86,11 +73,4 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool
GlobalNotifyAlarmManage.put(executorProperties.getThreadPoolId(), threadPoolNotifyAlarm);
});
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}

@ -17,7 +17,6 @@
package cn.hippo4j.core.springboot.starter.refresher;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
@ -40,8 +39,8 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
@Value(APOLLO_PROPERTY)
private String namespace;
public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public ApolloRefresherHandler(BootstrapCoreProperties bootstrapCoreProperties) {
super(bootstrapCoreProperties);
}
@Override
@ -49,16 +48,13 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
String[] apolloNamespaces = this.namespace.split(",");
this.namespace = apolloNamespaces[0];
Config config = ConfigService.getConfig(namespace);
ConfigChangeListener configChangeListener = configChangeEvent -> {
ConfigFile configFile = ConfigService.getConfigFile(
this.namespace.replaceAll("." + bootstrapCoreProperties.getConfigFileType().getValue(), ""),
ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue()));
String configInfo = configFile.getContent();
dynamicRefresh(configInfo);
};
config.addChangeListener(configChangeListener);
log.info("dynamic-thread-pool refresher, add apollo listener success, namespace: {}", namespace);
}

@ -18,7 +18,6 @@
package cn.hippo4j.core.springboot.starter.refresher;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
@ -38,16 +37,14 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef
private final NacosConfigManager nacosConfigManager;
public NacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public NacosCloudRefresherHandler(BootstrapCoreProperties bootstrapCoreProperties) {
super(bootstrapCoreProperties);
nacosConfigManager = ApplicationContextHolder.getBean(NacosConfigManager.class);
}
@Override
public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"),
nacosConfig.get("group"), new Listener() {

@ -18,7 +18,6 @@
package cn.hippo4j.core.springboot.starter.refresher;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
@ -38,9 +37,8 @@ public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
private final ConfigService configService;
public NacosRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public NacosRefresherHandler(BootstrapCoreProperties bootstrapCoreProperties) {
super(bootstrapCoreProperties);
configService = ApplicationContextHolder.getBean(ConfigService.class);
}

@ -17,7 +17,6 @@
package cn.hippo4j.core.springboot.starter.refresher;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import com.google.common.base.Charsets;
import lombok.extern.slf4j.Slf4j;
@ -44,8 +43,8 @@ public class ZookeeperRefresherHandler extends AbstractCoreThreadPoolDynamicRefr
private CuratorFramework curatorFramework;
public ZookeeperRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
public ZookeeperRefresherHandler(BootstrapCoreProperties bootstrapCoreProperties) {
super(bootstrapCoreProperties);
}
@Override
@ -62,7 +61,6 @@ public class ZookeeperRefresherHandler extends AbstractCoreThreadPoolDynamicRefr
loadNode(nodePath);
}
};
final CuratorListener curatorListener = (client, curatorEvent) -> {
final WatchedEvent watchedEvent = curatorEvent.getWatchedEvent();
if (null != watchedEvent) {
@ -76,7 +74,6 @@ public class ZookeeperRefresherHandler extends AbstractCoreThreadPoolDynamicRefr
}
}
};
curatorFramework.getConnectionStateListenable().addListener(connectionStateListener);
curatorFramework.getCuratorListenable().addListener(curatorListener);
curatorFramework.start();
@ -102,10 +99,8 @@ public class ZookeeperRefresherHandler extends AbstractCoreThreadPoolDynamicRefr
} catch (Exception e) {
e.printStackTrace();
}
content.append(nodeName).append("=").append(value).append("\n");
});
dynamicRefresh(content.toString());
registerNotifyAlarmManage();
} catch (Exception e) {

@ -15,7 +15,7 @@
* limitations under the License.
*/
package cn.hippo4j.core.springboot.starter.event;
package cn.hippo4j.core.springboot.starter.refresher.event;
import cn.hippo4j.common.notify.request.ChangeParameterNotifyRequest;
import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
@ -33,7 +33,6 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Objects;
@ -44,23 +43,20 @@ import java.util.concurrent.atomic.AtomicLong;
import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_DELIMITER;
import static cn.hippo4j.common.constant.ChangeThreadPoolConstants.CHANGE_THREAD_POOL_TEXT;
import static cn.hippo4j.core.springboot.starter.event.ThreadPoolDynamicRefreshEventOrder.EXECUTORS_LISTENER;
import static cn.hippo4j.core.springboot.starter.refresher.event.Hippo4jCoreDynamicRefreshEventOrder.EXECUTORS_LISTENER;
/**
* @author : wh
* @date : 2022/5/13 10:06
* @description:
* Executors listener.
*/
@Slf4j
@Component
@RequiredArgsConstructor
@Order(EXECUTORS_LISTENER)
public class ExecutorsListener implements ApplicationListener<ThreadPoolDynamicRefreshEvent> {
public class ExecutorsListener implements ApplicationListener<Hippo4jCoreDynamicRefreshEvent> {
private final ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler;
@Override
public void onApplicationEvent(ThreadPoolDynamicRefreshEvent threadPoolDynamicRefreshEvent) {
public void onApplicationEvent(Hippo4jCoreDynamicRefreshEvent threadPoolDynamicRefreshEvent) {
BootstrapCoreProperties bindableCoreProperties = threadPoolDynamicRefreshEvent.getBootstrapCoreProperties();
List<ExecutorProperties> executors = bindableCoreProperties.getExecutors();
for (ExecutorProperties properties : executors) {
@ -90,7 +86,6 @@ public class ExecutorsListener implements ApplicationListener<ThreadPoolDynamicR
log.error("Failed to send changSmartApplicationListenere notice. Message :: {}", ex.getMessage());
}
}
}
/**
@ -198,5 +193,4 @@ public class ExecutorsListener implements ApplicationListener<ThreadPoolDynamicR
}
}
}
}

@ -15,7 +15,7 @@
* limitations under the License.
*/
package cn.hippo4j.core.springboot.starter.event;
package cn.hippo4j.core.springboot.starter.refresher.event;
import cn.hippo4j.core.springboot.starter.config.BootstrapCoreProperties;
import lombok.Getter;
@ -23,17 +23,16 @@ import lombok.Setter;
import org.springframework.context.ApplicationEvent;
/**
* @author : wh
* @date : 2022/5/13 09:49
* @description:
* Hippo4j core dynamic refresh event.
*/
public class ThreadPoolDynamicRefreshEvent extends ApplicationEvent {
public class Hippo4jCoreDynamicRefreshEvent extends ApplicationEvent {
@Getter
@Setter
private BootstrapCoreProperties bootstrapCoreProperties;
public ThreadPoolDynamicRefreshEvent(Object source) {
public Hippo4jCoreDynamicRefreshEvent(Object source, BootstrapCoreProperties bootstrapCoreProperties) {
super(source);
this.bootstrapCoreProperties = bootstrapCoreProperties;
}
}

@ -15,19 +15,16 @@
* limitations under the License.
*/
package cn.hippo4j.core.springboot.starter.event;
package cn.hippo4j.core.springboot.starter.refresher.event;
/**
* @author : wh
* @date : 2022/5/13 10:25
* @description:
* Hippo4j core dynamic refresh event order.
*/
public interface ThreadPoolDynamicRefreshEventOrder {
public interface Hippo4jCoreDynamicRefreshEventOrder {
Integer WEB_EXECUTOR_LISTENER = 0;
Integer PLATFORMS_LISTENER = 1;
Integer EXECUTORS_LISTENER = 2;
int WEB_EXECUTOR_LISTENER = 0;
int PLATFORMS_LISTENER = 1;
int EXECUTORS_LISTENER = 2;
}

@ -15,7 +15,7 @@
* limitations under the License.
*/
package cn.hippo4j.core.springboot.starter.event;
package cn.hippo4j.core.springboot.starter.refresher.event;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.notify.HippoBaseSendMessageService;
@ -27,25 +27,20 @@ import cn.hippo4j.core.springboot.starter.config.ExecutorProperties;
import cn.hippo4j.core.springboot.starter.notify.CoreNotifyConfigBuilder;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import static cn.hippo4j.core.springboot.starter.event.ThreadPoolDynamicRefreshEventOrder.PLATFORMS_LISTENER;
import static cn.hippo4j.core.springboot.starter.refresher.event.Hippo4jCoreDynamicRefreshEventOrder.PLATFORMS_LISTENER;
/**
* @author : wh
* @date : 2022/5/13 10:03
* @description:
* Platforms listener.
*/
@Component
@Order(PLATFORMS_LISTENER)
public class PlatformsListener implements ApplicationListener<ThreadPoolDynamicRefreshEvent> {
public class PlatformsListener implements ApplicationListener<Hippo4jCoreDynamicRefreshEvent> {
@Override
public void onApplicationEvent(ThreadPoolDynamicRefreshEvent threadPoolDynamicRefreshEvent) {
public void onApplicationEvent(Hippo4jCoreDynamicRefreshEvent threadPoolDynamicRefreshEvent) {
BootstrapCoreProperties bindableCoreProperties = threadPoolDynamicRefreshEvent.getBootstrapCoreProperties();
List<ExecutorProperties> executors = bindableCoreProperties.getExecutors();
for (ExecutorProperties executor : executors) {
@ -59,6 +54,5 @@ public class PlatformsListener implements ApplicationListener<ThreadPoolDynamicR
wrapper.setInitFlag(Boolean.TRUE);
}
}
}
}

@ -15,7 +15,7 @@
* limitations under the License.
*/
package cn.hippo4j.core.springboot.starter.event;
package cn.hippo4j.core.springboot.starter.refresher.event;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.model.ThreadPoolParameter;
@ -27,29 +27,24 @@ import cn.hippo4j.core.springboot.starter.config.WebThreadPoolProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Objects;
import static cn.hippo4j.core.springboot.starter.event.ThreadPoolDynamicRefreshEventOrder.WEB_EXECUTOR_LISTENER;
import static cn.hippo4j.core.springboot.starter.refresher.event.Hippo4jCoreDynamicRefreshEventOrder.WEB_EXECUTOR_LISTENER;
/**
* @author : wh
* @date : 2022/5/13 09:53
* @description:
* Web executor listener.
*/
@Slf4j
@Component
@Order(WEB_EXECUTOR_LISTENER)
public class WebExecutorListener implements ApplicationListener<ThreadPoolDynamicRefreshEvent> {
public class WebExecutorListener implements ApplicationListener<Hippo4jCoreDynamicRefreshEvent> {
@Override
public void onApplicationEvent(ThreadPoolDynamicRefreshEvent threadPoolDynamicRefreshEvent) {
public void onApplicationEvent(Hippo4jCoreDynamicRefreshEvent threadPoolDynamicRefreshEvent) {
BootstrapCoreProperties bindableCoreProperties = threadPoolDynamicRefreshEvent.getBootstrapCoreProperties();
boolean isNullFlag = bindableCoreProperties.getJetty() == null
|| bindableCoreProperties.getUndertow() == null
|| bindableCoreProperties.getTomcat() == null;
&& bindableCoreProperties.getUndertow() == null
&& bindableCoreProperties.getTomcat() == null;
if (isNullFlag) {
return;
}
@ -68,7 +63,7 @@ public class WebExecutorListener implements ApplicationListener<ThreadPoolDynami
} catch (Exception ex) {
log.error("Failed to modify web thread pool.", ex);
}
}
private ThreadPoolParameterInfo buildWebPoolParameter(BootstrapCoreProperties bindableCoreProperties) {
Loading…
Cancel
Save