Skip to content

Commit

Permalink
refactor: class loader for dubbo invoker
Browse files Browse the repository at this point in the history
  • Loading branch information
QizhengMo committed Oct 22, 2024
1 parent b40c5fe commit 754ca41
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -36,11 +38,9 @@ public class ReplaySenderConfiguration {
private static final String NEW_INVOKER_PATH = "dubboInvoker.jar";
private static final List<String> remoteProtocol = Arrays.asList("http", "https", "ftp", "sftp", "nfs");

@Bean("replayExtensionInvoker")
@Bean("dubboInvokerLoader")
@ConditionalOnProperty(name = "replay.sender.extension.switch", havingValue = "true")
public List<ReplayExtensionInvoker> invokers() {
List<ReplayExtensionInvoker> invokers = new ArrayList<>();

public RemoteJarClassLoader dubboInvokerLoader() {
try {
URL classPathResource;
if (StringUtils.isNotBlank(jarFilePath)) {
Expand All @@ -52,11 +52,21 @@ public List<ReplayExtensionInvoker> invokers() {
} else {
classPathResource = loadLocalInvokerJar();
}
RemoteJarClassLoader loader = RemoteJarLoaderUtils.loadJar(classPathResource.getPath());
invokers.addAll(RemoteJarLoaderUtils.loadService(ReplayExtensionInvoker.class, loader));
return RemoteJarLoaderUtils.loadJar(classPathResource.getPath());
} catch (Throwable t) {
LOGGER.error("Load invoker jar failed, application startup blocked", t);
}
LOGGER.error("No invoker found, application startup blocked");
throw new RuntimeException("No invoker found");
}

@Bean("replayExtensionInvoker")
@ConditionalOnBean(name = "dubboInvokerLoader")
public List<ReplayExtensionInvoker> invokers(
@Qualifier("dubboInvokerLoader") RemoteJarClassLoader loader) {
List<ReplayExtensionInvoker> invokers = new ArrayList<>(
RemoteJarLoaderUtils.loadService(ReplayExtensionInvoker.class, loader));

if (invokers.isEmpty()) {
LOGGER.error("No invoker found, application startup blocked");
throw new RuntimeException("No invoker found");
Expand All @@ -66,12 +76,13 @@ public List<ReplayExtensionInvoker> invokers() {
}

@Bean
@ConditionalOnProperty(name = "replay.sender.extension.switch", havingValue = "true")
@ConditionalOnBean(name = "replayExtensionInvoker")
public DefaultDubboReplaySender dubboReplaySender(
@Value("#{'${arex.replay.header.excludes.dubbo}'.split(',')}") List<String> excludes,
List<ReplayExtensionInvoker> invokers) {
List<ReplayExtensionInvoker> invokers,
@Qualifier("dubboInvokerLoader") RemoteJarClassLoader loader) {

return new DefaultDubboReplaySender(excludes, invokers);
return new DefaultDubboReplaySender(excludes, invokers, loader);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.arextest.common.model.classloader.RemoteJarClassLoader;
import com.arextest.model.mock.MockCategoryType;
import com.arextest.schedule.common.CommonConstant;
import com.arextest.schedule.common.UrlUtil;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class DefaultDubboReplaySender extends AbstractReplaySender {

private final List<String> headerExcludes;
private final List<ReplayExtensionInvoker> replayExtensionInvokers;
private final RemoteJarClassLoader dubboInvokerLoader;

@Override
public boolean isSupported(String categoryType) {
Expand All @@ -49,9 +51,15 @@ public int getOrder() {

@Override
public boolean send(ReplayActionCaseItem caseItem) {
before(caseItem);
Map<String, String> headers = createHeaders(caseItem);
return doSend(caseItem, headers);
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(dubboInvokerLoader);
try {
before(caseItem);
Map<String, String> headers = createHeaders(caseItem);
return doSend(caseItem, headers);
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}

@Override
Expand Down

0 comments on commit 754ca41

Please sign in to comment.