Commit 65a36238 by Nepxion

增加动态版本功能

parent 1ec065cd
......@@ -30,6 +30,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.rest.ConfigClearRestInvoker;
import com.nepxion.discovery.console.rest.ConfigUpdateRestInvoker;
import com.nepxion.discovery.console.rest.VersionClearRestInvoker;
import com.nepxion.discovery.console.rest.VersionUpdateRestInvoker;
......@@ -79,6 +80,12 @@ public class ConsoleEndpoint {
return executeConfigUpdate(serviceId, config, false);
}
@RequestMapping(path = "/console/config/clear/{serviceId}", method = RequestMethod.POST)
@ApiOperation(value = "批量清除更新的规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "POST")
public ResponseEntity<?> configClear(@PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId) {
return executeConfigClear(serviceId);
}
@RequestMapping(path = "/console/version/update/{serviceId}", method = RequestMethod.POST)
@ApiOperation(value = "批量更新服务的动态版本", notes = "根据指定的localVersion更新服务的dynamicVersion。如果输入的localVersion不匹配服务的localVersion,则忽略;如果如果输入的localVersion为空,则直接更新服务的dynamicVersion", response = ResponseEntity.class, httpMethod = "POST")
public ResponseEntity<?> versionUpdate(@PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId, @RequestBody @ApiParam(value = "版本号,格式为[dynamicVersion]或者[dynamicVersion];[localVersion]", required = true) String version) {
......@@ -139,6 +146,14 @@ public class ConsoleEndpoint {
return configUpdateRestInvoker.invoke();
}
private ResponseEntity<?> executeConfigClear(String serviceId) {
List<ServiceInstance> serviceInstances = getInstances(serviceId);
ConfigClearRestInvoker configClearRestInvoker = new ConfigClearRestInvoker(serviceInstances, consoleRestTemplate);
return configClearRestInvoker.invoke();
}
private ResponseEntity<?> executeVersionUpdate(String serviceId, String version) {
List<ServiceInstance> serviceInstances = getInstances(serviceId);
......
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 ConfigClearRestInvoker extends AbstractRestInvoker {
public ConfigClearRestInvoker(List<ServiceInstance> serviceInstances, RestTemplate restTemplate) {
super(serviceInstances, restTemplate);
}
@Override
protected String getInfo() {
return "Config cleared";
}
@Override
protected String getUrl(String host, int port) {
return "http://" + host + ":" + port + "/config/clear";
}
@Override
protected String doRest(String url) {
return restTemplate.postForEntity(url, null, String.class).getBody();
}
}
\ No newline at end of file
......@@ -15,8 +15,11 @@ import io.swagger.annotations.ApiParam;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
......@@ -31,6 +34,7 @@ import com.nepxion.discovery.plugin.framework.constant.PluginConstant;
import com.nepxion.discovery.plugin.framework.context.PluginContextAware;
import com.nepxion.discovery.plugin.framework.entity.RuleEntity;
import com.nepxion.discovery.plugin.framework.event.PluginEventWapper;
import com.nepxion.discovery.plugin.framework.event.RuleClearedEvent;
import com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent;
@RestController
......@@ -57,17 +61,40 @@ public class ConfigEndpoint {
return update(config, false);
}
@RequestMapping(path = "/config/clear", method = RequestMethod.POST)
@ApiOperation(value = "清除更新的规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "POST")
public ResponseEntity<?> clear() {
Boolean discoveryControlEnabled = pluginContextAware.isDiscoveryControlEnabled();
if (!discoveryControlEnabled) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Discovery control is disabled");
}
pluginEventWapper.fireRuleCleared(new RuleClearedEvent(), true);
return ResponseEntity.ok().body("OK");
}
@RequestMapping(path = "/config/view", method = RequestMethod.GET)
@ApiOperation(value = "查看当前生效的规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "GET")
public ResponseEntity<?> view() {
RuleEntity ruleEntity = pluginAdapter.getRule();
if (ruleEntity == null) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("No config to view");
@ApiOperation(value = "查看本地和更新的规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "GET")
public ResponseEntity<List<String>> view() {
List<String> ruleList = new ArrayList<String>(2);
String localRuleContent = StringUtils.EMPTY;
RuleEntity localRuleEntity = pluginAdapter.getLocalRule();
if (localRuleEntity != null && StringUtils.isNotEmpty(localRuleEntity.getContent())) {
localRuleContent = localRuleEntity.getContent();
}
String dynamicRuleContent = StringUtils.EMPTY;
RuleEntity dynamicRuleEntity = pluginAdapter.getDynamicRule();
if (dynamicRuleEntity != null && StringUtils.isNotEmpty(dynamicRuleEntity.getContent())) {
dynamicRuleContent = dynamicRuleEntity.getContent();
}
String content = ruleEntity.getContent();
ruleList.add(localRuleContent);
ruleList.add(dynamicRuleContent);
return ResponseEntity.ok().body(content);
return ResponseEntity.ok().body(ruleList);
}
private ResponseEntity<?> update(String config, boolean async) {
......
......@@ -22,8 +22,8 @@ public class RuleCache {
public RuleCache() {
loadingCache = Caffeine.newBuilder()
.expireAfterWrite(365 * 100, TimeUnit.DAYS)
.initialCapacity(1)
.maximumSize(1)
.initialCapacity(2)
.maximumSize(10)
.recordStats()
.build(new CacheLoader<String, RuleEntity>() {
@Override
......
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