From 3859e77c6c5827e544245a0ab84434280ba5230a Mon Sep 17 00:00:00 2001 From: qyx <565485304@qq.com> Date: Fri, 7 Aug 2020 12:38:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E5=86=99=E4=BA=86=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../openapi/openapi/OpenapiApplication.java | 2 + .../openapi/model/dto/ABMessageDTO.java | 4 + .../model/dto/convert/ABMessageCovDTO.java | 40 +++++++ .../service/impl/ABClientServiceImpl.java | 17 +++ .../openapi/openapi/task/ABDownTask.java | 100 ++++++++++++++++++ src/main/resources/application.yml | 12 ++- 7 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/yuyou/openapi/openapi/model/dto/convert/ABMessageCovDTO.java create mode 100644 src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java diff --git a/pom.xml b/pom.xml index ef15f9c..b8ccfc2 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,12 @@ + + + cn.hutool + hutool-all + 5.3.10 + diff --git a/src/main/java/com/yuyou/openapi/openapi/OpenapiApplication.java b/src/main/java/com/yuyou/openapi/openapi/OpenapiApplication.java index a7a23be..1c8bd67 100644 --- a/src/main/java/com/yuyou/openapi/openapi/OpenapiApplication.java +++ b/src/main/java/com/yuyou/openapi/openapi/OpenapiApplication.java @@ -2,8 +2,10 @@ package com.yuyou.openapi.openapi; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication +@EnableAsync public class OpenapiApplication { public static void main(String[] args) { diff --git a/src/main/java/com/yuyou/openapi/openapi/model/dto/ABMessageDTO.java b/src/main/java/com/yuyou/openapi/openapi/model/dto/ABMessageDTO.java index 7e5c5bd..52d38e7 100644 --- a/src/main/java/com/yuyou/openapi/openapi/model/dto/ABMessageDTO.java +++ b/src/main/java/com/yuyou/openapi/openapi/model/dto/ABMessageDTO.java @@ -1,6 +1,8 @@ package com.yuyou.openapi.openapi.model.dto; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; /** * Copyright (C), 2012 - 2018, qyx @@ -13,6 +15,8 @@ import lombok.Data; * x 2020/8/5 v1.0 创建此文件 */ @Data +@AllArgsConstructor +@NoArgsConstructor public class ABMessageDTO { } diff --git a/src/main/java/com/yuyou/openapi/openapi/model/dto/convert/ABMessageCovDTO.java b/src/main/java/com/yuyou/openapi/openapi/model/dto/convert/ABMessageCovDTO.java new file mode 100644 index 0000000..cfde29a --- /dev/null +++ b/src/main/java/com/yuyou/openapi/openapi/model/dto/convert/ABMessageCovDTO.java @@ -0,0 +1,40 @@ +package com.yuyou.openapi.openapi.model.dto.convert; + + +import com.yuyou.openapi.openapi.model.dto.ABMessageDTO; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + + +/** + * 用于发送请求的转换实体类 + * + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ABMessageCovDTO { + + /** + * 唯一标志,对接时提供 + */ + private String app_id; + + /** + * 请求时的时间戳(1970年1月1日距今的秒数,2分钟之内的会处理,超过2分钟会回复超时) + */ + private long nonce_str; + + /** + * 校验码: sha1(“app_id=AppId&nonce_str=time_stamp”) + */ + private String signature; + + /** + * 具体数据,每条为json格式 + */ + private List data; +} diff --git a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java index b004d5d..519f8b6 100644 --- a/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java +++ b/src/main/java/com/yuyou/openapi/openapi/service/impl/ABClientServiceImpl.java @@ -2,8 +2,12 @@ package com.yuyou.openapi.openapi.service.impl; import com.yuyou.openapi.openapi.model.dto.ABMessageDTO; import com.yuyou.openapi.openapi.service.ABClientService; +import com.yuyou.openapi.openapi.task.ABDownTask; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; + /** * Copyright (C), 2012 - 2018, qyx * FileName: ABClientServiceImpl @@ -17,8 +21,21 @@ import org.springframework.stereotype.Service; @Service public class ABClientServiceImpl implements ABClientService { + + @Autowired + private ABDownTask abDownTask; + @Override public void recordAndSendABClientMsg(ABMessageDTO dto) { + // 调用异步任务进行转发AB单 - 这里是这有一条数据 + ArrayList dtoArrayList = new ArrayList<>(); + dtoArrayList.add(dto); + boolean handleResult = abDownTask.doRunTask(dtoArrayList); + // 处理数据模型转换 + + // 调用接口进行入库 + + // 返回处理结果 } } diff --git a/src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java b/src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java new file mode 100644 index 0000000..7271db0 --- /dev/null +++ b/src/main/java/com/yuyou/openapi/openapi/task/ABDownTask.java @@ -0,0 +1,100 @@ +package com.yuyou.openapi.openapi.task; + + +import cn.hutool.crypto.SecureUtil; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.yuyou.openapi.openapi.model.dto.ABMessageDTO; +import com.yuyou.openapi.openapi.model.dto.convert.ABMessageCovDTO; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.List; + +/** + * 下游处理任务类 + */ +@Service +@Slf4j +public class ABDownTask { + + /** + * 配置文件中加载配置信息 + */ + @Value("${ab.customer.app_id}") + private String appId; + + @Value("${ab.customer.url}") + private String customerUrl; + + + /** + * 任务处理入口,主要用于时间记录 + * + * @return + */ + @Async + public boolean doRunTask(List messageDTOList){ + Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start running, task name is {} ] ======", "ABDownTask"); + boolean resultTag = runTask(messageDTOList); + Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "ABDownTask", (endMilliSecond-satrtMilliSecond)); + return resultTag; + } + + /** + * 真实的任务执行入口 + * + * @return + */ + private boolean runTask(List messageDTOList){ + int count = 0; // 设置请求失败计数 + // 数据实体进行映射转换 - 目前是只有一条给 + ABMessageCovDTO abMessageCovDTO = new ABMessageCovDTO(); + abMessageCovDTO.setData(messageDTOList); + // 补充其他的下游请求字段 + abMessageCovDTO.setNonce_str((System.currentTimeMillis()/1000)); + abMessageCovDTO.setApp_id(appId); + abMessageCovDTO.setSignature(SecureUtil.sha1("app_id="+abMessageCovDTO.getApp_id()+"&nonce_str="+abMessageCovDTO.getNonce_str())); + // 数据实体转成Json 不忽略空kv 有序 + JSONObject jsonObject = JSONUtil.parseObj(abMessageCovDTO, false, true); + // 请求的响应处理 + // todo 失败重发请求3次 + while (count <= 3){ + // 调用HTTP请求发送数据 + HttpResponse httpResponse = sendReq(jsonObject); + if (httpResponse.isOk() && httpResponse.body().contains("成功")){ + break; + }else{ + count ++; + log.info("========== [request fail, response is {} ] ==========", httpResponse.body()); + } + } + if (count > 4) { + return Boolean.FALSE; + } + return Boolean.TRUE; + } + + /** + * 调用HTTP请求发送数据 + * + * @param jsonObject + * @return + */ + private HttpResponse sendReq(JSONObject jsonObject){ + HttpResponse httpResponse = HttpRequest + .post(customerUrl) + .body(jsonObject.toString()) + .execute(); + return httpResponse; + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 8fd2f24..c076487 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,6 +4,7 @@ spring: # 序列化忽略null的k-v配置 jackson: default-property-inclusion: non_null + --- # 端口 server: @@ -17,6 +18,11 @@ spring: url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false username: root password: root +# 下游请求配置信息 +ab: + customer: + url: https://sandbox.openapi.ppke.com.cn/ + app_id: app_id --- # 端口 server: @@ -30,4 +36,8 @@ spring: url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false username: root password: Yuyou@2020 - +# 下游请求配置信息 +ab: + customer: + url: https://openapi.ppke.com.cn/ + app_id: app_id \ No newline at end of file