Commit 337a8089 by Nepxion

独立控制增加读取远程配置功能

parent 31f9bbca
...@@ -10,21 +10,33 @@ package com.nepxion.discovery.console.extension.nacos.adapter; ...@@ -10,21 +10,33 @@ package com.nepxion.discovery.console.extension.nacos.adapter;
*/ */
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.nepxion.discovery.console.extension.nacos.constant.NacosConstant;
import com.nepxion.discovery.console.remote.ConfigAdapter; import com.nepxion.discovery.console.remote.ConfigAdapter;
public class NacosConfigAdapter implements ConfigAdapter { public class NacosConfigAdapter implements ConfigAdapter {
@Autowired @Autowired
private ConfigService configService; private ConfigService configService;
@Autowired
private Environment environment;
@Override @Override
public boolean configUpdate(String group, String serviceId, String config) throws Exception { public boolean updateConfig(String group, String serviceId, String config) throws Exception {
return configService.publishConfig(serviceId, group, config); return configService.publishConfig(serviceId, group, config);
} }
@Override @Override
public boolean configClear(String group, String serviceId) throws Exception { public boolean clearConfig(String group, String serviceId) throws Exception {
return configService.removeConfig(serviceId, group); return configService.removeConfig(serviceId, group);
} }
@Override
public String getConfig(String group, String serviceId) throws Exception {
long timeout = environment.getProperty(NacosConstant.TIMEOUT, Long.class, NacosConstant.DEFAULT_TIMEOUT);
return configService.getConfig(serviceId, group, timeout);
}
} }
\ No newline at end of file
...@@ -13,4 +13,6 @@ public class NacosConstant { ...@@ -13,4 +13,6 @@ public class NacosConstant {
public static final String URL_KEY = "serverAddr"; public static final String URL_KEY = "serverAddr";
public static final String URL = "nacos.url"; public static final String URL = "nacos.url";
public static final String TIMEOUT = "nacos.timout"; public static final String TIMEOUT = "nacos.timout";
public static final long DEFAULT_TIMEOUT = 30000;
} }
\ No newline at end of file
...@@ -86,7 +86,13 @@ public class ConsoleEndpoint { ...@@ -86,7 +86,13 @@ public class ConsoleEndpoint {
@RequestMapping(path = "/console/remote-config/clear/{group}/{serviceId}", method = RequestMethod.POST) @RequestMapping(path = "/console/remote-config/clear/{group}/{serviceId}", method = RequestMethod.POST)
@ApiOperation(value = "清除规则配置信息到远程配置中心", notes = "", response = ResponseEntity.class, httpMethod = "POST") @ApiOperation(value = "清除规则配置信息到远程配置中心", notes = "", response = ResponseEntity.class, httpMethod = "POST")
public ResponseEntity<?> remoteConfigClear(@PathVariable(value = "group") @ApiParam(value = "组名", required = true) String group, @PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId) { public ResponseEntity<?> remoteConfigClear(@PathVariable(value = "group") @ApiParam(value = "组名", required = true) String group, @PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId) {
return executeRemoteClearUpdate(group, serviceId); return executeRemoteConfigClear(group, serviceId);
}
@RequestMapping(path = "/console/remote-config/view/{group}/{serviceId}", method = RequestMethod.GET)
@ApiOperation(value = "查看远程配置中心的规则配置信息", notes = "", response = ResponseEntity.class, httpMethod = "GET")
public ResponseEntity<?> remoteConfigView(@PathVariable(value = "group") @ApiParam(value = "组名", required = true) String group, @PathVariable(value = "serviceId") @ApiParam(value = "服务名", required = true) String serviceId) {
return executeRemoteConfigView(group, serviceId);
} }
@RequestMapping(path = "/console/config/update-async/{serviceId}", method = RequestMethod.POST) @RequestMapping(path = "/console/config/update-async/{serviceId}", method = RequestMethod.POST)
...@@ -169,7 +175,7 @@ public class ConsoleEndpoint { ...@@ -169,7 +175,7 @@ public class ConsoleEndpoint {
} }
try { try {
boolean result = configAdapter.configUpdate(group, serviceId, config); boolean result = configAdapter.updateConfig(group, serviceId, config);
return ResponseEntity.ok().body(result ? "OK" : "NO"); return ResponseEntity.ok().body(result ? "OK" : "NO");
} catch (Exception e) { } catch (Exception e) {
...@@ -177,7 +183,7 @@ public class ConsoleEndpoint { ...@@ -177,7 +183,7 @@ public class ConsoleEndpoint {
} }
} }
private ResponseEntity<?> executeRemoteClearUpdate(String group, String serviceId) { private ResponseEntity<?> executeRemoteConfigClear(String group, String serviceId) {
if (configAdapter == null) { if (configAdapter == null) {
LOG.error("Remote config adapter isn't provided"); LOG.error("Remote config adapter isn't provided");
...@@ -185,7 +191,7 @@ public class ConsoleEndpoint { ...@@ -185,7 +191,7 @@ public class ConsoleEndpoint {
} }
try { try {
boolean result = configAdapter.configClear(group, serviceId); boolean result = configAdapter.clearConfig(group, serviceId);
return ResponseEntity.ok().body(result ? "OK" : "NO"); return ResponseEntity.ok().body(result ? "OK" : "NO");
} catch (Exception e) { } catch (Exception e) {
...@@ -193,6 +199,22 @@ public class ConsoleEndpoint { ...@@ -193,6 +199,22 @@ public class ConsoleEndpoint {
} }
} }
private ResponseEntity<?> executeRemoteConfigView(String group, String serviceId) {
if (configAdapter == null) {
LOG.error("Remote config adapter isn't provided");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Remote config adapter isn't provided");
}
try {
String config = configAdapter.getConfig(group, serviceId);
return ResponseEntity.ok().body(config);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
private ResponseEntity<?> executeConfigUpdate(String serviceId, String config, boolean async) { private ResponseEntity<?> executeConfigUpdate(String serviceId, String config, boolean async) {
List<ServiceInstance> serviceInstances = getInstances(serviceId); List<ServiceInstance> serviceInstances = getInstances(serviceId);
......
...@@ -10,7 +10,9 @@ package com.nepxion.discovery.console.remote; ...@@ -10,7 +10,9 @@ package com.nepxion.discovery.console.remote;
*/ */
public interface ConfigAdapter { public interface ConfigAdapter {
boolean configUpdate(String group, String serviceId, String config) throws Exception; boolean updateConfig(String group, String serviceId, String config) throws Exception;
boolean configClear(String group, String serviceId) throws Exception; boolean clearConfig(String group, String serviceId) throws Exception;
String getConfig(String group, String serviceId) throws Exception;
} }
\ No newline at end of file
...@@ -50,7 +50,7 @@ public class NacosConfigAdapter extends ConfigAdapter { ...@@ -50,7 +50,7 @@ public class NacosConfigAdapter extends ConfigAdapter {
String group = pluginAdapter.getGroup(); String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId(); String serviceId = pluginAdapter.getServiceId();
long timeout = pluginContextAware.getEnvironment().getProperty(NacosConstant.TIMEOUT, Long.class); long timeout = pluginContextAware.getEnvironment().getProperty(NacosConstant.TIMEOUT, Long.class, NacosConstant.DEFAULT_TIMEOUT);
LOG.info("Get remote config from Nacos server, {}={}, serviceId={}, timeout={}", groupKey, group, serviceId, timeout); LOG.info("Get remote config from Nacos server, {}={}, serviceId={}, timeout={}", groupKey, group, serviceId, timeout);
...@@ -63,7 +63,7 @@ public class NacosConfigAdapter extends ConfigAdapter { ...@@ -63,7 +63,7 @@ public class NacosConfigAdapter extends ConfigAdapter {
} }
@PostConstruct @PostConstruct
public void subscribe() throws Exception { public void subscribeConfig() throws Exception {
String groupKey = pluginContextAware.getGroupKey(); String groupKey = pluginContextAware.getGroupKey();
String group = pluginAdapter.getGroup(); String group = pluginAdapter.getGroup();
String serviceId = pluginAdapter.getServiceId(); String serviceId = pluginAdapter.getServiceId();
......
...@@ -13,4 +13,6 @@ public class NacosConstant { ...@@ -13,4 +13,6 @@ public class NacosConstant {
public static final String URL_KEY = "serverAddr"; public static final String URL_KEY = "serverAddr";
public static final String URL = "nacos.url"; public static final String URL = "nacos.url";
public static final String TIMEOUT = "nacos.timout"; public static final String TIMEOUT = "nacos.timout";
public static final long DEFAULT_TIMEOUT = 30000;
} }
\ 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