Repository navigation
Closed
Milestone
Description
- I have searched the issues of this repository and believe that this is not a duplicate.
- I have checked the FAQ of this repository and believe that this is not a duplicate.
PROBLEM
Most of the setXxxx and addParameter* in URL such as org.apache.dubbo.common.URL#addParameter and setXxxx will return a new URL object for chaining calls, and most of the "middle objects" are useless and unnecessary, example below:
class
org.apache.dubbo.config.spring.SimpleRegistryExporter
public static Exporter<RegistryService> export(int port, RegistryService registryService) {
return protocol.export(proxyFactory.getInvoker(registryService, RegistryService.class,
// HERE
new URL("dubbo", NetUtils.getLocalHost(), port, RegistryService.class.getName())
.setPath(RegistryService.class.getName()) // useless
.addParameter(Constants.INTERFACE_KEY, RegistryService.class.getName()) // useless
.addParameter(Constants.CLUSTER_STICKY_KEY, "true") // useless
.addParameter(Constants.CALLBACK_INSTANCES_LIMIT_KEY, "1000") // useless
.addParameter("ondisconnect", "disconnect") // useless
.addParameter("subscribe.1.callback", "true") // useless
.addParameter("unsubscribe.1.callback", "false")));
}class
org.apache.dubbo.config.ServiceConfig
@SuppressWarnings({"unchecked", "rawtypes"})
private void exportLocal(URL url) {
if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
// HERE
URL local = URL.valueOf(url.toFullString())
.setProtocol(Constants.LOCAL_PROTOCOL) // useless
.setHost(LOCALHOST_VALUE) // useless
.setPort(0);
Exporter<?> exporter = protocol.export(
proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
exporters.add(exporter);
logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry");
}
}SOLUTION
And I suggest refactoring the URL with Builder Pattern. And the possible usage may be:
URL url = ...;
URL newURL = new URLBuilder(url)
.addParameter(k, v)
.addParameter(k2, v2)
.setPath(yourPath)
.build();Though we cannot remove the original addXXX and setXxxx methods due to compability, we can mark them as deprecated and guide users to use the new URLBuilder.
I'd like to try refactoring this if it's good to the comunity :)
Metadata
Metadata
Assignees
Labels
No labels
Activity
chickenlj commentedon Feb 18, 2019
At least, this could avoid the recreation of URL in the
exportandreferprocess.But, please keep in mind that the URL is designed to be immutable when
Invokeris created.kezhenxu94 commentedon Feb 18, 2019
@chickenlj The SOLUTION I proposed doesn't break the immutability of
URL, the proposedURLBuilderis used to reduce the middle state objects when building a newURLfrom an old one.produces 7 useless
URLobjects, andleverages the same
URLBuilderobject and finally got theURLwanted.If it's not clear for you to understand my point, consider the
URLBuilderimplementation as follows:chickenlj commentedon Feb 18, 2019
Great, looking forward to URLBuilder being available soon.
kezhenxu94 commentedon Feb 18, 2019
@chickenlj FYI, I've opened a PR #3500 to fix this issue
chickenlj commentedon Feb 20, 2019
Cool, I am on it.
chickenlj commentedon Feb 27, 2019
I've started a thread discussing this feature on the mailing list.
[DUBBO-3494]: Refactor URL to URLBuilder (#3500)
Merge master branch to 3.x dev branch (#3718)