Commit 52317fdc by Nepxion

重构类结构

parent c6c62f83
...@@ -60,7 +60,7 @@ public class AdminEndpoint implements MvcEndpoint, ApplicationContextAware, Envi ...@@ -60,7 +60,7 @@ public class AdminEndpoint implements MvcEndpoint, ApplicationContextAware, Envi
public Object filter(@RequestParam("serviceId") String serviceId, @RequestParam("ip") String ip) { public Object filter(@RequestParam("serviceId") String serviceId, @RequestParam("ip") String ip) {
Boolean discoveryControlEnabled = environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class); Boolean discoveryControlEnabled = environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class);
if (!discoveryControlEnabled) { if (!discoveryControlEnabled) {
return new ResponseEntity<>(Collections.singletonMap("Message", "Admin endpoint is disabled"), HttpStatus.NOT_FOUND); return new ResponseEntity<>(Collections.singletonMap("Message", "Discovery control is disabled"), HttpStatus.NOT_FOUND);
} }
pluginCache.put(serviceId, ip); pluginCache.put(serviceId, ip);
......
...@@ -29,8 +29,8 @@ import com.nepxion.eventbus.annotation.EventBus; ...@@ -29,8 +29,8 @@ import com.nepxion.eventbus.annotation.EventBus;
import com.nepxion.eventbus.core.Event; import com.nepxion.eventbus.core.Event;
@EventBus @EventBus
public class ConfigRetriever { public class ConfigSubscriber {
private static final Logger LOG = LoggerFactory.getLogger(ConfigRetriever.class); private static final Logger LOG = LoggerFactory.getLogger(ConfigSubscriber.class);
@Value("${" + ConfigConstant.SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED + ":false}") @Value("${" + ConfigConstant.SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED + ":false}")
private Boolean remoteConfigEnabled; private Boolean remoteConfigEnabled;
...@@ -42,26 +42,16 @@ public class ConfigRetriever { ...@@ -42,26 +42,16 @@ public class ConfigRetriever {
private ConfigParser configParser; private ConfigParser configParser;
@PostConstruct @PostConstruct
public void initialize() throws PluginException { public void initialize() {
LOG.info("********** {} config starts to initialize **********", remoteConfigEnabled ? "Remote" : "Local"); LOG.info("********** {} config starts to initialize **********", remoteConfigEnabled ? "Remote" : "Local");
InputStream inputStream = null; InputStream inputStream = null;
try { if (remoteConfigEnabled) {
if (remoteConfigEnabled) { inputStream = configLoader.getRemoteInputStream();
inputStream = configLoader.getRemoteInputStream(); } else {
} else { inputStream = configLoader.getLocalInputStream();
inputStream = configLoader.getLocalInputStream();
}
parse(inputStream);
} catch (IOException e) {
throw new PluginException(e);
} catch (DocumentException e) {
throw new PluginException(e);
} finally {
if (inputStream != null) {
IOUtils.closeQuietly(inputStream);
}
} }
parse(inputStream);
} }
@Subscribe @Subscribe
...@@ -73,8 +63,16 @@ public class ConfigRetriever { ...@@ -73,8 +63,16 @@ public class ConfigRetriever {
LOG.info("********** Remote config change has been retrieved **********"); LOG.info("********** Remote config change has been retrieved **********");
InputStream inputStream = (InputStream) event.getSource(); InputStream inputStream = (InputStream) event.getSource();
parse(inputStream);
}
private void parse(InputStream inputStream) {
if (inputStream == null) {
throw new PluginException("Failed to load " + (remoteConfigEnabled ? "remote" : "local") + " config, no input stream returns");
}
try { try {
parse(inputStream); configParser.parse(inputStream);
} catch (IOException e) { } catch (IOException e) {
throw new PluginException(e); throw new PluginException(e);
} catch (DocumentException e) { } catch (DocumentException e) {
...@@ -85,12 +83,4 @@ public class ConfigRetriever { ...@@ -85,12 +83,4 @@ public class ConfigRetriever {
} }
} }
} }
private void parse(InputStream inputStream) throws DocumentException, IOException {
if (inputStream == null) {
throw new PluginException("Failed to load " + (remoteConfigEnabled ? "remote" : "local") + " config, no input stream returns");
}
configParser.parse(inputStream);
}
} }
\ No newline at end of file
...@@ -14,7 +14,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -14,7 +14,7 @@ import org.springframework.context.annotation.Configuration;
import com.nepxion.discovery.plugin.configcenter.ConfigParser; import com.nepxion.discovery.plugin.configcenter.ConfigParser;
import com.nepxion.discovery.plugin.configcenter.ConfigPublisher; import com.nepxion.discovery.plugin.configcenter.ConfigPublisher;
import com.nepxion.discovery.plugin.configcenter.ConfigRetriever; import com.nepxion.discovery.plugin.configcenter.ConfigSubscriber;
@Configuration @Configuration
public class ConfigAutoConfiguration { public class ConfigAutoConfiguration {
...@@ -24,8 +24,8 @@ public class ConfigAutoConfiguration { ...@@ -24,8 +24,8 @@ public class ConfigAutoConfiguration {
} }
@Bean @Bean
public ConfigRetriever configRetriever() { public ConfigSubscriber configSubscriber() {
return new ConfigRetriever(); return new ConfigSubscriber();
} }
@Bean @Bean
......
...@@ -16,12 +16,14 @@ import org.apache.commons.lang3.StringUtils; ...@@ -16,12 +16,14 @@ 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() throws IOException { public InputStream getLocalInputStream() {
String localContextPath = getLocalContextPath(); String localContextPath = getLocalContextPath();
if (StringUtils.isEmpty(localContextPath)) { if (StringUtils.isEmpty(localContextPath)) {
return null; return null;
...@@ -29,7 +31,11 @@ public abstract class AbstractConfigLoader implements ConfigLoader { ...@@ -29,7 +31,11 @@ public abstract class AbstractConfigLoader implements ConfigLoader {
String localFilePath = applicationContext.getEnvironment().resolvePlaceholders(localContextPath); String localFilePath = applicationContext.getEnvironment().resolvePlaceholders(localContextPath);
return applicationContext.getResource(localFilePath).getInputStream(); try {
return applicationContext.getResource(localFilePath).getInputStream();
} catch (IOException e) {
throw new PluginException(e);
}
} }
protected abstract String getLocalContextPath(); protected abstract String getLocalContextPath();
......
...@@ -9,11 +9,10 @@ package com.nepxion.discovery.plugin.configcenter.loader; ...@@ -9,11 +9,10 @@ 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;
public interface ConfigLoader { public interface ConfigLoader {
InputStream getLocalInputStream() throws IOException; InputStream getLocalInputStream();
InputStream getRemoteInputStream() throws IOException; InputStream getRemoteInputStream();
} }
\ No newline at end of file
...@@ -12,7 +12,6 @@ package com.nepxion.discovery.plugin.example.impl; ...@@ -12,7 +12,6 @@ package com.nepxion.discovery.plugin.example.impl;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader; import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader;
...@@ -20,7 +19,7 @@ import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader; ...@@ -20,7 +19,7 @@ import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader;
// 模拟从本地配置或远程配置中心获取配置 // 模拟从本地配置或远程配置中心获取配置
public class DiscoveryConfigLoader extends AbstractConfigLoader { public class DiscoveryConfigLoader extends AbstractConfigLoader {
@Override @Override
public InputStream getRemoteInputStream() throws IOException { public InputStream getRemoteInputStream() {
// 本地文件模拟代替远程文件 // 本地文件模拟代替远程文件
return getInputStream("src/main/resources/rule1.xml"); return getInputStream("src/main/resources/rule1.xml");
} }
......
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