Skip to content

Commit bcf9764

Browse files
authored
Fixes #9913, rmi protocol supoort group and version (#9951)
1 parent 81fabc7 commit bcf9764

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

dubbo-rpc/dubbo-rpc-rmi/src/main/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocol.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import static org.apache.dubbo.common.Version.isRelease270OrHigher;
3636
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
3737
import static org.apache.dubbo.common.constants.CommonConstants.RELEASE_KEY;
38+
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
39+
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
3840
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
3941

4042
/**
@@ -102,7 +104,14 @@ protected <T> T doRefer(final Class<T> serviceType, final URL url) throws RpcExc
102104
return invocation;
103105
});
104106
}
105-
String serviceUrl = url.toIdentityString();
107+
108+
String pathKey = URL.buildKey(url.getPath(), url.getParameter(GROUP_KEY), url.getParameter(VERSION_KEY));
109+
//The format is 'rmi://{host}:{ip}/{group}/{interfaceName}:{version}'
110+
StringBuilder buf = new StringBuilder();
111+
buf.append(url.getProtocol()).append("://")
112+
.append(url.getHost()).append(":").append(url.getPort())
113+
.append("/").append(pathKey);
114+
String serviceUrl = buf.toString();
106115
if (isGeneric) {
107116
serviceUrl = serviceUrl + "/" + GENERIC_KEY;
108117
}
@@ -137,9 +146,9 @@ private <T> RmiServiceExporter createExporter(T impl, Class<?> type, URL url, bo
137146
final RmiServiceExporter rmiServiceExporter = new RmiServiceExporter();
138147
rmiServiceExporter.setRegistryPort(url.getPort());
139148
if (isGeneric) {
140-
rmiServiceExporter.setServiceName(url.getPath() + "/" + GENERIC_KEY);
149+
rmiServiceExporter.setServiceName(url.getServiceKey() + "/" + GENERIC_KEY);
141150
} else {
142-
rmiServiceExporter.setServiceName(url.getPath());
151+
rmiServiceExporter.setServiceName(url.getServiceKey());
143152
}
144153
rmiServiceExporter.setServiceInterface(type);
145154
rmiServiceExporter.setService(impl);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.apache.dubbo.rpc.protocol.rmi;
18+
19+
import org.apache.dubbo.rpc.RpcContext;
20+
21+
import java.rmi.RemoteException;
22+
23+
/**
24+
* analog multi-implementation
25+
*/
26+
public class RemoteService2Impl implements RemoteService {
27+
28+
public String getThreadName() throws RemoteException {
29+
System.out.println("RpcContext.getContext().getRemoteHost()=" + RpcContext.getContext().getRemoteHost());
30+
return Thread.currentThread().getName();
31+
}
32+
33+
public String sayHello(String name) throws RemoteException {
34+
return "hello " + name + "@" + RemoteService2Impl.class.getName();
35+
}
36+
}

dubbo-rpc/dubbo-rpc-rmi/src/test/java/org/apache/dubbo/rpc/protocol/rmi/RmiProtocolTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,30 @@ public void testRemoteApplicationName() throws Exception {
155155

156156
}
157157

158+
@Test
159+
public void testRmiProtocalGroupAndVersion() throws Exception {
160+
int port = NetUtils.getAvailablePort();
161+
RemoteService remoteService = new RemoteServiceImpl();
162+
RemoteService remoteService2 = new RemoteService2Impl();
163+
164+
URL url = URL.valueOf("rmi://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?version=v1&group=g1");
165+
URL url2 = URL.valueOf("rmi://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?version=v2&group=g2");
166+
Exporter<?> rpcExporter = protocol.export(proxy.getInvoker(remoteService, RemoteService.class, url));
167+
Exporter<?> rpcExporter2 = protocol.export(proxy.getInvoker(remoteService2, RemoteService.class, url2));
168+
169+
remoteService = proxy.getProxy(protocol.refer(RemoteService.class, url));
170+
remoteService2 = proxy.getProxy(protocol.refer(RemoteService.class, url2));
171+
for (int i = 0; i < 1; i++) {
172+
String say = remoteService.sayHello("abcd");
173+
assertEquals("hello abcd@" + RemoteServiceImpl.class.getName(), say);
174+
String say2 = remoteService2.sayHello("group");
175+
assertEquals("hello group@" + RemoteService2Impl.class.getName(), say2);
176+
}
177+
rpcExporter.unexport();
178+
rpcExporter2.unexport();
179+
180+
}
181+
158182
public interface NonStdRmiInterface {
159183
void bark();
160184
}

0 commit comments

Comments
 (0)