From b548699124ec237880f4d3ac1b75e85fe2619e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=9D=E5=86=9B?= Date: Wed, 31 Aug 2022 21:33:17 +0800 Subject: [PATCH 1/3] feat: dubbo use new api --- .../apiserver/service/IDubboService.java | 9 +- .../service/impl/DubboServiceImpl.java | 103 +++++------------- .../envoy/meta/RouteRuleProxyInfo.java | 63 +++++++++++ .../impl/GetFromApiPlaneServiceImpl.java | 71 ++++++++---- 4 files changed, 146 insertions(+), 100 deletions(-) diff --git a/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/IDubboService.java b/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/IDubboService.java index 5b45a422..a42808a0 100644 --- a/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/IDubboService.java +++ b/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/IDubboService.java @@ -3,7 +3,6 @@ import org.hango.cloud.dashboard.apiserver.dto.DubboInfoDto; import org.hango.cloud.dashboard.apiserver.meta.DubboInfo; import org.hango.cloud.dashboard.apiserver.meta.errorcode.ErrorCode; -import org.hango.cloud.dashboard.envoy.web.dto.EnvoyRouteRuleHeaderOperationDto; /** * @author zhangbj @@ -83,13 +82,11 @@ public interface IDubboService { ErrorCode checkAndComplete(DubboInfoDto dto); + /** - * 将Dubbo信息转换为Envoy Head to Add 信息 - * + * 设置原始类型默认值 * @param dto - * @param headerOperationDto - * @return */ - EnvoyRouteRuleHeaderOperationDto getDubboHeaderOperation(DubboInfoDto dto, EnvoyRouteRuleHeaderOperationDto headerOperationDto); + void parseDefaultValue(DubboInfoDto dto); } diff --git a/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/impl/DubboServiceImpl.java b/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/impl/DubboServiceImpl.java index b291ca2f..34d7d5ea 100644 --- a/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/impl/DubboServiceImpl.java +++ b/gateway-portal/src/main/java/org/hango/cloud/dashboard/apiserver/service/impl/DubboServiceImpl.java @@ -3,7 +3,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; -import org.apache.commons.lang3.BooleanUtils; +import com.google.common.collect.Maps; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.hango.cloud.dashboard.apiserver.dao.IDubboDao; @@ -13,7 +13,11 @@ import org.hango.cloud.dashboard.apiserver.meta.RegistryCenterEnum; import org.hango.cloud.dashboard.apiserver.meta.errorcode.CommonErrorCode; import org.hango.cloud.dashboard.apiserver.meta.errorcode.ErrorCode; -import org.hango.cloud.dashboard.apiserver.service.*; +import org.hango.cloud.dashboard.apiserver.service.IDubboMetaService; +import org.hango.cloud.dashboard.apiserver.service.IDubboService; +import org.hango.cloud.dashboard.apiserver.service.IRouteRuleInfoService; +import org.hango.cloud.dashboard.apiserver.service.IRouteRuleProxyService; +import org.hango.cloud.dashboard.apiserver.service.IServiceProxyService; import org.hango.cloud.dashboard.apiserver.util.ClassTypeUtil; import org.hango.cloud.dashboard.apiserver.util.Const; import org.hango.cloud.dashboard.apiserver.util.ZkClientUtils; @@ -21,14 +25,17 @@ import org.hango.cloud.dashboard.envoy.meta.RouteRuleInfo; import org.hango.cloud.dashboard.envoy.meta.RouteRuleProxyInfo; import org.hango.cloud.dashboard.envoy.meta.ServiceProxyInfo; -import org.hango.cloud.dashboard.envoy.web.dto.EnvoyRouteRuleHeaderOperationDto; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import static org.hango.cloud.dashboard.apiserver.util.Const.POSITION_COOKIE; @@ -374,89 +381,35 @@ private boolean publishToEnvoy(DubboInfoDto dto, boolean isDelete) { logger.info("发布失败,未找到已发布的路由,已发布路由ID为 {}", dto.getObjectId()); return false; } + parseDefaultValue(dto); RouteRuleInfo routeRule = envoyRouteRuleInfoService.getRouteRuleInfoById(routeRuleProxy.getRouteRuleId()); if (routeRule == null) { logger.info("发布失败,未找到路由,路由ID为 {}", routeRuleProxy.getRouteRuleId()); return false; } - EnvoyRouteRuleHeaderOperationDto headerOperation = JSON.parseObject(routeRule.getHeaderOperation(), EnvoyRouteRuleHeaderOperationDto.class); - headerOperation = getDubboHeaderOperation(isDelete ? null : dto, headerOperation); - routeRuleProxy.setHeaderOperation(headerOperation); + Map metaMap = routeRuleProxy.getMetaMap() == null ? Maps.newHashMap() : routeRuleProxy.getMetaMap(); + metaMap.put("DubboMeta", isDelete ? StringUtils.EMPTY : JSON.toJSONString(dto)); + routeRuleProxy.setMetaMap(metaMap); return Const.ERROR_RESULT != envoyRouteRuleProxyService.updateEnvoyRouteRuleProxy(routeRuleProxy); } - - @Override - public EnvoyRouteRuleHeaderOperationDto getDubboHeaderOperation(DubboInfoDto dto, EnvoyRouteRuleHeaderOperationDto headerOperation) { - if (headerOperation == null) { - headerOperation = new EnvoyRouteRuleHeaderOperationDto(); - } - if (dto == null) { - dto = new DubboInfoDto(); - } - EnvoyRouteRuleHeaderOperationDto.RequestOperation requestOperation = headerOperation.getRequestOperation(); - if (requestOperation == null) { - requestOperation = headerOperation.new RequestOperation(); - headerOperation.setRequestOperation(requestOperation); - } - Map add = requestOperation.getAdd(); - if (add == null) { - add = new HashMap<>(); - requestOperation.setAdd(add); - } - - add.put(Const.HEADER_DUBBO_INTERFACE, dto.getInterfaceName()); - add.put(Const.HEADER_DUBBO_METHOD, dto.getMethod()); - add.put(Const.HEADER_DUBBO_GROUP, dto.getGroup()); - add.put(Const.HEADER_DUBBO_VERSION, dto.getVersion()); - add.put(Const.HEADER_DUBBO_PARAMS, dto.getParamToStr()); - add.put(Const.HEADER_DUBBO_PARAM_SOURCE, dto.getParamSource()); - add.put(Const.HEADER_DUBBO_CUSTOM_PARAMS_MAPPING_SWITCH, BooleanUtils.toStringTrueFalse(dto.getCustomParamMapping())); - List params = dto.getParams(); - if (!CollectionUtils.isEmpty(params)){ - List genericInfoList = params.stream().map(DubboInfoDto.DubboParam::getGenericInfo).collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(genericInfoList)){ - add.put(Const.HEADER_DUBBO_GENERIC, StringUtils.join(genericInfoList, ";")); - } - if (dto.getCustomParamMapping()){ - List defaultValueList = params.stream().map(o -> parseDefaultValue(o.getValue(), o.getDefaultValue())).collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(defaultValueList)){ - add.put(Const.HEADER_DUBBO_DEFAULT, JSONObject.toJSONString(defaultValueList)); - } - List requiredList = params.stream().map(o -> o.isRequired() ? "T" : "F").collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(requiredList)){ - add.put(Const.HEADER_DUBBO_REQUIRED, StringUtils.join(requiredList, ";")); - } - } - - } - List dubboAttachment = dto.getDubboAttachment(); - if (!CollectionUtils.isEmpty(dubboAttachment)){ - List headers = new ArrayList<>(); - List cookies = new ArrayList<>(); - for (DubboInfoDto.DubboAttachmentDto dubboAttachmentDto : dubboAttachment) { - String paramPosition = dubboAttachmentDto.getParamPosition(); - if (POSITION_HEADER.equalsIgnoreCase(paramPosition)){ - headers.add(dubboAttachmentDto.getClientParamName() + ":" + dubboAttachmentDto.getServerParamName()); - }else if (POSITION_COOKIE.equalsIgnoreCase(paramPosition)){ - cookies.add(dubboAttachmentDto.getClientParamName() + ":" + dubboAttachmentDto.getServerParamName()); - - } - } - if (!CollectionUtils.isEmpty(headers)){ - add.put(Const.HEADER_DUBBO_ATTACTMENT_HEADER, String.join(";", headers)); - } - if (!CollectionUtils.isEmpty(cookies)){ - add.put(Const.HEADER_DUBBO_ATTACTMENT_COOKIE, String.join(";", cookies)); - } - } - return headerOperation; - } - public Object parseDefaultValue(String typeString, Object value){ if (value == null && ClassTypeUtil.PrimitiveTypeEnum.isPrimitiveType(typeString)){ return ClassTypeUtil.PrimitiveTypeEnum.getDefaultValueByName(typeString); } return value; } + + public void parseDefaultValue(DubboInfoDto.DubboParam param){ + Object o = parseDefaultValue(param.getValue(), param.getDefaultValue()); + param.setDefaultValue(o); + } + + @Override + public void parseDefaultValue(DubboInfoDto dto){ + List params = dto.getParams(); + if (!CollectionUtils.isEmpty(params)){ + params.stream().forEach(this::parseDefaultValue); + } + } } diff --git a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/meta/RouteRuleProxyInfo.java b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/meta/RouteRuleProxyInfo.java index 9fa4f347..b7c58c37 100644 --- a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/meta/RouteRuleProxyInfo.java +++ b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/meta/RouteRuleProxyInfo.java @@ -6,6 +6,7 @@ import org.hango.cloud.dashboard.envoy.web.dto.HttpRetryDto; import java.util.List; +import java.util.Map; /** * 路由规则发布信息 @@ -101,6 +102,60 @@ public class RouteRuleProxyInfo extends RouteRuleMatchInfo { */ private long mirrorServiceId; + /** + * meta数据传输集 + * Map + * mata_type meta类型 + * + * mata_type: 路由meta数据类型 + * meta_data: 路由meta数据值,使用JSON传输 + * eg. + * { + * "DubboMeta": { + * "ObjectType": "route", + * "ObjectId": 259, + * "Params": [ + * { + * "Key": "str", + * "Value": "java.lang.String", + * "GenericInfo": ".dataA:com.demo.B,.dataAA:com.demo.B,.dataA.dataB:com.demo.C,.dataAA.dataB:com.demo.C", + * "Required": false, + * "DefaultValue": "sdfsdfs", + * "_formTableKey": 1660649073793, + * "index": 0 + * }, + * { + * "Key": "wer", + * "Value": "java.lang.Integer", + * "GenericInfo": null, + * "Required": true, + * "DefaultValue": null, + * "_formTableKey": 1660649073793 + * } + * ], + * "Method": "echoStrAndInt", + * "CustomParamMapping": true, + * "ParamSource": "body", + * "Attachment": [ + * { + * "ClientParamName": "xcvxcv", + * "Description": "cxvxcv", + * "ParamPosition": "Header", + * "ServerParamName": "cvcv", + * "distinctName": "Headerxcvxcv", + * "_formTableKey": 1660648830195 + * } + * ], + * "MethodWorks": true + * }, + * "StatsMeta": [ + * "/test", + * "/test1" + * ] + * } + */ + private Map metaMap; + public long getMirrorServiceId() { return mirrorServiceId; } @@ -269,6 +324,14 @@ public void setGwType(String gwType) { this.gwType = gwType; } + public Map getMetaMap() { + return metaMap; + } + + public void setMetaMap(Map metaMap) { + this.metaMap = metaMap; + } + @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); diff --git a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java index fdc7c737..da875885 100644 --- a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java +++ b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java @@ -7,6 +7,7 @@ import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.hango.cloud.dashboard.apiserver.config.ApiServerConfig; +import org.hango.cloud.dashboard.apiserver.dto.DubboInfoDto; import org.hango.cloud.dashboard.apiserver.dto.DubboMetaDto; import org.hango.cloud.dashboard.apiserver.dto.RegistryCenterDto; import org.hango.cloud.dashboard.apiserver.meta.GatewayInfo; @@ -21,7 +22,6 @@ import org.hango.cloud.dashboard.apiserver.service.IRouteRuleProxyService; import org.hango.cloud.dashboard.apiserver.service.IServiceInfoService; import org.hango.cloud.dashboard.apiserver.service.IServiceProxyService; -import org.hango.cloud.dashboard.apiserver.service.impl.DubboServiceImpl; import org.hango.cloud.dashboard.apiserver.util.Const; import org.hango.cloud.dashboard.apiserver.web.holder.ActionInfoHolder; import org.hango.cloud.dashboard.apiserver.web.holder.UserPermissionHolder; @@ -349,7 +349,6 @@ public JSONObject generateBodyForPublishOrDelete(GatewayInfo gatewayInfo, RouteR body.put("Order", routeRuleProxyInfo.getOrders()); body.put("ProjectId", routeRuleInfo.getProjectId()); EnvoyRouteRuleHeaderOperationDto headerOperation = JSON.parseObject(routeRuleInfo.getHeaderOperation(), EnvoyRouteRuleHeaderOperationDto.class); - headerOperation = processDubboInfoForPublishOrDelete(routeRuleInfo, routeRuleProxyInfo, headerOperation); if (headerOperation != null) { body.put("RequestOperation", headerOperation.getRequestOperation()); } @@ -430,35 +429,69 @@ public JSONObject generateBodyForPublishOrDelete(GatewayInfo gatewayInfo, RouteR body.put("MirrorTraffic", mirrorTraffic); } + body.put("MetaMap",processRouteMetadata(routeRuleProxyInfo)); return body; } /** - * 处理Dubbo路由额外信息 - *

- * 优先使用RouteRuleProxyInfo中的路由头信息 - * 该属性仅在单独处理Dubbo转换信息时,才被填充{@link DubboServiceImpl} + * 添加路由 metadata 数据 * - * @param routeRuleInfo * @param routeRuleProxyInfo - * @param headerOperation * @return */ - private EnvoyRouteRuleHeaderOperationDto processDubboInfoForPublishOrDelete(RouteRuleInfo routeRuleInfo, RouteRuleProxyInfo routeRuleProxyInfo, EnvoyRouteRuleHeaderOperationDto headerOperation) { - // - // - if (routeRuleProxyInfo.getHeaderOperation() != null) { - return routeRuleProxyInfo.getHeaderOperation(); - } - //dubbo 路由Head-To-Add组装 - ServiceInfo serviceInfo = serviceInfoService.getServiceById(String.valueOf(routeRuleInfo.getServiceId())); + private Map processRouteMetadata(RouteRuleProxyInfo routeRuleProxyInfo) { + Map metaMap = routeRuleProxyInfo.getMetaMap() == null ? Maps.newHashMap() : routeRuleProxyInfo.getMetaMap(); + //处理路由指标Meta数据 + processRouteStatsMeta(routeRuleProxyInfo, metaMap); + //处理Dubbo Meta相关的数据 + processDubboMeta(routeRuleProxyInfo, metaMap); + return metaMap; + } + + /** + * 添加路由指标Meta数据 + * + * @param routeRuleProxyInfo + * @param metaMap + */ + private void processRouteStatsMeta(RouteRuleProxyInfo routeRuleProxyInfo, Map metaMap) { + if (!apiServerConfig.getRouteMetricEnable()) { + return; + } + String statsValue = apiServerConfig.getRouteMetricPathStats() ? routeRuleProxyInfo.getUri() : String.valueOf(routeRuleProxyInfo.getRouteRuleId()); + Map stats = Maps.newHashMap(); + stats.put("route_rule_id", statsValue); + metaMap.put("StatsMeta", JSON.toJSONString(stats)); + } + + /** + * 添加Dubbo Meta相关的数据 + * + * @param routeRuleProxyInfo + * @param metaMap + */ + private void processDubboMeta(RouteRuleProxyInfo routeRuleProxyInfo, Map metaMap) { + //如果已存在就不进行复写 + //适用于Dubbo在创建、更新、删除的场景 + //@see DubboServiceImpl publishToEnvoy + String dubboMeta = "DubboMeta"; + if (metaMap.containsKey(dubboMeta)){ + return; + } + ServiceInfo serviceInfo = serviceInfoService.getServiceById(String.valueOf(routeRuleProxyInfo.getServiceId())); if (serviceInfo == null) { - return headerOperation; + return; } if (!ServiceType.dubbo.name().equals(serviceInfo.getServiceType())) { - return headerOperation; + return; + } + DubboInfoDto dubboDto = dubboService.getDubboDto(routeRuleProxyInfo.getId(), Const.ROUTE); + if (dubboDto == null) { + return; } - return dubboService.getDubboHeaderOperation(dubboService.getDubboDto(routeRuleProxyInfo.getId(), Const.ROUTE), headerOperation); + dubboService.parseDefaultValue(dubboDto); + metaMap.put(dubboMeta, JSON.toJSONString(dubboDto)); + } /** From 0aa9132358e7627d46d1f74eae0a6aafa719a34f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=AE=9D=E5=86=9B?= Date: Wed, 14 Sep 2022 16:35:49 +0800 Subject: [PATCH 2/3] feat: dubbo route full-republish logic enhanced --- .../envoy/service/impl/GetFromApiPlaneServiceImpl.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java index da875885..41ddcd2c 100644 --- a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java +++ b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java @@ -10,6 +10,7 @@ import org.hango.cloud.dashboard.apiserver.dto.DubboInfoDto; import org.hango.cloud.dashboard.apiserver.dto.DubboMetaDto; import org.hango.cloud.dashboard.apiserver.dto.RegistryCenterDto; +import org.hango.cloud.dashboard.apiserver.meta.DubboInfo; import org.hango.cloud.dashboard.apiserver.meta.GatewayInfo; import org.hango.cloud.dashboard.apiserver.meta.HttpClientResponse; import org.hango.cloud.dashboard.apiserver.meta.RegistryCenterEnum; @@ -485,10 +486,11 @@ private void processDubboMeta(RouteRuleProxyInfo routeRuleProxyInfo, Map Date: Fri, 30 Dec 2022 15:55:20 +0800 Subject: [PATCH 3/3] fix/to: delete unused config --- .../envoy/service/impl/GetFromApiPlaneServiceImpl.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java index 41ddcd2c..e271775d 100644 --- a/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java +++ b/gateway-portal/src/main/java/org/hango/cloud/dashboard/envoy/service/impl/GetFromApiPlaneServiceImpl.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.hango.cloud.dashboard.apiserver.config.ApiServerConfig; @@ -456,9 +457,6 @@ private Map processRouteMetadata(RouteRuleProxyInfo routeRulePro * @param metaMap */ private void processRouteStatsMeta(RouteRuleProxyInfo routeRuleProxyInfo, Map metaMap) { - if (!apiServerConfig.getRouteMetricEnable()) { - return; - } String statsValue = apiServerConfig.getRouteMetricPathStats() ? routeRuleProxyInfo.getUri() : String.valueOf(routeRuleProxyInfo.getRouteRuleId()); Map stats = Maps.newHashMap(); stats.put("route_rule_id", statsValue);