客户端优雅停机时, 请求服务端删除配置缓存.

pull/12/head
chen.ma 3 years ago
parent 1ced0c7786
commit d85c670aec

@ -16,6 +16,8 @@ public class Constants {
public static final String NAMESPACE = "namespace";
public static final String GROUP_KEY = "groupKey";
public static final String DEFAULT_NAMESPACE_ID = "public";
public static final String NULL = "";

@ -2,12 +2,14 @@ package cn.hippo4j.config.controller;
import cn.hippo4j.config.model.ConfigAllInfo;
import cn.hippo4j.config.model.ConfigInfoBase;
import cn.hippo4j.config.service.ConfigCacheService;
import cn.hippo4j.config.service.ConfigServletInner;
import cn.hippo4j.config.toolkit.Md5ConfigUtil;
import cn.hippo4j.config.service.biz.ConfigService;
import cn.hippo4j.common.constant.Constants;
import cn.hippo4j.common.web.base.Result;
import cn.hippo4j.common.web.base.Results;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.util.StringUtils;
@ -72,4 +74,14 @@ public class ConfigController {
configServletInner.doPollingConfig(request, response, clientMd5Map, probeModify.length());
}
@PostMapping("/remove/config/cache")
public Result removeConfigCache(@RequestBody Map<String, String> bodyMap) {
String groupKey = bodyMap.get(Constants.GROUP_KEY);
if (StrUtil.isNotBlank(groupKey)) {
ConfigCacheService.removeConfigCache(groupKey);
}
return Results.success();
}
}

@ -10,7 +10,9 @@ import cn.hippo4j.config.event.LocalDataChangeEvent;
import cn.hippo4j.config.model.CacheItem;
import cn.hippo4j.config.model.ConfigAllInfo;
import cn.hippo4j.config.toolkit.MapUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.util.List;
@ -25,10 +27,19 @@ import java.util.concurrent.ConcurrentHashMap;
* @author chen.ma
* @date 2021/6/24 21:19
*/
@Slf4j
public class ConfigCacheService {
static ConfigService configService = null;
private static ConfigService configService = null;
/**
* TODO: remove
* <p>
* key: message-produce+dynamic-threadpool-example+prescription+192.168.20.227:8088
* val:
* key: 192.168.20.227:8088
* val: {@link CacheItem}
*/
private static final ConcurrentHashMap<String, Map<String, CacheItem>> CACHE = new ConcurrentHashMap();
public static boolean isUpdateData(String groupKey, String md5, String ip) {
@ -43,7 +54,7 @@ public class ConfigCacheService {
* @param ip
* @return
*/
private static String getContentMd5IsNullPut(String groupKey, String ip) {
private synchronized static String getContentMd5IsNullPut(String groupKey, String ip) {
Map<String, CacheItem> cacheItemMap = Optional.ofNullable(CACHE.get(groupKey)).orElse(Maps.newHashMap());
CacheItem cacheItem = null;
@ -92,7 +103,7 @@ public class ConfigCacheService {
}
}
public static CacheItem makeSure(String groupKey, String ip) {
public synchronized static CacheItem makeSure(String groupKey, String ip) {
Map<String, CacheItem> ipCacheItemMap = CACHE.get(groupKey);
CacheItem item = ipCacheItemMap.get(ip);
if (null != item) {
@ -114,4 +125,18 @@ public class ConfigCacheService {
return returnStrCacheItemMap;
}
/**
* Remove config cache.
*
* @param groupKey + + IP
*/
public synchronized static void removeConfigCache(String groupKey) {
// 模糊搜索
List<String> identificationList = MapUtil.parseMapForFilter(CACHE, groupKey);
for (String cacheMapKey : identificationList) {
Map<String, CacheItem> removeCacheItem = CACHE.remove(cacheMapKey);
log.info("Remove invalidated config cache. config info :: {}", JSON.toJSONString(removeCacheItem));
}
}
}

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

@ -8,13 +8,18 @@ import cn.hippo4j.common.web.exception.ErrorCodeEnum;
import cn.hippo4j.starter.remote.HttpAgent;
import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder;
import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
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.Optional;
import java.util.concurrent.*;
import static cn.hippo4j.common.constant.Constants.GROUP_KEY;
/**
* Discovery client.
*
@ -88,15 +93,29 @@ public class DiscoveryClient implements DisposableBean {
@Override
public void destroy() throws Exception {
log.info("{}{} - destroy service...", PREFIX, appPathIdentifier);
String urlPath = Constants.BASE_PATH + "/apps/remove/";
Result removeResult;
String removeConfigCacheUrlPath = Constants.CONFIG_CONTROLLER_PATH + "/remove/config/cache";
Result removeConfigCacheResult;
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);
}
} catch (Throwable ex) {
log.error("{}{} - remove config cache fail.", PREFIX, appPathIdentifier, ex);
}
String removeNodeUrlPath = Constants.BASE_PATH + "/apps/remove/";
Result removeNodeResult;
try {
removeResult = httpAgent.httpPostByDiscovery(urlPath, instanceInfo);
if (removeResult.isSuccess()) {
log.info("{}{} - destroy service success...", PREFIX, appPathIdentifier);
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);
log.error("{}{} - destroy service fail.", PREFIX, appPathIdentifier, ex);
}
Optional.ofNullable(scheduler).ifPresent((each) -> each.shutdown());

Loading…
Cancel
Save