重构部分 API 以及格式调整.

pull/131/head
chen.ma 2 years ago
parent a0d95cbb14
commit 3080a97dbe

@ -13,41 +13,62 @@ public interface BootstrapPropertiesInterface {
* *
* @return * @return
*/ */
Boolean getEnable(); default Boolean getEnable() {
return null;
}
/** /**
* Get username. * Get username.
* *
* @return * @return
*/ */
String getUsername(); default String getUsername() {
return null;
}
/** /**
* Get password. * Get password.
* *
* @return * @return
*/ */
String getPassword(); default String getPassword() {
return null;
}
/** /**
* Get namespace. * Get namespace.
* *
* @return * @return
*/ */
String getNamespace(); default String getNamespace() {
return null;
}
/** /**
* Get item id. * Get item id.
* *
* @return * @return
*/ */
String getItemId(); default String getItemId() {
return null;
}
/** /**
* Get server addr. * Get server addr.
* *
* @return * @return
*/ */
String getServerAddr(); default String getServerAddr() {
return null;
}
/**
* Get banner.
*
* @return
*/
default Boolean getBanner() {
return null;
}
} }

@ -9,6 +9,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import java.util.Objects;
/** /**
* Before check configuration. * Before check configuration.
* *
@ -19,10 +21,13 @@ import org.springframework.core.env.ConfigurableEnvironment;
@AllArgsConstructor @AllArgsConstructor
public class BeforeCheckConfiguration { public class BeforeCheckConfiguration {
private final String bootstrapPropertiesClassName = "cn.hippo4j.starter.config.BootstrapProperties";
@Bean @Bean
public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapPropertiesInterface properties, public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapPropertiesInterface properties,
ConfigurableEnvironment environment) { ConfigurableEnvironment environment) {
if (properties != null && properties.getEnable()) { boolean checkFlag = properties != null && Objects.equals(bootstrapPropertiesClassName, properties.getClass().getName()) && properties.getEnable();
if (checkFlag) {
String username = properties.getUsername(); String username = properties.getUsername();
if (StringUtil.isBlank(username)) { if (StringUtil.isBlank(username)) {
throw new ConfigEmptyException( throw new ConfigEmptyException(

@ -1,6 +1,6 @@
package cn.hippo4j.starter.handler; package cn.hippo4j.core.handler;
import cn.hippo4j.starter.config.BootstrapProperties; import cn.hippo4j.core.config.BootstrapPropertiesInterface;
import lombok.NonNull; import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -20,7 +20,7 @@ import org.springframework.boot.ansi.AnsiStyle;
public class DynamicThreadPoolBannerHandler implements InitializingBean { public class DynamicThreadPoolBannerHandler implements InitializingBean {
@NonNull @NonNull
private final BootstrapProperties properties; private final BootstrapPropertiesInterface properties;
private final String DYNAMIC_THREAD_POOL = " :: Dynamic ThreadPool :: "; private final String DYNAMIC_THREAD_POOL = " :: Dynamic ThreadPool :: ";
@ -42,7 +42,7 @@ public class DynamicThreadPoolBannerHandler implements InitializingBean {
"|___/ \\_, |_||_\\__,_|_|_|_|_\\__| |_| |_| \n" + "|___/ \\_, |_||_\\__,_|_|_|_|_\\__| |_| |_| \n" +
" |__/ \n"; " |__/ \n";
if (properties.getEnableBanner()) { if (properties.getBanner()) {
String version = getVersion(); String version = getVersion();
version = (version != null) ? " (v" + version + ")" : "no version."; version = (version != null) ? " (v" + version + ")" : "no version.";

@ -63,6 +63,11 @@
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -1,5 +1,6 @@
package cn.hippo4j.core.starter.config; package cn.hippo4j.core.starter.config;
import cn.hippo4j.core.config.BootstrapPropertiesInterface;
import cn.hippo4j.core.starter.parser.ConfigFileTypeEnum; import cn.hippo4j.core.starter.parser.ConfigFileTypeEnum;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -17,19 +18,19 @@ import java.util.Map;
@Getter @Getter
@Setter @Setter
@ConfigurationProperties(prefix = BootstrapCoreProperties.PREFIX) @ConfigurationProperties(prefix = BootstrapCoreProperties.PREFIX)
public class BootstrapCoreProperties { public class BootstrapCoreProperties implements BootstrapPropertiesInterface {
public static final String PREFIX = "spring.dynamic.thread-pool"; public static final String PREFIX = "spring.dynamic.thread-pool";
/** /**
* Enabled banner. * Enabled banner.
*/ */
private Boolean enableBanner; private Boolean banner;
/*** /***
* Enabled collect. * Enabled collect.
*/ */
private Boolean enabledCollect; private Boolean collect;
/** /**
* Check state interval. * Check state interval.
@ -46,6 +47,11 @@ public class BootstrapCoreProperties {
*/ */
private Map<String, String> nacos; private Map<String, String> nacos;
/**
* Apollo config.
*/
private Map<String, String> apollo;
/** /**
* Notify platforms. * Notify platforms.
*/ */

