Commit f3ad0c0d by Nepxion

增加Adapter

parent f454e0b2
package com.nepxion.discovery.plugin.configcenter;
/**
* <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
*/
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
public abstract class ConfigAdapter extends AbstractConfigLoader {
@Autowired
private PluginPublisher pluginPublisher;
public void publish(InputStream inputStream) {
pluginPublisher.publish(inputStream);
}
}
\ No newline at end of file
...@@ -14,8 +14,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder; ...@@ -14,8 +14,7 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import com.nepxion.discovery.plugin.example.impl.DiscoveryConfigSubscriber; import com.nepxion.discovery.plugin.example.impl.DiscoveryConfigAdapter;
import com.nepxion.discovery.plugin.example.impl.DiscoveryConfigLoader;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -25,12 +24,7 @@ public class DiscoveryApplication { ...@@ -25,12 +24,7 @@ public class DiscoveryApplication {
} }
@Bean @Bean
public DiscoveryConfigLoader discoveryConfigLoader() { public DiscoveryConfigAdapter discoveryConfigLoader() {
return new DiscoveryConfigLoader(); return new DiscoveryConfigAdapter();
}
@Bean
public DiscoveryConfigSubscriber discoveryConfigSubscriber() {
return new DiscoveryConfigSubscriber();
} }
} }
\ No newline at end of file
...@@ -19,14 +19,31 @@ import java.util.concurrent.ThreadLocalRandom; ...@@ -19,14 +19,31 @@ import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher; import com.nepxion.discovery.plugin.configcenter.ConfigAdapter;
// 模拟从远程配置中心接受配置更新 // 模拟从本地配置或远程配置中心获取配置,订阅配置更新
public class DiscoveryConfigSubscriber { public class DiscoveryConfigAdapter extends ConfigAdapter {
@Autowired @Override
private PluginPublisher pluginPublisher; public InputStream getRemoteInputStream() {
// 本地文件模拟代替远程文件
try {
return FileUtils.openInputStream(new File("src/main/resources/rule1.xml"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected String getLocalContextPath() {
// 配置文件放在resources目录下
return "classpath:rule1.xml";
// 配置文件放在工程根目录下
// return "file:rule1.xml";
}
@PostConstruct @PostConstruct
public void initialize() { public void initialize() {
...@@ -41,7 +58,7 @@ public class DiscoveryConfigSubscriber { ...@@ -41,7 +58,7 @@ public class DiscoveryConfigSubscriber {
System.out.println("-------------------- rule" + index + ".xml is loaded --------------------"); System.out.println("-------------------- rule" + index + ".xml is loaded --------------------");
try { try {
InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule" + index + ".xml")); InputStream inputStream = FileUtils.openInputStream(new File("src/main/resources/rule" + index + ".xml"));
pluginPublisher.publish(inputStream); publish(inputStream);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
......
package com.nepxion.discovery.plugin.example.impl;
/**
* <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
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import com.nepxion.discovery.plugin.configcenter.loader.AbstractConfigLoader;
// 模拟从本地配置或远程配置中心获取配置
public class DiscoveryConfigLoader extends AbstractConfigLoader {
@Override
public InputStream getRemoteInputStream() {
// 本地文件模拟代替远程文件
try {
return FileUtils.openInputStream(new File("src/main/resources/rule1.xml"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected String getLocalContextPath() {
// 配置文件放在resources目录下
return "classpath:rule1.xml";
// 配置文件放在工程根目录下
// return "file:rule1.xml";
}
}
\ No newline at end of file
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