编写了请求的逻辑

master
土豆兄弟 4 years ago
parent a75cfcfe87
commit 3859e77c6c

@ -49,6 +49,12 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<!-- 工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.10</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -2,8 +2,10 @@ package com.yuyou.openapi.openapi;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication @SpringBootApplication
@EnableAsync
public class OpenapiApplication { public class OpenapiApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -1,6 +1,8 @@
package com.yuyou.openapi.openapi.model.dto; package com.yuyou.openapi.openapi.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* Copyright (C), 2012 - 2018, qyx * Copyright (C), 2012 - 2018, qyx
@ -13,6 +15,8 @@ import lombok.Data;
* x 2020/8/5 v1.0 * x 2020/8/5 v1.0
*/ */
@Data @Data
@AllArgsConstructor
@NoArgsConstructor
public class ABMessageDTO { public class ABMessageDTO {
} }

@ -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;
/**
* (197011,22)
*/
private long nonce_str;
/**
* : sha1(app_id=AppId&nonce_str=time_stamp)
*/
private String signature;
/**
* json
*/
private List<ABMessageDTO> data;
}

@ -2,8 +2,12 @@ package com.yuyou.openapi.openapi.service.impl;
import com.yuyou.openapi.openapi.model.dto.ABMessageDTO; import com.yuyou.openapi.openapi.model.dto.ABMessageDTO;
import com.yuyou.openapi.openapi.service.ABClientService; 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 org.springframework.stereotype.Service;
import java.util.ArrayList;
/** /**
* Copyright (C), 2012 - 2018, qyx * Copyright (C), 2012 - 2018, qyx
* FileName: ABClientServiceImpl * FileName: ABClientServiceImpl
@ -17,8 +21,21 @@ import org.springframework.stereotype.Service;
@Service @Service
public class ABClientServiceImpl implements ABClientService { public class ABClientServiceImpl implements ABClientService {
@Autowired
private ABDownTask abDownTask;
@Override @Override
public void recordAndSendABClientMsg(ABMessageDTO dto) { public void recordAndSendABClientMsg(ABMessageDTO dto) {
// 调用异步任务进行转发AB单 - 这里是这有一条数据
ArrayList<ABMessageDTO> dtoArrayList = new ArrayList<>();
dtoArrayList.add(dto);
boolean handleResult = abDownTask.doRunTask(dtoArrayList);
// 处理数据模型转换
// 调用接口进行入库
// 返回处理结果
} }
} }

@ -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<ABMessageDTO> 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<ABMessageDTO> 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;
}
}

@ -4,6 +4,7 @@ spring:
# 序列化忽略null的k-v配置 # 序列化忽略null的k-v配置
jackson: jackson:
default-property-inclusion: non_null default-property-inclusion: non_null
--- ---
# 端口 # 端口
server: server:
@ -17,6 +18,11 @@ spring:
url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root username: root
password: root password: root
# 下游请求配置信息
ab:
customer:
url: https://sandbox.openapi.ppke.com.cn/
app_id: app_id
--- ---
# 端口 # 端口
server: server:
@ -30,4 +36,8 @@ spring:
url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false url: jdbc:mysql://localhost:3306/push?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
username: root username: root
password: Yuyou@2020 password: Yuyou@2020
# 下游请求配置信息
ab:
customer:
url: https://openapi.ppke.com.cn/
app_id: app_id
Loading…
Cancel
Save