增加网关注册接口

This commit is contained in:
huangweixiong 2022-08-10 11:55:54 +08:00
parent b071097e96
commit bbefe4fff1
4 changed files with 257 additions and 6 deletions

View File

@ -0,0 +1,241 @@
package io.renren.modules.gateway.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.gateway.service.MonitorServiceV2;
import io.renren.modules.monitor.entity.Result;
import io.renren.modules.resource.dao.AttrDao;
import io.renren.modules.resource.dao.ResourceDao;
import io.renren.modules.resource.entity.AttrEntity;
import io.renren.modules.resource.entity.ResourceEntity;
import io.renren.modules.sys.dao.SysDeptDao;
import io.renren.modules.sys.entity.SysDeptEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
@RestController
@Api(tags = "网关统计")
@Log4j2
@RequestMapping("/gateway-monitor/v2")
public class MonitorControllerV2 {
@Autowired
private RestTemplate restTemplate;
@Value("${hisense.gateway.url}")
private String gatewayDomain;
@Value("${server.servlet.context-path}")
private String context_path;
@Autowired
private ResourceDao resourceDao;
@Autowired
private AttrDao attrDao;
@Autowired
private SysDeptDao sysDeptDao;
@Autowired
private MonitorServiceV2 monitorServiceV2;
@GetMapping("/queryGroupByAbility")
@ApiOperation("统计数据按能力归集")
public Result queryGroupByAbility(String query, String time){
String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}" + "&time=" + time;
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query);
HashMap body = entity.getBody();
HashMap data = (HashMap) body.get("data");
if (data != null){
List<HashMap> result = (List) data.get("result");
ArrayList<Map> results = new ArrayList<>();
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()) {
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
queryWrapper
.select(ResourceEntity::getId,
ResourceEntity::getName,
ResourceEntity::getApiMethodType,
ResourceEntity::getType,
ResourceEntity::getApiUrl)
.in(ResourceEntity::getId, abilityIds);
List<ResourceEntity> entities = resourceDao.selectList(queryWrapper);
for (Map map : results) {
Long groupInfo = (Long) map.get("groupInfo");
for (ResourceEntity resourceEntity : entities) {
if (groupInfo != null && groupInfo.equals(resourceEntity.getId())) {
map.put("name", resourceEntity.getName());
map.put("ApiMethodType", resourceEntity.getApiMethodType());
map.put("type", resourceEntity.getType());
map.put("apiUrl", resourceEntity.getApiUrl());
LambdaQueryWrapper<AttrEntity> attrQueryWrapper = new LambdaQueryWrapper<>();
attrQueryWrapper.select(AttrEntity::getAttrType,AttrEntity::getAttrValue)
.eq(AttrEntity::getDataResourceId,groupInfo)
.eq(AttrEntity::getAttrType,"服务商名")
.eq(AttrEntity::getDelFlag, 0);
AttrEntity attrEntity = attrDao.selectOne(attrQueryWrapper);
if (attrEntity.getAttrValue() != null) {
map.put("privider", attrEntity.getAttrValue());
}
entities.remove(resourceEntity);
break;
}
}
}
}
return Result.success(results);
}
return Result.success(Collections.emptyList());
}
@GetMapping("/queryGroupByDepartment")
@ApiOperation("统计数据按部门归集")
public Result queryGroupByDepartment(String query, String time){
String url = gatewayDomain + "/juapi/metrics/api/v1/query?query={query}" + "&time=" + time;
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query);
HashMap body = entity.getBody();
HashMap data = (HashMap) body.get("data");
if (data != null){
List<HashMap> result = (List) data.get("result");
ArrayList<Map> results = new ArrayList<>();
ArrayList<Long> abilityIds = new ArrayList<>();
for (HashMap hashMap : result) {
Map metric = (Map) hashMap.get("metric");
if (metric != null && metric.get("deptInfo") != 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("deptInfo"));
abilityIds.add(groupInfo);
metric.put("deptInfo", groupInfo);
results.add(metric);
}catch (Exception e) {
//忽略
}
}
}
}
if (!results.isEmpty()) {
LambdaQueryWrapper<SysDeptEntity> queryWrapper = new QueryWrapper<SysDeptEntity>().lambda();
queryWrapper.select(SysDeptEntity::getName,SysDeptEntity::getId)
.in(SysDeptEntity::getId, abilityIds);
List<SysDeptEntity> entities = sysDeptDao.selectList(queryWrapper);
for (Map map : results) {
Long deptInfo = (Long) map.get("deptInfo");
for (SysDeptEntity sysDeptEntity : entities) {
if (deptInfo != null && deptInfo.equals(sysDeptEntity.getId())) {
map.put("name", sysDeptEntity.getName());
entities.remove(sysDeptEntity);
break;
}
}
}
}
return Result.success(results);
}
return Result.success(Collections.emptyList());
}
@GetMapping("/queryGroupByDeptInRange")
@ApiOperation("统计数据按部门显示趋势")
public Result queryGroupByDeptInRange(String query, String start, String end, String step){
String url = gatewayDomain + "/juapi/metrics/api/v1/query_range?query={query}" +
"&start=" + start +
"&end=" + end +
"&step=" + step;
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class, query);
HashMap body = entity.getBody();
HashMap data = (HashMap) body.get("data");
if (data != null){
List<HashMap> result = (List) data.get("result");
ArrayList<HashMap> results = new ArrayList<>(result.size());
for (HashMap hashMap : result) {
Map metric = (Map) hashMap.get("metric");
if (metric != null && metric.get("deptInfo") != null){
try{
Long filterId = Long.valueOf((String) metric.get("deptInfo"));
SysDeptEntity sysDeptEntity = sysDeptDao.selectById(filterId);
if (sysDeptEntity != null && StringUtils.isNotBlank(sysDeptEntity.getName())){
metric.put("name", sysDeptEntity.getName());
results.add(hashMap);
}
}catch (Exception e){
log.warn("数据异常忽略", e);
}
}
}
return Result.success(results);
}
return Result.success(Collections.emptyList());
}
@GetMapping("/queryGroupCount")
@ApiOperation("查询总api数量")
public Result queryGroupCount(){
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
queryWrapper.isNotNull(ResourceEntity::getGroupId).eq(ResourceEntity::getDelFlag, 0);
Integer selectCount = resourceDao.selectCount(queryWrapper);
return Result.success(selectCount);
}
@GetMapping("/getCallCount")
@ApiOperation("查询总api调用总量")
public Result getCallCount(){
Long callCount = monitorServiceV2.getCallCount();
return Result.success(callCount);
}
}

