Merge branch 'master' into docker_package

This commit is contained in:
wangliwen 2022-09-08 10:33:57 +08:00
commit 4467a80b7a
2 changed files with 59 additions and 72 deletions

View File

@ -3,11 +3,12 @@ package io.renren.modules.processForm.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import io.renren.modules.processForm.dao.TAbilityApplicationDao;
import io.renren.modules.processForm.entity.TAbilityApplicationEntity;
import io.renren.modules.processForm.service.ApiGateway;
import io.renren.modules.resource.dao.ResourceDao;
import io.renren.modules.resource.dto.ResourceDTO;
import io.renren.modules.resource.entity.AttrEntity;
import io.renren.modules.resource.entity.ResourceEntity;
import io.renren.modules.resource.service.ResourceService;
@ -33,9 +34,6 @@ import java.util.regex.Pattern;
@Slf4j
public class HibrianApiGateway implements ApiGateway {
@Autowired
private ResourceDao resourceDao;
@Autowired
private ResourceService resourceService;
@ -55,39 +53,16 @@ public class HibrianApiGateway implements ApiGateway {
if (StringUtils.isBlank(formId) || StringUtils.isBlank(code)) {
throw new IllegalArgumentException(String.format("关键参数不能为空 formId:%s code:%s", formId, code));
}
TAbilityApplicationEntity applicationEntity = abilityApplicationDao.selectById(formId);
ResourceEntity resourceEntity = resourceDao.selectById(applicationEntity.getResourceId());
String serviceId = resourceEntity.getGroupId();
if (resourceEntity == null) {
throw new RuntimeException(String.format("找不到资源类 serviceId:%s", serviceId));
ResourceDTO resourceDTO = resourceService.get(Long.parseLong(applicationEntity.getResourceId()));
if (resourceDTO == null) {
throw new RuntimeException(String.format("找不到资源类 serviceId:%s", applicationEntity.getResourceId()));
}
String serviceId = resourceDTO.getGroupId();
//注册消费者一个表单关联一个消费者
HashMap consumerEntity = new HashMap();
//consumerEntity.put("id", formId);
consumerEntity.put("username", formId);
consumerEntity.put("key", code);
String consumerUrl = gatewayUrl + API_PREFIX + "/v1/consumers";
restTemplate.put(consumerUrl, consumerEntity, HashMap.class);
// if (consumerResponse == null || !formId.equals(consumerResponse.get("id"))) {
// throw new RuntimeException(String.format("消费者创建失败 response: %s", consumerResponse));
// }
putConsumers(ImmutableMap.of("username", formId, "key", code));
//订阅
HashMap subscribeEntity = new HashMap();
subscribeEntity.put("consumerId", formId);
subscribeEntity.put("resourceId", serviceId);
subscribeEntity.put("resourceType", "service");
String subscribeUrl = gatewayUrl + API_PREFIX + "/subscribers";
HashMap body = restTemplate.postForEntity(subscribeUrl, subscribeEntity, HashMap.class).getBody();
if (body == null || HttpStatus.OK.value() != Integer.parseInt(body.get("code").toString())) {
throw new RuntimeException(String.format("订阅失败 response: %s", body));
}
postSubscribers(ImmutableMap.of("consumerId", formId, "resourceId", serviceId, "resourceType", "service"));
LambdaUpdateWrapper<TAbilityApplicationEntity> updateWrapper = new UpdateWrapper<TAbilityApplicationEntity>().lambda()
.eq(TAbilityApplicationEntity::getId, formId)
.set(TAbilityApplicationEntity::getGatewayCode, code);
@ -99,9 +74,8 @@ public class HibrianApiGateway implements ApiGateway {
if (resourceId == null) {
throw new IllegalArgumentException("传入resourceId为空");
}
ResourceEntity resourceEntity = resourceDao.selectById(resourceId);
if (resourceEntity == null) {
ResourceDTO resourceDTO = resourceService.get(Long.parseLong(resourceId));
if (resourceDTO == null) {
throw new IllegalArgumentException(String.format("未找到对应的资源id:%s", resourceId));
}
String apiUrl = "";
@ -110,21 +84,17 @@ public class HibrianApiGateway implements ApiGateway {
apiUrl = optional.get().getAttrValue();
}
String methods = "";
if (resourceEntity.getApiMethodType() != null) {
methods = resourceEntity.getApiMethodType().toUpperCase();
if (resourceDTO.getApiMethodType() != null) {
methods = resourceDTO.getApiMethodType().toUpperCase();
}
Long deptId = resourceEntity.getDeptId();
Long deptId = resourceDTO.getDeptId();
HashSet supportMethod = Sets.newHashSet("POST", "GET");
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);
return;
}
//创建service
String domain = getIP(apiUrl);
String uris = apiUrl.substring(apiUrl.indexOf(domain) + domain.length());
@ -134,12 +104,10 @@ public class HibrianApiGateway implements ApiGateway {
String apiPrefix = "/hibrianapi/" + deptId + "/" + resourceId;
HashMap serviceEntity = new HashMap();
serviceEntity.put("id", resourceId);
serviceEntity.put("name", resourceEntity.getName());
serviceEntity.put("name", resourceDTO.getName());
serviceEntity.put("stripPrefixPattern", String.format("^%s/(.*)", apiPrefix));
serviceEntity.put("nodes", domain);
String serviceUrl = gatewayUrl + API_PREFIX + "/v1/services";
ResponseEntity<HashMap> responseEntity = restTemplate.postForEntity(serviceUrl, serviceEntity, HashMap.class);
ResponseEntity<HashMap> responseEntity = postServices(serviceEntity);
if (responseEntity.getStatusCode() == HttpStatus.OK && responseEntity.hasBody()) {
HashMap body = responseEntity.getBody();
@ -151,23 +119,22 @@ public class HibrianApiGateway implements ApiGateway {
throw new RuntimeException(error);
}
//建路由接口url
String routeUrl = gatewayUrl + API_PREFIX + "/v1/routes";
HashMap routeEntity = new HashMap();
routeEntity.put("name", "api:1:" + resourceEntity.getName());
routeEntity.put("name", "api:1:" + resourceDTO.getName());
routeEntity.put("service_id", id);
routeEntity.put("methods", methods);
routeEntity.put("uris", apiPrefix + uris);
routeEntity.put("enableMetric", true);
ResponseEntity<HashMap> routeResEntity = restTemplate.postForEntity(routeUrl, routeEntity, HashMap.class);
ResponseEntity<HashMap> routeResEntity = postRoutes(routeEntity);
if (routeResEntity.getStatusCode() != HttpStatus.OK || !responseEntity.hasBody() || HttpStatus.OK.value() != Integer.parseInt(routeResEntity.getBody().get("code").toString())) {
//失败则删除group
restTemplate.delete(serviceUrl + "/" + id);
//失败则删除service
deleteServices(id);
} else {
resourceEntity.setGroupId(id);
resourceDTO.setGroupId(id);
LambdaUpdateWrapper<ResourceEntity> updateWrapper = new UpdateWrapper<ResourceEntity>().lambda();
updateWrapper.eq(ResourceEntity::getId, resourceEntity.getId());
updateWrapper.eq(ResourceEntity::getId, resourceDTO.getId());
updateWrapper.set(ResourceEntity::getGroupId, id);
resourceDao.update(null, updateWrapper);
resourceService.update(null, updateWrapper);
}
}
}
@ -175,31 +142,18 @@ public class HibrianApiGateway implements ApiGateway {
@Override
public void resetApiGroup(String serviceId) {
String apiQueryUrl = gatewayUrl + API_PREFIX + "/v1/routes/filter?serviceId=" + serviceId;
ResponseEntity<HashMap> forEntity = restTemplate.getForEntity(apiQueryUrl, HashMap.class);
HashMap body = forEntity.getBody();
HashMap body = filterRoutes(serviceId).getBody();
if (!body.isEmpty()) {
List<Map> content = (List<Map>) body.get("data");
for (Map map : content) {
for (Map map : (List<Map>) body.get("data")) {
String id = (String) ((Map) map.get("value")).get("id");
if (StringUtils.isNotBlank(id)) {
String apiDelUrl = gatewayUrl + API_PREFIX + "/v1/routes/" + id;
try {
restTemplate.delete(apiDelUrl);
} catch (Exception e) {
}
deleteRoutes(id);
}
}
deleteGroup(serviceId);
deleteServices(serviceId);
}
}
public void deleteGroup(String serviceId) {
String serviceUrl = gatewayUrl + API_PREFIX + "/v1/services";
restTemplate.delete(serviceUrl + "/" + serviceId);
}
private String getIP(String url) {
String re = "((http|ftp|https)://)(([a-zA-Z0-9._-]+)|([0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}))(([a-zA-Z]{2,6})|(:[0-9]{1,4})?)";
String str = "";
@ -219,4 +173,36 @@ public class HibrianApiGateway implements ApiGateway {
int lastIndexOf = str.lastIndexOf("/");
return str.substring(lastIndexOf + 1);
}
private void deleteServices(String serviceId) {
restTemplate.delete(gatewayUrl + API_PREFIX + "/v1/services/" + serviceId);
}
private ResponseEntity postServices(Map serviceEntity){
return restTemplate.postForEntity(gatewayUrl + API_PREFIX + "/v1/services", serviceEntity, HashMap.class);
}
private ResponseEntity postRoutes(Map routeEntity){
return restTemplate.postForEntity(gatewayUrl + API_PREFIX + "/v1/routes", routeEntity, HashMap.class);
}
private void deleteRoutes(String routeId){
restTemplate.delete(gatewayUrl + API_PREFIX + "/v1/routes/" + routeId);
}
private ResponseEntity<HashMap> filterRoutes(String serviceId){
return restTemplate.getForEntity(gatewayUrl + API_PREFIX + "/v1/routes/filter?serviceId=" + serviceId, HashMap.class);
}
private void putConsumers(Map consumerEntity){
restTemplate.put(gatewayUrl + API_PREFIX + "/v1/consumers", consumerEntity, HashMap.class);
}
private void postSubscribers(Map subscribeEntity){
HashMap body = restTemplate.postForEntity(gatewayUrl + API_PREFIX + "/subscribers", subscribeEntity, HashMap.class).getBody();
if (body == null || HttpStatus.OK.value() != Integer.parseInt(body.get("code").toString())) {
throw new RuntimeException(String.format("订阅失败 response: %s", body));
}
}
}

View File

@ -22,6 +22,7 @@ census:
hisense:
gateway:
sync-enabled: true
name: 聚好看网关
url: http://devtest-security-app.hismarttv.com:8080
dp-url: http://devtest-security-app.hismarttv.com:8080
# Tomcat