From a302e5a2ac197e8961959e3685b8a47a03fbba7c Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Tue, 14 Dec 2021 20:55:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20Jackson=20=E6=9B=BF?= =?UTF-8?q?=E6=8D=A2=20FastJson=20=E7=BB=84=E4=BB=B6.=20(#25)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/filter/JWTAuthorizationFilter.java | 4 +- hippo4j-common/pom.xml | 10 +- .../cn/hippo4j/common/api/JsonFacade.java | 41 ++++++ .../common/api/impl/JacksonHandler.java | 57 ++++++++ .../hippo4j/common/toolkit/ContentUtil.java | 3 +- .../cn/hippo4j/common/toolkit/JSONUtil.java | 47 +++++++ hippo4j-config/pom.xml | 5 - .../hippo4j/config/config/CommonConfig.java | 7 + .../hippo4j/config/model/ConfigAllInfo.java | 16 +-- .../hippo4j/config/model/ConfigInfoBase.java | 6 +- .../model/biz/tenant/TenantUpdateReqDTO.java | 4 +- .../config/monitor/RuntimeDataResolver.java | 5 +- .../config/service/ConfigCacheService.java | 77 +++++----- .../config/service/LongPollingService.java | 132 ++++++++++++------ .../service/biz/impl/ConfigServiceImpl.java | 30 ++-- .../hippo4j/config/toolkit/Md5ConfigUtil.java | 4 +- .../hippo4j/config/toolkit/RequestUtil.java | 13 +- hippo4j-spring-boot-starter/pom.xml | 5 - .../starter/alarm/BaseSendMessageService.java | 6 +- .../wechat/WeChatSendMessageHandler.java | 4 +- .../DynamicThreadPoolAutoConfiguration.java | 7 + .../cn/hippo4j/starter/core/ClientWorker.java | 18 ++- .../core/DynamicThreadPoolPostProcessor.java | 43 +++--- .../core/ThreadPoolDynamicRefresh.java | 8 +- .../starter/toolkit/HttpClientUtil.java | 16 +-- pom.xml | 10 +- 26 files changed, 397 insertions(+), 181 deletions(-) create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/api/JsonFacade.java create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/api/impl/JacksonHandler.java create mode 100644 hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/JSONUtil.java diff --git a/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java b/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java index 2c7c621b..dbe0132f 100644 --- a/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java +++ b/hippo4j-auth/src/main/java/cn/hippo4j/auth/filter/JWTAuthorizationFilter.java @@ -1,10 +1,10 @@ package cn.hippo4j.auth.filter; import cn.hippo4j.auth.toolkit.JwtTokenUtil; +import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.UserContext; import cn.hippo4j.common.web.base.Results; import cn.hippo4j.common.web.exception.ServiceException; -import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -50,7 +50,7 @@ public class JWTAuthorizationFilter extends BasicAuthenticationFilter { // 返回 Json 形式的错误信息 response.setCharacterEncoding("UTF-8"); response.setContentType("application/json; charset=utf-8"); - response.getWriter().write(JSON.toJSONString(Results.failure("-1", ex.getMessage()))); + response.getWriter().write(JSONUtil.toJSONString(Results.failure("-1", ex.getMessage()))); response.getWriter().flush(); return; } diff --git a/hippo4j-common/pom.xml b/hippo4j-common/pom.xml index 77959f09..7ab6b3f1 100644 --- a/hippo4j-common/pom.xml +++ b/hippo4j-common/pom.xml @@ -21,11 +21,6 @@ spring-boot-starter - - com.alibaba - fastjson - - org.projectlombok lombok @@ -35,6 +30,11 @@ cn.hutool hutool-all + + + org.springframework.boot + spring-boot-starter-json + diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/api/JsonFacade.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/JsonFacade.java new file mode 100644 index 00000000..566bbb71 --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/JsonFacade.java @@ -0,0 +1,41 @@ +package cn.hippo4j.common.api; + +import java.util.List; + +/** + * Json facade. + * + * @author chen.ma + * @date 2021/12/13 20:01 + */ +public interface JsonFacade { + + /** + * To JSON string. + * + * @param object + * @return + */ + String toJSONString(Object object); + + /** + * Parse object. + * + * @param text + * @param clazz + * @param + * @return + */ + T parseObject(String text, Class clazz); + + /** + * Parse array. + * + * @param text + * @param clazz + * @param + * @return + */ + List parseArray(String text, Class clazz); + +} diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/api/impl/JacksonHandler.java b/hippo4j-common/src/main/java/cn/hippo4j/common/api/impl/JacksonHandler.java new file mode 100644 index 00000000..f64ae3fc --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/api/impl/JacksonHandler.java @@ -0,0 +1,57 @@ +package cn.hippo4j.common.api.impl; + +import cn.hippo4j.common.api.JsonFacade; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.type.CollectionType; +import lombok.SneakyThrows; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; + +/** + * Jackson util. + * + * @author chen.ma + * @date 2021/12/13 20:02 + */ +public class JacksonHandler implements JsonFacade { + + private static ObjectMapper MAPPER = new ObjectMapper(); + + static { + MAPPER.enable(JsonGenerator.Feature.IGNORE_UNKNOWN); + String dateTimeFormat = "yyyy-MM-dd HH:mm:ss"; + MAPPER.setDateFormat(new SimpleDateFormat(dateTimeFormat)); + MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); + MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL); + MAPPER.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + } + + @Override + @SneakyThrows + public String toJSONString(Object object) { + return MAPPER.writeValueAsString(object); + } + + @Override + @SneakyThrows + public T parseObject(String text, Class clazz) { + JavaType javaType = MAPPER.getTypeFactory().constructType(clazz); + return MAPPER.readValue(text, javaType); + } + + @Override + @SneakyThrows + public List parseArray(String text, Class clazz) { + CollectionType collectionType = MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, clazz); + return MAPPER.readValue(text, collectionType); + } + +} diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java index 82f78eea..32b76149 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/ContentUtil.java @@ -1,7 +1,6 @@ package cn.hippo4j.common.toolkit; import cn.hippo4j.common.constant.Constants; -import com.alibaba.fastjson.JSON; import cn.hippo4j.common.model.PoolParameter; import cn.hippo4j.common.model.PoolParameterInfo; @@ -27,7 +26,7 @@ public class ContentUtil { .setCapacityAlarm(parameter.getCapacityAlarm()) .setLivenessAlarm(parameter.getLivenessAlarm()) .setRejectedType(parameter.getRejectedType()); - return JSON.toJSONString(poolInfo); + return JSONUtil.toJSONString(poolInfo); } public static String getGroupKey(PoolParameter parameter) { diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/JSONUtil.java b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/JSONUtil.java new file mode 100644 index 00000000..6a978114 --- /dev/null +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/toolkit/JSONUtil.java @@ -0,0 +1,47 @@ +package cn.hippo4j.common.toolkit; + +import cn.hippo4j.common.api.JsonFacade; +import cn.hippo4j.common.config.ApplicationContextHolder; +import cn.hutool.core.util.StrUtil; + +import java.util.List; + +/** + * JSON util. + * + * @author chen.ma + * @date 2021/12/13 20:27 + */ +public class JSONUtil { + + private static JsonFacade jsonFacade; + + static { + JSONUtil.jsonFacade = ApplicationContextHolder.getBean(JsonFacade.class); + } + + public static String toJSONString(Object object) { + if (object == null) { + return null; + } + + return jsonFacade.toJSONString(object); + } + + public static T parseObject(String text, Class clazz) { + if (StrUtil.isBlank(text)) { + return null; + } + + return jsonFacade.parseObject(text, clazz); + } + + public static List parseArray(String text, Class clazz) { + if (StrUtil.isBlank(text)) { + return null; + } + + return jsonFacade.parseArray(text, clazz); + } + +} diff --git a/hippo4j-config/pom.xml b/hippo4j-config/pom.xml index 2c187126..b71eeb86 100644 --- a/hippo4j-config/pom.xml +++ b/hippo4j-config/pom.xml @@ -51,11 +51,6 @@ hippo4j-common - - com.alibaba - fastjson - - com.github.dozermapper dozer-core diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/config/CommonConfig.java b/hippo4j-config/src/main/java/cn/hippo4j/config/config/CommonConfig.java index 63e5d14a..403256ed 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/config/CommonConfig.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/config/CommonConfig.java @@ -1,5 +1,7 @@ package cn.hippo4j.config.config; +import cn.hippo4j.common.api.JsonFacade; +import cn.hippo4j.common.api.impl.JacksonHandler; import cn.hippo4j.common.config.ApplicationContextHolder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -13,6 +15,11 @@ import org.springframework.context.annotation.Configuration; @Configuration public class CommonConfig { + @Bean + public JsonFacade jacksonHandler() { + return new JacksonHandler(); + } + @Bean public ApplicationContextHolder simpleApplicationContextHolder() { return new ApplicationContextHolder(); diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigAllInfo.java b/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigAllInfo.java index 7792cfc9..970e5ce2 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigAllInfo.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigAllInfo.java @@ -1,12 +1,12 @@ package cn.hippo4j.config.model; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.annotation.JSONField; +import cn.hippo4j.common.model.PoolParameter; +import cn.hippo4j.common.toolkit.JSONUtil; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; -import cn.hippo4j.common.model.PoolParameter; +import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import java.util.Date; @@ -26,21 +26,21 @@ public class ConfigAllInfo extends ConfigInfo implements PoolParameter { /** * desc */ - @JSONField(serialize = false) + @JsonIgnore @TableField(exist = false, fill = FieldFill.UPDATE) private String desc; /** * gmtCreate */ - @JSONField(serialize = false) + @JsonIgnore @TableField(fill = FieldFill.INSERT) private Date gmtCreate; /** * gmtModified */ - @JSONField(serialize = false) + @JsonIgnore @TableField(fill = FieldFill.INSERT_UPDATE) private Date gmtModified; @@ -48,13 +48,13 @@ public class ConfigAllInfo extends ConfigInfo implements PoolParameter { * delFlag */ @TableLogic - @JSONField(serialize = false) + @JsonIgnore @TableField(fill = FieldFill.INSERT) private Integer delFlag; @Override public String toString() { - return JSON.toJSONString(this); + return JSONUtil.toJSONString(this); } } diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java b/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java index ba11acce..54181bb5 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/model/ConfigInfoBase.java @@ -1,8 +1,8 @@ package cn.hippo4j.config.model; -import com.alibaba.fastjson.annotation.JSONField; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; +import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.Data; import java.io.Serializable; @@ -87,13 +87,13 @@ public class ConfigInfoBase implements Serializable { /** * MD5 */ - @JSONField(serialize = false) + @JsonIgnore private String md5; /** * content */ - @JSONField(serialize = false) + @JsonIgnore private String content; } diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/tenant/TenantUpdateReqDTO.java b/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/tenant/TenantUpdateReqDTO.java index 884b54b8..e4bc3a77 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/tenant/TenantUpdateReqDTO.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/tenant/TenantUpdateReqDTO.java @@ -1,6 +1,6 @@ package cn.hippo4j.config.model.biz.tenant; -import com.alibaba.fastjson.JSON; +import cn.hippo4j.common.toolkit.JSONUtil; import lombok.Data; /** @@ -39,7 +39,7 @@ public class TenantUpdateReqDTO { @Override public String toString() { - return JSON.toJSONString(this); + return JSONUtil.toJSONString(this); } } diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/monitor/RuntimeDataResolver.java b/hippo4j-config/src/main/java/cn/hippo4j/config/monitor/RuntimeDataResolver.java index c3578937..615d563c 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/monitor/RuntimeDataResolver.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/monitor/RuntimeDataResolver.java @@ -2,9 +2,8 @@ package cn.hippo4j.config.monitor; import cn.hippo4j.common.monitor.MessageTypeEnum; import cn.hippo4j.common.monitor.RuntimeMessage; +import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.config.service.biz.HisRunDataService; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -29,7 +28,7 @@ public class RuntimeDataResolver extends AbstractMonitorDataExecuteStrategy - * key: message-produce+dynamic-threadpool-example+prescription+192.168.20.227:8088 + * key: message-produce+dynamic-threadpool-example+prescription+192.168.20.227:8088_xxx * val: - * key: 192.168.20.227:8088 + * key: 192.168.20.227:8088_xxx * val: {@link CacheItem} */ - private static final ConcurrentHashMap> CACHE = new ConcurrentHashMap(); + private static final ConcurrentHashMap> CLIENT_CONFIG_CACHE = new ConcurrentHashMap(); - public static boolean isUpdateData(String groupKey, String md5, String ip) { - String contentMd5 = ConfigCacheService.getContentMd5IsNullPut(groupKey, ip); + public static boolean isUpdateData(String groupKey, String md5, String clientIdentify) { + String contentMd5 = ConfigCacheService.getContentMd5IsNullPut(groupKey, clientIdentify); return Objects.equals(contentMd5, md5); } @@ -51,38 +52,38 @@ public class ConfigCacheService { * Get Md5. * * @param groupKey - * @param ip + * @param clientIdentify * @return */ - private synchronized static String getContentMd5IsNullPut(String groupKey, String ip) { - Map cacheItemMap = Optional.ofNullable(CACHE.get(groupKey)).orElse(Maps.newHashMap()); + private synchronized static String getContentMd5IsNullPut(String groupKey, String clientIdentify) { + Map cacheItemMap = Optional.ofNullable(CLIENT_CONFIG_CACHE.get(groupKey)).orElse(Maps.newHashMap()); CacheItem cacheItem = null; - if (CollUtil.isNotEmpty(cacheItemMap) && (cacheItem = cacheItemMap.get(ip)) != null) { + if (CollUtil.isNotEmpty(cacheItemMap) && (cacheItem = cacheItemMap.get(clientIdentify)) != null) { return cacheItem.md5; } - if (configService == null) { - configService = ApplicationContextHolder.getBean(ConfigService.class); + if (CONFIG_SERVICE == null) { + CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class); } String[] params = groupKey.split("\\+"); - ConfigAllInfo config = configService.findConfigRecentInfo(params); + ConfigAllInfo config = CONFIG_SERVICE.findConfigRecentInfo(params); if (config != null && !StringUtils.isEmpty(config.getTpId())) { cacheItem = new CacheItem(groupKey, config); - cacheItemMap.put(ip, cacheItem); - CACHE.put(groupKey, cacheItemMap); + cacheItemMap.put(clientIdentify, cacheItem); + CLIENT_CONFIG_CACHE.put(groupKey, cacheItemMap); } return (cacheItem != null) ? cacheItem.md5 : Constants.NULL; } public static String getContentMd5(String groupKey) { - if (configService == null) { - configService = ApplicationContextHolder.getBean(ConfigService.class); + if (CONFIG_SERVICE == null) { + CONFIG_SERVICE = ApplicationContextHolder.getBean(ConfigService.class); } String[] params = groupKey.split("\\+"); - ConfigAllInfo config = configService.findConfigRecentInfo(params); + ConfigAllInfo config = CONFIG_SERVICE.findConfigRecentInfo(params); if (config == null || StringUtils.isEmpty(config.getTpId())) { String errorMessage = String.format("config is null. tpId :: %s, itemId :: %s, tenantId :: %s", params[0], params[1], params[2]); throw new RuntimeException(errorMessage); @@ -91,40 +92,46 @@ public class ConfigCacheService { return Md5Util.getTpContentMd5(config); } - public static void updateMd5(String groupKey, String ip, String md5) { - CacheItem cache = makeSure(groupKey, ip); + public static void updateMd5(String groupKey, String identify, String md5) { + CacheItem cache = makeSure(groupKey, identify); if (cache.md5 == null || !cache.md5.equals(md5)) { cache.md5 = md5; String[] params = groupKey.split("\\+"); - ConfigAllInfo config = configService.findConfigRecentInfo(params); + ConfigAllInfo config = CONFIG_SERVICE.findConfigRecentInfo(params); cache.configAllInfo = config; cache.lastModifiedTs = System.currentTimeMillis(); - NotifyCenter.publishEvent(new LocalDataChangeEvent(ip, groupKey)); + NotifyCenter.publishEvent(new LocalDataChangeEvent(identify, groupKey)); } } public synchronized static CacheItem makeSure(String groupKey, String ip) { - Map ipCacheItemMap = CACHE.get(groupKey); - CacheItem item = ipCacheItemMap.get(ip); - if (null != item) { + Map ipCacheItemMap = CLIENT_CONFIG_CACHE.get(groupKey); + CacheItem item; + if (ipCacheItemMap != null && (item = ipCacheItemMap.get(ip)) != null) { return item; } CacheItem tmp = new CacheItem(groupKey); Map cacheItemMap = Maps.newHashMap(); cacheItemMap.put(ip, tmp); - CACHE.putIfAbsent(groupKey, cacheItemMap); + CLIENT_CONFIG_CACHE.putIfAbsent(groupKey, cacheItemMap); return tmp; } public static Map getContent(String identification) { - List identificationList = MapUtil.parseMapForFilter(CACHE, identification); + List identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, identification); Map returnStrCacheItemMap = Maps.newHashMap(); - identificationList.forEach(each -> returnStrCacheItemMap.putAll(CACHE.get(each))); + identificationList.forEach(each -> returnStrCacheItemMap.putAll(CLIENT_CONFIG_CACHE.get(each))); return returnStrCacheItemMap; } + public static synchronized Integer getTotal() { + AtomicInteger total = new AtomicInteger(); + CLIENT_CONFIG_CACHE.forEach((key, val) -> total.addAndGet(val.values().size())); + return total.get(); + } + /** * Remove config cache. * @@ -132,10 +139,10 @@ public class ConfigCacheService { */ public synchronized static void removeConfigCache(String groupKey) { // 模糊搜索 - List identificationList = MapUtil.parseMapForFilter(CACHE, groupKey); + List identificationList = MapUtil.parseMapForFilter(CLIENT_CONFIG_CACHE, groupKey); for (String cacheMapKey : identificationList) { - Map removeCacheItem = CACHE.remove(cacheMapKey); - log.info("Remove invalidated config cache. config info :: {}", JSON.toJSONString(removeCacheItem)); + Map removeCacheItem = CLIENT_CONFIG_CACHE.remove(cacheMapKey); + log.info("Remove invalidated config cache. config info :: {}", JSONUtil.toJSONString(removeCacheItem)); } } diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java index 9ee579ec..2cd28608 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/LongPollingService.java @@ -1,21 +1,22 @@ package cn.hippo4j.config.service; +import cn.hippo4j.common.toolkit.JSONUtil; +import cn.hippo4j.common.toolkit.Md5Util; +import cn.hippo4j.common.web.base.Results; +import cn.hippo4j.config.event.Event; +import cn.hippo4j.config.event.LocalDataChangeEvent; import cn.hippo4j.config.notify.NotifyCenter; -import cn.hippo4j.config.toolkit.Md5ConfigUtil; -import cn.hutool.core.util.StrUtil; -import com.alibaba.fastjson.JSON; import cn.hippo4j.config.notify.listener.Subscriber; import cn.hippo4j.config.toolkit.ConfigExecutor; import cn.hippo4j.config.toolkit.MapUtil; +import cn.hippo4j.config.toolkit.Md5ConfigUtil; import cn.hippo4j.config.toolkit.RequestUtil; -import cn.hippo4j.common.toolkit.Md5Util; -import cn.hippo4j.common.web.base.Results; -import cn.hippo4j.config.event.Event; -import cn.hippo4j.config.event.LocalDataChangeEvent; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; import com.google.common.collect.Lists; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; import javax.servlet.AsyncContext; import javax.servlet.http.HttpServletRequest; @@ -113,8 +114,8 @@ public class LongPollingService { parseMapForFilter.forEach(each -> { if (clientSub.clientMd5Map.containsKey(each)) { - getRetainIps().put(clientSub.ip, System.currentTimeMillis()); - ConfigCacheService.updateMd5(each, clientSub.ip, ConfigCacheService.getContentMd5(identity)); + getRetainIps().put(clientSub.clientIdentify, System.currentTimeMillis()); + ConfigCacheService.updateMd5(each, clientSub.clientIdentify, ConfigCacheService.getContentMd5(each)); iter.remove(); clientSub.sendResponse(Arrays.asList(groupKey)); } @@ -126,18 +127,14 @@ public class LongPollingService { } } - public static boolean isSupportLongPolling(HttpServletRequest req) { - return null != req.getHeader(LONG_POLLING_HEADER); - } - - private static boolean isFixedPolling() { - return SwitchService.getSwitchBoolean(SwitchService.FIXED_POLLING, false); - } - - private static int getFixedPollingInterval() { - return SwitchService.getSwitchInteger(SwitchService.FIXED_POLLING_INTERVAL, FIXED_POLLING_INTERVAL_MS); - } - + /** + * Add long polling client. + * + * @param req + * @param rsp + * @param clientMd5Map + * @param probeRequestSize + */ public void addLongPollingClient(HttpServletRequest req, HttpServletResponse rsp, Map clientMd5Map, int probeRequestSize) { String str = req.getHeader(LONG_POLLING_HEADER); @@ -159,14 +156,17 @@ public class LongPollingService { } } - String ip = RequestUtil.getRemoteIp(req); + String clientIdentify = RequestUtil.getClientIdentify(req); final AsyncContext asyncContext = req.startAsync(); asyncContext.setTimeout(0L); - ConfigExecutor.executeLongPolling(new ClientLongPolling(asyncContext, clientMd5Map, ip, probeRequestSize, timeout - delayTime, appName)); + ConfigExecutor.executeLongPolling(new ClientLongPolling(asyncContext, clientMd5Map, clientIdentify, probeRequestSize, timeout - delayTime, appName)); } + /** + * Regularly check the configuration for changes. + */ class ClientLongPolling implements Runnable { final AsyncContext asyncContext; @@ -175,7 +175,7 @@ public class LongPollingService { final long createTime; - final String ip; + final String clientIdentify; final String appName; @@ -185,10 +185,10 @@ public class LongPollingService { Future asyncTimeoutFuture; - public ClientLongPolling(AsyncContext asyncContext, Map clientMd5Map, String ip, int probeRequestSize, long timeout, String appName) { + public ClientLongPolling(AsyncContext asyncContext, Map clientMd5Map, String clientIdentify, int probeRequestSize, long timeout, String appName) { this.asyncContext = asyncContext; this.clientMd5Map = clientMd5Map; - this.ip = ip; + this.clientIdentify = clientIdentify; this.probeRequestSize = probeRequestSize; this.timeoutTime = timeout; this.appName = appName; @@ -199,7 +199,7 @@ public class LongPollingService { public void run() { asyncTimeoutFuture = ConfigExecutor.scheduleLongPolling(() -> { try { - getRetainIps().put(ClientLongPolling.this.ip, System.currentTimeMillis()); + getRetainIps().put(ClientLongPolling.this.clientIdentify, System.currentTimeMillis()); allSubs.remove(ClientLongPolling.this); if (isFixedPolling()) { @@ -221,6 +221,11 @@ public class LongPollingService { allSubs.add(this); } + /** + * Send response. + * + * @param changedGroups Changed thread pool group key + */ private void sendResponse(List changedGroups) { // Cancel time out task. if (null != asyncTimeoutFuture) { @@ -229,6 +234,11 @@ public class LongPollingService { generateResponse(changedGroups); } + /** + * Generate async response. + * + * @param changedGroups Changed thread pool group key + */ private void generateResponse(List changedGroups) { if (null == changedGroups) { // Tell web container to send http response. @@ -239,17 +249,15 @@ public class LongPollingService { HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); try { - String respStr = Md5Util.compareMd5ResultString(changedGroups); - String resultStr = JSON.toJSONString(Results.success(respStr)); - + String respStr = buildRespStr(changedGroups); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache,no-store"); response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println(resultStr); - asyncContext.complete(); + response.getWriter().println(respStr); } catch (Exception ex) { - log.error(ex.toString(), ex); + log.error("Response client failed to return data.", ex); + } finally { asyncContext.complete(); } } @@ -260,20 +268,66 @@ public class LongPollingService { return retainIps; } + /** + * Generate sync response. + * + * @param response response + * @param changedGroups Changed thread pool group key + */ private void generateResponse(HttpServletResponse response, List changedGroups) { - if (!CollectionUtils.isEmpty(changedGroups)) { + if (CollUtil.isNotEmpty(changedGroups)) { try { - final String changedGroupKeStr = Md5ConfigUtil.compareMd5ResultString(changedGroups); - final String respString = JSON.toJSONString(Results.success(changedGroupKeStr)); + String respStr = buildRespStr(changedGroups); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); response.setHeader("Cache-Control", "no-cache,no-store"); response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println(respString); + response.getWriter().println(respStr); } catch (Exception ex) { - log.error(ex.toString(), ex); + log.error("Response client failed to return data.", ex); } } } + /** + * Build resp str. + * + * @param changedGroups Changed thread pool group key + * @return + */ + @SneakyThrows + private String buildRespStr(List changedGroups) { + String changedGroupStr = Md5Util.compareMd5ResultString(changedGroups); + String respStr = JSONUtil.toJSONString(Results.success(changedGroupStr)); + return respStr; + } + + /** + * Is support long polling. + * + * @param req + * @return + */ + public static boolean isSupportLongPolling(HttpServletRequest req) { + return null != req.getHeader(LONG_POLLING_HEADER); + } + + /** + * Is fixed polling. + * + * @return + */ + private static boolean isFixedPolling() { + return SwitchService.getSwitchBoolean(SwitchService.FIXED_POLLING, false); + } + + /** + * Get fixed polling interval. + * + * @return + */ + private static int getFixedPollingInterval() { + return SwitchService.getSwitchInteger(SwitchService.FIXED_POLLING_INTERVAL, FIXED_POLLING_INTERVAL_MS); + } + } diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/ConfigServiceImpl.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/ConfigServiceImpl.java index 91ef3091..cadf479d 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/ConfigServiceImpl.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/ConfigServiceImpl.java @@ -3,6 +3,7 @@ package cn.hippo4j.config.service.biz.impl; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.toolkit.ConditionUtil; import cn.hippo4j.common.toolkit.ContentUtil; +import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.toolkit.Md5Util; import cn.hippo4j.config.event.LocalDataChangeEvent; import cn.hippo4j.config.mapper.ConfigInfoMapper; @@ -15,7 +16,6 @@ import cn.hippo4j.config.service.biz.ConfigService; import cn.hippo4j.config.toolkit.BeanUtil; import cn.hippo4j.tools.logrecord.annotation.LogRecord; import cn.hutool.core.util.StrUtil; -import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; @@ -62,18 +62,22 @@ public class ConfigServiceImpl implements ConfigService { public ConfigAllInfo findConfigRecentInfo(String... params) { ConfigAllInfo resultConfig; ConfigAllInfo configInstance = null; - LambdaQueryWrapper instanceQueryWrapper = Wrappers.lambdaQuery(ConfigInstanceInfo.class) - .eq(ConfigInstanceInfo::getInstanceId, params[3]) - .orderByDesc(ConfigInstanceInfo::getGmtCreate) - .last("LIMIT 1"); - - ConfigInstanceInfo instanceInfo = configInstanceMapper.selectOne(instanceQueryWrapper); - if (instanceInfo != null) { - String content = instanceInfo.getContent(); - configInstance = JSON.parseObject(content, ConfigAllInfo.class); - configInstance.setContent(content); - configInstance.setGmtCreate(instanceInfo.getGmtCreate()); - configInstance.setMd5(Md5Util.getTpContentMd5(configInstance)); + + String instanceId = params[3]; + if (StrUtil.isNotBlank(instanceId)) { + LambdaQueryWrapper instanceQueryWrapper = Wrappers.lambdaQuery(ConfigInstanceInfo.class) + .eq(ConfigInstanceInfo::getInstanceId, params[3]) + .orderByDesc(ConfigInstanceInfo::getGmtCreate) + .last("LIMIT 1"); + + ConfigInstanceInfo instanceInfo = configInstanceMapper.selectOne(instanceQueryWrapper); + if (instanceInfo != null) { + String content = instanceInfo.getContent(); + configInstance = JSONUtil.parseObject(content, ConfigAllInfo.class); + configInstance.setContent(content); + configInstance.setGmtCreate(instanceInfo.getGmtCreate()); + configInstance.setMd5(Md5Util.getTpContentMd5(configInstance)); + } } ConfigAllInfo configAllInfo = findConfigAllInfo(params[0], params[1], params[2]); diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/Md5ConfigUtil.java b/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/Md5ConfigUtil.java index feef9fe7..b0d47329 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/Md5ConfigUtil.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/Md5ConfigUtil.java @@ -50,8 +50,8 @@ public class Md5ConfigUtil { public static List compareMd5(HttpServletRequest request, Map clientMd5Map) { List changedGroupKeys = new ArrayList(); clientMd5Map.forEach((key, val) -> { - String remoteIp = RequestUtil.getRemoteIp(request); - boolean isUpdateData = ConfigCacheService.isUpdateData(key, val, remoteIp); + String clientIdentify = RequestUtil.getClientIdentify(request); + boolean isUpdateData = ConfigCacheService.isUpdateData(key, val, clientIdentify); if (!isUpdateData) { changedGroupKeys.add(key); } diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/RequestUtil.java b/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/RequestUtil.java index cd4a4075..26b3b51b 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/RequestUtil.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/toolkit/RequestUtil.java @@ -1,6 +1,6 @@ package cn.hippo4j.config.toolkit; -import org.springframework.util.StringUtils; +import cn.hutool.core.util.StrUtil; import javax.servlet.http.HttpServletRequest; @@ -20,14 +20,9 @@ public class RequestUtil { private static final String X_FORWARDED_FOR_SPLIT_SYMBOL = ","; - public static String getRemoteIp(HttpServletRequest request) { - String xForwardedFor = request.getHeader(X_FORWARDED_FOR); - if (!StringUtils.isEmpty(xForwardedFor)) { - return xForwardedFor.split(X_FORWARDED_FOR_SPLIT_SYMBOL)[0].trim(); - } - String nginxHeader = request.getHeader(X_REAL_IP); - String ipPort = request.getHeader(LONG_PULLING_CLIENT_IDENTIFICATION); - return StringUtils.isEmpty(nginxHeader) ? ipPort : nginxHeader; + public static String getClientIdentify(HttpServletRequest request) { + String identify = request.getHeader(LONG_PULLING_CLIENT_IDENTIFICATION); + return StrUtil.isBlank(identify) ? "" : identify; } } diff --git a/hippo4j-spring-boot-starter/pom.xml b/hippo4j-spring-boot-starter/pom.xml index de597534..fea37620 100644 --- a/hippo4j-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot-starter/pom.xml @@ -36,11 +36,6 @@ logging-interceptor - - com.alibaba - fastjson - - cn.hippo4j hippo4j-common diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java index 47e6db85..39be88cc 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/BaseSendMessageService.java @@ -3,6 +3,7 @@ package cn.hippo4j.starter.alarm; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.model.PoolParameterInfo; import cn.hippo4j.common.toolkit.GroupKey; +import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.starter.config.BootstrapProperties; import cn.hippo4j.starter.core.DynamicThreadPoolExecutor; @@ -10,7 +11,6 @@ import cn.hippo4j.starter.core.GlobalThreadPoolManage; import cn.hippo4j.starter.remote.HttpAgent; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.StrUtil; -import com.alibaba.fastjson.JSON; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import com.google.common.collect.Lists; @@ -125,8 +125,8 @@ public class BaseSendMessageService implements InitializingBean, SendMessageServ } if (result != null && result.isSuccess() && result.getData() != null) { - String resultDataStr = JSON.toJSONString(result.getData()); - List resultData = JSON.parseArray(resultDataStr, ThreadPoolNotify.class); + String resultDataStr = JSONUtil.toJSONString(result.getData()); + List resultData = JSONUtil.parseArray(resultDataStr, ThreadPoolNotify.class); resultData.forEach(each -> ALARM_NOTIFY_CONFIG.put(each.getNotifyKey(), each.getNotifyList())); ALARM_NOTIFY_CONFIG.forEach((key, val) -> diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/wechat/WeChatSendMessageHandler.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/wechat/WeChatSendMessageHandler.java index 148e128c..0009d5b6 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/wechat/WeChatSendMessageHandler.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/alarm/wechat/WeChatSendMessageHandler.java @@ -2,6 +2,7 @@ package cn.hippo4j.starter.alarm.wechat; import cn.hippo4j.common.model.InstanceInfo; import cn.hippo4j.common.model.PoolParameterInfo; +import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.starter.alarm.NotifyDTO; import cn.hippo4j.starter.alarm.NotifyPlatformEnum; import cn.hippo4j.starter.alarm.SendMessageHandler; @@ -13,7 +14,6 @@ import cn.hippo4j.starter.wrapper.DynamicThreadPoolWrapper; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.http.HttpRequest; -import com.alibaba.fastjson.JSON; import com.google.common.base.Joiner; import lombok.AllArgsConstructor; import lombok.Data; @@ -155,7 +155,7 @@ public class WeChatSendMessageHandler implements SendMessageHandler { markdown.setContent(text); weChatReq.setMarkdown(markdown); - HttpRequest.post(serverUrl).body(JSON.toJSONString(weChatReq)).execute(); + HttpRequest.post(serverUrl).body(JSONUtil.toJSONString(weChatReq)).execute(); } catch (Exception ex) { log.error("WeChat failed to send message", ex); } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java index 88c8812a..02565f46 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -1,5 +1,7 @@ package cn.hippo4j.starter.config; +import cn.hippo4j.common.api.JsonFacade; +import cn.hippo4j.common.api.impl.JacksonHandler; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.starter.controller.PoolRunStateController; import cn.hippo4j.starter.core.ConfigService; @@ -105,6 +107,11 @@ public class DynamicThreadPoolAutoConfiguration { return new HttpScheduledHealthCheck(httpAgent); } + @Bean + public JsonFacade jacksonHandler() { + return new JacksonHandler(); + } + } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ClientWorker.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ClientWorker.java index fbe3a40c..ca482539 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ClientWorker.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ClientWorker.java @@ -3,12 +3,12 @@ package cn.hippo4j.starter.core; import cn.hippo4j.common.model.PoolParameterInfo; import cn.hippo4j.common.toolkit.ContentUtil; import cn.hippo4j.common.toolkit.GroupKey; +import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.starter.remote.HttpAgent; import cn.hippo4j.starter.remote.ServerHealthCheck; import cn.hippo4j.starter.toolkit.thread.ThreadFactoryBuilder; import cn.hutool.core.util.StrUtil; -import com.alibaba.fastjson.JSON; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.util.StringUtils; @@ -110,7 +110,7 @@ public class ClientWorker { try { String content = getServerConfig(namespace, itemId, tpId, 3000L); CacheData cacheData = cacheMap.get(tpId); - String poolContent = ContentUtil.getPoolContent(JSON.parseObject(content, PoolParameterInfo.class)); + String poolContent = ContentUtil.getPoolContent(JSONUtil.parseObject(content, PoolParameterInfo.class)); cacheData.setContent(poolContent); } catch (Exception ex) { // ignore @@ -191,7 +191,7 @@ public class ClientWorker { Result result = agent.httpGetByConfig(CONFIG_CONTROLLER_PATH, null, params, readTimeout); if (result.isSuccess()) { - return result.getData().toString(); + return JSONUtil.toJSONString(result.getData()); } log.error("[Sub server] namespace :: {}, itemId :: {}, tpId :: {}, result code :: {}", @@ -217,18 +217,16 @@ public class ClientWorker { String[] keyArr = dataIdAndGroup.split(WORD_SEPARATOR); String dataId = keyArr[0]; String group = keyArr[1]; - if (keyArr.length == 2) { - updateList.add(GroupKey.getKey(dataId, group)); - log.info("[{}] [Polling resp] config changed. dataId={}, group={}", dataId, group); - } else if (keyArr.length == 3) { + if (keyArr.length == 3) { String tenant = keyArr[2]; updateList.add(GroupKey.getKeyTenant(dataId, group, tenant)); - log.info("[Polling resp] config changed. dataId={}, group={}, tenant={}", dataId, group, tenant); + log.info("Refresh thread pool changed. [{}]", dataId); } else { - log.error("[{}] [Polling resp] invalid dataIdAndGroup error {}", dataIdAndGroup); + log.error("[{}] [Polling resp] invalid dataIdAndGroup error.", dataIdAndGroup); } } } + return updateList; } @@ -251,7 +249,7 @@ public class ClientWorker { String serverConfig = null; try { serverConfig = getServerConfig(namespace, itemId, tpId, 3000L); - PoolParameterInfo poolInfo = JSON.parseObject(serverConfig, PoolParameterInfo.class); + PoolParameterInfo poolInfo = JSONUtil.parseObject(serverConfig, PoolParameterInfo.class); cacheData.setContent(ContentUtil.getPoolContent(poolInfo)); } catch (Exception ex) { log.error("[Cache Data] Error. Service Unavailable :: {}", ex.getMessage()); diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicThreadPoolPostProcessor.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicThreadPoolPostProcessor.java index 095130f3..455c9c8e 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicThreadPoolPostProcessor.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/DynamicThreadPoolPostProcessor.java @@ -3,6 +3,7 @@ package cn.hippo4j.starter.core; import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.constant.Constants; import cn.hippo4j.common.model.PoolParameterInfo; +import cn.hippo4j.common.toolkit.JSONUtil; import cn.hippo4j.common.web.base.Result; import cn.hippo4j.starter.common.CommonDynamicThreadPool; import cn.hippo4j.starter.config.BootstrapProperties; @@ -11,7 +12,6 @@ import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum; import cn.hippo4j.starter.toolkit.thread.RejectedTypeEnum; import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder; import cn.hippo4j.starter.wrapper.DynamicThreadPoolWrapper; -import com.alibaba.fastjson.JSON; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.var; @@ -108,26 +108,29 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor { try { result = httpAgent.httpGetByConfig(Constants.CONFIG_CONTROLLER_PATH, null, queryStrMap, 5000L); - if (result.isSuccess() && result.getData() != null && (ppi = JSON.toJavaObject((JSON) result.getData(), PoolParameterInfo.class)) != null) { - // 使用相关参数创建线程池 - BlockingQueue workQueue = QueueTypeEnum.createBlockingQueue(ppi.getQueueType(), ppi.getCapacity()); - poolExecutor = ThreadPoolBuilder.builder() - .dynamicPool() - .workQueue(workQueue) - .threadFactory(tpId) - .poolThreadSize(ppi.getCoreSize(), ppi.getMaxSize()) - .keepAliveTime(ppi.getKeepAliveTime(), TimeUnit.SECONDS) - .rejected(RejectedTypeEnum.createPolicy(ppi.getRejectedType())) - .alarmConfig(ppi.getIsAlarm(), ppi.getCapacityAlarm(), ppi.getLivenessAlarm()) - .build(); - - if (poolExecutor instanceof DynamicExecutorConfigurationSupport) { - TaskDecorator taskDecorator = ((DynamicThreadPoolExecutor) dynamicThreadPoolWrap.getExecutor()).getTaskDecorator(); - ((DynamicThreadPoolExecutor) poolExecutor).setTaskDecorator(taskDecorator); + if (result.isSuccess() && result.getData() != null) { + String resultJsonStr = JSONUtil.toJSONString(result.getData()); + if ((ppi = JSONUtil.parseObject(resultJsonStr, PoolParameterInfo.class)) != null) { + // 使用相关参数创建线程池 + BlockingQueue workQueue = QueueTypeEnum.createBlockingQueue(ppi.getQueueType(), ppi.getCapacity()); + poolExecutor = ThreadPoolBuilder.builder() + .dynamicPool() + .workQueue(workQueue) + .threadFactory(tpId) + .poolThreadSize(ppi.getCoreSize(), ppi.getMaxSize()) + .keepAliveTime(ppi.getKeepAliveTime(), TimeUnit.SECONDS) + .rejected(RejectedTypeEnum.createPolicy(ppi.getRejectedType())) + .alarmConfig(ppi.getIsAlarm(), ppi.getCapacityAlarm(), ppi.getLivenessAlarm()) + .build(); + + if (poolExecutor instanceof DynamicExecutorConfigurationSupport) { + TaskDecorator taskDecorator = ((DynamicThreadPoolExecutor) dynamicThreadPoolWrap.getExecutor()).getTaskDecorator(); + ((DynamicThreadPoolExecutor) poolExecutor).setTaskDecorator(taskDecorator); + } + + dynamicThreadPoolWrap.setExecutor(poolExecutor); + isSubscribe = true; } - - dynamicThreadPoolWrap.setExecutor(poolExecutor); - isSubscribe = true; } } catch (Exception ex) { poolExecutor = dynamicThreadPoolWrap.getExecutor() != null ? dynamicThreadPoolWrap.getExecutor() : CommonDynamicThreadPool.getInstance(tpId); diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ThreadPoolDynamicRefresh.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ThreadPoolDynamicRefresh.java index f0835491..40911f78 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ThreadPoolDynamicRefresh.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/core/ThreadPoolDynamicRefresh.java @@ -1,11 +1,11 @@ package cn.hippo4j.starter.core; +import cn.hippo4j.common.model.PoolParameterInfo; +import cn.hippo4j.common.toolkit.JSONUtil; +import cn.hippo4j.starter.alarm.ThreadPoolAlarmManage; import cn.hippo4j.starter.toolkit.thread.QueueTypeEnum; import cn.hippo4j.starter.toolkit.thread.RejectedTypeEnum; import cn.hippo4j.starter.toolkit.thread.ResizableCapacityLinkedBlockIngQueue; -import com.alibaba.fastjson.JSON; -import cn.hippo4j.common.model.PoolParameterInfo; -import cn.hippo4j.starter.alarm.ThreadPoolAlarmManage; import lombok.extern.slf4j.Slf4j; import java.util.Objects; @@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit; public class ThreadPoolDynamicRefresh { public static void refreshDynamicPool(String content) { - PoolParameterInfo parameter = JSON.parseObject(content, PoolParameterInfo.class); + PoolParameterInfo parameter = JSONUtil.parseObject(content, PoolParameterInfo.class); ThreadPoolAlarmManage.sendPoolConfigChange(parameter); ThreadPoolDynamicRefresh.refreshDynamicPool(parameter); } diff --git a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java index 31c9d225..c5218472 100644 --- a/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java +++ b/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/toolkit/HttpClientUtil.java @@ -1,6 +1,6 @@ package cn.hippo4j.starter.toolkit; -import com.alibaba.fastjson.JSON; +import cn.hippo4j.common.toolkit.JSONUtil; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import okhttp3.*; @@ -63,7 +63,7 @@ public class HttpClientUtil { */ public T restApiGet(String url, Class clazz) { String resp = get(url); - return JSON.parseObject(resp, clazz); + return JSONUtil.parseObject(resp, clazz); } /** @@ -77,7 +77,7 @@ public class HttpClientUtil { @SneakyThrows public T restApiGetHealth(String url, Class clazz) { String resp = new String(doGet(url), "utf-8"); - return JSON.parseObject(resp, clazz); + return JSONUtil.parseObject(resp, clazz); } /** @@ -92,7 +92,7 @@ public class HttpClientUtil { public T restApiGet(String url, Map queryString, Class clazz) { String fullUrl = buildUrl(url, queryString); String resp = get(fullUrl); - return JSON.parseObject(resp, clazz); + return JSONUtil.parseObject(resp, clazz); } /** @@ -121,7 +121,7 @@ public class HttpClientUtil { */ public T restApiPost(String url, Object body, Class clazz) { String resp = restApiPost(url, body); - return JSON.parseObject(resp, clazz); + return JSONUtil.parseObject(resp, clazz); } /** @@ -159,7 +159,7 @@ public class HttpClientUtil { @SneakyThrows private String doPost(String url, Object body) { - String jsonBody = JSON.toJSONString(body); + String jsonBody = JSONUtil.toJSONString(body); RequestBody requestBody = RequestBody.create(jsonMediaType, jsonBody); Request request = new Request.Builder() .url(url) @@ -205,7 +205,7 @@ public class HttpClientUtil { throw new RuntimeException(msg); } - return JSON.parseObject(resp.body().string(), clazz); + return JSONUtil.parseObject(resp.body().string(), clazz); } @SneakyThrows @@ -227,7 +227,7 @@ public class HttpClientUtil { log.error(msg); throw new RuntimeException(msg); } - return JSON.parseObject(resp.body().string(), clazz); + return JSONUtil.parseObject(resp.body().string(), clazz); } } diff --git a/pom.xml b/pom.xml index 7bedca0b..0cda7340 100644 --- a/pom.xml +++ b/pom.xml @@ -26,7 +26,7 @@ 1.8 - 1.0.0-RC3 + 1.0.0-SNAPSHOT 6.5.0 29.0-jre @@ -38,6 +38,7 @@ 2.12.1 1.5.11 0.9.0 + 2.11.1 1.0.1 @@ -195,6 +196,13 @@ logback-classic ${logback.version} + + + org.springframework.boot + spring-boot-starter-json + ${spring-boot.version} + compile +