Commit 52317fdc by Nepxion

重构类结构

parent c6c62f83
......@@ -60,7 +60,7 @@ public class AdminEndpoint implements MvcEndpoint, ApplicationContextAware, Envi
public Object filter(@RequestParam("serviceId") String serviceId, @RequestParam("ip") String ip) {
Boolean discoveryControlEnabled = environment.getProperty(PluginConstant.SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED, Boolean.class);
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);
......
......@@ -29,8 +29,8 @@ import com.nepxion.eventbus.annotation.EventBus;
import com.nepxion.eventbus.core.Event;
@EventBus
public class ConfigRetriever {
private static final Logger LOG = LoggerFactory.getLogger(ConfigRetriever.class);
public class ConfigSubscriber {
private static final Logger LOG = LoggerFactory.getLogger(ConfigSubscriber.class);
@Value("${" + ConfigConstant.SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED + ":false}")
private Boolean remoteConfigEnabled;
......@@ -42,26 +42,16 @@ public class ConfigRetriever {
private ConfigParser configParser;
@PostConstruct
public void initialize() throws PluginException {
public void initialize() {
LOG.info("********** {} config starts to initialize **********", remoteConfigEnabled ? "Remote" : "Local");
InputStream inputStream = null;
try {
if (remoteConfigEnabled) {
inputStream = configLoader.getRemoteInputStream();
} else {
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);
}
}
}
@Subscribe
......@@ -73,8 +63,16 @@ public class ConfigRetriever {
LOG.info("********** Remote config change has been retrieved **********");
InputStream inputStream = (InputStream) event.getSource();
try {
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 {
configParser.parse(inputStream);
} catch (IOException e) {
throw new PluginException(e);
} catch (DocumentException e) {
......@@ -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;
import com.nepxion.discovery.plugin.configcenter.ConfigParser;
import com.nepxion.discovery.plugin.configcenter.ConfigPublisher;
import com.nepxion.discovery.plugin.configcenter.ConfigRetriever;
import com.nepxion.discovery.plugin.configcenter.ConfigSubscriber;
@Configuration
public class ConfigAutoConfiguration {
......@@ -24,8 +24,8 @@ public class ConfigAutoConfiguration {
}
@Bean
public ConfigRetriever configRetriever() {
return new ConfigRetriever();
public ConfigSubscriber configSubscriber() {
return new ConfigSubscriber();
}
@Bean
......
......@@ -16,12 +16,14 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
public abstract class AbstractConfigLoader implements ConfigLoader {
@Autowired
private ApplicationContext applicationContext;
@Override
public InputStream getLocalInputStream() throws IOException {
public InputStream getLocalInputStream() {
String localContextPath = getLocalContextPath();
if (StringUtils.isEmpty(localContextPath)) {
return null;
......@@ -29,7 +31,11 @@ public abstract class AbstractConfigLoader implements ConfigLoader {
String localFilePath = applicationContext.getEnvironment().resolvePlaceholders(localContextPath);
try {
return applicationContext.getResource(localFilePath).getInputStream();
} catch (IOException e) {
throw new PluginException(e);
}
}
protected abstract String getLocalContextPath();
......
......@@ -9,11 +9,10 @@ package com.nepxion.discovery.plugin.configcenter.loader;
* @version 1.0
*/
import java.io.IOException;
import java.io.InputStream;
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
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 {
@Override
public InputStream getRemoteInputStream() throws IOException {
public InputStream getRemoteInputStream() {
// 本地文件模拟代替远程文件
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