Skip to content

Commit

Permalink
Merge branch 'develop' into optimization
Browse files Browse the repository at this point in the history
# Conflicts:
#	sermant-plugins/sermant-router/spring-router-plugin/src/main/java/com/huaweicloud/sermant/router/spring/interceptor/FeignClientInterceptor.java
  • Loading branch information
provenceee committed Nov 2, 2022
2 parents 27a35b5 + e041ff9 commit cdc5215
Show file tree
Hide file tree
Showing 24 changed files with 649 additions and 24 deletions.
50 changes: 50 additions & 0 deletions sermant-backend-lite/deployment/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: v1
kind: Service
metadata:
name: sermant-backend-lite-service
labels:
app: sermant-backend-lite
spec:
type: NodePort
ports:
- port: 8900
nodePort: 30001
name: sermant-backend-lite-service-http
- port: 6888
nodePort: 30002
name: sermant-backend-lite-service-netty
selector:
app: sermant-backend-lite
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sermant-backend-lite-deployment
labels:
app: sermant-backend-lite
spec:
replicas: 1
selector:
matchLabels:
app: sermant-backend-lite
template:
metadata:
labels:
app: sermant-backend-lite
spec:
containers:
- name: sermant-backend-lite-deployment
image: { image.address }
imagePullPolicy: Always
resources:
requests:
memory: "4096Mi"
cpu: "2"
limits:
memory: "4096Mi"
cpu: "2"
ports:
- containerPort: 8900
- containerPort: 6888
imagePullSecrets:
- name: default-secret
10 changes: 10 additions & 0 deletions sermant-backend-lite/image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM java:8

WORKDIR /home

COPY sermant-backend-lite.jar /home
COPY start.sh /home

RUN chmod -R 777 /home

ENTRYPOINT ["sh", "/home/start.sh"]
3 changes: 3 additions & 0 deletions sermant-backend-lite/image/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

java -jar /home/sermant-backend-lite.jar
1 change: 1 addition & 0 deletions sermant-backend-lite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion sermant-plugins/sermant-router/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ router.plugin:
# 是否使用请求信息做路由
use-request-router: false
# 使用请求信息做路由时的tags
request-tags: []
request-tags: []
# 需要解析的请求头的tag
parse-header-tag: ''
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.common.request.RequestHeader;
import com.huaweicloud.sermant.router.common.utils.CollectionUtils;
import com.huaweicloud.sermant.router.common.utils.FlowContextUtils;
import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils;
import com.huaweicloud.sermant.router.config.cache.ConfigCache;
import com.huaweicloud.sermant.router.config.entity.EnabledStrategy;
Expand Down Expand Up @@ -122,6 +123,17 @@ public Object selectInvokers(Object registryDirectory, Object[] arguments, Objec
return getZoneInvokers(targetService, targetInvokers);
}

/**
* 解析下dubbo的附件信息
*
* @param invocation dubbo的invocation
* @return {@link Map}<{@link String}, {@link Object}>
*/
private Map<String, Object> parseAttachments(Object invocation) {
Map<String, Object> attachments = DubboReflectUtils.getAttachments(invocation);
return FlowContextUtils.decodeAttachments(attachments);
}

/**
* 获取dubbo应用 group
*
Expand Down Expand Up @@ -179,7 +191,7 @@ private List<Object> getTargetInvokersByRules(List<Object> invokers, Object invo
List<Rule> rules = RuleUtils
.getRules(configuration, targetService, interfaceName, DubboCache.INSTANCE.getAppName());
List<Route> routes = RouteUtils.getRoutes(rules, DubboReflectUtils.getArguments(invocation),
DubboReflectUtils.getAttachments(invocation));
parseAttachments(invocation));
if (!CollectionUtils.isEmpty(routes)) {
return RuleStrategyHandler.INSTANCE.getMatchInvokers(targetService, invokers, routes);
} else {
Expand All @@ -189,7 +201,7 @@ private List<Object> getTargetInvokersByRules(List<Object> invokers, Object invo
}

private List<Object> getTargetInvokersByRequest(String targetName, List<Object> invokers, Object invocation) {
Map<String, Object> attachments = DubboReflectUtils.getAttachments(invocation);
Map<String, Object> attachments = parseAttachments(invocation);
List<String> requestTags = routerConfig.getRequestTags();
if (CollectionUtils.isEmpty(requestTags)) {
return invokers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public class RouterConfig implements PluginConfig {
*/
private Map<String, String> parameters;

/**
* 需要解析的请求头的tag
*/
@ConfigFieldKey("parse-header-tag")
private String parseHeaderTag;

