From 4638a6ad94a2118c627b6246f320d9fc15ee719c Mon Sep 17 00:00:00 2001 From: huangweixiong Date: Wed, 27 Apr 2022 16:32:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E6=9E=B6=E5=AE=A1=E6=89=B9=E7=BB=93?= =?UTF-8?q?=E6=9D=9F=E6=8E=A8=E9=80=81api=E7=BD=91=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/RestTemplateConfig.java | 34 ++++++ .../service/ApiGatewayService.java | 111 ++++++++++++++++++ .../listener/ResourceOwnerListener.java | 19 ++- 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 renren-admin/src/main/java/io/renren/common/config/RestTemplateConfig.java create mode 100644 renren-admin/src/main/java/io/renren/modules/processForm/service/ApiGatewayService.java diff --git a/renren-admin/src/main/java/io/renren/common/config/RestTemplateConfig.java b/renren-admin/src/main/java/io/renren/common/config/RestTemplateConfig.java new file mode 100644 index 00000000..f910548b --- /dev/null +++ b/renren-admin/src/main/java/io/renren/common/config/RestTemplateConfig.java @@ -0,0 +1,34 @@ +package io.renren.common.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpRequest; +import org.springframework.http.client.*; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.Collections; + +@Configuration +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate(ClientHttpRequestFactory factory) { + RestTemplate restTemplate = new RestTemplate(factory); + restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8"))); + return restTemplate; + } + + @Bean + public ClientHttpRequestFactory simpleClientHttpRequestFactory() { + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); + factory.setReadTimeout(30000);//单位为ms + factory.setConnectTimeout(30000);//单位为ms + return factory; + } +} + + diff --git a/renren-admin/src/main/java/io/renren/modules/processForm/service/ApiGatewayService.java b/renren-admin/src/main/java/io/renren/modules/processForm/service/ApiGatewayService.java new file mode 100644 index 00000000..38544dac --- /dev/null +++ b/renren-admin/src/main/java/io/renren/modules/processForm/service/ApiGatewayService.java @@ -0,0 +1,111 @@ +package io.renren.modules.processForm.service; + + +import com.alibaba.fastjson.JSON; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; +import io.renren.modules.resource.dao.ResourceDao; +import io.renren.modules.resource.entity.ResourceEntity; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Service +@Slf4j +public class ApiGatewayService { + + @Autowired + private ResourceDao resourceDao; + + @Autowired + private RestTemplate restTemplate; + + @Value("${hisense.gateway.url: http://devtest-security-app.hismarttv.com:8080}") + private String gatewayUrl; + + /** + * @param resourceId 能力资源的id + * @return + */ + public void registerApi2Gateway(String resourceId){ + + if (resourceId == null) { + log.warn("传入resourceId为空"); + return; + } + + ResourceEntity resourceEntity = resourceDao.selectById(resourceId); + String apiUrl = resourceEntity.getApiUrl(); + + if (apiUrl == null || !apiUrl.startsWith("http")){ + log.warn("非法apiurl!! apiUrl:{} resourceId:{}",apiUrl, resourceId); + return; + } + + //建group + String domain = getIP(apiUrl); + HashMap groupEntity = new HashMap(); + groupEntity.put("name", resourceEntity.getName()); + groupEntity.put("serviceName",domain ); + + String groupUrl = gatewayUrl + "/apiops/api/groups"; + ResponseEntity responseEntity = restTemplate.postForEntity(groupUrl, groupEntity, HashMap.class); + if (responseEntity.getStatusCode() == HttpStatus.OK && responseEntity.hasBody()) { + HashMap body = responseEntity.getBody(); + String id = (String) body.get("id"); + if (StringUtils.isBlank(id)){ + log.error("创建group时id为空 {} body:{}", JSON.toJSONString(groupEntity), body); + return; + } + + //建路由(接口url) + String routeUrl = gatewayUrl + "/apisix/admin/routes"; + HashMap routeEntity = new HashMap(); + routeEntity.put("name", "api:1:" + resourceEntity.getName()); + routeEntity.put("group", id); + routeEntity.put("methods", resourceEntity.getApiMethodType().toUpperCase()); + routeEntity.put("uris", apiUrl.substring(apiUrl.indexOf(domain) + domain.length())); + ResponseEntity routeResEntity = restTemplate.postForEntity(routeUrl, routeEntity, HashMap.class); + if (routeResEntity.getStatusCode() != HttpStatus.OK || !responseEntity.hasBody()){ + //失败则删除group + restTemplate.delete(groupUrl + "/" + id); + }else { + resourceEntity.setGroupId(id); + LambdaUpdateWrapper updateWrapper = new UpdateWrapper().lambda(); + updateWrapper.eq(ResourceEntity::getId, resourceEntity.getId()); + updateWrapper.set(ResourceEntity::getGroupId, id); + resourceDao.update(resourceEntity, updateWrapper); + } + + } + } + + 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 = ""; + Pattern pattern = Pattern.compile(re); + Matcher matcher = pattern.matcher(url); + if (matcher.matches()) { + str = url; + } else { + String[] split2 = url.split(re); + if (split2.length > 1) { + String substring = url.substring(0, url.length() - split2[1].length()); + str = substring; + } else { + str = split2[0]; + } + } + int lastIndexOf = str.lastIndexOf("/"); + return str.substring(lastIndexOf+1); + } +} diff --git a/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java b/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java index 811346ff..9aa6f696 100644 --- a/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java +++ b/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java @@ -3,6 +3,7 @@ package io.renren.modules.resourceMountApply.listener; import com.alibaba.fastjson.JSONObject; import com.google.gson.Gson; import com.google.gson.JsonElement; +import io.renren.modules.processForm.service.ApiGatewayService; import io.renren.modules.resource.dto.ResourceDTO; import io.renren.modules.resourceMountApply.dto.TResourceMountApplyDTO; import io.renren.modules.sys.dto.SysDeptDTO; @@ -43,7 +44,7 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A @Autowired private SysUserService sysUserService; @Autowired - private SysRoleUserService sysRoleUserService; + private ApiGatewayService apiGatewayService; @Autowired private SysDeptService sysDeptService; @@ -68,6 +69,9 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A case EVENTNAME_CREATE: // 创建当前审批节点事件 create(delegateTask, roleDTO); break; + case EVENTNAME_COMPLETE: + complete(delegateTask); + break; default: logger.error("未处理该事件:" + eventName); } @@ -93,6 +97,19 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A return false; } + /** + * 流程结束,推送 + * @param delegateTask + */ + private void complete(DelegateTask delegateTask) { + Map kv = delegateTask.getVariables(); + Gson gson = new Gson(); + JsonElement jsonElement = gson.toJsonTree(kv); + TResourceMountApplyDTO resourceMountApplyDTO = gson.fromJson(jsonElement, TResourceMountApplyDTO.class); + Long resourceID = resourceMountApplyDTO.getResourceDTO().getId(); + apiGatewayService.registerApi2Gateway(String.valueOf(resourceID)); + } + /** * 节点创建时动态分配资源部门审核人 *