Skip to content

Commit

Permalink
fix #10958 and polish duplicates code (#11102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenFeng312 committed Dec 15, 2022
1 parent fc7f019 commit b201cb0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -318,6 +319,16 @@ public ConcurrentNavigableMap<String, SortedSet<URL>> getExportedServiceURLs() {
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
Expand Down
Expand Up @@ -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;
Expand All @@ -25,10 +27,8 @@
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;

Expand All @@ -39,7 +39,7 @@
* @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();
Expand All @@ -48,17 +48,17 @@ public void customize(ServiceInstance serviceInstance, ApplicationModel applicat
}

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
Expand Down
Expand Up @@ -26,10 +26,7 @@
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;

Expand All @@ -53,13 +50,7 @@ public void customize(ServiceInstance serviceInstance, ApplicationModel applicat

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();
Expand Down

0 comments on commit b201cb0

Please sign in to comment.