Skip to content

Commit f4c210b

Browse files
jerrick-zhuchickenlj
authored andcommitted
Merge pull request #1873, support proxy for provider side.
fixes #67
1 parent e1171fa commit f4c210b

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ServiceConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,13 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
500500
if (logger.isInfoEnabled()) {
501501
logger.info("Register dubbo service " + interfaceClass.getName() + " url " + url + " to registry " + registryURL);
502502
}
503+
504+
// For providers, this is used to enable custom proxy to generate invoker
505+
String proxy = url.getParameter(Constants.PROXY_KEY);
506+
if (StringUtils.isNotEmpty(proxy)) {
507+
registryURL = registryURL.addParameter(Constants.PROXY_KEY, proxy);
508+
}
509+
503510
Invoker<?> invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString()));
504511
DelegateProviderMetaDataInvoker wrapperInvoker = new DelegateProviderMetaDataInvoker(invoker, this);
505512

dubbo-config/dubbo-config-api/src/test/java/com/alibaba/dubbo/config/ServiceConfigTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.alibaba.dubbo.common.URL;
2222
import com.alibaba.dubbo.config.api.DemoService;
2323
import com.alibaba.dubbo.config.api.Greeting;
24+
import com.alibaba.dubbo.config.mock.TestProxyFactory;
2425
import com.alibaba.dubbo.config.provider.impl.DemoServiceImpl;
2526
import com.alibaba.dubbo.config.mock.MockProtocol2;
2627
import com.alibaba.dubbo.config.mock.MockRegistryFactory2;
@@ -45,6 +46,7 @@
4546
import static org.hamcrest.Matchers.hasEntry;
4647
import static org.hamcrest.Matchers.hasKey;
4748
import static org.hamcrest.Matchers.hasSize;
49+
import static org.junit.Assert.assertEquals;
4850
import static org.junit.Assert.assertThat;
4951
import static org.mockito.Mockito.withSettings;
5052

@@ -53,6 +55,7 @@ public class ServiceConfigTest {
5355
private Registry registryDelegate = Mockito.mock(Registry.class);
5456
private Exporter exporter = Mockito.mock(Exporter.class);
5557
private ServiceConfig<DemoServiceImpl> service = new ServiceConfig<DemoServiceImpl>();
58+
private ServiceConfig<DemoServiceImpl> service2 = new ServiceConfig<DemoServiceImpl>();
5659

5760

5861
@Before
@@ -87,6 +90,14 @@ public void setUp() throws Exception {
8790
service.setInterface(DemoService.class);
8891
service.setRef(new DemoServiceImpl());
8992
service.setMethods(Collections.singletonList(method));
93+
94+
service2.setProvider(provider);
95+
service2.setApplication(app);
96+
service2.setRegistry(registry);
97+
service2.setInterface(DemoService.class);
98+
service2.setRef(new DemoServiceImpl());
99+
service2.setMethods(Collections.singletonList(method));
100+
service2.setProxy("testproxyfactory");
90101
}
91102

92103
@Test
@@ -112,6 +123,14 @@ public void testExport() throws Exception {
112123
Mockito.verify(protocolDelegate).export(Mockito.any(Invoker.class));
113124
}
114125

126+
@Test
127+
public void testProxy() throws Exception {
128+
service2.export();
129+
130+
assertThat(service2.getExportedUrls(), hasSize(1));
131+
assertEquals(2, TestProxyFactory.count); // local injvm and registry protocol, so expected is 2
132+
}
133+
115134
@Test
116135
@Ignore("cannot pass in travis")
117136
public void testUnexport() throws Exception {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.alibaba.dubbo.config.mock;
19+
20+
import com.alibaba.dubbo.common.URL;
21+
import com.alibaba.dubbo.rpc.Invoker;
22+
import com.alibaba.dubbo.rpc.RpcException;
23+
import com.alibaba.dubbo.rpc.proxy.jdk.JdkProxyFactory;
24+
25+
public class TestProxyFactory extends JdkProxyFactory {
26+
public static int count = 0;
27+
28+
@Override
29+
public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) throws RpcException {
30+
count++;
31+
return super.getInvoker(proxy, type, url);
32+
}
33+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
mockproxyfactory=com.alibaba.dubbo.config.mock.MockProxyFactory
1+
mockproxyfactory=com.alibaba.dubbo.config.mock.MockProxyFactory
2+
testproxyfactory=com.alibaba.dubbo.config.mock.TestProxyFactory

0 commit comments

Comments
 (0)