mirror of https://github.com/longtai-cn/hippo4j
优化客户端关闭时调用服务端钩子函数. (#65)
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();
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue