From 3da00d0ce93890fe15b1c271418a4eeb36612636 Mon Sep 17 00:00:00 2001 From: lizhicheng Date: Thu, 8 Sep 2022 10:30:17 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BD=91=E5=85=B3?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- renren-admin/src/main/resources/application.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/renren-admin/src/main/resources/application.yml b/renren-admin/src/main/resources/application.yml index 11f3d504..2e56a66f 100644 --- a/renren-admin/src/main/resources/application.yml +++ b/renren-admin/src/main/resources/application.yml @@ -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 From 3c51f94840461dbbba2100b482776a843a5f139a Mon Sep 17 00:00:00 2001 From: lizhicheng Date: Thu, 8 Sep 2022 10:31:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BA=91=E8=84=91=E7=BE=A4=E7=BD=91?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/HibrianApiGateway.java | 130 ++++++++---------- 1 file changed, 58 insertions(+), 72 deletions(-) diff --git a/renren-admin/src/main/java/io/renren/modules/processForm/service/impl/HibrianApiGateway.java b/renren-admin/src/main/java/io/renren/modules/processForm/service/impl/HibrianApiGateway.java index 982ab91a..d5a85ed8 100644 --- a/renren-admin/src/main/java/io/renren/modules/processForm/service/impl/HibrianApiGateway.java +++ b/renren-admin/src/main/java/io/renren/modules/processForm/service/impl/HibrianApiGateway.java @@ -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 updateWrapper = new UpdateWrapper().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 responseEntity = restTemplate.postForEntity(serviceUrl, serviceEntity, HashMap.class); + ResponseEntity 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 routeResEntity = restTemplate.postForEntity(routeUrl, routeEntity, HashMap.class); + ResponseEntity 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 updateWrapper = new UpdateWrapper().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 forEntity = restTemplate.getForEntity(apiQueryUrl, HashMap.class); - HashMap body = forEntity.getBody(); + HashMap body = filterRoutes(serviceId).getBody(); if (!body.isEmpty()) { - List content = (List) body.get("data"); - for (Map map : content) { + for (Map map : (List) 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 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)); + } + } + }