上架审批结束推送api网关

This commit is contained in:
huangweixiong 2022-04-27 16:32:58 +08:00
parent a09436b162
commit 4638a6ad94
3 changed files with 163 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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<HashMap> 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<HashMap> routeResEntity = restTemplate.postForEntity(routeUrl, routeEntity, HashMap.class);
if (routeResEntity.getStatusCode() != HttpStatus.OK || !responseEntity.hasBody()){
//失败则删除group
restTemplate.delete(groupUrl + "/" + id);
}else {
resourceEntity.setGroupId(id);
LambdaUpdateWrapper<ResourceEntity> updateWrapper = new UpdateWrapper<ResourceEntity>().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);
}
}

View File

@ -3,6 +3,7 @@ package io.renren.modules.resourceMountApply.listener;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import io.renren.modules.processForm.service.ApiGatewayService;
import io.renren.modules.resource.dto.ResourceDTO; import io.renren.modules.resource.dto.ResourceDTO;
import io.renren.modules.resourceMountApply.dto.TResourceMountApplyDTO; import io.renren.modules.resourceMountApply.dto.TResourceMountApplyDTO;
import io.renren.modules.sys.dto.SysDeptDTO; import io.renren.modules.sys.dto.SysDeptDTO;
@ -43,7 +44,7 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A
@Autowired @Autowired
private SysUserService sysUserService; private SysUserService sysUserService;
@Autowired @Autowired
private SysRoleUserService sysRoleUserService; private ApiGatewayService apiGatewayService;
@Autowired @Autowired
private SysDeptService sysDeptService; private SysDeptService sysDeptService;
@ -68,6 +69,9 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A
case EVENTNAME_CREATE: // 创建当前审批节点事件 case EVENTNAME_CREATE: // 创建当前审批节点事件
create(delegateTask, roleDTO); create(delegateTask, roleDTO);
break; break;
case EVENTNAME_COMPLETE:
complete(delegateTask);
break;
default: default:
logger.error("未处理该事件:" + eventName); logger.error("未处理该事件:" + eventName);
} }
@ -93,6 +97,19 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A
return false; return false;
} }
/**
* 流程结束推送
* @param delegateTask
*/
private void complete(DelegateTask delegateTask) {
Map<String, Object> 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));
}
/** /**
* 节点创建时动态分配资源部门审核人 * 节点创建时动态分配资源部门审核人
* *