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