Commit 283a1453 by Nepxion

优化Console接口

parent 61101852
......@@ -18,16 +18,11 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;
......@@ -40,14 +35,14 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.console.entity.InstanceEntity;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
import com.nepxion.discovery.console.rest.ConfigUpdateRestInvoker;
import com.nepxion.discovery.console.rest.VersionClearRestInvoker;
import com.nepxion.discovery.console.rest.VersionUpdateRestInvoker;
@RestController
@Api(tags = { "控制台接口" })
@ManagedResource(description = "Console Endpoint")
public class ConsoleEndpoint implements MvcEndpoint {
private static final Logger LOG = LoggerFactory.getLogger(ConsoleEndpoint.class);
@Autowired
private DiscoveryClient discoveryClient;
......@@ -160,101 +155,26 @@ public class ConsoleEndpoint implements MvcEndpoint {
private ResponseEntity<?> executeConfigUpdate(String serviceId, String config, boolean async) {
List<ServiceInstance> serviceInstances = getInstances(serviceId);
if (CollectionUtils.isEmpty(serviceInstances)) {
LOG.warn("No service instances found");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("No service instances found");
}
StringBuilder stringBuilder = new StringBuilder();
for (ServiceInstance serviceInstance : serviceInstances) {
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
String url = "http://" + host + ":" + port + "/config/update-" + (async ? "async" : "sync");
String result = consoleRestTemplate.postForEntity(url, config, String.class).getBody();
if (!StringUtils.equals(result, "OK")) {
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) consoleRestTemplate.getErrorHandler();
result = errorHandler.getCause();
}
stringBuilder.append("Result : serviceId=").append(serviceId).append(", url=").append(url).append(", result=").append(result).append("\n");
}
ConfigUpdateRestInvoker configUpdateRestInvoker = new ConfigUpdateRestInvoker(serviceInstances, consoleRestTemplate, config, async);
String result = stringBuilder.toString();
if (result.contains("\n")) {
result = result.substring(0, result.lastIndexOf("\n"));
}
LOG.info("Config updated results :\n{}", result);
return ResponseEntity.ok().body(result);
return configUpdateRestInvoker.invoke();
}
private ResponseEntity<?> executeVersionUpdate(String serviceId, String version) {
List<ServiceInstance> serviceInstances = getInstances(serviceId);
if (CollectionUtils.isEmpty(serviceInstances)) {
LOG.warn("No service instances found");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("No service instances found");
}
StringBuilder stringBuilder = new StringBuilder();
for (ServiceInstance serviceInstance : serviceInstances) {
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
String url = "http://" + host + ":" + port + "/version/update";
String result = consoleRestTemplate.postForEntity(url, version, String.class).getBody();
if (!StringUtils.equals(result, "OK")) {
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) consoleRestTemplate.getErrorHandler();
result = errorHandler.getCause();
}
stringBuilder.append("Result : serviceId=").append(serviceId).append(", url=").append(url).append(", result=").append(result).append("\n");
}
String result = stringBuilder.toString();
if (result.contains("\n")) {
result = result.substring(0, result.lastIndexOf("\n"));
}
VersionUpdateRestInvoker versionUpdateRestInvoker = new VersionUpdateRestInvoker(serviceInstances, consoleRestTemplate, version);
LOG.info("Version updated results :\n{}", result);
return ResponseEntity.ok().body(result);
return versionUpdateRestInvoker.invoke();
}
private ResponseEntity<?> executeVersionClear(String serviceId, String version) {
List<ServiceInstance> serviceInstances = getInstances(serviceId);
if (CollectionUtils.isEmpty(serviceInstances)) {
LOG.warn("No service instances found");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("No service instances found");
}
StringBuilder stringBuilder = new StringBuilder();
for (ServiceInstance serviceInstance : serviceInstances) {
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
String url = "http://" + host + ":" + port + "/version/clear";
String result = consoleRestTemplate.postForEntity(url, version, String.class).getBody();
if (!StringUtils.equals(result, "OK")) {
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) consoleRestTemplate.getErrorHandler();
result = errorHandler.getCause();
}
stringBuilder.append("Result : serviceId=").append(serviceId).append(", url=").append(url).append(", result=").append(result).append("\n");
}
String result = stringBuilder.toString();
if (result.contains("\n")) {
result = result.substring(0, result.lastIndexOf("\n"));
}
LOG.info("Version cleared results :\n{}", result);
VersionClearRestInvoker versionClearRestInvoker = new VersionClearRestInvoker(serviceInstances, consoleRestTemplate, version);
return ResponseEntity.ok().body(result);
return versionClearRestInvoker.invoke();
}
@Override
......
package com.nepxion.discovery.console.entity;
/**
* <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.Serializable;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class ResultEntity implements Serializable {
private static final long serialVersionUID = -3322655604556025836L;
private String serviceId;
private String url;
private String result;
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals(Object object) {
return EqualsBuilder.reflectionEquals(this, object);
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}
\ No newline at end of file
package com.nepxion.discovery.console.rest;
/**
* <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.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.console.entity.ResultEntity;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
public abstract class AbstractRestInvoker {
private static final Logger LOG = LoggerFactory.getLogger(AbstractRestInvoker.class);
protected List<ServiceInstance> serviceInstances;
protected RestTemplate restTemplate;
public AbstractRestInvoker(List<ServiceInstance> serviceInstances, RestTemplate restTemplate) {
this.serviceInstances = serviceInstances;
this.restTemplate = restTemplate;
}
public ResponseEntity<?> invoke() {
if (CollectionUtils.isEmpty(serviceInstances)) {
LOG.warn("No service instances found");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("No service instances found");
}
List<ResultEntity> resultEntityList = new ArrayList<ResultEntity>();
for (ServiceInstance serviceInstance : serviceInstances) {
String serviceId = serviceInstance.getServiceId().toLowerCase();
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
String url = getUrl(host, port);
String result = doRest(url);
if (!StringUtils.equals(result, "OK")) {
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) restTemplate.getErrorHandler();
result = errorHandler.getCause();
}
ResultEntity resultEntity = new ResultEntity();
resultEntity.setServiceId(serviceId);
resultEntity.setUrl(url);
resultEntity.setResult(result);
resultEntityList.add(resultEntity);
}
String info = getInfo();
LOG.info(info + " results=\n{}", resultEntityList);
return ResponseEntity.ok().body(resultEntityList);
}
protected abstract String getInfo();
protected abstract String getUrl(String host, int port);
protected abstract String doRest(String url);
}
\ No newline at end of file
package com.nepxion.discovery.console.rest;
/**
* <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.util.List;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.client.RestTemplate;
public class ConfigUpdateRestInvoker extends AbstractRestInvoker {
private String config;
private boolean async;
public ConfigUpdateRestInvoker(List<ServiceInstance> serviceInstances, RestTemplate restTemplate, String config, boolean async) {
super(serviceInstances, restTemplate);
this.config = config;
this.async = async;
}
@Override
protected String getInfo() {
return "Config updated";
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/config/update-" + (async ? "async" : "sync");
}
@Override
protected String doRest(String url) {
return restTemplate.postForEntity(url, config, String.class).getBody();
}
}
\ No newline at end of file
package com.nepxion.discovery.console.rest;
/**
* <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.util.List;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.client.RestTemplate;
public class VersionClearRestInvoker extends AbstractRestInvoker {
private String version;
public VersionClearRestInvoker(List<ServiceInstance> serviceInstances, RestTemplate restTemplate, String version) {
super(serviceInstances, restTemplate);
this.version = version;
}
@Override
protected String getInfo() {
return "Version cleared";
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/version/clear";
}
@Override
protected String doRest(String url) {
return restTemplate.postForEntity(url, version, String.class).getBody();
}
}
\ No newline at end of file
package com.nepxion.discovery.console.rest;
/**
* <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.util.List;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.client.RestTemplate;
public class VersionUpdateRestInvoker extends AbstractRestInvoker {
private String version;
public VersionUpdateRestInvoker(List<ServiceInstance> serviceInstances, RestTemplate restTemplate, String version) {
super(serviceInstances, restTemplate);
this.version = version;
}
@Override
protected String getInfo() {
return "Version updated";
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/version/update";
}
@Override
protected String doRest(String url) {
return restTemplate.postForEntity(url, version, String.class).getBody();
}
}
\ 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