View File

@ -153,6 +153,8 @@ public class ApiGatewayService {
}
}
deleteGroup(groupId);
}
}

View File

@ -263,6 +263,20 @@ public class ResourceController {
return new Result().ok(dto.getId() == null ? "" : dto.getId());
}
@PostMapping("/registerApi2Gateway")
public Result registerApi2Gateway(@RequestParam String source) {
try {
apiGatewayService.resetApiGroup(source);
apiGatewayService.registerApi2Gateway(source);
}catch (Exception exception){
//注册失败忽略简单记录一下
logger.error("挂接网关注册失败", exception);
return new Result().error(exception.getMessage());
}
return new Result().ok(null);
}
@PostMapping("/importResource")
@ApiOperation("导入")
@LogOperation("导入")

View File

@ -43,7 +43,6 @@ public class ApiGatewayServiceTest {
resourceEntities.forEach(item -> {
String id = String.valueOf(item.getId());
apiGatewayService.resetApiGroup(id);
apiGatewayService.deleteGroup(id);
apiGatewayService.registerApi2Gateway(id);
});
@ -53,11 +52,6 @@ public class ApiGatewayServiceTest {
public void registerAPI(){
String id = "1522550194544123907";
apiGatewayService.resetApiGroup(id);
try{
apiGatewayService.deleteGroup(id);
}catch (Exception e){
e.printStackTrace();
}
apiGatewayService.registerApi2Gateway(id);
}