Commit 770f14b1 by Nepxion

增加在线查看配置功能

parent 26ce3bfd
......@@ -152,6 +152,12 @@ public class DiscoveryConfigSubscriber {
利用Post执行http://IP:PORT/admin/config,发送的内容即规则XML
```
## 查看配置
使用者可以通过Rest方式主动向一个微服务推送配置信息,但该方式只能每次推送到一个微服务上
```xml
利用Get执行http://IP:PORT/admin/view
```
## 示例
### B服务实现
B服务的两个实例B1、B2和B3采用标准的Spring Cloud入口,参考discovery-springcloud-example-b1、discovery-springcloud-example-b2和discovery-springcloud-example-b3工程
......
......@@ -34,6 +34,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginPublisher;
import com.nepxion.discovery.plugin.framework.exception.PluginException;
......@@ -50,6 +51,9 @@ public class AdminEndpoint extends AbstractMvcEndpoint implements ApplicationCon
@Autowired
private PluginPublisher pluginPublisher;
@Autowired
private RuleEntity ruleEntity;
@SuppressWarnings("rawtypes")
public AdminEndpoint(ServiceRegistry serviceRegistry) {
super("/admin", true, true);
......@@ -84,6 +88,17 @@ public class AdminEndpoint extends AbstractMvcEndpoint implements ApplicationCon
return "success";
}
@RequestMapping(path = "view", method = RequestMethod.GET)
@ResponseBody
@ManagedOperation
public Object view() {
if (registration == null) {
throw new PluginException("No registration found");
}
return ruleEntity.getContent();
}
@RequestMapping(path = "status", method = RequestMethod.POST)
@ResponseBody
@ManagedOperation
......
......@@ -77,16 +77,18 @@ public class ConfigParser extends Dom4JParser {
}
}
String text = getText();
try {
reentrantReadWriteLock.writeLock().lock();
ruleEntity.setRegisterEntity(registerEntity);
ruleEntity.setDiscoveryEntity(discoveryEntity);
ruleEntity.setContent(text);
} finally {
reentrantReadWriteLock.writeLock().unlock();
}
LOG.info("Rule entity=\n{}", ruleEntity);
LOG.info("Rule xml=\n{}", text);
}
@SuppressWarnings("rawtypes")
......
......@@ -102,7 +102,9 @@ public class ConfigSubscriber {
}
try {
configParser.parse(inputStream);
String content = IOUtils.toString(inputStream, PluginConstant.ENCODING_UTF_8);
configParser.parse(content);
} catch (IOException e) {
throw new PluginException(e);
} catch (DocumentException e) {
......
......@@ -19,39 +19,55 @@ import org.dom4j.DocumentException;
import org.dom4j.Element;
public abstract class Dom4JParser {
private String text;
private File file;
private InputStream inputStream;
public void parse(String text) throws DocumentException {
Document document = Dom4JReader.getDocument(text);
this.text = text;
parse(document);
}
public void parseFormat(String text) throws DocumentException, UnsupportedEncodingException {
Document document = Dom4JReader.getFormatDocument(text);
this.text = text;
parse(document);
}
public void parse(File file) throws DocumentException, IOException, UnsupportedEncodingException {
Document document = Dom4JReader.getDocument(file);
this.file = file;
parse(document);
}
public void parseFormat(File file) throws DocumentException, IOException, UnsupportedEncodingException {
Document document = Dom4JReader.getFormatDocument(file);
this.file = file;
parse(document);
}
public void parse(InputStream inputStream) throws DocumentException, IOException {
Document document = Dom4JReader.getDocument(inputStream);
this.inputStream = inputStream;
parse(document);
}
public void parseFormat(InputStream inputStream) throws DocumentException, IOException, UnsupportedEncodingException {
Document document = Dom4JReader.getFormatDocument(inputStream);
this.inputStream = inputStream;
parse(document);
}
......@@ -61,5 +77,17 @@ public abstract class Dom4JParser {
parseRoot(rootElement);
}
public String getText() {
return text;
}
public File getFile() {
return file;
}
public InputStream getInputStream() {
return inputStream;
}
protected abstract void parseRoot(Element element);
}
\ No newline at end of file
......@@ -21,6 +21,7 @@ public class RuleEntity implements Serializable {
private RegisterEntity registerEntity;
private DiscoveryEntity discoveryEntity;
private String content;
public RegisterEntity getRegisterEntity() {
return registerEntity;
......@@ -38,6 +39,14 @@ public class RuleEntity implements Serializable {
this.discoveryEntity = discoveryEntity;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
......
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