fix #10958 and polish duplicates code (#11102)

This commit is contained in:
ShenFeng312 2022-12-15 16:53:06 +08:00 committed by GitHub
parent fc7f019a08
commit b201cb09bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 22 deletions

View File

@ -30,6 +30,7 @@ import org.apache.dubbo.common.utils.StringUtils;
import java.beans.Transient;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@ -318,6 +319,16 @@ public class MetadataInfo implements Serializable {
return exportedServiceURLs;
}
public Set<URL> collectExportedURLSet() {
if (exportedServiceURLs == null) {
return Collections.emptySet();
}
return exportedServiceURLs.values().stream()
.filter(CollectionUtils::isNotEmpty)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
}
private boolean addURL(Map<String, SortedSet<URL>> serviceURLs, URL url) {
SortedSet<URL> urls = serviceURLs.computeIfAbsent(url.getServiceKey(), this::newSortedURLs);
// make sure the parameters of tmpUrl is variable

View File

@ -17,6 +17,8 @@
package org.apache.dubbo.registry.client.metadata;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.metadata.MetadataInfo;
import org.apache.dubbo.registry.client.ServiceInstance;
@ -25,10 +27,8 @@ import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.setEndpoints;
@ -39,7 +39,7 @@ import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataU
* @since 2.7.5
*/
public class ProtocolPortsMetadataCustomizer implements ServiceInstanceCustomizer {
private static final ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(ProtocolPortsMetadataCustomizer.class);
@Override
public void customize(ServiceInstance serviceInstance, ApplicationModel applicationModel) {
MetadataInfo metadataInfo = serviceInstance.getServiceMetadata();
@ -48,17 +48,17 @@ public class ProtocolPortsMetadataCustomizer implements ServiceInstanceCustomize
}
Map<String, Integer> protocols = new HashMap<>();
Set<URL> urls = new HashSet<>();
Map<String, SortedSet<URL>> exportedURLS = metadataInfo.getExportedServiceURLs();
for (Map.Entry<String, SortedSet<URL>> entry : exportedURLS.entrySet()) {
if (entry.getValue() != null) {
urls.addAll(entry.getValue());
}
}
Set<URL> urls = metadataInfo.collectExportedURLSet();
urls.forEach(url -> {
// TODO, same protocol listen on different ports will override with each other.
protocols.put(url.getProtocol(), url.getPort());
String protocol = url.getProtocol();
Integer oldPort = protocols.get(protocol);
int newPort = url.getPort();
if (oldPort != null) {
LOGGER.warn("same protocol " + "[" + protocol + "]" + " listen on different ports " + "[" + oldPort + "," + newPort + "]" + " will override with each other" +
".Override port [" + oldPort + "] with port [" + newPort + "]");
}
protocols.put(protocol, newPort);
});
if (protocols.size() > 0) {// set endpoints only for multi-protocol scenario

View File

@ -26,10 +26,7 @@ import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.ServiceInstanceCustomizer;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_FAILED_INIT_SERIALIZATION_OPTIMIZER;
@ -53,13 +50,7 @@ public class ServiceInstanceHostPortCustomizer implements ServiceInstanceCustomi
String host = null;
int port = -1;
Set<URL> urls = new HashSet<>();
Map<String, SortedSet<URL>> exportedURLS = metadataInfo.getExportedServiceURLs();
for (Map.Entry<String, SortedSet<URL>> entry : exportedURLS.entrySet()) {
if (entry.getValue() != null) {
urls.addAll(entry.getValue());
}
}
Set<URL> urls = metadataInfo.collectExportedURLSet();
if (CollectionUtils.isNotEmpty(urls)) {
String preferredProtocol = applicationModel.getCurrentConfig().getProtocol();