Skip to content

Commit

Permalink
feat: dubbo use new api
Browse files Browse the repository at this point in the history
  • Loading branch information
markGbug authored and trickMin committed Nov 28, 2022
1 parent dcde964 commit 263d4d0
Show file tree
Hide file tree
Showing 10 changed files with 852 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package org.hango.cloud.core.gateway.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import me.snowdrop.istio.api.networking.v1alpha3.VirtualService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.hango.cloud.core.gateway.handler.meta.UriMatchMeta;
import org.hango.cloud.core.template.TemplateParams;
import org.hango.cloud.meta.API;
import org.hango.cloud.meta.CRDMetaEnum;
import org.hango.cloud.meta.PairMatch;
import org.hango.cloud.meta.UriMatch;
import org.hango.cloud.meta.dto.DubboInfoDto;
import org.hango.cloud.util.CommonUtil;
import org.hango.cloud.util.PriorityUtil;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.hango.cloud.core.template.TemplateConst.*;

public abstract class APIDataHandler implements DataHandler<API> {

private static final Logger logger = LoggerFactory.getLogger(APIDataHandler.class);

@Override
public List<TemplateParams> handle(API api) {
TemplateParams tp = handleApi(api);
Expand Down Expand Up @@ -75,11 +85,82 @@ public TemplateParams handleApi(API api) {
.put(VIRTUAL_SERVICE_REQUEST_HEADERS, api.getRequestOperation())
.put(VIRTUAL_SERVICE_VIRTUAL_CLUSTER_NAME, api.getVirtualClusterName())
.put(VIRTUAL_SERVICE_VIRTUAL_CLUSTER_HEADERS, getVirtualClusterHeaders(api))
.put(VIRTUAL_SERVICE_STATS, api.getStatsMeta());
;

return handleApiMetaMap(api,tp);
}

/**
* 处理VirtualService metadata 数据
*
* @param api 上层输入的API数据
* @param tp 模板参数
* @return TemplateParams
*/
private TemplateParams handleApiMetaMap(API api, TemplateParams tp) {
if (CollectionUtils.isEmpty(api.getMetaMap())) {
return tp;
}
Iterator<Map.Entry<String, String>> iterator = api.getMetaMap().entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
handleApiMeta(next.getKey(), next.getValue(), tp);
}
return tp;
}

/**
* 处理VirtualService metadata 数据
*
* @param name 上层输入的metadata类型
* @param value 上层输入的metadata数据
* @param tp 模板参数
* @return TemplateParams
*/
private TemplateParams handleApiMeta(String name, String value, TemplateParams tp) {
CRDMetaEnum metaEnum = CRDMetaEnum.get(VirtualService.class, name);
if (metaEnum == null) {
logger.warn("find null meta enum ,please check input content , target class is {} , name is {} ", VirtualService.class, name);
return tp;
}
try {
switch (metaEnum) {
case VIRTUAL_SERVICE_STATS_META:
tp.put(metaEnum.getTemplateName(), metaEnum.getTransData(value));
break;
case VIRTUAL_SERVICE_DUBBO_META:
handleDubboMeta(tp, metaEnum.getTransData(value));
break;
default:
break;
}
} catch (JsonProcessingException e) {
logger.warn("meta content parse failed , errMsg is {}", e.getMessage());
}
return tp;
}

/**
* 将Dubbo meta 元数据信息加入模板参数中
*
* @param tp 模板参数
* @param info dubbo meta信息
* @return TemplateParams
*/
private TemplateParams handleDubboMeta(TemplateParams tp, DubboInfoDto info) {
tp.put(VIRTUAL_SERVICE_DUBBO_META_SERVICE, info.getInterfaceName())
.put(VIRTUAL_SERVICE_DUBBO_META_VERSION, info.getVersion())
.put(VIRTUAL_SERVICE_DUBBO_META_METHOD, info.getMethod())
.put(VIRTUAL_SERVICE_DUBBO_META_GROUP, info.getGroup())
.put(VIRTUAL_SERVICE_DUBBO_META_SOURCE, info.getParamSource())
.put(VIRTUAL_SERVICE_DUBBO_META_PARAMS, info.getParams())
.put(VIRTUAL_SERVICE_DUBBO_META_ATTACHMENTS, info.getDubboAttachment())
;
return tp;
}



protected String getOrDefault(String value, String defaultValue) {
return StringUtils.isEmpty(value) ? defaultValue : value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public interface TemplateConst {
String VIRTUAL_SERVICE_MIRROR_SUBSET = "t_virtual_service_mirror_subset";
String VIRTUAL_SERVICE_MIRROR_YAML = "t_virtual_service_mirror_yaml";
String VIRTUAL_SERVICE_STATS = "t_virtual_service_stats";
String VIRTUAL_SERVICE_DUBBO = "t_virtual_service_dubbo";

/**
* ServiceInfo
Expand Down Expand Up @@ -265,4 +266,15 @@ public interface TemplateConst {
*
*/

/**
* Dubbo Meta 相关信息
*/
String VIRTUAL_SERVICE_DUBBO_META_SERVICE ="t_virtual_service_dubbo_meta_service";
String VIRTUAL_SERVICE_DUBBO_META_VERSION ="t_virtual_service_dubbo_meta_version";
String VIRTUAL_SERVICE_DUBBO_META_METHOD ="t_virtual_service_dubbo_meta_method";
String VIRTUAL_SERVICE_DUBBO_META_GROUP ="t_virtual_service_dubbo_meta_group";
String VIRTUAL_SERVICE_DUBBO_META_SOURCE ="t_virtual_service_dubbo_meta_source";
String VIRTUAL_SERVICE_DUBBO_META_PARAMS ="t_virtual_service_dubbo_meta_params";
String VIRTUAL_SERVICE_DUBBO_META_ATTACHMENTS ="t_virtual_service_dubbo_meta_attachments";

}
63 changes: 57 additions & 6 deletions hango-api-plane-server/src/main/java/org/hango/cloud/meta/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import java.util.List;
import java.util.Map;


public class API extends CommonModel {
Expand Down Expand Up @@ -170,9 +171,59 @@ public class API extends CommonModel {
private Service mirrorTraffic;

/**
* 路由指标,为空不开启
* meta数据传输集
* Map<mata_type,meta_data>
* mata_type meta类型
*
* @see CRDMetaEnum
* 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 List<String> statsMeta;
private Map<String, String> metaMap;

public Service getMirrorTraffic() {
return mirrorTraffic;
Expand Down Expand Up @@ -471,11 +522,11 @@ public void setVirtualClusterHeaders(List<PairMatch> virtualClusterHeaders) {
this.virtualClusterHeaders = virtualClusterHeaders;
}

public List<String> getStatsMeta() {
return statsMeta;
public Map<String, String> getMetaMap() {
return metaMap;
}

public void setStatsMeta(final List<String> statsMeta) {
this.statsMeta = statsMeta;
public void setMetaMap(Map<String, String> metaMap) {
this.metaMap = metaMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.hango.cloud.meta;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.api.model.KubernetesResource;
import me.snowdrop.istio.api.networking.v1alpha3.VirtualService;
import org.hango.cloud.core.template.TemplateConst;
import org.hango.cloud.meta.dto.DubboInfoDto;
import org.hango.cloud.meta.dto.DubboMetaDto;

import java.util.List;
import java.util.Map;


/**
* @author zhangbj
* @version 1.0
* @Type
* @Desc 自定义资源meta枚举
* @date 2022/8/16
*/
public enum CRDMetaEnum {

VIRTUAL_SERVICE_STATS_META(VirtualService.class, "StatsMeta", TemplateConst.VIRTUAL_SERVICE_STATS, new TypeReference<Map<String,String>>() {
}),
VIRTUAL_SERVICE_DUBBO_META(VirtualService.class, "DubboMeta", TemplateConst.VIRTUAL_SERVICE_DUBBO, new TypeReference<DubboInfoDto>() {
}),
;


private Class<? extends KubernetesResource> target;

private String name;

private String templateName;

private TypeReference transModel;

CRDMetaEnum(Class<? extends KubernetesResource> target, String name, String templateName, TypeReference transModel) {
this.target = target;
this.name = name;
this.templateName = templateName;
this.transModel = transModel;
}

public TypeReference getTransModel() {
return transModel;
}

public void setTransModel(TypeReference transModel) {
this.transModel = transModel;
}

public Class<? extends KubernetesResource> getTarget() {
return target;
}

public String getName() {
return name;
}

public String getTemplateName() {
return templateName;
}

/**
* 通过名称获取模板名称
*
* @return
*/
public static CRDMetaEnum get(Class<? extends KubernetesResource> target, String name) {
for (CRDMetaEnum value : values()) {
if (value.target.equals(target) && value.name.equals(name)) {
return value;
}
}
return null;

}

/**
* 获取上层传输的Meta信息内容
*
* @param content
* @param <T>
* @return
* @throws JsonProcessingException
*/
public <T> T getTransData(String content) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return ((T) objectMapper.readValue(content, this.transModel));

}

}
Loading

0 comments on commit 263d4d0

Please sign in to comment.