diff --git a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java index 5a986fa4..514f4932 100644 --- a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java +++ b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/controller/ApplicationController.java @@ -52,4 +52,10 @@ public class ApplicationController { return Results.success(); } + @PostMapping("/remove") + public Result remove(@RequestBody InstanceInfo instanceInfo) { + instanceRegistry.remove(instanceInfo); + return Results.success(); + } + } diff --git a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java index b9a45ee5..857c7229 100644 --- a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java +++ b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/BaseInstanceRegistry.java @@ -135,6 +135,31 @@ public class BaseInstanceRegistry implements InstanceRegistry { return true; } + @Override + public void remove(InstanceInfo info) { + ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock(); + writeLock.lock(); + try { + String appName = info.getAppName(); + String instanceId = info.getInstanceId(); + Map> leaseMap = registry.get(appName); + if (CollectionUtils.isEmpty(leaseMap)) { + log.warn("Failed to remove unhealthy node, no application found :: {}", appName); + return; + } + + Lease remove = leaseMap.remove(instanceId); + if (remove == null) { + log.warn("Failed to remove unhealthy node, no instance found :: {}", instanceId); + return; + } + + log.info("Remove unhealthy node, node ID :: {}", instanceId); + } finally { + writeLock.unlock(); + } + } + static class CircularQueue extends AbstractQueue { private final ArrayBlockingQueue delegate; diff --git a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/InstanceRegistry.java b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/InstanceRegistry.java index 2045ec64..dc2f75af 100644 --- a/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/InstanceRegistry.java +++ b/hippo4j-discovery/src/main/java/cn/hippo4j/discovery/core/InstanceRegistry.java @@ -35,4 +35,11 @@ public interface InstanceRegistry { */ boolean renew(InstanceInfo.InstanceRenew instanceRenew); + /** + * Remove. + * + * @param info + */ + void remove(T info); + }