网关新接口

This commit is contained in:
huangweixiong 2022-07-20 16:56:54 +08:00
parent 9b94a3b913
commit 9c720dff85
3 changed files with 68 additions and 3 deletions

View File

@ -1,6 +1,11 @@
package io.renren.modules.gateway.controller; 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.monitor.entity.Result;
import io.renren.modules.resource.dao.ResourceDao;
import io.renren.modules.resource.entity.ResourceEntity;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
@ -41,6 +46,9 @@ public class MonitorController {
@Value("${server.servlet.context-path}") @Value("${server.servlet.context-path}")
private String context_path; private String context_path;
@Autowired
private ResourceDao resourceDao;
// @RequestMapping("/metrics/**") // @RequestMapping("/metrics/**")
void forward(HttpServletRequest request, HttpServletResponse response) throws IOException { void forward(HttpServletRequest request, HttpServletResponse response) throws IOException {
//类似nginx匹配前缀 //类似nginx匹配前缀
@ -136,4 +144,53 @@ public class MonitorController {
e.printStackTrace(); e.printStackTrace();
} }
} }
@GetMapping("/gateway-monitor/queryGroupByAbility")
@ApiOperation("前端访问图片请求转发")
public Result queryGroupByAbility(String query, String time){
String url = gatewayDomain + "/metrics/api/v1/query";
ResponseEntity<HashMap> entity = restTemplate.getForEntity(url, HashMap.class);
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));
Long groupInfo = Long.valueOf((String) metric.get("groupInfo"));
abilityIds.add(groupInfo);
metric.put("groupInfo", groupInfo);
results.add(metric);
}
}
}
if (results.size() > 0) {
LambdaQueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<ResourceEntity>().lambda();
queryWrapper.select(ResourceEntity::getName)
.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 (resourceEntity.getId() == groupInfo) {
map.put("name", resourceEntity.getName());
entities.remove(resourceEntity);
break;
}
}
}
}
return Result.success(results);
}
return Result.success(Collections.emptyList());
}
} }

View File

@ -7,6 +7,7 @@ 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 com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.Sets;
import io.renren.modules.processForm.dao.TAbilityApplicationDao; import io.renren.modules.processForm.dao.TAbilityApplicationDao;
import io.renren.modules.processForm.entity.TAbilityApplicationEntity; import io.renren.modules.processForm.entity.TAbilityApplicationEntity;
import io.renren.modules.resource.dao.ResourceDao; import io.renren.modules.resource.dao.ResourceDao;
@ -20,7 +21,9 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -62,9 +65,12 @@ public class ApiGatewayService {
} }
String apiUrl = resourceEntity.getApiUrl(); String apiUrl = resourceEntity.getApiUrl();
String methods = resourceEntity.getApiMethodType().toUpperCase(); String methods = resourceEntity.getApiMethodType().toUpperCase();
Long deptId = resourceEntity.getDeptId();
if (StringUtils.isBlank(apiUrl) || StringUtils.isBlank(methods)){ HashSet supportMethod = Sets.newHashSet("POST", "GET");
String msg = String.format("注册api参数为空跳过 apiUrl:%s, methods:%s, resourceId:%s", apiUrl, methods, resourceId);
if (StringUtils.isBlank(apiUrl) || deptId == null || deptId == 0 || StringUtils.isBlank(methods) || !supportMethod.contains(methods)){
String msg = String.format("注册api参数为空跳过 apiUrl:%s, deptId:%ld methods:%s, resourceId:%s", apiUrl, deptId, methods, resourceId);
//重要参数没有当成不需要注册 //重要参数没有当成不需要注册
log.info(msg); log.info(msg);
return; return;
@ -76,7 +82,7 @@ public class ApiGatewayService {
if (StringUtils.isBlank(uris)) { if (StringUtils.isBlank(uris)) {
uris = "/"; uris = "/";
} }
String apiPrefix = "/juapi/" + resourceId; String apiPrefix = "/juapi/" + deptId + "/" + resourceId;
HashMap groupEntity = new HashMap(); HashMap groupEntity = new HashMap();
groupEntity.put("id", resourceId); groupEntity.put("id", resourceId);
groupEntity.put("name", resourceEntity.getName()); groupEntity.put("name", resourceEntity.getName());

View File

@ -84,6 +84,8 @@ public class ShiroConfig {
filterMap.put("/upload/**", "anon"); filterMap.put("/upload/**", "anon");
filterMap.put("/census/center/v3/**", "oauth2"); filterMap.put("/census/center/v3/**", "oauth2");
filterMap.put("/census/center/**", "anon"); // 全局各类统计 包含 /census/center/v2 filterMap.put("/census/center/**", "anon"); // 全局各类统计 包含 /census/center/v2
filterMap.put("/metrics/**", "anon");
filterMap.put("/gateway-monitor/", "anon");
filterMap.put("/**", "oauth2"); filterMap.put("/**", "oauth2");
shiroFilter.setFilterChainDefinitionMap(filterMap); shiroFilter.setFilterChainDefinitionMap(filterMap);