Skip to content

Commit

Permalink
*增加跨线程传递数据功能
Browse files Browse the repository at this point in the history
  • Loading branch information
provenceee committed Aug 12, 2023
1 parent 7a465c3 commit eb012fb
Show file tree
Hide file tree
Showing 68 changed files with 2,232 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# agent config
agent.config.isEnhanceBootStrapEnable=false
agent.config.isEnhanceBootStrapEnable=true
agent.config.ignoredPrefixes=com.huawei.sermant,com.huaweicloud.sermant
agent.config.ignoredInterfaces=org.springframework.cglib.proxy.Factory
agent.config.combineStrategy=ALL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# agent config
agent.config.isEnhanceBootStrapEnable=false
agent.config.isEnhanceBootStrapEnable=true
agent.config.ignoredPrefixes=com.huawei.sermant,com.huaweicloud.sermant
agent.config.ignoredInterfaces=org.springframework.cglib.proxy.Factory
agent.config.combineStrategy=ALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ public static MethodMatcher isMemberMethod() {
return methodTypeMatches(MethodType.MEMBER);
}

/**
* 匹配公有方法,见{@link #methodTypeMatches}
*
* @return 方法匹配器对象
*/
public static MethodMatcher isPublicMethod() {
return methodTypeMatches(MethodType.PUBLIC);
}

/**
* 匹配符合类型的方法,包括静态方法,构造函数和成员方法三种
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ public boolean match(MethodDescription methodDescription) {
public boolean match(MethodDescription methodDescription) {
return !methodDescription.isStatic() && !methodDescription.isConstructor();
}
},
/**
* 公有方法
*/
PUBLIC() {
@Override
public boolean match(MethodDescription methodDescription) {
return methodDescription.isPublic();
}
};

