Commit eb7fb816 by Nepxion

修改测试用例

parent 0ca447f3
...@@ -12,6 +12,10 @@ package com.nepxion.discovery.plugin.example; ...@@ -12,6 +12,10 @@ package com.nepxion.discovery.plugin.example;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; 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 com.nepxion.discovery.plugin.example.impl.DiscoveryPluginConfigSimulator;
import com.nepxion.discovery.plugin.example.impl.DiscoveryPluginFileLoader;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
...@@ -19,4 +23,14 @@ public class DiscoveryPluginApplication { ...@@ -19,4 +23,14 @@ public class DiscoveryPluginApplication {
public static void main(String[] args) { public static void main(String[] args) {
new SpringApplicationBuilder(DiscoveryPluginApplication.class).web(true).run(args); new SpringApplicationBuilder(DiscoveryPluginApplication.class).web(true).run(args);
} }
@Bean
public DiscoveryPluginFileLoader discoveryPluginFileLoader() {
return new DiscoveryPluginFileLoader();
}
@Bean
public DiscoveryPluginConfigSimulator discoveryPluginConfigSimulator() {
return new DiscoveryPluginConfigSimulator();
}
} }
\ No newline at end of file
package com.nepxion.discovery.plugin.example; package com.nepxion.discovery.plugin.example.impl;
/** /**
* <p>Title: Nepxion Discovery</p> * <p>Title: Nepxion Discovery</p>
...@@ -12,7 +12,6 @@ package com.nepxion.discovery.plugin.example; ...@@ -12,7 +12,6 @@ package com.nepxion.discovery.plugin.example;
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 java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
...@@ -21,37 +20,11 @@ import java.util.concurrent.ThreadLocalRandom; ...@@ -21,37 +20,11 @@ import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import com.nepxion.discovery.plugin.config.DiscoveryPluginConfigPublisher; import com.nepxion.discovery.plugin.config.DiscoveryPluginConfigPublisher;
import com.nepxion.discovery.plugin.loader.AbstractFileLoader;
import com.nepxion.discovery.plugin.loader.FileLoader;
@Component // 模拟从远程配置中心接受配置更新
public class DiscoveryPluginSimulator { public class DiscoveryPluginConfigSimulator {
// 模拟从本地配置和远程配置获取discovery.xml
@Bean
public FileLoader fileLoader() {
return new AbstractFileLoader() {
@Override
public InputStream getRemoteInputStream() throws IOException {
// 本地文件模拟代替远程文件
return createInputStream("src/main/resources/discovery1.xml");
}
@Override
protected String getLocalContextPath() {
// 配置文件放在resources目录下
return "classpath:discovery1.xml";
// 配置文件放在工程根目录下
// return "file:discovery.xml";
}
};
}
// 模拟每隔15秒通过事件总线接收远程配置中心推送过来的配置更新
@Autowired @Autowired
private DiscoveryPluginConfigPublisher discoveryPluginConfigPublisher; private DiscoveryPluginConfigPublisher discoveryPluginConfigPublisher;
...@@ -59,11 +32,12 @@ public class DiscoveryPluginSimulator { ...@@ -59,11 +32,12 @@ public class DiscoveryPluginSimulator {
public void initialize() { public void initialize() {
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
// 模拟每隔15秒通过EventBus接收远程配置中心推送过来的配置更新
Timer timer = new Timer(); Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() { timer.scheduleAtFixedRate(new TimerTask() {
public void run() { public void run() {
// 本地文件模拟代替远程文件,随机读取 // 本地文件模拟代替远程文件,随机读取
int index = threadLocalRandom.nextInt(3) + 1; int index = threadLocalRandom.nextInt(4) + 1;
InputStream inputStream = createInputStream("src/main/resources/discovery" + index + ".xml"); InputStream inputStream = createInputStream("src/main/resources/discovery" + index + ".xml");
discoveryPluginConfigPublisher.publish(inputStream); discoveryPluginConfigPublisher.publish(inputStream);
} }
......
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.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.nepxion.discovery.plugin.loader.AbstractFileLoader;
// 模拟从本地配置或远程配置中心获取配置
public class DiscoveryPluginFileLoader extends AbstractFileLoader {
@Override
public InputStream getRemoteInputStream() throws IOException {
// 本地文件模拟代替远程文件
return createInputStream("src/main/resources/discovery1.xml");
}
@Override
protected String getLocalContextPath() {
// 配置文件放在resources目录下
return "classpath:discovery1.xml";
// 配置文件放在工程根目录下
// return "file:discovery.xml";
}
private InputStream createInputStream(String fileName) {
File file = new File(fileName);
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return inputStream;
}
}
\ 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