This is an automated email from the ASF dual-hosted git repository. iluo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/master by this push: new c536eec [Dubbo-4351]Fix getRemoteApplicationName() returns null #4351 (#4722) c536eec is described below commit c536eec4b595a6de36f6fb64add1dd491b1a67c8 Author: huazhongming <crazy...@gmail.com> AuthorDate: Sat Sep 7 23:39:31 2019 +0800 [Dubbo-4351]Fix getRemoteApplicationName() returns null #4351 (#4722) * fix RpcContext.getContext().getRemoteApplicationName() returns null #4351 * Handling conflicts * Handling conflicts --- .../dubbo/rpc/filter/ConsumerContextFilter.java | 6 +- .../org/apache/dubbo/rpc/filter/ContextFilter.java | 17 ++- .../rpc/protocol/dubbo/DubboProtocolTest.java | 34 ++++-- .../rpc/protocol/dubbo/support/DemoService.java | 2 + .../protocol/dubbo/support/DemoServiceImpl.java | 5 + .../rpc/protocol/hessian/HessianProtocolTest.java | 16 +++ .../dubbo/rpc/protocol/hessian/HessianService.java | 2 + .../rpc/protocol/hessian/HessianServiceImpl.java | 5 + .../rpc/protocol/httpinvoker/HttpService.java | 2 + .../rpc/protocol/httpinvoker/HttpServiceImpl.java | 7 ++ .../dubbo/rpc/protocol/http/HttpProtocolTest.java | 136 ++++++++++----------- .../http/{JsonRpcService.java => HttpService.java} | 4 +- ...sonRpcServiceImpl.java => HttpServiceImpl.java} | 9 +- .../dubbo/rpc/protocol/injvm/DemoService.java | 3 + .../dubbo/rpc/protocol/injvm/DemoServiceImpl.java | 6 + .../rpc/protocol/injvm/InjvmProtocolTest.java | 19 ++- .../dubbo/rpc/protocol/rest/DemoService.java | 4 + .../dubbo/rpc/protocol/rest/DemoServiceImpl.java | 5 + .../dubbo/rpc/protocol/rest/RestProtocolTest.java | 17 +++ .../apache/dubbo/rpc/protocol/rmi/DemoService.java | 2 + .../dubbo/rpc/protocol/rmi/DemoServiceImpl.java | 6 + .../dubbo/rpc/protocol/rmi/RmiProtocolTest.java | 14 +++ 22 files changed, 230 insertions(+), 91 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java index 43353f7..d97dc45 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java @@ -27,7 +27,8 @@ import org.apache.dubbo.rpc.RpcException; import org.apache.dubbo.rpc.RpcInvocation; import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER; - +import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_APPLICATION_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; /** * ConsumerContextFilter set current RpcContext with invoker,invocation, local host, remote host and port * for consumer invoker.It does it to make the requires info available to execution thread's RpcContext. @@ -49,7 +50,8 @@ public class ConsumerContextFilter extends ListenableFilter { .setInvocation(invocation) .setLocalAddress(NetUtils.getLocalHost(), 0) .setRemoteAddress(invoker.getUrl().getHost(), - invoker.getUrl().getPort()); + invoker.getUrl().getPort()) + .setAttachment(REMOTE_APPLICATION_KEY, invoker.getUrl().getParameter(APPLICATION_KEY)); if (invocation instanceof RpcInvocation) { ((RpcInvocation) invocation).setInvoker(invoker); } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java index eb6cebe..3723d8d 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ContextFilter.java @@ -17,6 +17,7 @@ package org.apache.dubbo.rpc.filter; import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.Invocation; import org.apache.dubbo.rpc.Invoker; import org.apache.dubbo.rpc.ListenableFilter; @@ -72,12 +73,20 @@ public class ContextFilter extends ListenableFilter { attachments.remove(TAG_KEY); attachments.remove(FORCE_USE_TAG); } - RpcContext.getContext() - .setInvoker(invoker) + RpcContext context = RpcContext.getContext(); + + context.setInvoker(invoker) .setInvocation(invocation) // .setAttachments(attachments) // merged from dubbox - .setLocalAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort()) - .setRemoteApplicationName(invoker.getUrl().getParameter(REMOTE_APPLICATION_KEY)); + .setLocalAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort()); + String remoteApplication = invocation.getAttachment(REMOTE_APPLICATION_KEY); + if (StringUtils.isNotEmpty(remoteApplication)) { + context.setRemoteApplicationName(remoteApplication); + } else { + context.setRemoteApplicationName(RpcContext.getContext().getAttachment(REMOTE_APPLICATION_KEY)); + + } + // merged from dubbox // we may already added some attachments into RpcContext before this filter (e.g. in rest protocol) diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java index 8989c64..b692fb1 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java @@ -60,7 +60,7 @@ public class DubboProtocolTest { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9020/" + DemoService.class.getName() + "?codec=exchange"))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9020/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", - 3000L))); + 3000L))); assertEquals(service.getSize(new String[]{"", "", ""}), 3); } @@ -69,7 +69,7 @@ public class DubboProtocolTest { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName()))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName()).addParameter("timeout", - 3000L))); + 3000L))); assertEquals(service.enumlength(new Type[]{}), Type.Lower); assertEquals(service.getSize(null), -1); assertEquals(service.getSize(new String[]{"", "", ""}), 3); @@ -81,7 +81,7 @@ public class DubboProtocolTest { service.invoke("dubbo://127.0.0.1:9010/" + DemoService.class.getName() + "", "invoke"); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName() + "?client=netty").addParameter("timeout", - 3000L))); + 3000L))); // test netty client StringBuffer buf = new StringBuffer(); for (int i = 0; i < 1024 * 32 + 32; i++) @@ -90,7 +90,7 @@ public class DubboProtocolTest { // cast to EchoService EchoService echo = proxy.getProxy(protocol.refer(EchoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName() + "?client=netty").addParameter("timeout", - 3000L))); + 3000L))); assertEquals(echo.$echo(buf.toString()), buf.toString()); assertEquals(echo.$echo("test"), "test"); assertEquals(echo.$echo("abcdefg"), "abcdefg"); @@ -102,7 +102,7 @@ public class DubboProtocolTest { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9011/" + DemoService.class.getName()).addParameter(Constants.SERVER_KEY, "mina"))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9011/" + DemoService.class.getName()).addParameter(Constants.CLIENT_KEY, "mina").addParameter("timeout", - 3000L))); + 3000L))); for (int i = 0; i < 10; i++) { assertEquals(service.enumlength(new Type[]{}), Type.Lower); assertEquals(service.getSize(null), -1); @@ -118,7 +118,7 @@ public class DubboProtocolTest { } service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9011/" + DemoService.class.getName() + "?client=mina").addParameter("timeout", - 3000L))); + 3000L))); // test netty client StringBuffer buf = new StringBuffer(); for (int i = 0; i < 1024 * 32 + 32; i++) @@ -127,7 +127,7 @@ public class DubboProtocolTest { // cast to EchoService EchoService echo = proxy.getProxy(protocol.refer(EchoService.class, URL.valueOf("dubbo://127.0.0.1:9011/" + DemoService.class.getName() + "?client=mina").addParameter("timeout", - 3000L))); + 3000L))); for (int i = 0; i < 10; i++) { assertEquals(echo.$echo(buf.toString()), buf.toString()); assertEquals(echo.$echo("test"), "test"); @@ -141,12 +141,12 @@ public class DubboProtocolTest { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName()))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName()).addParameter("timeout", - 3000L))); + 3000L))); RemoteService remote = new RemoteServiceImpl(); protocol.export(proxy.getInvoker(remote, RemoteService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + RemoteService.class.getName()))); remote = proxy.getProxy(protocol.refer(RemoteService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + RemoteService.class.getName()).addParameter("timeout", - 3000L))); + 3000L))); service.sayHello("world"); @@ -166,7 +166,7 @@ public class DubboProtocolTest { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange"))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", - 3000L))); + 3000L))); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) service.getSize(new String[]{"", "", ""}); @@ -178,7 +178,7 @@ public class DubboProtocolTest { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange"))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", - 3000L))); + 3000L))); try { service.nonSerializedParameter(new NonSerialized()); Assertions.fail(); @@ -192,7 +192,7 @@ public class DubboProtocolTest { DemoService service = new DemoServiceImpl(); protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange"))); service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9050/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", - 3000L))); + 3000L))); try { service.returnNonSerialized(); Assertions.fail(); @@ -200,4 +200,14 @@ public class DubboProtocolTest { Assertions.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable")); } } + + @Test + public void testRemoteApplicationName() throws Exception { + DemoService service = new DemoServiceImpl(); + URL url = URL.valueOf("dubbo://127.0.0.1:9020/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", + 3000L).addParameter("application", "consumer"); + protocol.export(proxy.getInvoker(service, DemoService.class, url)); + service = proxy.getProxy(protocol.refer(DemoService.class, url)); + assertEquals(service.getRemoteApplicationName(), "consumer"); + } } diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java index a1323b8..940a9f6 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java @@ -64,4 +64,6 @@ public interface DemoService { int getPerson(Person person1, Person perso2); String getPerson(Man man); + + String getRemoteApplicationName(); } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java index a5af9be..99abff6 100644 --- a/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-dubbo/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java @@ -121,4 +121,9 @@ public class DemoServiceImpl implements DemoService { public String getPerson(Man man) { return man.getName(); } + + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java index 9fcadaa..53e5063 100644 --- a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianProtocolTest.java @@ -220,4 +220,20 @@ public class HessianProtocolTest { exporter.unexport(); } + + @Test + public void testRemoteApplicationName() { + HessianServiceImpl server = new HessianServiceImpl(); + ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); + Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + URL url = URL.valueOf("hessian://127.0.0.1:5342/" + HessianService.class.getName() + "?version=1.0.0&hessian.overload.method=true").addParameter("application", "consumer"); + Exporter<HessianService> exporter = protocol.export(proxyFactory.getInvoker(server, HessianService.class, url)); + Invoker<HessianService> invoker = protocol.refer(HessianService.class, url); + HessianService client = proxyFactory.getProxy(invoker); + String result = client.getRemoteApplicationName(); + Assertions.assertEquals("consumer", result); + invoker.destroy(); + exporter.unexport(); + } + } diff --git a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianService.java b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianService.java index 9b3481a..7de8333 100644 --- a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianService.java +++ b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianService.java @@ -32,4 +32,6 @@ public interface HessianService { String context(String name); + String getRemoteApplicationName(); + } diff --git a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianServiceImpl.java b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianServiceImpl.java index 8afd334..0440a4c 100644 --- a/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-hessian/src/test/java/org/apache/dubbo/rpc/protocol/hessian/HessianServiceImpl.java @@ -67,4 +67,9 @@ public class HessianServiceImpl implements HessianService { super(message); } } + + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } } diff --git a/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpService.java b/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpService.java index e546c9b..5205dc6 100644 --- a/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpService.java +++ b/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpService.java @@ -30,4 +30,6 @@ public interface HttpService { String customException(); + String getRemoteApplicationName(); + } diff --git a/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpServiceImpl.java b/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpServiceImpl.java index 336ffbf..d47e813 100644 --- a/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-http-invoker/src/test/java/org/apache/dubbo/rpc/protocol/httpinvoker/HttpServiceImpl.java @@ -16,6 +16,8 @@ */ package org.apache.dubbo.rpc.protocol.httpinvoker; +import org.apache.dubbo.rpc.RpcContext; + /** * HttpServiceImpl */ @@ -49,6 +51,11 @@ public class HttpServiceImpl implements HttpService { } } + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } + public String customException() { throw new MyException("custom exception"); } diff --git a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java index cee35be..4de9086 100644 --- a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpProtocolTest.java @@ -1,69 +1,69 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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 org.apache.dubbo.rpc.protocol.http; - - -import org.apache.dubbo.common.URL; -import org.apache.dubbo.common.extension.ExtensionLoader; -import org.apache.dubbo.rpc.Exporter; -import org.apache.dubbo.rpc.Invoker; -import org.apache.dubbo.rpc.Protocol; -import org.apache.dubbo.rpc.ProxyFactory; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class HttpProtocolTest { - - @Test - public void testJsonrpcProtocol() { - JsonRpcServiceImpl server = new JsonRpcServiceImpl(); - assertFalse(server.isCalled()); - ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); - Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); - URL url = URL.valueOf("http://127.0.0.1:5342/" + JsonRpcService.class.getName() + "?version=1.0.0"); - Exporter<JsonRpcService> exporter = protocol.export(proxyFactory.getInvoker(server, JsonRpcService.class, url)); - Invoker<JsonRpcService> invoker = protocol.refer(JsonRpcService.class, url); - JsonRpcService client = proxyFactory.getProxy(invoker); - String result = client.sayHello("haha"); - assertTrue(server.isCalled()); - assertEquals("Hello, haha", result); - invoker.destroy(); - exporter.unexport(); - } - - @Test - public void testJsonrpcProtocolForServerJetty() { - JsonRpcServiceImpl server = new JsonRpcServiceImpl(); - assertFalse(server.isCalled()); - ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); - Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); - URL url = URL.valueOf("http://127.0.0.1:5342/" + JsonRpcService.class.getName() + "?version=1.0.0&server=jetty"); - Exporter<JsonRpcService> exporter = protocol.export(proxyFactory.getInvoker(server, JsonRpcService.class, url)); - Invoker<JsonRpcService> invoker = protocol.refer(JsonRpcService.class, url); - JsonRpcService client = proxyFactory.getProxy(invoker); - String result = client.sayHello("haha"); - assertTrue(server.isCalled()); - assertEquals("Hello, haha", result); - invoker.destroy(); - exporter.unexport(); - } - +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.dubbo.rpc.protocol.http; + + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.rpc.Exporter; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Protocol; +import org.apache.dubbo.rpc.ProxyFactory; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class HttpProtocolTest { + + @Test + public void testJsonrpcProtocol() { + HttpServiceImpl server = new HttpServiceImpl(); + assertFalse(server.isCalled()); + ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); + Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?version=1.0.0"); + Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url)); + Invoker<HttpService> invoker = protocol.refer(HttpService.class, url); + HttpService client = proxyFactory.getProxy(invoker); + String result = client.sayHello("haha"); + assertTrue(server.isCalled()); + assertEquals("Hello, haha", result); + invoker.destroy(); + exporter.unexport(); + } + + @Test + public void testJsonrpcProtocolForServerJetty() { + HttpServiceImpl server = new HttpServiceImpl(); + assertFalse(server.isCalled()); + ProxyFactory proxyFactory = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); + Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + URL url = URL.valueOf("http://127.0.0.1:5342/" + HttpService.class.getName() + "?version=1.0.0&server=jetty"); + Exporter<HttpService> exporter = protocol.export(proxyFactory.getInvoker(server, HttpService.class, url)); + Invoker<HttpService> invoker = protocol.refer(HttpService.class, url); + HttpService client = proxyFactory.getProxy(invoker); + String result = client.sayHello("haha"); + assertTrue(server.isCalled()); + assertEquals("Hello, haha", result); + invoker.destroy(); + exporter.unexport(); + } + } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/JsonRpcService.java b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpService.java similarity index 93% rename from dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/JsonRpcService.java rename to dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpService.java index e01fc6e..f05994e 100644 --- a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/JsonRpcService.java +++ b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpService.java @@ -16,10 +16,12 @@ */ package org.apache.dubbo.rpc.protocol.http; -public interface JsonRpcService { +public interface HttpService { String sayHello(String name); void timeOut(int millis); String customException(); + + String getRemoteApplicationName(); } diff --git a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/JsonRpcServiceImpl.java b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpServiceImpl.java similarity index 87% rename from dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/JsonRpcServiceImpl.java rename to dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpServiceImpl.java index 4284753..1830aa2 100644 --- a/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/JsonRpcServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-http/src/test/java/org/apache/dubbo/rpc/protocol/http/HttpServiceImpl.java @@ -16,7 +16,9 @@ */ package org.apache.dubbo.rpc.protocol.http; -public class JsonRpcServiceImpl implements JsonRpcService { +import org.apache.dubbo.rpc.RpcContext; + +public class HttpServiceImpl implements HttpService { private boolean called; public String sayHello(String name) { @@ -48,4 +50,9 @@ public class JsonRpcServiceImpl implements JsonRpcService { super(message); } } + + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } } 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 8ecfaee..821015f 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 @@ -38,4 +38,7 @@ public interface DemoService { int stringLength(String str); Type enumlength(Type... types); + + + String getRemoteApplicationName(); } \ No newline at end of file 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 ee7c368..a6e3f2c 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 @@ -69,4 +69,10 @@ public class DemoServiceImpl implements DemoService { public int stringLength(String str) { return str.length(); } + + + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } } \ No newline at end of file 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 df5bb55..ea6de85 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 @@ -49,7 +49,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class InjvmProtocolTest { - static{ + static { InjvmProtocol injvm = InjvmProtocol.getInjvmProtocol(); } @@ -76,7 +76,7 @@ public class InjvmProtocolTest { assertEquals(service.getSize(new String[]{"", "", ""}), 3); service.invoke("injvm://127.0.0.1/TestService", "invoke"); - InjvmInvoker injvmInvoker = new InjvmInvoker(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"),null,new HashMap<String, Exporter<?>>()); + InjvmInvoker injvmInvoker = new InjvmInvoker(DemoService.class, URL.valueOf("injvm://127.0.0.1/TestService"), null, new HashMap<String, Exporter<?>>()); assertFalse(injvmInvoker.isAvailable()); } @@ -99,7 +99,7 @@ public class InjvmProtocolTest { url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(SCOPE_KEY, SCOPE_LOCAL); assertTrue(InjvmProtocol.getInjvmProtocol().isInjvmRefer(url)); - url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(LOCAL_PROTOCOL,true); + url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(LOCAL_PROTOCOL, true); assertTrue(InjvmProtocol.getInjvmProtocol().isInjvmRefer(url)); url = URL.valueOf("fake://127.0.0.1/TestService").addParameter(SCOPE_KEY, SCOPE_REMOTE); @@ -110,4 +110,17 @@ public class InjvmProtocolTest { } + + @Test + public void testRemoteApplicationName() throws Exception { + DemoService service = new DemoServiceImpl(); + URL url = URL.valueOf("injvm://127.0.0.1/TestService").addParameter(INTERFACE_KEY, DemoService.class.getName()).addParameter("application", "consumer"); + 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(service.getRemoteApplicationName(), "consumer"); + } + } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java index bb9e4a8..78a132a 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoService.java @@ -38,4 +38,8 @@ public interface DemoService { @Path("/say") @Consumes({MediaType.TEXT_PLAIN}) String sayHello(String name); + + @GET + @Path("/getRemoteApplicationName") + String getRemoteApplicationName(); } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java index 3708d43..3b7b876 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/DemoServiceImpl.java @@ -49,4 +49,9 @@ public class DemoServiceImpl implements DemoService { public static Map<String, String> getAttachments() { return context; } + + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } } diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java index 92bed01..2e805d9 100644 --- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/RestProtocolTest.java @@ -241,4 +241,21 @@ public class RestProtocolTest { public void testDefaultPort() { assertThat(protocol.getDefaultPort(), is(80)); } + + @Test + public void testRemoteApplicationName() { + URL url = URL.valueOf("rest://127.0.0.1:5342/rest/say?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.DemoService").addParameter("application","consumer"); + DemoServiceImpl server = new DemoServiceImpl(); + ProviderModel providerModel = new ProviderModel(url.getPathKey(), server, DemoService.class); + ApplicationModel.initProviderModel(url.getPathKey(), providerModel); + + Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, url)); + Invoker<DemoService> invoker = protocol.refer(DemoService.class, url); + + DemoService client = proxy.getProxy(invoker); + String result = client.getRemoteApplicationName(); + Assertions.assertEquals("consumer", result); + invoker.destroy(); + exporter.unexport(); + } } diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java index c139b1a..4f88fa9 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoService.java @@ -42,4 +42,6 @@ public interface DemoService { int stringLength(String str); Type enumlength(Type... types); + + String getRemoteApplicationName(); } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java index ec461eb..c2300c4 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/DemoServiceImpl.java @@ -80,4 +80,10 @@ public class DemoServiceImpl implements DemoService { } catch (InterruptedException e) { } } + + + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } } \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java index 9f3cb33..5d0e90d 100644 --- a/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java +++ b/dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java @@ -132,6 +132,20 @@ public class RmiProtocolTest { exporter.unexport(); } + + @Test + public void testRemoteApplicationName() throws Exception { + DemoService service = new DemoServiceImpl(); + URL url = URL.valueOf("rmi://127.0.0.1:9001/TestService?release=2.7.0").addParameter("application", "consumer"); + Exporter<?> rpcExporter = protocol.export(proxy.getInvoker(service, DemoService.class, url)); + + service = proxy.getProxy(protocol.refer(DemoService.class, url)); + assertEquals(service.getRemoteApplicationName(), "consumer"); + + rpcExporter.unexport(); + + } + public interface NonStdRmiInterface { void bark(); }