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 59ff73b..e4f0d24 100644 --- a/austin-support/pom.xml +++ b/austin-support/pom.xml @@ -63,6 +63,23 @@ spring-boot-starter-data-redis + + com.ctrip.framework.apollo + apollo-client-config-data + + + + + org.springframework.boot + spring-boot-starter-actuator + + + io.micrometer + micrometer-registry-prometheus + runtime + + + \ 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 f8b24cd..5532f54 100644 --- a/austin-web/src/main/resources/application.yml +++ b/austin-web/src/main/resources/application.yml @@ -44,9 +44,36 @@ spring: port: password: -# tomcat / HikariPool(数据库连接池 配置) TODO # 消息topicName TODO austin: topic: - name: austin \ No newline at end of file + name: austin + +# 监控配置 TODO +management: + endpoint: + health: + show-details: always + metrics: + enabled: true + prometheus: + enabled: true + endpoints: + web: + exposure: + include: '*' + metrics: + export: + 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 990dc0a..3c6651a 100644 --- a/pom.xml +++ b/pom.xml @@ -74,6 +74,13 @@ tencentcloud-sdk-java 3.1.390 + + + + com.ctrip.framework.apollo + apollo-client-config-data + 1.9.1 +