Merge pull request #121 from HKMV/develop

[code]完成nacos适配解析properties配置文件.
pull/123/head
龙台 Long Tai 2 years ago committed by GitHub
commit a4265b5ef9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -12,12 +12,15 @@ import cn.hippo4j.common.notify.platform.WeChatSendMessageHandler;
import cn.hippo4j.core.config.UtilAutoConfiguration;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.notify.CoreNotifyConfigBuilder;
import cn.hippo4j.core.starter.refresher.ConfigParserHandler;
import cn.hippo4j.core.starter.refresher.NacosCloudRefresherHandler;
import cn.hippo4j.core.starter.refresher.NacosRefresherHandler;
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
import cn.hippo4j.core.starter.refresher.config.impl.PropConfigParser;
import cn.hippo4j.core.starter.refresher.config.impl.YmlConfigParser;
import cn.hippo4j.core.starter.support.DynamicThreadPoolPostProcessor;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -28,6 +31,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import java.util.List;
/**
* Dynamic thread pool auto configuration.
*
@ -46,6 +51,9 @@ public class DynamicThreadPoolCoreAutoConfiguration {
private static final String NACOS_CONFIG_KEY = "com.alibaba.nacos.api.config";
private final List<String> yamlList = Lists.newArrayList("yaml", "yml");
private final List<String> propList = Lists.newArrayList("properties");
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ApplicationContextHolder hippo4JApplicationContextHolder() {
@ -98,23 +106,32 @@ public class DynamicThreadPoolCoreAutoConfiguration {
@ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY)
public NacosRefresherHandler nacosRefresherHandler(ConfigService configService,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ConfigParserHandler configParserHandler,
ConfigParser configParser,
BootstrapCoreProperties bootstrapCoreProperties) {
return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties);
return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, configParser, bootstrapCoreProperties);
}
@Bean
@ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY)
public NacosCloudRefresherHandler nacosCloudRefresherHandler(NacosConfigManager nacosConfigManager,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ConfigParserHandler configParserHandler,
ConfigParser configParser,
BootstrapCoreProperties bootstrapCoreProperties) {
return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties);
return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, configParser, bootstrapCoreProperties);
}
@Bean
public ConfigParserHandler configParserHandler() {
return new ConfigParserHandler();
public ConfigParser configParserHandler() {
// return new ConfigParserHandler();
String configFileType = bootstrapCoreProperties.getConfigFileType();
if (yamlList.contains(configFileType)) {
return new YmlConfigParser();
}
if (propList.contains(configFileType)) {
return new PropConfigParser();
}
throw new UnsupportedOperationException("暂不支持的配置文件类型: " + configFileType);
}
}

@ -9,6 +9,7 @@ import cn.hippo4j.core.executor.support.*;
import cn.hippo4j.core.proxy.RejectedProxyUtil;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.starter.config.ExecutorProperties;
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
import cn.hippo4j.core.starter.support.GlobalCoreThreadPoolManage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -40,7 +41,7 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool
private final ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler;
private final ConfigParserHandler configParserHandler;
private final ConfigParser configParser;
protected final BootstrapCoreProperties bootstrapCoreProperties;
@ -51,7 +52,7 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool
@Override
public void dynamicRefresh(String content) {
Map<Object, Object> configInfo = configParserHandler.parseConfig(content, bootstrapCoreProperties.getConfigFileType());
Map<Object, Object> configInfo = configParser.parseConfig(content);
ConfigurationPropertySource sources = new MapConfigurationPropertySource(configInfo);
Binder binder = new Binder(sources);

@ -1,44 +0,0 @@
package cn.hippo4j.core.starter.refresher;
import cn.hippo4j.common.toolkit.StringUtil;
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 java.util.List;
import java.util.Map;
/**
* Config parser service.
*
* @author chen.ma
* @date 2022/2/26 17:33
*/
public class ConfigParserHandler {
private final List<String> yamlList = Lists.newArrayList("yaml", "yml");
/**
* Parse config.
*
* @param content
* @param configFileType
* @return
*/
public Map<Object, Object> parseConfig(String content, String configFileType) {
Map<Object, Object> resultMap = Maps.newHashMap();
if (StringUtil.isBlank(content)) {
return resultMap;
}
if (yamlList.contains(configFileType)) {
YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes()));
resultMap = yamlPropertiesFactoryBean.getObject();
}
return resultMap;
}
}

@ -2,6 +2,7 @@ package cn.hippo4j.core.starter.refresher;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import lombok.extern.slf4j.Slf4j;
@ -23,9 +24,9 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef
public NacosCloudRefresherHandler(NacosConfigManager nacosConfigManager,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ConfigParserHandler configParserHandler,
ConfigParser configParser,
BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties);
super(threadPoolNotifyAlarmHandler, configParser, bootstrapCoreProperties);
this.nacosConfigManager = nacosConfigManager;
}

@ -2,6 +2,7 @@ package cn.hippo4j.core.starter.refresher;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.starter.refresher.config.ConfigParser;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import lombok.extern.slf4j.Slf4j;
@ -23,9 +24,9 @@ public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
public NacosRefresherHandler(ConfigService configService,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ConfigParserHandler configParserHandler,
ConfigParser configParser,
BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties);
super(threadPoolNotifyAlarmHandler, configParser, bootstrapCoreProperties);
this.configService = configService;
}

@ -0,0 +1,18 @@
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);
}

@ -0,0 +1,37 @@
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());
}
}

@ -0,0 +1,32 @@
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;
}
}
Loading…
Cancel
Save