优化客户端关闭时调用服务端钩子函数. (#65)

pull/84/head
chen.ma 3 years ago
parent 4ccbef3318
commit 65348253b1

@ -0,0 +1,42 @@
package cn.hippo4j.common.api;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* Client close hook execute.
*
* @author chen.ma
* @date 2022/1/6 22:14
*/
public interface ClientCloseHookExecute {
/**
* Client close hook function execution.
*
* @param req
*/
void closeHook(ClientCloseHookReq req);
@Data
@Accessors(chain = true)
class ClientCloseHookReq {
/**
* appName
*/
private String appName;
/**
* instanceId
*/
private String instanceId;
/**
* groupKey
*/
private String groupKey;
}
}

@ -0,0 +1,37 @@
package cn.hippo4j.config.service.handler;
import cn.hippo4j.common.api.ClientCloseHookExecute;
import cn.hippo4j.common.toolkit.JSONUtil;
import cn.hippo4j.config.service.ConfigCacheService;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* Client close hook remove config cache.
*
* @author chen.ma
* @date 2022/1/6 22:20
*/
@Slf4j
@Component
public class ClientCloseHookRemoveConfigCache implements ClientCloseHookExecute {
@Override
public void closeHook(ClientCloseHookReq req) {
log.info(
"Remove Config Cache, Execute client hook function. Req :: {}",
JSONUtil.toJSONString(req)
);
try {
String groupKey = req.getGroupKey();
if (StrUtil.isNotBlank(groupKey)) {
ConfigCacheService.removeConfigCache(groupKey);
}
} catch (Exception ex) {
log.error("Failed to remove config cache hook.", ex);
}
}
}

@ -0,0 +1,32 @@
package cn.hippo4j.console.controller;
import cn.hippo4j.common.api.ClientCloseHookExecute;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.common.web.base.Results;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* Client close hook controller.
*
* @author chen.ma
* @date 2022/1/6 22:30
*/
@RestController
@RequestMapping(Constants.BASE_PATH + "/client/close")
public class ClientCloseHookController {
@PostMapping
public Result clientCloseHook(@RequestBody ClientCloseHookExecute.ClientCloseHookReq req) {
Map<String, ClientCloseHookExecute> clientCloseHookExecuteMap = ApplicationContextHolder.getBeansOfType(ClientCloseHookExecute.class);
clientCloseHookExecuteMap.forEach((key, execute) -> execute.closeHook(req));
return Results.success();
}
}

@ -54,7 +54,7 @@ public class ApplicationController {
@PostMapping("/remove")
public Result remove(@RequestBody InstanceInfo instanceInfo) {
instanceRegistry.remove(instanceInfo);
instanceRegistry.remove(instanceInfo);
return Results.success();
}

@ -0,0 +1,39 @@
package cn.hippo4j.discovery.core;
import cn.hippo4j.common.api.ClientCloseHookExecute;
import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.common.toolkit.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* Client close hook remove node.
*
* @author chen.ma
* @date 2022/1/6 22:24
*/
@Slf4j
@Component
@AllArgsConstructor
public class ClientCloseHookRemoveNode implements ClientCloseHookExecute {
private final InstanceRegistry instanceRegistry;
@Override
public void closeHook(ClientCloseHookReq req) {
log.info(
"Remove Node, Execute client hook function. Req :: {}",
JSONUtil.toJSONString(req)
);
try {
InstanceInfo instanceInfo = new InstanceInfo();
instanceInfo.setAppName(req.getAppName()).setInstanceId(req.getInstanceId());
instanceRegistry.remove(instanceInfo);
} catch (Exception ex) {
log.error("Failed to delete node hook.", ex);
}
}
}

@ -1,5 +1,6 @@
package cn.hippo4j.starter.core;
import cn.hippo4j.common.api.ClientCloseHookExecute;
import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.model.InstanceInfo;
import cn.hippo4j.common.web.base.Result;
@ -9,17 +10,14 @@ import cn.hippo4j.starter.remote.HttpAgent;
import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static cn.hippo4j.common.constant.Constants.BASE_PATH;
import static cn.hippo4j.common.constant.Constants.GROUP_KEY;
/**
* Discovery client.
@ -84,33 +82,30 @@ public class DiscoveryClient implements DisposableBean {
@Override
public void destroy() throws Exception {
log.info("{}{} - destroy service...", PREFIX, appPathIdentifier);
String removeConfigCacheUrlPath = Constants.CONFIG_CONTROLLER_PATH + "/remove/config/cache";
Result removeConfigCacheResult;
String ClientCloseUrlPath = Constants.BASE_PATH + "/client/close";
Result clientCLoseResult;
try {
String groupKeyIp = StrBuilder.create().append(instanceInfo.getGroupKey()).append(Constants.GROUP_KEY_DELIMITER).append(instanceInfo.getIdentify()).toString();
Map<String, String> bodyMap = Maps.newHashMap();
bodyMap.put(GROUP_KEY, groupKeyIp);
removeConfigCacheResult = httpAgent.httpPostByDiscovery(removeConfigCacheUrlPath, bodyMap);
if (removeConfigCacheResult.isSuccess()) {
log.info("{}{} - remove config cache success.", PREFIX, appPathIdentifier);
String groupKeyIp = StrBuilder.create()
.append(instanceInfo.getGroupKey())
.append(Constants.GROUP_KEY_DELIMITER)
.append(instanceInfo.getIdentify())
.toString();
ClientCloseHookExecute.ClientCloseHookReq clientCloseHookReq = new ClientCloseHookExecute.ClientCloseHookReq();
clientCloseHookReq.setAppName(instanceInfo.getAppName())
.setInstanceId(instanceInfo.getInstanceId())
.setGroupKey(groupKeyIp);
clientCLoseResult = httpAgent.httpPostByDiscovery(ClientCloseUrlPath, clientCloseHookReq);
if (clientCLoseResult.isSuccess()) {
log.info("{}{} -client close hook success.", PREFIX, appPathIdentifier);
}
} catch (Throwable ex) {
if (ex instanceof ShutdownExecuteException) {
return;
}
log.error("{}{} - remove config cache fail.", PREFIX, appPathIdentifier, ex);
}
String removeNodeUrlPath = BASE_PATH + "/apps/remove/";
Result removeNodeResult;
try {
removeNodeResult = httpAgent.httpPostByDiscovery(removeNodeUrlPath, instanceInfo);
if (removeNodeResult.isSuccess()) {
log.info("{}{} - destroy service success.", PREFIX, appPathIdentifier);
}
} catch (Throwable ex) {
log.error("{}{} - destroy service fail.", PREFIX, appPathIdentifier, ex);
log.error("{}{} - client close hook fail.", PREFIX, appPathIdentifier, ex);
}
}

Loading…
Cancel
Save