This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 9a9b28bf09 Fix unable to getAppName in InjvmInvoker (#12574)
9a9b28bf09 is described below
commit 9a9b28bf098229dc912f2962178fef748577ff14
Author: Albumen Kevin <[email protected]>
AuthorDate: Mon Jun 19 21:28:45 2023 +0800
Fix unable to getAppName in InjvmInvoker (#12574)
---
.../dubbo/rpc/protocol/injvm/InjvmInvoker.java | 31 +++++++++++------
.../dubbo/rpc/protocol/injvm/DemoService.java | 4 +++
.../dubbo/rpc/protocol/injvm/DemoServiceImpl.java | 9 +++++
.../rpc/protocol/injvm/InjvmProtocolTest.java | 40 +++++++++++++++++++++-
4 files changed, 72 insertions(+), 12 deletions(-)
diff --git
a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java
b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java
index 971b4f7402..70cf0b3534 100644
---
a/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java
+++
b/dubbo-rpc/dubbo-rpc-injvm/src/main/java/org/apache/dubbo/rpc/protocol/injvm/InjvmInvoker.java
@@ -94,7 +94,6 @@ public class InjvmInvoker<T> extends AbstractInvoker<T> {
if (exporter == null) {
throw new RpcException("Service [" + key + "] not found.");
}
- RpcContext.getServiceContext().setRemoteAddress(LOCALHOST_VALUE, 0);
// Solve local exposure, the server opens the token, and the client
call fails.
Invoker<?> invoker = exporter.getInvoker();
URL serverURL = invoker.getUrl();
@@ -122,16 +121,24 @@ public class InjvmInvoker<T> extends AbstractInvoker<T> {
// use consumer executor
ExecutorService executor =
executorRepository.createExecutorIfAbsent(ExecutorUtil.setThreadName(getUrl(),
SERVER_THREAD_POOL_NAME));
CompletableFuture<AppResponse> appResponseFuture =
CompletableFuture.supplyAsync(() -> {
- Result result = invoker.invoke(copiedInvocation);
- if (result.hasException()) {
- AppResponse appResponse = new
AppResponse(result.getException());
- appResponse.setObjectAttachments(new
HashMap<>(result.getObjectAttachments()));
- return appResponse;
- } else {
- rebuildValue(invocation, desc, result);
- AppResponse appResponse = new
AppResponse(result.getValue());
- appResponse.setObjectAttachments(new
HashMap<>(result.getObjectAttachments()));
- return appResponse;
+ // clear thread local before child invocation, prevent context
pollution
+ InternalThreadLocalMap originTL =
InternalThreadLocalMap.getAndRemove();
+ try {
+
RpcContext.getServiceContext().setRemoteAddress(LOCALHOST_VALUE, 0);
+
RpcContext.getServiceContext().setRemoteApplicationName(getUrl().getApplication());
+ Result result = invoker.invoke(copiedInvocation);
+ if (result.hasException()) {
+ AppResponse appResponse = new
AppResponse(result.getException());
+ appResponse.setObjectAttachments(new
HashMap<>(result.getObjectAttachments()));
+ return appResponse;
+ } else {
+ rebuildValue(invocation, desc, result);
+ AppResponse appResponse = new
AppResponse(result.getValue());
+ appResponse.setObjectAttachments(new
HashMap<>(result.getObjectAttachments()));
+ return appResponse;
+ }
+ } finally {
+ InternalThreadLocalMap.set(originTL);
}
}, executor);
// save for 2.6.x compatibility, for example, TraceFilter in
Zipkin uses com.alibaba.xxx.FutureAdapter
@@ -144,6 +151,8 @@ public class InjvmInvoker<T> extends AbstractInvoker<T> {
// clear thread local before child invocation, prevent context
pollution
InternalThreadLocalMap originTL =
InternalThreadLocalMap.getAndRemove();
try {
+
RpcContext.getServiceContext().setRemoteAddress(LOCALHOST_VALUE, 0);
+
RpcContext.getServiceContext().setRemoteApplicationName(getUrl().getApplication());
result = invoker.invoke(copiedInvocation);
} finally {
InternalThreadLocalMap.set(originTL);
diff --git
a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoService.java
b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoService.java
index c9d2e63d49..9a09ed9617 100644
---
a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoService.java
+++
b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoService.java
@@ -40,4 +40,8 @@ public interface DemoService {
Type enumlength(Type... types);
String getAsyncResult();
+
+ String getApplication();
+
+ String getRemoteAddress();
}
diff --git
a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoServiceImpl.java
b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoServiceImpl.java
index c59847b298..b4d2314a6d 100644
---
a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoServiceImpl.java
+++
b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/DemoServiceImpl.java
@@ -80,4 +80,13 @@ public class DemoServiceImpl implements DemoService {
return "DONE";
}
+ @Override
+ public String getApplication() {
+ return RpcContext.getServiceContext().getRemoteApplicationName();
+ }
+
+ @Override
+ public String getRemoteAddress() {
+ return RpcContext.getServiceContext().getRemoteAddressString();
+ }
}
diff --git
a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java
b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java
index 5d4103a840..3ef342e8ea 100644
---
a/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java
+++
b/dubbo-rpc/dubbo-rpc-injvm/src/test/java/org/apache/dubbo/rpc/protocol/injvm/InjvmProtocolTest.java
@@ -19,10 +19,13 @@ package org.apache.dubbo.rpc.protocol.injvm;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Exporter;
+import org.apache.dubbo.rpc.FutureContext;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
+import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
@@ -32,7 +35,9 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
+import java.util.concurrent.ExecutionException;
+import static
org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
@@ -129,7 +134,7 @@ class InjvmProtocolTest {
}
@Test
- void testLocalProtocolAsync() {
+ void testLocalProtocolAsync() throws ExecutionException,
InterruptedException {
DemoService service = new DemoServiceImpl();
URL url = URL.valueOf("injvm://127.0.0.1/TestService")
.addParameter(ASYNC_KEY, true)
@@ -141,6 +146,39 @@ class InjvmProtocolTest {
exporters.add(exporter);
service = proxy.getProxy(protocol.refer(DemoService.class, url));
assertNull(service.getAsyncResult());
+ assertEquals("DONE",
FutureContext.getContext().getCompletableFuture().get());
+ }
+
+ @Test
+ void testApplication() {
+ DemoService service = new DemoServiceImpl();
+ URL url = URL.valueOf("injvm://127.0.0.1/TestService")
+ .addParameter(INTERFACE_KEY,
DemoService.class.getName()).addParameter("application", "consumer")
+ .addParameter(APPLICATION_KEY, "test-app")
+ .setScopeModel(ApplicationModel.defaultModel().getDefaultModule());
+ Invoker<?> invoker = proxy.getInvoker(service, DemoService.class, url);
+ assertTrue(invoker.isAvailable());
+ Exporter<?> exporter = protocol.export(invoker);
+ exporters.add(exporter);
+ service = proxy.getProxy(protocol.refer(DemoService.class, url));
+ assertEquals("test-app", service.getApplication());
+
assertTrue(StringUtils.isEmpty(RpcContext.getServiceContext().getRemoteApplicationName()));
+ }
+
+ @Test
+ void testRemoteAddress() {
+ DemoService service = new DemoServiceImpl();
+ URL url = URL.valueOf("injvm://127.0.0.1/TestService")
+ .addParameter(INTERFACE_KEY,
DemoService.class.getName()).addParameter("application", "consumer")
+ .addParameter(APPLICATION_KEY, "test-app")
+ .setScopeModel(ApplicationModel.defaultModel().getDefaultModule());
+ Invoker<?> invoker = proxy.getInvoker(service, DemoService.class, url);
+ assertTrue(invoker.isAvailable());
+ Exporter<?> exporter = protocol.export(invoker);
+ exporters.add(exporter);
+ service = proxy.getProxy(protocol.refer(DemoService.class, url));
+ assertEquals("127.0.0.1:0", service.getRemoteAddress());
+ assertNull(RpcContext.getServiceContext().getRemoteAddress());
}
}