网关重启适配

This commit is contained in:
huangweixiong 2022-08-22 11:11:02 +08:00
parent 9bcdd78bae
commit c51e335f9f
1 changed files with 82 additions and 68 deletions

View File

@ -3,6 +3,8 @@ package io.renren.modules.gateway.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.gateway.dao.ApiCountHistoryDao;
import io.renren.modules.gateway.entity.ApiCountHistoryEntity;
import io.renren.modules.gateway.service.MonitorServiceV2; import io.renren.modules.gateway.service.MonitorServiceV2;
import io.renren.modules.monitor.entity.Result; import io.renren.modules.monitor.entity.Result;
import io.renren.modules.resource.dao.AttrDao; import io.renren.modules.resource.dao.AttrDao;
@ -37,6 +39,7 @@ import java.net.URLConnection;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
@RestController @RestController
@Api(tags = "网关统计") @Api(tags = "网关统计")
@ -67,16 +70,38 @@ public class MonitorControllerV2 {
@Autowired @Autowired
private MonitorServiceV2 monitorServiceV2; private MonitorServiceV2 monitorServiceV2;
@Autowired
private ApiCountHistoryDao apiCountHistoryDao;
@GetMapping("/queryGroupByAbility") @GetMapping("/queryGroupByAbility")
@ApiOperation("统计数据按能力归集") @ApiOperation("统计数据按能力归集")
public Result queryGroupByAbility(String query, String time) throws InterruptedException, ExecutionException, TimeoutException { public Result queryGroupByAbility(Long start, Long end) throws InterruptedException, ExecutionException, TimeoutException {
String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}&time={time}"; String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}&time={time}";
List<String> querys = new ArrayList<>();
List<CompletableFuture<List<?>>> futures = querys.stream().map(item -> {
return CompletableFuture.supplyAsync(() -> {
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query, time); //查询重启记录时间节点
LambdaQueryWrapper<ApiCountHistoryEntity> historyEntityLambdaQueryWrapper = new QueryWrapper<ApiCountHistoryEntity>().lambda()
.select(ApiCountHistoryEntity::getUpdateTime)
.ge(ApiCountHistoryEntity::getUpdateTime, new Date(start * 1000))
.le(ApiCountHistoryEntity::getUpdateTime, new Date(end * 1000))
.orderByAsc(ApiCountHistoryEntity::getUpdateTime);
List<ApiCountHistoryEntity> apiCountHistoryEntities = apiCountHistoryDao.selectList(historyEntityLambdaQueryWrapper);
List<Long> timePoint = apiCountHistoryEntities.stream()
.map(item -> item.getUpdateTime().getTime()/1000)
.distinct()
.collect(Collectors.toList());
timePoint.add(0, start);
timePoint.add(end);
//根据时间拆分按照时间段异步请求
List<CompletableFuture<List<?>>> futures = new ArrayList<>();
for (int i = 0; i < timePoint.size()-1; i++) {
HashMap<String, Long> map = new HashMap<String, Long>();
Long startTime = timePoint.get(i);
Long endTime = timePoint.get(i+1);
String query = String.format("topk(10, sum(label_replace(increase(apigateway_http_status[%ds:1s]), \"groupInfo\", \"$2\", \"matched_uri\", \"/juapi/(.*?)/(.*?)/.*\")) by (groupInfo))", endTime - startTime);
futures.add(CompletableFuture.supplyAsync(() -> {
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query, endTime);
/** 接口数据示例 /** 接口数据示例
* { * {
* "status": "success", * "status": "success",
@ -108,6 +133,7 @@ public class MonitorControllerV2 {
continue; continue;
} }
try { try {
metric.put("count", Double.valueOf((String) value.get(1)).intValue());
Long groupInfo = Long.valueOf((String) metric.get("groupInfo")); Long groupInfo = Long.valueOf((String) metric.get("groupInfo"));
metric.put("groupInfo", groupInfo); metric.put("groupInfo", groupInfo);
results.add(metric); results.add(metric);
@ -119,42 +145,32 @@ public class MonitorControllerV2 {
return results; return results;
} }
return Collections.emptyList(); return Collections.emptyList();
}, executor); }, executor));
}).collect(Collectors.toList()); }
//不同时间段的数据相加
HashMap<String, Map<String, Object>> filterMap = new HashMap<String, Map<String, Object>>(); HashMap<Long, Map<String, Object>> filterMap = new HashMap<Long, Map<String, Object>>();
for (CompletableFuture<List<?>> future : futures) { for (CompletableFuture<List<?>> future : futures) {
List<Map<String, Object>> list = (List<Map<String, Object>>) future.get(30, TimeUnit.SECONDS); List<Map<String, Object>> list = (List<Map<String, Object>>) future.get(30, TimeUnit.SECONDS);
list.forEach(item -> { list.forEach(item -> {
// filterMap.containsKey(item.) Long groupInfo = (Long) item.get("groupInfo");
if (filterMap.containsKey(groupInfo)) {
Map<String, Object> objectMap = filterMap.get(groupInfo);
Integer count = (Integer) objectMap.get("count");
Integer itemCount = (Integer) item.get("count");
objectMap.put("count", count + itemCount);
}else {
filterMap.put(groupInfo, item);
}
}); });
} }
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query, time); //重新排序
HashMap body = entity.getBody(); List<Map<String, Object>> results = filterMap.values().stream()
HashMap data = (HashMap) body.get("data"); .sorted(Comparator.comparingInt(item -> (int) ((Map) item).get("count"))
if (data != null){ .reversed())
List<HashMap> result = (List) data.get("result"); .limit(10)
ArrayList<Map> results = new ArrayList<>(); .collect(Collectors.toList());
ArrayList<Long> abilityIds = new ArrayList<>();
for (HashMap hashMap : result) {
Map metric = (Map) hashMap.get("metric");
if (metric != null && metric.get("groupInfo") != null){
List value = (List) hashMap.get("value");
if (value.size() == 2){
metric.put("count", value.get(1));
try{
Long groupInfo = Long.valueOf((String) metric.get("groupInfo"));
abilityIds.add(groupInfo);
metric.put("groupInfo", groupInfo);
results.add(metric);
}catch (Exception e) {
//忽略
}
}
}
}
//数据聚合
if (!results.isEmpty()) { if (!results.isEmpty()) {
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda(); LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
queryWrapper queryWrapper
@ -163,7 +179,7 @@ public class MonitorControllerV2 {
ResourceEntity::getApiMethodType, ResourceEntity::getApiMethodType,
ResourceEntity::getType, ResourceEntity::getType,
ResourceEntity::getApiUrl) ResourceEntity::getApiUrl)
.in(ResourceEntity::getId, abilityIds); .in(ResourceEntity::getId, filterMap.keySet());
List<ResourceEntity> entities = resourceDao.selectList(queryWrapper); List<ResourceEntity> entities = resourceDao.selectList(queryWrapper);
for (Map map : results) { for (Map map : results) {
Long groupInfo = (Long) map.get("groupInfo"); Long groupInfo = (Long) map.get("groupInfo");
@ -175,7 +191,7 @@ public class MonitorControllerV2 {
map.put("apiUrl", resourceEntity.getApiUrl()); map.put("apiUrl", resourceEntity.getApiUrl());
LambdaQueryWrapper<AttrEntity> attrQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AttrEntity> attrQueryWrapper = new LambdaQueryWrapper<>();
attrQueryWrapper.select(AttrEntity::getAttrType,AttrEntity::getAttrValue) attrQueryWrapper.select(AttrEntity::getAttrType,AttrEntity::getAttrValue)
.eq(AttrEntity::getDataResourceId,groupInfo) .eq(AttrEntity::getDataResourceId, groupInfo)
.eq(AttrEntity::getAttrType,"服务商名") .eq(AttrEntity::getAttrType,"服务商名")
.eq(AttrEntity::getDelFlag, 0); .eq(AttrEntity::getDelFlag, 0);
AttrEntity attrEntity = attrDao.selectOne(attrQueryWrapper); AttrEntity attrEntity = attrDao.selectOne(attrQueryWrapper);
@ -190,9 +206,7 @@ public class MonitorControllerV2 {
} }
return Result.success(results); return Result.success(results);
}
return Result.success(Collections.emptyList());
} }
@GetMapping("/queryGroupByDepartment") @GetMapping("/queryGroupByDepartment")