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 :)
PROBLEM
Most of the
setXxxxandaddParameter*inURLsuch asorg.apache.dubbo.common.URL#addParameterandsetXxxxwill return a newURLobject for chaining calls, and most of the "middle objects" are useless and unnecessary, example below:SOLUTION
And I suggest refactoring the
URLwith Builder Pattern. And the possible usage may be:Though we cannot remove the original
addXXXandsetXxxxmethods due to compability, we can mark them as deprecated and guide users to use the newURLBuilder.