@ -15,6 +15,6 @@ public interface ConfigParser {
Map<Object, Object> doParse(String content) throws IOException; Map<Object, Object> doParse(String content) throws IOException;
List<ConfigFileTypeEnum> getConfigFileTypes(); List<ConfigFileTypeEnum> getConfigFileTypes();
} }

@ -14,7 +14,7 @@ import java.util.Map;
* @date : 2022/3/1 07:57 * @date : 2022/3/1 07:57
* @description: * @description:
*/ */
public class YamlConfigParser extends AbstractConfigParser{ public class YamlConfigParser extends AbstractConfigParser {
@Override @Override
public Map<Object, Object> doParse(String content) { public Map<Object, Object> doParse(String content) {

@ -50,8 +50,8 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool
.build(); .build();
@Override @Override
public void dynamicRefresh(String content){ public void dynamicRefresh(String content) {
Map<Object, Object> configInfo = null; Map<Object, Object> configInfo;
try { try {
configInfo = ConfigParserHandler.getInstance().parseConfig(content, bootstrapCoreProperties.getConfigFileType()); configInfo = ConfigParserHandler.getInstance().parseConfig(content, bootstrapCoreProperties.getConfigFileType());
} catch (IOException e) { } catch (IOException e) {

@ -24,8 +24,7 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
@Value(APOLLO_PROPERTY) @Value(APOLLO_PROPERTY)
private String namespace; private String namespace;
public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, BootstrapCoreProperties bootstrapCoreProperties) {
public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler , BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
} }
@ -36,8 +35,11 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
Config config = ConfigService.getConfig(namespace); Config config = ConfigService.getConfig(namespace);
ConfigChangeListener configChangeListener = configChangeEvent -> { ConfigChangeListener configChangeListener = configChangeEvent -> {
ConfigFile configFile = ConfigService.getConfigFile(namespace, ConfigFile configFile = ConfigService.getConfigFile(
ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue())); namespace,
ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue())
);
String configInfo = configFile.getContent(); String configInfo = configFile.getContent();
dynamicRefresh(configInfo); dynamicRefresh(configInfo);
}; };

@ -25,8 +25,7 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef
public NacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, public NacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) { BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
nacosConfigManager = ApplicationContextHolder.getBean( nacosConfigManager = ApplicationContextHolder.getBean(NacosConfigManager.class);
NacosConfigManager.class);
} }
@Override @Override
@ -34,16 +33,17 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos(); Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"), nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"),
nacosConfig.get("group"), new Listener() { nacosConfig.get("group"), new Listener() {
@Override @Override
public Executor getExecutor() { public Executor getExecutor() {
return dynamicRefreshExecutorService; return dynamicRefreshExecutorService;
} }
@Override @Override
public void receiveConfigInfo(String configInfo) { public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo); dynamicRefresh(configInfo);
} }
}); });
} }
} }

@ -33,17 +33,18 @@ public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos(); Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"),
new Listener() { new Listener() {
@Override @Override
public Executor getExecutor() { public Executor getExecutor() {
return dynamicRefreshExecutorService; return dynamicRefreshExecutorService;
}
@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
} }
);
@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
} }
} }

