From ec12a145549f5e064a7f5329cc348062a0e2b20b Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Wed, 10 Aug 2022 20:21:22 +0800 Subject: [PATCH] hippo4j server add dynamic registration notification alarm --- .../DynamicThreadPoolRegisterWrapper.java | 20 ++++++++---- ...readPoolRegisterServerNotifyParameter.java | 10 ------ .../config/model/biz/notify/NotifyReqDTO.java | 7 ++++ .../config/service/biz/NotifyService.java | 7 ++++ .../service/biz/impl/ConfigServiceImpl.java | 32 ++++++++++++++++--- .../service/biz/impl/NotifyServiceImpl.java | 17 ++++++++++ .../RegisterDynamicThreadPoolTest.java | 4 +-- 7 files changed, 73 insertions(+), 24 deletions(-) diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/DynamicThreadPoolRegisterWrapper.java b/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/DynamicThreadPoolRegisterWrapper.java index 421302aa..1df939c0 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/DynamicThreadPoolRegisterWrapper.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/DynamicThreadPoolRegisterWrapper.java @@ -33,6 +33,15 @@ import lombok.NoArgsConstructor; @AllArgsConstructor public class DynamicThreadPoolRegisterWrapper { + // Hippo4j common ---------------------------------------------------------------------- + + /** + * Dynamic thread-pool register parameter + */ + private DynamicThreadPoolRegisterParameter dynamicThreadPoolRegisterParameter; + + // Hippo4j server ---------------------------------------------------------------------- + /** * Tenant id */ @@ -49,17 +58,14 @@ public class DynamicThreadPoolRegisterWrapper { private Boolean updateIfExists = Boolean.TRUE; /** - * Dynamic thread-pool register parameter + * Dynamic thread-pool server notify parameter */ - private DynamicThreadPoolRegisterParameter dynamicThreadPoolRegisterParameter; + private DynamicThreadPoolRegisterServerNotifyParameter dynamicThreadPoolRegisterServerNotifyParameter; + + // Hippo4j core ---------------------------------------------------------------------- /** * Dynamic thread-pool core notify parameter */ private DynamicThreadPoolRegisterCoreNotifyParameter dynamicThreadPoolRegisterCoreNotifyParameter; - - /** - * Dynamic thread-pool server notify parameter - */ - private DynamicThreadPoolRegisterServerNotifyParameter dynamicThreadPoolRegisterServerNotifyParameter; } diff --git a/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/notify/DynamicThreadPoolRegisterServerNotifyParameter.java b/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/notify/DynamicThreadPoolRegisterServerNotifyParameter.java index 33d0971e..28644195 100644 --- a/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/notify/DynamicThreadPoolRegisterServerNotifyParameter.java +++ b/hippo4j-common/src/main/java/cn/hippo4j/common/model/register/notify/DynamicThreadPoolRegisterServerNotifyParameter.java @@ -31,21 +31,11 @@ import lombok.NoArgsConstructor; @AllArgsConstructor public class DynamicThreadPoolRegisterServerNotifyParameter { - /** - * Thread-pool id - */ - private String threadPoolId; - /** * Platform */ private String platform; - /** - * Config type - */ - private String type; - /** * Secret key */ diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/notify/NotifyReqDTO.java b/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/notify/NotifyReqDTO.java index 3c5a41b4..23f9c89d 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/notify/NotifyReqDTO.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/model/biz/notify/NotifyReqDTO.java @@ -18,6 +18,7 @@ package cn.hippo4j.config.model.biz.notify; import lombok.Data; +import lombok.experimental.Accessors; /** * Notify req DTO. @@ -26,6 +27,7 @@ import lombok.Data; * @date 2021/11/18 20:15 */ @Data +@Accessors(chain = true) public class NotifyReqDTO { /** @@ -58,6 +60,11 @@ public class NotifyReqDTO { */ private String platform; + /** + * Type + */ + private String type; + /** * Config type */ diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/NotifyService.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/NotifyService.java index bfc764be..a0bfc412 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/NotifyService.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/NotifyService.java @@ -60,6 +60,13 @@ public interface NotifyService { */ void update(NotifyReqDTO reqDTO); + /** + * Save or update. + * + * @param reqDTO + */ + void saveOrUpdate(NotifyReqDTO reqDTO); + /** * Delete. * 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 02e521d8..d92ce741 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 @@ -21,6 +21,7 @@ import cn.hippo4j.common.config.ApplicationContextHolder; import cn.hippo4j.common.enums.DelEnum; import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterParameter; import cn.hippo4j.common.model.register.DynamicThreadPoolRegisterWrapper; +import cn.hippo4j.common.model.register.notify.DynamicThreadPoolRegisterServerNotifyParameter; import cn.hippo4j.common.toolkit.*; import cn.hippo4j.common.web.exception.ServiceException; import cn.hippo4j.config.event.LocalDataChangeEvent; @@ -30,18 +31,17 @@ import cn.hippo4j.config.model.ConfigAllInfo; import cn.hippo4j.config.model.ConfigInfoBase; import cn.hippo4j.config.model.ConfigInstanceInfo; import cn.hippo4j.config.model.LogRecordInfo; +import cn.hippo4j.config.model.biz.notify.NotifyReqDTO; import cn.hippo4j.config.service.ConfigCacheService; import cn.hippo4j.config.service.ConfigChangePublisher; -import cn.hippo4j.config.service.biz.ConfigService; -import cn.hippo4j.config.service.biz.ItemService; -import cn.hippo4j.config.service.biz.OperationLogService; -import cn.hippo4j.config.service.biz.TenantService; +import cn.hippo4j.config.service.biz.*; import cn.hippo4j.config.toolkit.BeanUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; +import com.google.common.collect.Lists; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -68,6 +68,8 @@ public class ConfigServiceImpl implements ConfigService { private final OperationLogService operationLogService; + private final NotifyService notifyService; + @Override public ConfigAllInfo findConfigAllInfo(String tpId, String itemId, String tenantId) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(ConfigAllInfo.class) @@ -151,6 +153,28 @@ public class ConfigServiceImpl implements ConfigService { ConfigServiceImpl configService = ApplicationContextHolder.getBean(this.getClass()); configService.updateConfigInfo(null, false, configAllInfo); } + DynamicThreadPoolRegisterServerNotifyParameter serverNotifyParameter = registerWrapper.getDynamicThreadPoolRegisterServerNotifyParameter(); + if (serverNotifyParameter != null) { + ArrayList notifyTypes = Lists.newArrayList("CONFIG", "ALARM"); + notifyTypes.forEach(each -> { + NotifyReqDTO notifyReqDTO = new NotifyReqDTO(); + notifyReqDTO.setType(each) + .setEnable(1) + .setTenantId(registerWrapper.getTenantId()) + .setItemId(registerWrapper.getItemId()) + .setTpId(configAllInfo.getTpId()) + .setPlatform(serverNotifyParameter.getPlatform()) + .setReceives(serverNotifyParameter.getReceives()) + .setSecretKey(serverNotifyParameter.getSecretKey()); + if (Objects.equals(each, "ALARM")) { + notifyReqDTO.setInterval(serverNotifyParameter.getInterval()); + notifyReqDTO.setAlarmType(true); + } else { + notifyReqDTO.setConfigType(true); + } + notifyService.saveOrUpdate(notifyReqDTO); + }); + } } private ConfigAllInfo parseConfigAllInfo(DynamicThreadPoolRegisterWrapper registerWrapper) { diff --git a/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/NotifyServiceImpl.java b/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/NotifyServiceImpl.java index b555e13b..35c886ee 100644 --- a/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/NotifyServiceImpl.java +++ b/hippo4j-config/src/main/java/cn/hippo4j/config/service/biz/impl/NotifyServiceImpl.java @@ -119,6 +119,23 @@ public class NotifyServiceImpl implements NotifyService { } } + @Override + public void saveOrUpdate(NotifyReqDTO reqDTO) { + try { + existNotify(reqDTO.getType(), reqDTO); + save(reqDTO); + } catch (Exception ignored) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(NotifyInfo.class) + .eq(NotifyInfo::getTenantId, reqDTO.getTenantId()) + .eq(NotifyInfo::getItemId, reqDTO.getItemId()) + .eq(NotifyInfo::getTpId, reqDTO.getTpId()) + .eq(NotifyInfo::getPlatform, reqDTO.getPlatform()) + .eq(NotifyInfo::getType, reqDTO.getType()); + List notifyInfos = notifyInfoMapper.selectList(queryWrapper); + notifyInfos.forEach(each -> update(reqDTO.setId(String.valueOf(each.getId())))); + } + } + @Override public void delete(NotifyReqDTO reqDTO) { LambdaUpdateWrapper updateWrapper = Wrappers.lambdaUpdate(NotifyInfo.class) diff --git a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java index fe7bcb8a..18fd7b5c 100644 --- a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java +++ b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/inittest/RegisterDynamicThreadPoolTest.java @@ -53,8 +53,7 @@ public class RegisterDynamicThreadPoolTest { parameterInfo.setCapacityAlarm(90); parameterInfo.setActiveAlarm(90); parameterInfo.setAllowCoreThreadTimeOut(Boolean.TRUE); - - // core 模式和 server 模式,各选其一即可 + // Core mode and server mode, you can choose one of them. DynamicThreadPoolRegisterCoreNotifyParameter coreNotifyParameter = DynamicThreadPoolRegisterCoreNotifyParameter.builder() .activeAlarm(80) .capacityAlarm(80) @@ -65,7 +64,6 @@ public class RegisterDynamicThreadPoolTest { DynamicThreadPoolRegisterServerNotifyParameter serverNotifyParameter = DynamicThreadPoolRegisterServerNotifyParameter.builder() .platform(NotifyPlatformEnum.WECHAT.name()) .secretKey("xxx") - .threadPoolId(threadPoolId) .interval(5) .receives("chen.ma") .build();