/**
Expand Down
3 changes: 3 additions & 0 deletions sermant-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<profiles>
<profile>
<id>agent</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ hystrix:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
timeoutInMilliseconds: 50000

ribbon:
ConnectTimeout: 10000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
import com.huawei.dynamic.config.DynamicConfiguration;
import com.huawei.dynamic.config.RefreshNotifier;

import com.huaweicloud.sermant.core.operation.OperationManager;
import com.huaweicloud.sermant.core.operation.converter.api.YamlConverter;
import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEvent;
import com.huaweicloud.sermant.core.service.dynamicconfig.common.DynamicConfigEventType;
import com.huaweicloud.sermant.implement.operation.converter.YamlConverterImpl;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockedStatic;
Expand All @@ -38,15 +43,30 @@
* @since 2022-09-05
*/
public class SpringEventPublisherTest {
private MockedStatic<OperationManager> operationManagerMockedStatic;

@Mock
private ApplicationEventPublisher applicationEventPublisher;

@Before
public void setUp() {
operationManagerMockedStatic = Mockito.mockStatic(OperationManager.class);
operationManagerMockedStatic.when(() -> OperationManager.getOperation(YamlConverter.class))
.thenReturn(new YamlConverterImpl());
}

@After
public void tearDown() {
operationManagerMockedStatic.close();
}

@Test
public void test() {
MockitoAnnotations.openMocks(this);
try (final MockedStatic<PluginConfigManager> pluginConfigManagerMockedStatic = Mockito.mockStatic(PluginConfigManager.class)){
try (final MockedStatic<PluginConfigManager> pluginConfigManagerMockedStatic = Mockito
.mockStatic(PluginConfigManager.class)) {
pluginConfigManagerMockedStatic.when(() -> PluginConfigManager.getPluginConfig(DynamicConfiguration.class))
.thenReturn(new DynamicConfiguration());
.thenReturn(new DynamicConfiguration());
final SpringEventPublisher springEventPublisher = new SpringEventPublisher();
springEventPublisher.setApplicationEventPublisher(applicationEventPublisher);
final OriginConfigCenterDisableListenerTest originConfigCenterDisableListenerTest = new OriginConfigCenterDisableListenerTest();
Expand Down
7 changes: 7 additions & 0 deletions sermant-plugins/sermant-router/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ router.plugin:
request-tags: []
# 需要解析的请求头的tag
parse-header-tag: ''
transmit.plugin:
# 是否在直接new Thread时传递标签
enabled-thread: true
# 是否在非定时线程池中传递标签
enabled-thread-pool: true
# 是否在定时线程池的schedule/scheduleAtFixedRate/scheduleWithFixedDelay方法中传递标签
enabled-scheduler: false
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.huaweicloud.sermant.router.dubbo.interceptor;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;
import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.service.ServiceManager;
import com.huaweicloud.sermant.router.common.config.TransmitConfig;
import com.huaweicloud.sermant.router.common.request.RequestHeader;
import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils;
import com.huaweicloud.sermant.router.dubbo.service.DubboConfigService;
Expand Down Expand Up @@ -50,6 +52,8 @@ public class ContextFilterInterceptorTest {

private static MockedStatic<ServiceManager> mockServiceManager;

private static MockedStatic<PluginConfigManager> mockPluginConfigManager;

/**
* UT执行前进行mock
*/
Expand All @@ -70,6 +74,10 @@ public Set<String> getMatchKeys() {
return keys;
}
});

mockPluginConfigManager = Mockito.mockStatic(PluginConfigManager.class);
mockPluginConfigManager.when(() -> PluginConfigManager.getPluginConfig(TransmitConfig.class))
.thenReturn(new TransmitConfig());
}

/**
Expand All @@ -78,6 +86,7 @@ public Set<String> getMatchKeys() {
@AfterClass
public static void after() {
mockServiceManager.close();
mockPluginConfigManager.close();
}

public ContextFilterInterceptorTest() throws NoSuchMethodException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.core.utils.StringUtils;
import com.huaweicloud.sermant.router.common.config.RouterConfig;
import com.huaweicloud.sermant.router.common.config.TransmitConfig;
import com.huaweicloud.sermant.router.common.constants.RouterConstant;
import com.huaweicloud.sermant.router.common.request.RequestHeader;
import com.huaweicloud.sermant.router.common.utils.ThreadLocalUtils;
Expand Down Expand Up @@ -77,6 +78,8 @@ public static void before() {
mockPluginConfigManager = Mockito.mockStatic(PluginConfigManager.class);
mockPluginConfigManager.when(() -> PluginConfigManager.getPluginConfig(RouterConfig.class))
.thenReturn(config);
mockPluginConfigManager.when(() -> PluginConfigManager.getPluginConfig(TransmitConfig.class))
.thenReturn(new TransmitConfig());
service = new AbstractDirectoryServiceImpl();
}

Expand Down
3 changes: 3 additions & 0 deletions sermant-plugins/sermant-router/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<module>router-common</module>
<module>router-config-common</module>
<module>router-config-service</module>
<module>router-transmit-plugin</module>
<module>spring-router-plugin</module>
<module>spring-router-service</module>
</modules>
Expand All @@ -45,6 +46,7 @@
<module>router-common</module>
<module>router-config-common</module>
<module>router-config-service</module>
<module>router-transmit-plugin</module>
<module>spring-router-plugin</module>
<module>spring-router-service</module>
</modules>
Expand All @@ -57,6 +59,7 @@
<module>router-common</module>
<module>router-config-common</module>
<module>router-config-service</module>
<module>router-transmit-plugin</module>
<module>spring-router-plugin</module>
<module>spring-router-service</module>
</modules>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2021-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.common.config;

import com.huaweicloud.sermant.core.config.common.ConfigFieldKey;
import com.huaweicloud.sermant.core.config.common.ConfigTypeKey;
import com.huaweicloud.sermant.core.plugin.config.PluginConfig;

/**
* 路由配置
*
* @author provenceee
* @since 2021-11-18
*/
@ConfigTypeKey("transmit.plugin")
public class TransmitConfig implements PluginConfig {
/**
* 是否在直接new Thread时传递标签
*/
@ConfigFieldKey("enabled-thread")
private boolean enabledThread;

/**
* 是否在非定时线程池中传递标签
*/
@ConfigFieldKey("enabled-thread-pool")
private boolean enabledThreadPool;

/**
* 是否在定时线程池的schedule/scheduleAtFixedRate/scheduleWithFixedDelay方法中传递标签
*/
@ConfigFieldKey("enabled-scheduler")
private boolean enabledScheduler;

public boolean isEnabledThread() {
return enabledThread;
}

public void setEnabledThread(boolean enabledThread) {
this.enabledThread = enabledThread;
}

public boolean isEnabledThreadPool() {
return enabledThreadPool;
}

public void setEnabledThreadPool(boolean enabledThreadPool) {
this.enabledThreadPool = enabledThreadPool;
}

public boolean isEnabledScheduler() {
return enabledScheduler;
}

public void setEnabledScheduler(boolean enabledScheduler) {
this.enabledScheduler = enabledScheduler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ public String getPath() {
public String getHttpMethod() {
return httpMethod;
}

@Override
public String toString() {
return "{"
+ "path='" + path + '\''
+ ", httpMethod='" + httpMethod + '\''
+ ", header='" + getHeader() + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ public RequestHeader(Map<String, List<String>> header) {
public Map<String, List<String>> getHeader() {
return Optional.ofNullable(header).orElse(Collections.emptyMap());
}

@Override
public String toString() {
return "{"
+ "header='" + getHeader() + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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

import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager;
import com.huaweicloud.sermant.router.common.config.TransmitConfig;
import com.huaweicloud.sermant.router.common.request.RequestData;
import com.huaweicloud.sermant.router.common.request.RequestHeader;

Expand All @@ -26,13 +28,24 @@
* @since 2022-07-08
*/
public class ThreadLocalUtils {
private static final ThreadLocal<RequestHeader> HEADER = new ThreadLocal<>();
private static final ThreadLocal<RequestHeader> HEADER;

private static final ThreadLocal<RequestData> DATA = new ThreadLocal<>();
private static final ThreadLocal<RequestData> DATA;

private ThreadLocalUtils() {
}

static {
TransmitConfig transmitConfig = PluginConfigManager.getPluginConfig(TransmitConfig.class);
if (transmitConfig.isEnabledThread()) {
HEADER = new InheritableThreadLocal<>();
DATA = new InheritableThreadLocal<>();
} else {
HEADER = new ThreadLocal<>();
DATA = new ThreadLocal<>();
}
}

/**
* 获取线程变量
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
#

com.huaweicloud.sermant.router.common.config.RouterConfig
com.huaweicloud.sermant.router.common.config.TransmitConfig
Loading

0 comments on commit eb012fb

Please sign in to comment.