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

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

@ -13,41 +13,62 @@ public interface BootstrapPropertiesInterface {
*
* @return
*/
Boolean getEnable();
default Boolean getEnable() {
return null;
}
/**
* Get username.
*
* @return
*/
String getUsername();
default String getUsername() {
return null;
}
/**
* Get password.
*
* @return
*/
String getPassword();
default String getPassword() {
return null;
}
/**
* Get namespace.
*
* @return
*/
String getNamespace();
default String getNamespace() {
return null;
}
/**
* Get item id.
*
* @return
*/
String getItemId();
default String getItemId() {
return null;
}
/**
* Get server addr.
*
* @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.core.env.ConfigurableEnvironment;
import java.util.Objects;
/**
* Before check configuration.
*
@ -19,10 +21,13 @@ import org.springframework.core.env.ConfigurableEnvironment;
@AllArgsConstructor
public class BeforeCheckConfiguration {
private final String bootstrapPropertiesClassName = "cn.hippo4j.starter.config.BootstrapProperties";
@Bean
public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapPropertiesInterface properties,
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();
if (StringUtil.isBlank(username)) {
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.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -20,7 +20,7 @@ import org.springframework.boot.ansi.AnsiStyle;
public class DynamicThreadPoolBannerHandler implements InitializingBean {
@NonNull
private final BootstrapProperties properties;
private final BootstrapPropertiesInterface properties;
private final String DYNAMIC_THREAD_POOL = " :: Dynamic ThreadPool :: ";
@ -42,7 +42,7 @@ public class DynamicThreadPoolBannerHandler implements InitializingBean {
"|___/ \\_, |_||_\\__,_|_|_|_|_\\__| |_| |_| \n" +
" |__/ \n";
if (properties.getEnableBanner()) {
if (properties.getBanner()) {
String version = getVersion();
version = (version != null) ? " (v" + version + ")" : "no version.";

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

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

@ -15,6 +15,6 @@ public interface ConfigParser {
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
* @description:
*/
public class YamlConfigParser extends AbstractConfigParser{
public class YamlConfigParser extends AbstractConfigParser {
@Override
public Map<Object, Object> doParse(String content) {

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

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

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

@ -33,17 +33,18 @@ public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"),
new Listener() {
@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
new Listener() {
@Override
public Executor getExecutor() {
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
*/
private Boolean enableBanner = true;
private Boolean banner = true;
/**
* Enable client data collect
*/
private Boolean enableCollect = true;
private Boolean collect = true;
/**
* Task buffer container capacity

@ -15,7 +15,7 @@ import cn.hippo4j.starter.core.ThreadPoolOperation;
import cn.hippo4j.core.enable.MarkerConfiguration;
import cn.hippo4j.starter.event.ApplicationContentPostProcessor;
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.web.*;
import cn.hippo4j.starter.monitor.ReportingEventExecutor;

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

Loading…
Cancel
Save