mirror of https://github.com/longtai-cn/hippo4j
commit
018554ee07
@ -0,0 +1,15 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:50
|
||||
* @description:
|
||||
*/
|
||||
public abstract class AbstractConfigParser implements ConfigParser{
|
||||
|
||||
@Override
|
||||
public boolean supports(ConfigFileTypeEnum type) {
|
||||
return getConfigFileTypes().contains(type);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:47
|
||||
* @description:
|
||||
*/
|
||||
@Getter
|
||||
public enum ConfigFileTypeEnum {
|
||||
|
||||
PROPERTIES("properties"),
|
||||
XML("xml"),
|
||||
JSON("json"),
|
||||
YML("yml"),
|
||||
YAML("yaml"),
|
||||
TXT("txt");
|
||||
|
||||
private final String value;
|
||||
|
||||
ConfigFileTypeEnum(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ConfigFileTypeEnum of(String value) {
|
||||
for (ConfigFileTypeEnum typeEnum : ConfigFileTypeEnum.values()) {
|
||||
if (typeEnum.value.equals(value)) {
|
||||
return typeEnum;
|
||||
}
|
||||
}
|
||||
return PROPERTIES;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:47
|
||||
* @description:
|
||||
*/
|
||||
public interface ConfigParser {
|
||||
|
||||
boolean supports(ConfigFileTypeEnum type);
|
||||
|
||||
Map<Object, Object> doParse(String content) throws IOException;
|
||||
|
||||
List<ConfigFileTypeEnum> getConfigFileTypes();
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 08:02
|
||||
* @description:
|
||||
*/
|
||||
public class ConfigParserHandler {
|
||||
|
||||
private static final List<ConfigParser> PARSERS = Lists.newArrayList();
|
||||
|
||||
private ConfigParserHandler() {
|
||||
ServiceLoader<ConfigParser> loader = ServiceLoader.load(ConfigParser.class);
|
||||
for (ConfigParser configParser : loader) {
|
||||
PARSERS.add(configParser);
|
||||
}
|
||||
|
||||
PARSERS.add(new PropertiesConfigParser());
|
||||
PARSERS.add(new YamlConfigParser());
|
||||
}
|
||||
|
||||
public Map<Object, Object> parseConfig(String content, ConfigFileTypeEnum type) throws IOException {
|
||||
for (ConfigParser parser : PARSERS) {
|
||||
if (parser.supports(type)) {
|
||||
return parser.doParse(content);
|
||||
}
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
public static ConfigParserHandler getInstance() {
|
||||
return ConfigParserHandlerHolder.INSTANCE;
|
||||
}
|
||||
|
||||
private static class ConfigParserHandlerHolder {
|
||||
private static final ConfigParserHandler INSTANCE = new ConfigParserHandler();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:49
|
||||
* @description:
|
||||
*/
|
||||
public class PropertiesConfigParser extends AbstractConfigParser{
|
||||
|
||||
|
||||
@Override
|
||||
public Map<Object, Object> doParse(String content) throws IOException {
|
||||
Properties properties = new Properties();
|
||||
properties.load(new StringReader(content));
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConfigFileTypeEnum> getConfigFileTypes() {
|
||||
return Lists.newArrayList(ConfigFileTypeEnum.PROPERTIES);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package cn.hippo4j.core.starter.parser;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/3/1 07:57
|
||||
* @description:
|
||||
*/
|
||||
public class YamlConfigParser extends AbstractConfigParser{
|
||||
|
||||
@Override
|
||||
public Map<Object, Object> doParse(String content) {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return Maps.newHashMapWithExpectedSize(0);
|
||||
}
|
||||
YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
|
||||
bean.setResources(new ByteArrayResource(content.getBytes()));
|
||||
return bean.getObject();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConfigFileTypeEnum> getConfigFileTypes() {
|
||||
return Lists.newArrayList(ConfigFileTypeEnum.YML, ConfigFileTypeEnum.YAML);
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package cn.hippo4j.core.starter.refresher;
|
||||
|
||||
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
|
||||
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
|
||||
import com.ctrip.framework.apollo.Config;
|
||||
import com.ctrip.framework.apollo.ConfigChangeListener;
|
||||
import com.ctrip.framework.apollo.ConfigFile;
|
||||
import com.ctrip.framework.apollo.ConfigService;
|
||||
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
|
||||
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
/**
|
||||
* @author : wh
|
||||
* @date : 2022/2/28 21:32
|
||||
* @description:
|
||||
*/
|
||||
@Slf4j
|
||||
public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements ConfigChangeListener, InitializingBean {
|
||||
|
||||
private static final String APOLLO_PROPERTY = "${apollo.bootstrap.namespaces:application}";
|
||||
|
||||
@Value(APOLLO_PROPERTY)
|
||||
private String namespace;
|
||||
|
||||
|
||||
public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler , BootstrapCoreProperties bootstrapCoreProperties) {
|
||||
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(ConfigChangeEvent configChangeEvent) {
|
||||
ConfigFile configFile = ConfigService.getConfigFile(namespace,
|
||||
ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue()));
|
||||
String configInfo = configFile.getContent();
|
||||
dynamicRefresh(configInfo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
String[] apolloNamespaces = this.namespace.split(",");
|
||||
this.namespace = apolloNamespaces[0];
|
||||
Config config = ConfigService.getConfig(namespace);
|
||||
config.addChangeListener(this);
|
||||
log.info("dynamic-thread-pool refresher, add apollo listener success, namespace: {}", namespace);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue