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