热搜接口异步编排调用
This commit is contained in:
parent
0914ee4f37
commit
3d49ded018
|
|
@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static com.xjs.consts.RedisConst.HOT;
|
||||
|
|
@ -52,7 +53,14 @@ public class ApiTopSearchController {
|
|||
Map<String, List> cacheObject = redisService.getCacheObject(HOT);
|
||||
return AjaxResult.success(cacheObject);
|
||||
}
|
||||
Map<String, List> listHashMap = topSearchService.getAllTopSearch();
|
||||
Map<String, List> listHashMap = null;
|
||||
try {
|
||||
listHashMap = topSearchService.getAllTopSearch();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
//把数据存入redis,十分钟过期
|
||||
redisService.setCacheObject(HOT,listHashMap,HOT_EXPIRE, TimeUnit.MINUTES);
|
||||
return AjaxResult.success(listHashMap);
|
||||
|
|
@ -75,7 +83,14 @@ public class ApiTopSearchController {
|
|||
public R<Map<String, List>> topSearchForRPC() {
|
||||
//清理重复数据
|
||||
topSearchService.deleteRepeatData();
|
||||
Map<String, List> map = topSearchService.getAllTopSearch();
|
||||
Map<String, List> map = null;
|
||||
try {
|
||||
map = topSearchService.getAllTopSearch();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return R.ok(map);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package com.xjs.topsearch.service;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* 热搜榜服务接口
|
||||
|
|
@ -14,7 +15,7 @@ public interface TopSearchService {
|
|||
* 获取所有热搜(内含插入)
|
||||
* @return
|
||||
*/
|
||||
Map<String, List> getAllTopSearch();
|
||||
Map<String, List> getAllTopSearch() throws ExecutionException, InterruptedException;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -13,9 +13,14 @@ import org.springframework.stereotype.Service;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
* 热搜服务实现
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-22
|
||||
*/
|
||||
|
|
@ -45,41 +50,68 @@ public class TopSearchServiceImpl implements TopSearchService {
|
|||
private ApiTopsearchWeiboService apiTopsearchWeiboService;
|
||||
|
||||
|
||||
private ExecutorService executor = Executors.newFixedThreadPool(5);
|
||||
|
||||
@Override
|
||||
public Map<String, List> getAllTopSearch() {
|
||||
//获取全网热搜
|
||||
List<ApiTopsearchAllnetwork> allnetworkList = tianXingTopsearchAllnetworkFactory.topSearchApi();
|
||||
//获取微博热搜
|
||||
List<ApiTopsearchWeibo> weiboList = tianXingTopsearchWeiboFactory.topSearchApi();
|
||||
//获取抖音热搜
|
||||
List<ApiTopsearchDouyin> douyinList = tianXingTopsearchDouyinFactory.topSearchApi();
|
||||
//获取微信热搜
|
||||
List<ApiTopsearchWechat> wechatList = tianXingTopsearchWechatFactory.topSearchApi();
|
||||
//获取百度热搜
|
||||
List<ApiTopsearchBaidu> baiduList = tianXingTopsearchBaiduFactory.topSearchApi();
|
||||
public Map<String, List> getAllTopSearch() throws ExecutionException, InterruptedException {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
CompletableFuture<List<ApiTopsearchAllnetwork>> future1 = CompletableFuture.supplyAsync(() -> {
|
||||
//获取全网热搜
|
||||
return tianXingTopsearchAllnetworkFactory.topSearchApi();
|
||||
}, executor);
|
||||
|
||||
CompletableFuture<List<ApiTopsearchWeibo>> future2 = CompletableFuture.supplyAsync(() -> {
|
||||
//获取微博热搜
|
||||
return tianXingTopsearchWeiboFactory.topSearchApi();
|
||||
}, executor);
|
||||
|
||||
CompletableFuture<List<ApiTopsearchDouyin>> future3 = CompletableFuture.supplyAsync(() -> {
|
||||
//获取抖音热搜
|
||||
return tianXingTopsearchDouyinFactory.topSearchApi();
|
||||
}, executor);
|
||||
|
||||
CompletableFuture<List<ApiTopsearchWechat>> future4 = CompletableFuture.supplyAsync(() -> {
|
||||
//获取微信热搜
|
||||
return tianXingTopsearchWechatFactory.topSearchApi();
|
||||
}, executor);
|
||||
|
||||
CompletableFuture<List<ApiTopsearchBaidu>> future5 = CompletableFuture.supplyAsync(() -> {
|
||||
//获取百度热搜
|
||||
return tianXingTopsearchBaiduFactory.topSearchApi();
|
||||
}, executor);
|
||||
|
||||
// 必须等待所有异步任务执行完成才能返回结果
|
||||
CompletableFuture.allOf(future1, future2, future3, future4, future5).get();
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
long count = end - start;
|
||||
log.info("异步获取热搜榜耗费时间:{}ms",count);
|
||||
|
||||
|
||||
Map<String, List> listHashMap = new HashMap<>();
|
||||
listHashMap.put("allnetworkList", allnetworkList);
|
||||
listHashMap.put("wechatList", wechatList);
|
||||
listHashMap.put("baiduList", baiduList);
|
||||
listHashMap.put("weiboList", weiboList);
|
||||
listHashMap.put("douyinList", douyinList);
|
||||
listHashMap.put("allnetworkList", future1.get());
|
||||
listHashMap.put("wechatList", future2.get());
|
||||
listHashMap.put("baiduList", future3.get());
|
||||
listHashMap.put("weiboList", future4.get());
|
||||
listHashMap.put("douyinList", future5.get());
|
||||
return listHashMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer deleteRepeatData() {
|
||||
Integer allNetworkCount = apiTopsearchAllnetworkService.deleteRepeatData();
|
||||
log.info("thread id:{},清除全网热搜榜重复数据,重复数:{}", Thread.currentThread().getId(),allNetworkCount);
|
||||
log.info("thread id:{},清除全网热搜榜重复数据,重复数:{}", Thread.currentThread().getId(), allNetworkCount);
|
||||
Integer wechatCount = apiTopsearchWechatService.deleteRepeatData();
|
||||
log.info("thread id:{},清除微信热搜榜重复数据,重复数:{}", Thread.currentThread().getId(),wechatCount);
|
||||
log.info("thread id:{},清除微信热搜榜重复数据,重复数:{}", Thread.currentThread().getId(), wechatCount);
|
||||
Integer baiduCount = apiTopsearchBaiduService.deleteRepeatData();
|
||||
log.info("thread id:{},清除百度热搜榜重复数据,重复数:{}", Thread.currentThread().getId(),baiduCount);
|
||||
log.info("thread id:{},清除百度热搜榜重复数据,重复数:{}", Thread.currentThread().getId(), baiduCount);
|
||||
Integer douyinCount = apiTopsearchDouyinService.deleteRepeatData();
|
||||
log.info("thread id:{},清除抖音热搜榜重复数据,重复数:{}", Thread.currentThread().getId(),douyinCount);
|
||||
log.info("thread id:{},清除抖音热搜榜重复数据,重复数:{}", Thread.currentThread().getId(), douyinCount);
|
||||
Integer weiboCount = apiTopsearchWeiboService.deleteRepeatData();
|
||||
log.info("thread id:{},清除微博热搜榜重复数据,重复数:{}", Thread.currentThread().getId(),weiboCount);
|
||||
return allNetworkCount+wechatCount+baiduCount+douyinCount+weiboCount;
|
||||
log.info("thread id:{},清除微博热搜榜重复数据,重复数:{}", Thread.currentThread().getId(), weiboCount);
|
||||
return allNetworkCount + wechatCount + baiduCount + douyinCount + weiboCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import com.ruoyi.common.core.domain.R;
|
|||
import com.ruoyi.common.core.web.domain.AjaxResult;
|
||||
import com.ruoyi.common.log.annotation.Log;
|
||||
import com.ruoyi.common.security.annotation.RequiresPermissions;
|
||||
import com.xjs.annotation.RateLimiter;
|
||||
import com.xjs.translation.domain.qo.translation.TranslationQo;
|
||||
import com.xjs.translation.domain.vo.translation.TranslationVo;
|
||||
import com.xjs.translation.factory.TranslationFactory;
|
||||
|
|
@ -41,6 +42,7 @@ public class TranslationController {
|
|||
@ApiOperation("翻译接口")
|
||||
@Log(title = "获取翻译")
|
||||
@RequiresPermissions("openapi:translation:api")
|
||||
@RateLimiter(count = 2, time = 10)
|
||||
public AjaxResult translation(@Validated @RequestBody TranslationQo translationQo) {
|
||||
TranslationVo translationVo = new TranslationVo();
|
||||
if (BAIDU.equals(translationQo.getTranslationType())) {
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ public class reptileLogAspect {
|
|||
//获取目标类名及方法名
|
||||
Signature signature = joinPoint.getSignature();
|
||||
String method = signature.getName();
|
||||
Class aClass = signature.getDeclaringType();
|
||||
Class<?> aClass = signature.getDeclaringType();
|
||||
Method[] methods = aClass.getMethods();
|
||||
|
||||
//根据目标的方法名判断当前方法
|
||||
|
|
|
|||
Loading…
Reference in New Issue