@ -1,18 +0,0 @@
package cn.hippo4j.core.starter.refresher.config;
import java.util.Map;
/**
*
*
* @author serenity SerenitySir@outlook.com
* @since 2022/2/28
*/
public interface ConfigParser {
/**
*
* @param content
* @return k,v
*/
Map<Object, Object> parseConfig(String content);
}

@ -1,37 +0,0 @@
package cn.hippo4j.core.starter.refresher.config.impl;
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
import com.google.common.collect.Maps;
import lombok.SneakyThrows;
import org.springframework.util.StringUtils;
import java.io.StringReader;
import java.util.Map;
import java.util.Properties;
/**
* Properties
*
* @author serenity SerenitySir@outlook.com
* @since 2022/2/28
*/
public class PropConfigParser implements ConfigParser {
@SneakyThrows
@Override
public Map<Object, Object> parseConfig(String content) {
if (!StringUtils.hasText(content)){
return Maps.newHashMap();
}
Properties properties = new Properties();
properties.load(new StringReader(content));
return properties;
}
public static void main(String[] args) {
ConfigParser propConfigParser = new PropConfigParser();
Map<Object, Object> map = propConfigParser.parseConfig("db.aa=11\ndb.bb=22");
System.out.println(map.toString());
}
}

@ -1,32 +0,0 @@
package cn.hippo4j.core.starter.refresher.config.impl;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ByteArrayResource;
import java.util.Map;
/**
* Yml
*
* @author serenity SerenitySir@outlook.com
* @since 2022/2/28
*/
public class YmlConfigParser implements ConfigParser {
@Override
public Map<Object, Object> parseConfig(String content) {
Map<Object, Object> resultMap = Maps.newHashMap();
if (StringUtil.isBlank(content)) {
return resultMap;
}
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes()));
resultMap = yamlPropertiesFactoryBean.getObject();
return resultMap;
}
}

@ -53,12 +53,12 @@ public class BootstrapProperties implements BootstrapPropertiesInterface {
/** /**
* Print dynamic thread pool banner * Print dynamic thread pool banner
*/ */
private Boolean enableBanner = true; private Boolean banner = true;
/** /**
* Enable client data collect * Enable client data collect
*/ */
private Boolean enableCollect = true; private Boolean collect = true;
/** /**
* Task buffer container capacity * Task buffer container capacity

@ -15,7 +15,7 @@ import cn.hippo4j.starter.core.ThreadPoolOperation;
import cn.hippo4j.core.enable.MarkerConfiguration; import cn.hippo4j.core.enable.MarkerConfiguration;
import cn.hippo4j.starter.event.ApplicationContentPostProcessor; import cn.hippo4j.starter.event.ApplicationContentPostProcessor;
import cn.hippo4j.starter.handler.BaseThreadDetailStateHandler; import cn.hippo4j.starter.handler.BaseThreadDetailStateHandler;
import cn.hippo4j.starter.handler.DynamicThreadPoolBannerHandler; import cn.hippo4j.core.handler.DynamicThreadPoolBannerHandler;
import cn.hippo4j.starter.handler.ThreadPoolRunStateHandler; import cn.hippo4j.starter.handler.ThreadPoolRunStateHandler;
import cn.hippo4j.starter.handler.web.*; import cn.hippo4j.starter.handler.web.*;
import cn.hippo4j.starter.monitor.ReportingEventExecutor; import cn.hippo4j.starter.monitor.ReportingEventExecutor;

@ -76,7 +76,7 @@ public class ReportingEventExecutor implements Runnable, CommandLineRunner, Disp
@Override @Override
public void run(String... args) { public void run(String... args) {
if (properties.getEnableCollect()) { if (properties.getCollect()) {
Integer bufferSize = properties.getTaskBufferSize(); Integer bufferSize = properties.getTaskBufferSize();
messageCollectVessel = new ArrayBlockingQueue(bufferSize); messageCollectVessel = new ArrayBlockingQueue(bufferSize);

Loading…
Cancel
Save