Commit da7c05a9 authored by matianhao's avatar matianhao

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	src/main/java/com/mth/requestsecret/scheduler/RequestSecretSchedulerTask.java
parents 3c8b6170 37ae5c79
......@@ -13,14 +13,11 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import static com.mth.requestsecret.constant.Constants.*;
import static com.mth.requestsecret.constant.Constants.SJJ_REQUEST_SECRET_PREFIX;
/**
* @author fyl
......@@ -44,6 +41,8 @@ public class RestTemplateService {
private ThreadLocal<Integer> threadLocal = ThreadLocal.withInitial(() -> 0);
private static final Integer REQUEST_LIMIT = 20;
@Value("${ruicheng.address}")
private String pathUrl;
......@@ -109,32 +108,32 @@ public class RestTemplateService {
ResponseEntity<String> responseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, request, String.class);
log.info("api response:{}", responseEntity);
try {
// 接口返回码
String code = JSONObject.parseObject(responseEntity.getBody()).getString("code");
Integer count = threadLocal.get();
String threadName = Thread.currentThread().getName();
// 如非成功返回码,则重新获取秘钥并发送请求,限制重新请求次数两次
if (!RESPONSE_CODE_00.equals(code) && count < 2) {
threadLocal.set(++count);
log.info("线程名称:{},重新请求次数:{}", threadName, count);
// redis 中获取过期时间
String expireTimeStr = redisUtil.get(SJJ_REQUEST_EXPIRE_TIME_PREFIX);
LocalDateTime expireTime = Instant.ofEpochMilli(Long.parseLong(expireTimeStr)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDateTime now = LocalDateTime.now();
// 当前时间超过过期时间,重新请求秘钥,否则重新发送请求
if (now.isAfter(expireTime)) {
log.info("redis中过期时间:{},当前时间:{}", expireTime, now);
// 重新获取秘钥
schedulerTask.getRequestSecret();
}
// 重新调用请求
return commonSendRequest(paramMap, apiMethod);
}
} finally {
threadLocal.remove();
}
// try {
// // 接口返回码
// String code = JSONObject.parseObject(responseEntity.getBody()).getString("code");
// Integer count = threadLocal.get();
// String threadName = Thread.currentThread().getName();
//
// // 如非成功返回码,则重新获取秘钥并发送请求,限制重新请求次数
// if (!RESPONSE_CODE_00.equals(code) && count < REQUEST_LIMIT) {
// threadLocal.set(++count);
// log.info("线程名称:{},重新请求次数:{}", threadName, count);
// // redis 中获取过期时间
// String expireTimeStr = redisUtil.get(SJJ_REQUEST_EXPIRE_TIME_PREFIX);
// LocalDateTime expireTime = Instant.ofEpochMilli(Long.parseLong(expireTimeStr)).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
// LocalDateTime now = LocalDateTime.now();
// // 当前时间超过过期时间,重新请求秘钥,否则重新发送请求
// if (now.isAfter(expireTime)) {
// log.info("redis中过期时间:{},当前时间:{}", expireTime, now);
// // 重新获取秘钥
// schedulerTask.getRequestSecret();
// }
// // 重新调用请求
// return commonSendRequest(paramMap, apiMethod);
// }
// } finally {
// threadLocal.remove();
// }
return responseEntity;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment