|
@ -37,6 +37,8 @@ import java.time.LocalDate; |
|
|
import java.time.LocalTime; |
|
|
import java.time.LocalTime; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
|
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.concurrent.TimeUnit; |
|
|
import java.util.regex.Matcher; |
|
|
import java.util.regex.Matcher; |
|
|
import java.util.regex.Pattern; |
|
|
import java.util.regex.Pattern; |
|
@ -79,6 +81,7 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
* 3. 在请求成功时,解析响应体中的令牌信息并更新本地存储的令牌。 |
|
|
* 3. 在请求成功时,解析响应体中的令牌信息并更新本地存储的令牌。 |
|
|
*/ |
|
|
*/ |
|
|
//@Scheduled(cron = "0 0 */5 * * ?")
|
|
|
//@Scheduled(cron = "0 0 */5 * * ?")
|
|
|
|
|
|
/* |
|
|
public static void refreshAccessToken() { |
|
|
public static void refreshAccessToken() { |
|
|
OkHttp okHttp = new OkHttp(); |
|
|
OkHttp okHttp = new OkHttp(); |
|
|
try { |
|
|
try { |
|
@ -114,7 +117,42 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public static CompletableFuture<String> refreshAccessToken() { |
|
|
|
|
|
CompletableFuture<String> future = new CompletableFuture<>(); |
|
|
|
|
|
OkHttp okHttp = new OkHttp(); |
|
|
|
|
|
try { |
|
|
|
|
|
okHttp.url(baseUrl + UniversalEnum.DATA_CENTER_TRAFFIC_STATISTICS_OBTAIN_THE_TOKEN_URI.getValue()) |
|
|
|
|
|
.post(new Callback() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onFailure(Call call, IOException e) { |
|
|
|
|
|
future.completeExceptionally(e); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onResponse(Call call, Response response) { |
|
|
|
|
|
try { |
|
|
|
|
|
if (response.isSuccessful() && response.body() != null) { |
|
|
|
|
|
JSONObject token = JSON.parseObject(response.body().string()); |
|
|
|
|
|
String accessToken = token.getString("access_token"); |
|
|
|
|
|
// 将 access token 存储到 Redis
|
|
|
|
|
|
RedisCache redisCache = SpringUtils.getBean(RedisCache.class); |
|
|
|
|
|
redisCache.setCacheObject(RedisKeyConstants.TRAFFIC_FLOW, token.getString("token_type") + " " + accessToken, token.getInteger("expires_in"), TimeUnit.SECONDS); |
|
|
|
|
|
future.complete(accessToken); |
|
|
|
|
|
} else { |
|
|
|
|
|
future.completeExceptionally(new IOException("意外响应码:" + response.code())); |
|
|
|
|
|
} |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
future.completeExceptionally(e); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} catch (HttpException e) { |
|
|
|
|
|
future.completeExceptionally(e); |
|
|
|
|
|
} |
|
|
|
|
|
return future; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 获取访问令牌。 |
|
|
* 获取访问令牌。 |
|
@ -128,13 +166,21 @@ public class DcTrafficStatisticsServiceImpl implements IDcTrafficStatisticsServi |
|
|
RedisCache redisCache = SpringUtils.getBean(RedisCache.class); |
|
|
RedisCache redisCache = SpringUtils.getBean(RedisCache.class); |
|
|
|
|
|
|
|
|
String token = redisCache.getCacheObject(RedisKeyConstants.TRAFFIC_FLOW); |
|
|
String token = redisCache.getCacheObject(RedisKeyConstants.TRAFFIC_FLOW); |
|
|
|
|
|
|
|
|
// 检查token是否存在
|
|
|
// 检查token是否存在
|
|
|
if (token == null) { |
|
|
if (token == null) { |
|
|
// 组合并返回token类型和访问令牌
|
|
|
// // 组合并返回token类型和访问令牌
|
|
|
refreshAccessToken(); |
|
|
// refreshAccessToken();
|
|
|
token = redisCache.getCacheObject(RedisKeyConstants.TRAFFIC_FLOW); |
|
|
// token = redisCache.getCacheObject(RedisKeyConstants.TRAFFIC_FLOW);
|
|
|
|
|
|
// 异步获取新token,并更新缓存
|
|
|
|
|
|
try { |
|
|
|
|
|
// 异步获取新token,并更新缓存
|
|
|
|
|
|
token = refreshAccessToken().get(); // 等待异步操作完成
|
|
|
|
|
|
} catch (InterruptedException | ExecutionException e) { |
|
|
|
|
|
// 处理异常情况
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
// 如果token不存在,返回null
|
|
|
|
|
|
return token; |
|
|
return token; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|