Commit ffde3a04 by Nepxion

精简配置项

parent b46c5bb5
...@@ -41,7 +41,6 @@ public class ConfigInitializer { ...@@ -41,7 +41,6 @@ public class ConfigInitializer {
public void initialize() { public void initialize() {
Boolean registerControlEnabled = pluginContextAware.isRegisterControlEnabled(); Boolean registerControlEnabled = pluginContextAware.isRegisterControlEnabled();
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
Boolean remoteConfigEnabled = pluginContextAware.isRemoteConfigEnabled();
if (!registerControlEnabled && !discoveryControlEnabled) { if (!registerControlEnabled && !discoveryControlEnabled) {
LOG.info("********** Register and Discovery controls are all disabled, ignore to initialize **********"); LOG.info("********** Register and Discovery controls are all disabled, ignore to initialize **********");
...@@ -55,14 +54,9 @@ public class ConfigInitializer { ...@@ -55,14 +54,9 @@ public class ConfigInitializer {
return; return;
} }
LOG.info("********** {} config starts to initialize **********", remoteConfigEnabled ? "Remote" : "Local"); LOG.info("********** Rule starts to initialize **********");
InputStream inputStream = null; InputStream inputStream = getInputStream(configLoader);
if (remoteConfigEnabled) {
inputStream = configLoader.getRemoteInputStream();
} else {
inputStream = configLoader.getLocalInputStream();
}
try { try {
RuleEntity ruleEntity = configParser.parse(inputStream); RuleEntity ruleEntity = configParser.parse(inputStream);
pluginAdapter.setLocalRule(ruleEntity); pluginAdapter.setLocalRule(ruleEntity);
...@@ -70,4 +64,35 @@ public class ConfigInitializer { ...@@ -70,4 +64,35 @@ public class ConfigInitializer {
LOG.error("Parse rule xml failed", e); LOG.error("Parse rule xml failed", e);
} }
} }
public InputStream getInputStream(ConfigLoader configLoader) {
InputStream inputStream = null;
try {
inputStream = configLoader.getRemoteInputStream();
} catch (Exception e) {
LOG.warn("Get remote input stream failed", e);
}
if (inputStream != null) {
LOG.info("********** Remote input stream is retrieved **********");
return inputStream;
}
try {
inputStream = configLoader.getLocalInputStream();
} catch (Exception e) {
LOG.warn("Get local input stream failed", e);
}
if (inputStream != null) {
LOG.info("********** Local input stream is retrieved **********");
return inputStream;
}
LOG.info("********** No input stream is retrieved **********");
return null;
}
} }
\ No newline at end of file
...@@ -9,21 +9,18 @@ package com.nepxion.discovery.plugin.configcenter.loader; ...@@ -9,21 +9,18 @@ package com.nepxion.discovery.plugin.configcenter.loader;
* @version 1.0 * @version 1.0
*/ */
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public abstract class AbstractConfigLoader implements ConfigLoader { public abstract class AbstractConfigLoader implements ConfigLoader {
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Override @Override
public InputStream getLocalInputStream() { public InputStream getLocalInputStream() throws Exception {
String localContextPath = getLocalContextPath(); String localContextPath = getLocalContextPath();
if (StringUtils.isEmpty(localContextPath)) { if (StringUtils.isEmpty(localContextPath)) {
return null; return null;
...@@ -31,11 +28,7 @@ public abstract class AbstractConfigLoader implements ConfigLoader { ...@@ -31,11 +28,7 @@ public abstract class AbstractConfigLoader implements ConfigLoader {
String localFilePath = applicationContext.getEnvironment().resolvePlaceholders(localContextPath); String localFilePath = applicationContext.getEnvironment().resolvePlaceholders(localContextPath);
try { return applicationContext.getResource(localFilePath).getInputStream();
return applicationContext.getResource(localFilePath).getInputStream();
} catch (IOException e) {
throw new PluginException(e);
}
} }
protected abstract String getLocalContextPath(); protected abstract String getLocalContextPath();
......
...@@ -12,7 +12,7 @@ package com.nepxion.discovery.plugin.configcenter.loader; ...@@ -12,7 +12,7 @@ package com.nepxion.discovery.plugin.configcenter.loader;
import java.io.InputStream; import java.io.InputStream;
public interface ConfigLoader { public interface ConfigLoader {
InputStream getLocalInputStream(); InputStream getLocalInputStream() throws Exception;
InputStream getRemoteInputStream(); InputStream getRemoteInputStream() throws Exception;
} }
\ No newline at end of file
...@@ -11,9 +11,7 @@ package com.nepxion.discovery.plugin.framework.constant; ...@@ -11,9 +11,7 @@ package com.nepxion.discovery.plugin.framework.constant;
public class PluginConstant { public class PluginConstant {
public static final String SPRING_APPLICATION_REGISTER_CONTROL_ENABLED = "spring.application.register.control.enabled"; public static final String SPRING_APPLICATION_REGISTER_CONTROL_ENABLED = "spring.application.register.control.enabled";
public static final String SPRING_APPLICATION_REGISTER_FAILURE_EVENT_ENABLED = "spring.application.register.failure.event.enabled";
public static final String SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED = "spring.application.discovery.control.enabled"; public static final String SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED = "spring.application.discovery.control.enabled";
public static final String SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED = "spring.application.discovery.remote.config.enabled";
public static final String SPRING_APPLICATION_NAME = "spring.application.name"; public static final String SPRING_APPLICATION_NAME = "spring.application.name";
public static final String SERVICE_ID = "serviceId"; public static final String SERVICE_ID = "serviceId";
......
...@@ -92,10 +92,6 @@ public class PluginContextAware implements ApplicationContextAware { ...@@ -92,10 +92,6 @@ public class PluginContextAware implements ApplicationContextAware {
return isDiscoveryControlEnabled(environment); return isDiscoveryControlEnabled(environment);
} }
public Boolean isRemoteConfigEnabled() {
return isRemoteConfigEnabled(environment);
}
public static Boolean isRegisterControlEnabled(Environment environment) { public static Boolean isRegisterControlEnabled(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, Boolean.class, Boolean.TRUE); return environment.getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_CONTROL_ENABLED, Boolean.class, Boolean.TRUE);
} }
...@@ -103,8 +99,4 @@ public class PluginContextAware implements ApplicationContextAware { ...@@ -103,8 +99,4 @@ public class PluginContextAware implements ApplicationContextAware {
public static Boolean isDiscoveryControlEnabled(Environment environment) { public static Boolean isDiscoveryControlEnabled(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class, Boolean.TRUE); return environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class, Boolean.TRUE);
} }
public static Boolean isRemoteConfigEnabled(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED, Boolean.class, Boolean.TRUE);
}
} }
\ No newline at end of file
...@@ -45,20 +45,12 @@ public class PluginSubscriber { ...@@ -45,20 +45,12 @@ public class PluginSubscriber {
@Subscribe @Subscribe
public void onRuleUpdated(RuleUpdatedEvent ruleUpdatedEvent) { public void onRuleUpdated(RuleUpdatedEvent ruleUpdatedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
Boolean remoteConfigEnabled = pluginContextAware.isRemoteConfigEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("********** Discovery control is disabled, ignore to subscribe **********");
return; return;
} }
if (!remoteConfigEnabled) {
LOG.info("********** Remote config is disabled, ignore to subscribe **********");
return;
}
LOG.info("********** Remote rule updating has been triggered **********"); LOG.info("********** Remote rule updating has been triggered **********");
if (ruleUpdatedEvent == null) { if (ruleUpdatedEvent == null) {
...@@ -81,20 +73,12 @@ public class PluginSubscriber { ...@@ -81,20 +73,12 @@ public class PluginSubscriber {
@Subscribe @Subscribe
public void onRuleCleared(RuleClearedEvent ruleClearedEvent) { public void onRuleCleared(RuleClearedEvent ruleClearedEvent) {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled(); Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
Boolean remoteConfigEnabled = pluginContextAware.isRemoteConfigEnabled();
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
LOG.info("********** Discovery control is disabled, ignore to subscribe **********"); LOG.info("********** Discovery control is disabled, ignore to subscribe **********");
return; return;
} }
if (!remoteConfigEnabled) {
LOG.info("********** Remote config is disabled, ignore to subscribe **********");
return;
}
LOG.info("********** Remote rule clearing has been triggered **********"); LOG.info("********** Remote rule clearing has been triggered **********");
if (ruleClearedEvent == null) { if (ruleClearedEvent == null) {
......
...@@ -76,10 +76,7 @@ public class CountFilterRegisterListener extends AbstractRegisterListener { ...@@ -76,10 +76,7 @@ public class CountFilterRegisterListener extends AbstractRegisterListener {
private void onRegisterFailure(int maxCount, String serviceId, String host, int port) { private void onRegisterFailure(int maxCount, String serviceId, String host, int port) {
String description = host + " isn't allowed to register to Register server, reach max limited count=" + maxCount; String description = host + " isn't allowed to register to Register server, reach max limited count=" + maxCount;
Boolean registerFailureEventEnabled = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_FAILURE_EVENT_ENABLED, Boolean.class, Boolean.FALSE); pluginEventWapper.fireRegisterFailure(new RegisterFailureEvent(PluginConstant.REACH_MAX_LIMITED_COUNT, description, serviceId, host, port));
if (registerFailureEventEnabled) {
pluginEventWapper.fireRegisterFailure(new RegisterFailureEvent(PluginConstant.REACH_MAX_LIMITED_COUNT, description, serviceId, host, port));
}
throw new PluginException(description); throw new PluginException(description);
} }
......
...@@ -16,7 +16,6 @@ import java.util.Map; ...@@ -16,7 +16,6 @@ import java.util.Map;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.entity.FilterType; import com.nepxion.discovery.plugin.framework.entity.FilterType;
import com.nepxion.discovery.plugin.framework.entity.HostFilterEntity; import com.nepxion.discovery.plugin.framework.entity.HostFilterEntity;
import com.nepxion.discovery.plugin.framework.entity.RegisterEntity; import com.nepxion.discovery.plugin.framework.entity.RegisterEntity;
...@@ -100,10 +99,7 @@ public class HostFilterRegisterListener extends AbstractRegisterListener { ...@@ -100,10 +99,7 @@ public class HostFilterRegisterListener extends AbstractRegisterListener {
private void onRegisterFailure(FilterType filterType, List<String> allFilterValueList, String serviceId, String host, int port) { private void onRegisterFailure(FilterType filterType, List<String> allFilterValueList, String serviceId, String host, int port) {
String description = host + " isn't allowed to register to Register server, not match host " + filterType + "=" + allFilterValueList; String description = host + " isn't allowed to register to Register server, not match host " + filterType + "=" + allFilterValueList;
Boolean registerFailureEventEnabled = pluginContextAware.getEnvironment().getProperty(PluginConstant.SPRING_APPLICATION_REGISTER_FAILURE_EVENT_ENABLED, Boolean.class, Boolean.FALSE); pluginEventWapper.fireRegisterFailure(new RegisterFailureEvent(filterType.toString(), description, serviceId, host, port));
if (registerFailureEventEnabled) {
pluginEventWapper.fireRegisterFailure(new RegisterFailureEvent(filterType.toString(), description, serviceId, host, port));
}
throw new PluginException(description); throw new PluginException(description);
} }
......
...@@ -10,7 +10,6 @@ package com.nepxion.discovery.plugin.example.adapter; ...@@ -10,7 +10,6 @@ package com.nepxion.discovery.plugin.example.adapter;
*/ */
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
...@@ -33,24 +32,14 @@ public class MyConfigAdapter extends ConfigAdapter { ...@@ -33,24 +32,14 @@ public class MyConfigAdapter extends ConfigAdapter {
// 从远程配置中心获取规则 // 从远程配置中心获取规则
@Override @Override
public InputStream getRemoteInputStream() { public InputStream getRemoteInputStream() throws Exception {
try { return FileUtils.openInputStream(new File("src/main/resources/rule.xml"));
return FileUtils.openInputStream(new File("src/main/resources/rule.xml"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
} }
// 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息) // 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息)
/*@PostConstruct /*@PostConstruct
public void update() { public void update() throws Exception {
try { InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule.xml"));
InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule.xml")); fireRuleUpdated(new RuleUpdatedEvent(inputStream), true);
fireRuleUpdated(new RuleUpdatedEvent(inputStream), true);
} catch (IOException e) {
e.printStackTrace();
}
}*/ }*/
} }
\ No newline at end of file
...@@ -23,15 +23,10 @@ management.security.enabled=false ...@@ -23,15 +23,10 @@ management.security.enabled=false
# management.health.consul.enabled=true # management.health.consul.enabled=true
# Plugin config # Plugin config
# 开启和关闭服务注册层面的控制。一旦关闭,服务注册的黑/白名单过滤功能将失效。缺失则默认为true # 开启和关闭服务注册层面的控制。一旦关闭,服务注册的黑/白名单过滤功能将失效,最大注册数的限制过滤功能将失效。缺失则默认为true
spring.application.register.control.enabled=true spring.application.register.control.enabled=true
# 开启和关闭禁止注册后发送异步事件通知。一旦关闭,禁止注册后,不会发送异步事件通知。缺失则默认为false
spring.application.register.failure.event.enabled=false
# 开启和关闭服务发现层面的控制。一旦关闭,服务多版本调用的控制功能将失效,动态屏蔽指定IP地址的服务实例被发现的功能将失效。缺失则默认为true # 开启和关闭服务发现层面的控制。一旦关闭,服务多版本调用的控制功能将失效,动态屏蔽指定IP地址的服务实例被发现的功能将失效。缺失则默认为true
spring.application.discovery.control.enabled=true spring.application.discovery.control.enabled=true
# 开启和关闭远程配置中心规则文件读取。一旦关闭,默认读取本地规则文件(例如:rule.xml)。缺失则默认为true
spring.application.discovery.remote.config.enabled=true
# Swagger config # Swagger config
swagger.service.base.package=com.nepxion.discovery.plugin.admincenter.endpoint swagger.service.base.package=com.nepxion.discovery.plugin.admincenter.endpoint
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment