From c856ab4eb55ceca923623cbbe491b9cb915d591d Mon Sep 17 00:00:00 2001 From: 3y Date: Sat, 25 Dec 2021 11:06:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=BC=95=E5=85=A5SpringBoot=20=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=EF=BC=8C=E6=9A=B4=E9=9C=B2=E7=BB=99prometheus=202.=20?= =?UTF-8?q?=E5=BC=95=E5=85=A5apollo=203.=20=E5=AE=9E=E7=8E=B0=E4=B8=A2?= =?UTF-8?q?=E5=BC=83=E6=B6=88=E6=81=AF=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=8E=BB?= =?UTF-8?q?=E9=87=8D=E7=9B=B8=E5=85=B3=E9=85=8D=E7=BD=AE=E4=BB=8Eapollo?= =?UTF-8?q?=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../austin/constant/AustinConstant.java | 9 +++++ .../java/com/java3y/austin/pending/Task.java | 12 +++++-- .../DeduplicationRuleService.java | 26 ++++++++++++-- .../discard/DiscardMessageService.java | 36 +++++++++++++++++++ austin-support/pom.xml | 10 +++--- .../com/java3y/austin/AustinApplication.java | 3 ++ .../austin/controller/ApolloController.java | 18 ++++++++++ austin-web/src/main/resources/application.yml | 11 ++++-- pom.xml | 4 +-- 9 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java create mode 100644 austin-web/src/main/java/com/java3y/austin/controller/ApolloController.java diff --git a/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java b/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java index 1631b38..cfff2e8 100644 --- a/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java +++ b/austin-common/src/main/java/com/java3y/austin/constant/AustinConstant.java @@ -19,4 +19,13 @@ public class AustinConstant { */ public final static String YYYYMMDD = "yyyyMMdd"; + + /** + * apollo默认的值 + */ + public final static String APOLLO_DEFAULT_VALUE_JSON_OBJECT = "{}"; + public final static String APOLLO_DEFAULT_VALUE_JSON_ARRAY = "[]"; + + + } diff --git a/austin-handler/src/main/java/com/java3y/austin/pending/Task.java b/austin-handler/src/main/java/com/java3y/austin/pending/Task.java index dff270a..87941af 100644 --- a/austin-handler/src/main/java/com/java3y/austin/pending/Task.java +++ b/austin-handler/src/main/java/com/java3y/austin/pending/Task.java @@ -5,6 +5,7 @@ import cn.hutool.core.collection.CollUtil; import com.java3y.austin.domain.TaskInfo; import com.java3y.austin.handler.HandlerHolder; import com.java3y.austin.service.deduplication.DeduplicationRuleService; +import com.java3y.austin.service.discard.DiscardMessageService; import lombok.Data; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; @@ -29,16 +30,23 @@ public class Task implements Runnable { @Autowired private DeduplicationRuleService deduplicationRuleService; + @Autowired + private DiscardMessageService discardMessageService; + private TaskInfo taskInfo; + @Override public void run() { - // 0. TODO 丢弃消息 + + // 0. 丢弃消息 + if (discardMessageService.isDiscard(taskInfo)) { + return; + } // 1.平台通用去重 deduplicationRuleService.duplication(taskInfo); - // 2. 真正发送消息 if (CollUtil.isNotEmpty(taskInfo.getReceiver())) { handlerHolder.route(taskInfo.getSendChannel()) diff --git a/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java b/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java index c302e83..18ac54b 100644 --- a/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java +++ b/austin-handler/src/main/java/com/java3y/austin/service/deduplication/DeduplicationRuleService.java @@ -1,6 +1,11 @@ package com.java3y.austin.service.deduplication; import cn.hutool.core.date.DateUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; +import com.java3y.austin.constant.AustinConstant; import com.java3y.austin.domain.DeduplicationParam; import com.java3y.austin.domain.TaskInfo; import org.springframework.beans.factory.annotation.Autowired; @@ -16,25 +21,42 @@ import java.util.Date; @Service public class DeduplicationRuleService { + /** + * 配置样例:{"contentDeduplication":{"num":1,"time":300},"frequencyDeduplication":{"num":5}} + */ + private static final String DEDUPLICATION_RULE_KEY = "deduplication"; + private static final String CONTENT_DEDUPLICATION = "contentDeduplication"; + private static final String FREQUENCY_DEDUPLICATION = "frequencyDeduplication"; + private static final String TIME = "time"; + private static final String NUM = "num"; + @Autowired private ContentDeduplicationService contentDeduplicationService; @Autowired private FrequencyDeduplicationService frequencyDeduplicationService; + @ApolloConfig("boss.austin") + private Config config; public void duplication(TaskInfo taskInfo) { + JSONObject property = JSON.parseObject(config.getProperty(DEDUPLICATION_RULE_KEY, AustinConstant.APOLLO_DEFAULT_VALUE_JSON_OBJECT)); + JSONObject contentDeduplication = property.getJSONObject(CONTENT_DEDUPLICATION); + JSONObject frequencyDeduplication = property.getJSONObject(FREQUENCY_DEDUPLICATION); // 文案去重 DeduplicationParam contentParams = DeduplicationParam.builder() - .deduplicationTime(300L).countNum(1).taskInfo(taskInfo) + .deduplicationTime(contentDeduplication.getLong(TIME)) + .countNum(contentDeduplication.getInteger(NUM)).taskInfo(taskInfo) .build(); contentDeduplicationService.deduplication(contentParams); + // 运营总规则去重(一天内用户收到最多同一个渠道的消息次数) Long seconds = (DateUtil.endOfDay(new Date()).getTime() - DateUtil.current()) / 1000; DeduplicationParam businessParams = DeduplicationParam.builder() - .deduplicationTime(seconds).countNum(5).taskInfo(taskInfo) + .deduplicationTime(seconds) + .countNum(frequencyDeduplication.getInteger(NUM)).taskInfo(taskInfo) .build(); frequencyDeduplicationService.deduplication(businessParams); } diff --git a/austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java b/austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java new file mode 100644 index 0000000..876b8fd --- /dev/null +++ b/austin-handler/src/main/java/com/java3y/austin/service/discard/DiscardMessageService.java @@ -0,0 +1,36 @@ +package com.java3y.austin.service.discard; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; +import com.java3y.austin.constant.AustinConstant; +import com.java3y.austin.domain.TaskInfo; +import org.springframework.stereotype.Service; + +/** + * 丢弃模板消息 + * @author 3y + */ +@Service +public class DiscardMessageService { + private static final String DISCARD_MESSAGE_KEY = "discard"; + + @ApolloConfig("boss.austin") + private Config config; + + /** + * 丢弃消息,配置在apollo + * @param taskInfo + * @return + */ + public boolean isDiscard(TaskInfo taskInfo) { + JSONArray array = JSON.parseArray(config.getProperty(DISCARD_MESSAGE_KEY, + AustinConstant.APOLLO_DEFAULT_VALUE_JSON_ARRAY)); + if (array.contains(String.valueOf(taskInfo.getMessageTemplateId()))) { + return true; + } + return false; + } + +} diff --git a/austin-support/pom.xml b/austin-support/pom.xml index fc2a758..e4f0d24 100644 --- a/austin-support/pom.xml +++ b/austin-support/pom.xml @@ -63,6 +63,10 @@ spring-boot-starter-data-redis + + com.ctrip.framework.apollo + apollo-client-config-data + @@ -76,12 +80,6 @@ - - com.ctrip.framework.apollo - apollo-client - - - \ No newline at end of file diff --git a/austin-web/src/main/java/com/java3y/austin/AustinApplication.java b/austin-web/src/main/java/com/java3y/austin/AustinApplication.java index dc2abd0..3a4a784 100644 --- a/austin-web/src/main/java/com/java3y/austin/AustinApplication.java +++ b/austin-web/src/main/java/com/java3y/austin/AustinApplication.java @@ -10,6 +10,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class AustinApplication { public static void main(String[] args) { + // TODO apollo的地址 + //System.setProperty("apollo.config-service", "http://ip:7000"); + SpringApplication.run(AustinApplication.class, args); } } diff --git a/austin-web/src/main/java/com/java3y/austin/controller/ApolloController.java b/austin-web/src/main/java/com/java3y/austin/controller/ApolloController.java new file mode 100644 index 0000000..a54d2f5 --- /dev/null +++ b/austin-web/src/main/java/com/java3y/austin/controller/ApolloController.java @@ -0,0 +1,18 @@ +package com.java3y.austin.controller; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.spring.annotation.ApolloConfig; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ApolloController { + + @ApolloConfig("boss.austin") + private Config config; + + @RequestMapping("/apollo") + public String testApollo() { + return config.getProperty("a", "b"); + } +} diff --git a/austin-web/src/main/resources/application.yml b/austin-web/src/main/resources/application.yml index 8dcf29b..5532f54 100644 --- a/austin-web/src/main/resources/application.yml +++ b/austin-web/src/main/resources/application.yml @@ -45,8 +45,6 @@ spring: password: -# tomcat / HikariPool(数据库连接池 配置) TODO - # 消息topicName TODO austin: topic: @@ -70,3 +68,12 @@ management: prometheus: enabled: true +# apollo TODO +app: + id: austin +apollo: + bootstrap: + enabled: true + namespaces: boss.austin + +# tomcat / HikariPool(数据库连接池 配置) TODO \ No newline at end of file diff --git a/pom.xml b/pom.xml index 854be52..3c6651a 100644 --- a/pom.xml +++ b/pom.xml @@ -78,11 +78,9 @@ com.ctrip.framework.apollo - apollo-client + apollo-client-config-data 1.9.1 - -