Commit d2b74e04 by Nepxion

控制台增加批量异步推送规则配置信息接口

parent 045d44d4
......@@ -21,6 +21,11 @@
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
......
......@@ -17,6 +17,7 @@ import org.springframework.context.annotation.Import;
import org.springframework.web.client.RestTemplate;
import com.nepxion.discovery.console.endpoint.ConsoleEndpoint;
import com.nepxion.discovery.console.handler.ConsoleErrorHandler;
@Configuration
// @ComponentScan(basePackages = { "com.nepxion.discovery.console.endpoint" })
......@@ -45,7 +46,10 @@ public class ConsoleAutoConfiguration {
@Bean
public RestTemplate consoleRestTemplate() {
return new RestTemplate();
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ConsoleErrorHandler());
return restTemplate;
}
}
}
\ No newline at end of file
......@@ -38,6 +38,7 @@ 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;
@RestController
@Api(tags = { "控制台接口" })
......@@ -140,6 +141,8 @@ public class ConsoleEndpoint implements MvcEndpoint {
}
private ResponseEntity<?> send(String serviceId, String config, boolean async) {
StringBuilder stringBuilder = new StringBuilder();
List<ServiceInstance> serviceInstances = getInstances(serviceId);
for (ServiceInstance serviceInstance : serviceInstances) {
String host = serviceInstance.getHost();
......@@ -148,15 +151,20 @@ public class ConsoleEndpoint implements MvcEndpoint {
String url = "http://" + host + ":" + port + "/config/send-" + (async ? "async" : "sync");
String result = consoleRestTemplate.postForEntity(url, config, String.class).getBody();
LOG.info("Send rule, serviceId={} url={}, result={}", serviceId, url, result);
// 这里需要考虑分布式事务
// 这里最好考虑分布式事务
if (!StringUtils.equals(result, "OK")) {
return ResponseEntity.ok().body("Send rule failed for url=" + url + ", cause=" + result);
ConsoleErrorHandler errorHandler = (ConsoleErrorHandler) consoleRestTemplate.getErrorHandler();
result = errorHandler.getCause();
}
stringBuilder.append("Rule sent, serviceId=").append(serviceId).append(", url=").append(url).append(", result=").append(result).append("\n");
}
return ResponseEntity.ok().body("OK");
String result = stringBuilder.toString();
result = result.substring(0, result.lastIndexOf("\n"));
LOG.info("\n{}", result);
return ResponseEntity.ok().body(result);
}
@Override
......
package com.nepxion.discovery.console.handler;
/**
* <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.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
public class ConsoleErrorHandler extends DefaultResponseErrorHandler {
private String cause;
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// 这里绝对不能关闭InputStream
InputStream inputStream = response.getBody();
cause = IOUtils.toString(inputStream, "UTF-8");
}
public String getCause() {
return cause;
}
}
\ 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