/**
* 构造方法
*/
Expand Down Expand Up @@ -167,4 +173,12 @@ public Map<String, String> getParameters() {
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}

public String getParseHeaderTag() {
return parseHeaderTag;
}

public void setParseHeaderTag(String parseHeaderTag) {
this.parseHeaderTag = parseHeaderTag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@

package com.huaweicloud.sermant.router.common.utils;

import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.utils.StringUtils;
import com.huaweicloud.sermant.router.common.config.RouterConfig;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
Expand All @@ -35,12 +38,13 @@
public class FlowContextUtils {
private static final Base64.Decoder DECODER = Base64.getDecoder();
private static final int TAG_PART_NUM = 2;
private static volatile RouterConfig routerConfig;

private FlowContextUtils() {
}

/**
* sw8-correlation流量标识解码.
* 解析base64加密信息成明文信息
*
* @param encodeTagsString encodeTagsString
* @return 解密后的流量标识
Expand All @@ -58,8 +62,62 @@ public static Map<String, List<String>> decodeTags(String encodeTagsString) {
}
List<String> list = new ArrayList<>();
list.add(new String(DECODER.decode(parts[1]), StandardCharsets.UTF_8));
tagMapping.put(new String(DECODER.decode(parts[0]), StandardCharsets.UTF_8), list);
tagMapping.put(decode(parts[0]).toLowerCase(Locale.ENGLISH), list);
}
return tagMapping;
}

/**
* 解码附件
*
* @param attachments 附件信息
* @return {@link Map}<{@link String}, {@link Object}>
*/
public static Map<String, Object> decodeAttachments(Map<String, Object> attachments) {
if (attachments == null || attachments.isEmpty() || StringUtils.isBlank(getTagName())) {
return attachments;
}
Object encode = attachments.get(getTagName());
if (encode == null) {
return attachments;
}
String encodeTags = String.valueOf(encode);
Map<String, Object> allAttachments = new HashMap<>(attachments);
String[] tags = encodeTags.split(",");
for (String tag : tags) {
final String[] parts = tag.split(":");
if (parts.length != TAG_PART_NUM) {
continue;
}
allAttachments.put(decode(parts[0]).toLowerCase(Locale.ENGLISH), decode(parts[1]));
}
return Collections.unmodifiableMap(allAttachments);
}

/**
* 解码
*
* @param encodeString 编码的字符串
* @return {@link String}
*/
private static String decode(String encodeString) {
return new String(DECODER.decode(encodeString), StandardCharsets.UTF_8);
}

/**
* 获取请求头中需要解析的tag
*
* @return {@link String}
*/
public static String getTagName() {
if (routerConfig == null) {
synchronized (FlowContextUtils.class) {
if (routerConfig == null) {
routerConfig = PluginConfigManager.getPluginConfig(RouterConfig.class);
return routerConfig.getParseHeaderTag();
}
}
}
return routerConfig.getParseHeaderTag();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.router.spring.declarer;

import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;

/**
* 定义JDK 1.8版本的java.net.HttpURLConnection的拦截点信息<br>
*
* @author yuzl 俞真龙
* @since 2022-10-25
*/
public class HttpUrlConnectionConnectDeclarer extends AbstractPluginDeclarer {
private static final String METHOD_NAME = "connect";

private static final String INTERCEPTOR_CLASS =
"com.huaweicloud.sermant.router.spring.interceptor.HttpUrlConnectionConnectInterceptor";

@Override
public ClassMatcher getClassMatcher() {
return ClassMatcher.isExtendedFrom("java.net.HttpURLConnection");
}

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[] {
InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME), INTERCEPTOR_CLASS)};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.huaweicloud.sermant.router.spring.declarer;

import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer;
import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher;
import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher;

/**
* org.springframework.web.client.RestTemplate的拦截点定义<br>
*
* @author yuzl 俞真龙
* @since 2022-10-27
*/
public class RestTemplateDeclarer extends AbstractPluginDeclarer {
private static final String ENHANCE_CLASS = "org.springframework.web.client.RestTemplate";
private static final String ENHANCE_METHOD = "doExecute";
private static final String INTERCEPTOR_CLASS =
"com.huaweicloud.sermant.router.spring.interceptor.RestTemplateInterceptor";

@Override
public ClassMatcher getClassMatcher() {
return ClassMatcher.nameEquals(ENHANCE_CLASS);
}

@Override
public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) {
return new InterceptDeclarer[] {
InterceptDeclarer.build(MethodMatcher.nameEquals(ENHANCE_METHOD), INTERCEPTOR_CLASS)};
}
}
Loading

0 comments on commit cdc5215

Please sign in to comment.