Commit 2cf18ac2 by Nepxion

拆分获取本地配置和远程配置

parent 57157137
...@@ -11,11 +11,11 @@ package com.nepxion.discovery.plugin.configcenter; ...@@ -11,11 +11,11 @@ package com.nepxion.discovery.plugin.configcenter;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader; import com.nepxion.discovery.plugin.configcenter.loader.RemoteConfigLoader;
import com.nepxion.discovery.plugin.framework.event.PluginEventWapper; import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent; import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent;
public abstract class ConfigAdapter extends AbstractConfigLoader { public abstract class ConfigAdapter extends RemoteConfigLoader {
@Autowired @Autowired
private PluginEventWapper pluginEventWapper; private PluginEventWapper pluginEventWapper;
......
...@@ -17,7 +17,8 @@ import org.slf4j.Logger; ...@@ -17,7 +17,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.configcenter.loader.ConfigLoader; import com.nepxion.discovery.plugin.configcenter.loader.LocalConfigLoader;
import com.nepxion.discovery.plugin.configcenter.loader.RemoteConfigLoader;
import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter; import com.nepxion.discovery.plugin.framework.adapter.PluginAdapter;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware; import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity; import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
...@@ -31,8 +32,11 @@ public class ConfigInitializer { ...@@ -31,8 +32,11 @@ public class ConfigInitializer {
@Autowired @Autowired
private PluginAdapter pluginAdapter; private PluginAdapter pluginAdapter;
@Autowired
private LocalConfigLoader localConfigLoader;
@Autowired(required = false) @Autowired(required = false)
private ConfigLoader configLoader; private RemoteConfigLoader remoteConfigLoader;
@Autowired @Autowired
private ConfigParser configParser; private ConfigParser configParser;
...@@ -48,15 +52,9 @@ public class ConfigInitializer { ...@@ -48,15 +52,9 @@ public class ConfigInitializer {
return; return;
} }
if (configLoader == null) {
LOG.info("********** ConfigLoader isn't provided, ignore to initialize **********");
return;
}
LOG.info("********** Rule starts to initialize **********"); LOG.info("********** Rule starts to initialize **********");
InputStream inputStream = getInputStream(configLoader); InputStream inputStream = getInputStream();
try { try {
RuleEntity ruleEntity = configParser.parse(inputStream); RuleEntity ruleEntity = configParser.parse(inputStream);
pluginAdapter.setLocalRule(ruleEntity); pluginAdapter.setLocalRule(ruleEntity);
...@@ -65,22 +63,27 @@ public class ConfigInitializer { ...@@ -65,22 +63,27 @@ public class ConfigInitializer {
} }
} }
public InputStream getInputStream(ConfigLoader configLoader) { public InputStream getInputStream() {
InputStream inputStream = null; InputStream inputStream = null;
try {
inputStream = configLoader.getRemoteInputStream();
} catch (Exception e) {
LOG.warn("Get remote input stream failed", e);
}
if (inputStream != null) { if (remoteConfigLoader != null) {
LOG.info("********** Remote input stream is retrieved **********"); try {
inputStream = remoteConfigLoader.getInputStream();
} catch (Exception e) {
LOG.warn("Get remote input stream failed", e);
}
return inputStream; if (inputStream != null) {
LOG.info("********** Remote input stream is retrieved **********");
return inputStream;
}
} else {
LOG.info("********** Remote config loader isn't provided, use local config loader **********");
} }
try { try {
inputStream = configLoader.getLocalInputStream(); inputStream = localConfigLoader.getInputStream();
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Get local input stream failed", e); LOG.warn("Get local input stream failed", e);
} }
......
...@@ -9,14 +9,30 @@ package com.nepxion.discovery.plugin.configcenter.configuration; ...@@ -9,14 +9,30 @@ package com.nepxion.discovery.plugin.configcenter.configuration;
* @version 1.0 * @version 1.0
*/ */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.configcenter.ConfigInitializer; import com.nepxion.discovery.plugin.configcenter.ConfigInitializer;
import com.nepxion.discovery.plugin.configcenter.ConfigParser; import com.nepxion.discovery.plugin.configcenter.ConfigParser;
import com.nepxion.discovery.plugin.configcenter.loader.LocalConfigLoader;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
@Configuration @Configuration
public class ConfigAutoConfiguration { public class ConfigAutoConfiguration {
@Autowired
private PluginContextAware pluginContextAware;
@Bean
public LocalConfigLoader localConfigLoader() {
return new LocalConfigLoader() {
@Override
protected String getPath() {
return pluginContextAware.getConfigPath();
}
};
}
@Bean @Bean
public ConfigInitializer configInitializer() { public ConfigInitializer configInitializer() {
return new ConfigInitializer(); return new ConfigInitializer();
......
...@@ -12,7 +12,5 @@ package com.nepxion.discovery.plugin.configcenter.loader; ...@@ -12,7 +12,5 @@ package com.nepxion.discovery.plugin.configcenter.loader;
import java.io.InputStream; import java.io.InputStream;
public interface ConfigLoader { public interface ConfigLoader {
InputStream getLocalInputStream() throws Exception; InputStream getInputStream() throws Exception;
InputStream getRemoteInputStream() throws Exception;
} }
\ No newline at end of file
...@@ -15,21 +15,21 @@ import org.apache.commons.lang3.StringUtils; ...@@ -15,21 +15,21 @@ 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;
public abstract class AbstractConfigLoader implements ConfigLoader { public abstract class LocalConfigLoader implements ConfigLoader {
@Autowired @Autowired
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@Override @Override
public InputStream getLocalInputStream() throws Exception { public InputStream getInputStream() throws Exception {
String localContextPath = getLocalContextPath(); String path = getPath();
if (StringUtils.isEmpty(localContextPath)) { if (StringUtils.isEmpty(path)) {
return null; return null;
} }
String localFilePath = applicationContext.getEnvironment().resolvePlaceholders(localContextPath); String filePath = applicationContext.getEnvironment().resolvePlaceholders(path);
return applicationContext.getResource(localFilePath).getInputStream(); return applicationContext.getResource(filePath).getInputStream();
} }
protected abstract String getLocalContextPath(); protected abstract String getPath();
} }
\ No newline at end of file
package com.nepxion.discovery.plugin.configcenter.loader;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
public abstract class RemoteConfigLoader implements ConfigLoader {
}
\ No newline at end of file
...@@ -14,6 +14,7 @@ public class PluginConstant { ...@@ -14,6 +14,7 @@ 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_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_CONFIG_REST_CONTROL_ENABLED = "spring.application.config.rest.control.enabled"; public static final String SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED = "spring.application.config.rest.control.enabled";
public static final String SPRING_APPLICATION_CONFIG_PATH = "spring.application.config.path";
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";
......
...@@ -96,6 +96,10 @@ public class PluginContextAware implements ApplicationContextAware { ...@@ -96,6 +96,10 @@ public class PluginContextAware implements ApplicationContextAware {
return isConfigRestControlEnabled(environment); return isConfigRestControlEnabled(environment);
} }
public String getConfigPath() {
return getConfigPath(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);
} }
...@@ -107,4 +111,8 @@ public class PluginContextAware implements ApplicationContextAware { ...@@ -107,4 +111,8 @@ public class PluginContextAware implements ApplicationContextAware {
public static Boolean isConfigRestControlEnabled(Environment environment) { public static Boolean isConfigRestControlEnabled(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, Boolean.class, Boolean.TRUE); return environment.getProperty(PluginConstant.SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED, Boolean.class, Boolean.TRUE);
} }
public static String getConfigPath(Environment environment) {
return environment.getProperty(PluginConstant.SPRING_APPLICATION_CONFIG_PATH);
}
} }
\ No newline at end of file
...@@ -15,7 +15,6 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; ...@@ -15,7 +15,6 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.adapter.MyConfigAdapter;
import com.nepxion.discovery.plugin.example.extension.MyDiscoveryListener; import com.nepxion.discovery.plugin.example.extension.MyDiscoveryListener;
import com.nepxion.discovery.plugin.example.extension.MyRegisterListener; import com.nepxion.discovery.plugin.example.extension.MyRegisterListener;
import com.nepxion.discovery.plugin.example.extension.MySubscriber; import com.nepxion.discovery.plugin.example.extension.MySubscriber;
...@@ -30,10 +29,10 @@ public class DiscoveryApplicationA1 { ...@@ -30,10 +29,10 @@ public class DiscoveryApplicationA1 {
new SpringApplicationBuilder(DiscoveryApplicationA1.class).run(args); new SpringApplicationBuilder(DiscoveryApplicationA1.class).run(args);
} }
@Bean /*@Bean
public MyConfigAdapter myConfigAdapter() { public MyConfigAdapter myConfigAdapter() {
return new MyConfigAdapter(); return new MyConfigAdapter();
} }*/
@Bean @Bean
public MyRegisterListener myRegisterListener() { public MyRegisterListener myRegisterListener() {
......
...@@ -12,33 +12,26 @@ package com.nepxion.discovery.plugin.example.adapter; ...@@ -12,33 +12,26 @@ package com.nepxion.discovery.plugin.example.adapter;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import javax.annotation.PostConstruct;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import com.nepxion.discovery.plugin.configcenter.ConfigAdapter; import com.nepxion.discovery.plugin.configcenter.ConfigAdapter;
import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent;
// 模拟主动从本地或远程配置中心获取规则 // 模拟主动从本地或远程配置中心获取规则
// 模拟订阅远程配置中心的规则更新 // 模拟订阅远程配置中心的规则更新
public class MyConfigAdapter extends ConfigAdapter { public class MyConfigAdapter extends ConfigAdapter {
// 从本地获取规则
@Override
protected String getLocalContextPath() {
// 规则文件放在resources目录下
return "classpath:rule.xml";
// 规则文件放在工程根目录下
// return "file:rule.xml";
}
// 从远程配置中心获取规则 // 从远程配置中心获取规则
@Override @Override
public InputStream getRemoteInputStream() throws Exception { public InputStream getInputStream() throws Exception {
return FileUtils.openInputStream(new File("src/main/resources/rule.xml")); return FileUtils.openInputStream(new File("src/main/resources/rule.xml"));
} }
// 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息) // 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息)
/*@PostConstruct @PostConstruct
public void update() throws Exception { public void update() throws Exception {
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);
}*/ }
} }
\ No newline at end of file
...@@ -29,6 +29,11 @@ spring.application.register.control.enabled=true ...@@ -29,6 +29,11 @@ spring.application.register.control.enabled=true
spring.application.discovery.control.enabled=true spring.application.discovery.control.enabled=true
# 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true # 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true
spring.application.config.rest.control.enabled=true spring.application.config.rest.control.enabled=true
# 本地规则文件的路径。缺失则默认为不装载本地规则
# 两种方式放置方式:
# classpath:rule.xml,规则文件放在resources目录下,便于把规则文件打包进jar
# file:rule.xml,规则文件放在工程根目录下,便于把规则文件放置在外部,方便修改
spring.application.config.path=classpath:rule.xml
# 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