From f4c210b6d6846cc72f92997b52c3dd4c563347be Mon Sep 17 00:00:00 2001 From: Jerrick Zhu Date: Fri, 1 Jun 2018 17:18:25 +0800 Subject: [PATCH] Merge pull request #1873, support proxy for provider side. fixes #67 --- .../alibaba/dubbo/config/ServiceConfig.java | 7 ++++ .../dubbo/config/ServiceConfigTest.java | 19 +++++++++++ .../dubbo/config/mock/TestProxyFactory.java | 33 +++++++++++++++++++ .../com.alibaba.dubbo.rpc.ProxyFactory | 3 +- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/mock/TestProxyFactory.java diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java index 8637662d335..ec6dcd982c8 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java @@ -500,6 +500,13 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r if (logger.isInfoEnabled()) { logger.info("Register dubbo service " + interfaceClass.getName() + " url " + url + " to registry " + registryURL); } + + // For providers, this is used to enable custom proxy to generate invoker + String proxy = url.getParameter(Constants.PROXY_KEY); + if (StringUtils.isNotEmpty(proxy)) { + registryURL = registryURL.addParameter(Constants.PROXY_KEY, proxy); + } + Invoker invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString())); DelegateProviderMetaDataInvoker wrapperInvoker = new DelegateProviderMetaDataInvoker(invoker, this); diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/ServiceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/ServiceConfigTest.java index d0eb6bc8d0c..ed5a2e33d7f 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/ServiceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/ServiceConfigTest.java @@ -21,6 +21,7 @@ import com.alibaba.dubbo.common.URL; import com.alibaba.dubbo.config.api.DemoService; import com.alibaba.dubbo.config.api.Greeting; +import com.alibaba.dubbo.config.mock.TestProxyFactory; import com.alibaba.dubbo.config.provider.impl.DemoServiceImpl; import com.alibaba.dubbo.config.mock.MockProtocol2; import com.alibaba.dubbo.config.mock.MockRegistryFactory2; @@ -45,6 +46,7 @@ import static org.hamcrest.Matchers.hasEntry; import static org.hamcrest.Matchers.hasKey; import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.withSettings; @@ -53,6 +55,7 @@ public class ServiceConfigTest { private Registry registryDelegate = Mockito.mock(Registry.class); private Exporter exporter = Mockito.mock(Exporter.class); private ServiceConfig service = new ServiceConfig(); + private ServiceConfig service2 = new ServiceConfig(); @Before @@ -87,6 +90,14 @@ public void setUp() throws Exception { service.setInterface(DemoService.class); service.setRef(new DemoServiceImpl()); service.setMethods(Collections.singletonList(method)); + + service2.setProvider(provider); + service2.setApplication(app); + service2.setRegistry(registry); + service2.setInterface(DemoService.class); + service2.setRef(new DemoServiceImpl()); + service2.setMethods(Collections.singletonList(method)); + service2.setProxy("testproxyfactory"); } @Test @@ -112,6 +123,14 @@ public void testExport() throws Exception { Mockito.verify(protocolDelegate).export(Mockito.any(Invoker.class)); } + @Test + public void testProxy() throws Exception { + service2.export(); + + assertThat(service2.getExportedUrls(), hasSize(1)); + assertEquals(2, TestProxyFactory.count); // local injvm and registry protocol, so expected is 2 + } + @Test @Ignore("cannot pass in travis") public void testUnexport() throws Exception { diff --git a/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/mock/TestProxyFactory.java b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/mock/TestProxyFactory.java new file mode 100644 index 00000000000..d70ecf76409 --- /dev/null +++ b/dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/mock/TestProxyFactory.java @@ -0,0 +1,33 @@ +/* + * 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 com.alibaba.dubbo.config.mock; + +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.rpc.Invoker; +import com.alibaba.dubbo.rpc.RpcException; +import com.alibaba.dubbo.rpc.proxy.jdk.JdkProxyFactory; + +public class TestProxyFactory extends JdkProxyFactory { + public static int count = 0; + + @Override + public Invoker getInvoker(T proxy, Class type, URL url) throws RpcException { + count++; + return super.getInvoker(proxy, type, url); + } +} diff --git a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.ProxyFactory b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.ProxyFactory index 0ae5c21f89f..d1af1477872 100644 --- a/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.ProxyFactory +++ b/dubbo-config/dubbo-config-api/src/test/resources/META-INF/services/com.alibaba.dubbo.rpc.ProxyFactory @@ -1 +1,2 @@ -mockproxyfactory=com.alibaba.dubbo.config.mock.MockProxyFactory \ No newline at end of file +mockproxyfactory=com.alibaba.dubbo.config.mock.MockProxyFactory +testproxyfactory=com.alibaba.dubbo.config.mock.TestProxyFactory \ No newline at end of file