#502 Support update instance

This commit is contained in:
nkorange 2019-02-22 19:29:56 +08:00
parent 59c2765ada
commit 862ff80688
6 changed files with 42 additions and 17 deletions

View File

@ -93,7 +93,12 @@ public class InstanceController {
@CanDistro
@RequestMapping(value = "/instance", method = RequestMethod.POST)
public String register(HttpServletRequest request) throws Exception {
return registerInstance(request);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, UtilsAndCommons.DEFAULT_NAMESPACE_ID);
serviceManager.registerInstance(namespaceId, serviceName, parseInstance(request));
return "ok";
}
@CanDistro
@ -116,7 +121,11 @@ public class InstanceController {
@RequestMapping(value = {"/instance/update", "instance"}, method = RequestMethod.PUT)
public String update(HttpServletRequest request) throws Exception {
return registerInstance(request);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, UtilsAndCommons.DEFAULT_NAMESPACE_ID);
serviceManager.updateInstance(namespaceId, serviceName, parseInstance(request));
return "ok";
}
@RequestMapping(value = {"/instances", "/instance/list"}, method = RequestMethod.GET)
@ -232,7 +241,7 @@ public class InstanceController {
instance.setInstanceId(instance.generateInstanceId());
instance.setEphemeral(clientBeat.isEphemeral());
serviceManager.registerInstance(namespaceId, serviceName, clusterName, instance);
serviceManager.registerInstance(namespaceId, serviceName, instance);
}
Service service = serviceManager.getService(namespaceId, serviceName);
@ -305,11 +314,9 @@ public class InstanceController {
return result;
}
private String registerInstance(HttpServletRequest request) throws Exception {
private Instance parseInstance(HttpServletRequest request) throws Exception {
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String clusterName = WebUtils.optional(request, CommonParams.CLUSTER_NAME, UtilsAndCommons.DEFAULT_CLUSTER_NAME);
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, UtilsAndCommons.DEFAULT_NAMESPACE_ID);
String app = WebUtils.optional(request, "app", "DEFAULT");
String metadata = WebUtils.optional(request, "metadata", StringUtils.EMPTY);
@ -332,9 +339,7 @@ public class InstanceController {
+ " in " + switchDomain.getServerMode() + " mode.");
}
serviceManager.registerInstance(namespaceId, serviceName, clusterName, instance);
return "ok";
return instance;
}
private Instance getIPAddress(HttpServletRequest request) {

View File

@ -354,7 +354,7 @@ public class ServiceManager implements RecordListener<Service> {
* @param instance instance to register
* @throws Exception any error occurred in the process
*/
public void registerInstance(String namespaceId, String serviceName, String clusterName, Instance instance) throws Exception {
public void registerInstance(String namespaceId, String serviceName, Instance instance) throws NacosException {
if (ServerMode.AP.name().equals(switchDomain.getServerMode())) {
createEmptyService(namespaceId, serviceName);
@ -371,10 +371,26 @@ public class ServiceManager implements RecordListener<Service> {
throw new NacosException(NacosException.INVALID_PARAM, "instance already exist: " + instance);
}
addInstance(namespaceId, serviceName, clusterName, instance.isEphemeral(), instance);
addInstance(namespaceId, serviceName, instance.isEphemeral(), instance);
}
public void addInstance(String namespaceId, String serviceName, String clusterName, boolean ephemeral, Instance... ips) throws NacosException {
public void updateInstance(String namespaceId, String serviceName, Instance instance) throws NacosException {
Service service = getService(namespaceId, serviceName);
if (service == null) {
throw new NacosException(NacosException.INVALID_PARAM,
"service not found, namespace: " + namespaceId + ", service: " + serviceName);
}
if (!service.allIPs().contains(instance)) {
throw new NacosException(NacosException.INVALID_PARAM, "instance not exist: " + instance);
}
addInstance(namespaceId, serviceName, instance.isEphemeral(), instance);
}
public void addInstance(String namespaceId, String serviceName, boolean ephemeral, Instance... ips) throws NacosException {
String key = KeyBuilder.buildInstanceListKey(namespaceId, serviceName, ephemeral);

View File

@ -202,13 +202,13 @@ public class TcpSuperSenseProcessor implements HealthCheckProcessor, Runnable {
return;
}
if (key.isHealthy() && key.isConnectable()) {
if (key.isValid() && key.isConnectable()) {
//connected
channel.finishConnect();
beat.finishCheck(true, false, System.currentTimeMillis() - beat.getTask().getStartTime(), "tcp:ok+");
}
if (key.isHealthy() && key.isReadable()) {
if (key.isValid() && key.isReadable()) {
//disconnected
ByteBuffer buffer = ByteBuffer.allocate(128);
if (channel.read(buffer) == -1) {
@ -332,7 +332,7 @@ public class TcpSuperSenseProcessor implements HealthCheckProcessor, Runnable {
@Override
public void run() {
if (key != null && key.isHealthy()) {
if (key != null && key.isValid()) {
SocketChannel channel = (SocketChannel) key.channel();
Beat beat = (Beat) key.attachment();
@ -378,7 +378,7 @@ public class TcpSuperSenseProcessor implements HealthCheckProcessor, Runnable {
Cluster cluster = beat.getTask().getCluster();
BeatKey beatKey = keyMap.get(beat.toString());
if (beatKey != null && beatKey.key.isHealthy()) {
if (beatKey != null && beatKey.key.isValid()) {
if (System.currentTimeMillis() - beatKey.birthTime < TCP_KEEP_ALIVE_MILLIS) {
instance.setBeingChecked(false);
return null;

View File

@ -138,7 +138,6 @@ public class NamingProxy {
result = HttpClient.httpGet("http://" + curServer + api, headers, params);
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content;
}

View File

@ -82,6 +82,8 @@ public class DistroFilter implements Filter {
}
if (method.isAnnotationPresent(CanDistro.class) && !distroMapper.responsible(serviceName)) {
// TODO proxy request:
String url = "http://" + distroMapper.mapSrv(serviceName) +
req.getRequestURI() + "?" + req.getQueryString();
try {

View File

@ -58,6 +58,9 @@ public class FilterBase {
RequestMapping requestMapping = clazz.getAnnotation(RequestMapping.class);
String classPath = requestMapping.value()[0];
for (Method method : clazz.getMethods()) {
if (!method.isAnnotationPresent(RequestMapping.class)) {
continue;
}
requestMapping = method.getAnnotation(RequestMapping.class);
RequestMethod[] requestMethods = requestMapping.method();
if (requestMethods.length == 0) {