Skip to content

Commit

Permalink
Add preferred protocol support for application service discovery (apa…
Browse files Browse the repository at this point in the history
…che#14849)

* Support preferred protocol first for application service discovery.

* Support preferred protocol first for application service discovery.

* update comments

* format code
  • Loading branch information
chickenlj authored Nov 7, 2024
1 parent 909eb01 commit 7c7788d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static boolean isMatch(ProtocolServiceKey rule, ProtocolServiceKey target
}

// 4.match protocol
// 4.1. if rule group is *, match all
// 4.1. if rule protocol is *, match all
if (!CommonConstants.ANY_VALUE.equals(rule.getProtocol())) {
// 4.2. if rule protocol is null, match all
if (StringUtils.isNotEmpty(rule.getProtocol())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INSTANCE_REGISTER_MODE;
import static org.apache.dubbo.common.constants.CommonConstants.IS_EXTRA;
import static org.apache.dubbo.common.constants.CommonConstants.PREFERRED_PROTOCOL;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_FAILED_DESTROY_INVOKER;
Expand Down Expand Up @@ -447,23 +448,12 @@ private Map<ProtocolServiceKeyWithAddress, Invoker<T>> toInvokers(
}

// filter all the service available (version wildcard, group wildcard, protocol wildcard)
int port = instanceAddressURL.getPort();
List<ProtocolServiceKey> matchedProtocolServiceKeys =
instanceAddressURL.getMetadataInfo().getMatchedServiceInfos(consumerProtocolServiceKey).stream()
.filter(serviceInfo -> serviceInfo.getPort() <= 0 || serviceInfo.getPort() == port)
// special filter for extra protocols.
.filter(serviceInfo -> {
if (StringUtils.isNotEmpty(
consumerProtocolServiceKey
.getProtocol())) { // if consumer side protocol is specified, use all
// the protocols we got in hand now directly
return true;
} else { // if consumer side protocol is not specified, remove all extra protocols
return StringUtils.isEmpty(serviceInfo.getParameter(IS_EXTRA));
}
})
.map(MetadataInfo.ServiceInfo::getProtocolServiceKey)
.collect(Collectors.toList());
getMatchedProtocolServiceKeys(instanceAddressURL, true);
if (CollectionUtils.isEmpty(matchedProtocolServiceKeys)) {
// if preferred protocol is not specified, use the default main protocol
matchedProtocolServiceKeys = getMatchedProtocolServiceKeys(instanceAddressURL, false);
}

// see org.apache.dubbo.common.ProtocolServiceKey.isSameWith
// check if needed to override the consumer url
Expand Down Expand Up @@ -524,6 +514,31 @@ private Map<ProtocolServiceKeyWithAddress, Invoker<T>> toInvokers(
return newUrlInvokerMap;
}

private List<ProtocolServiceKey> getMatchedProtocolServiceKeys(
InstanceAddressURL instanceAddressURL, boolean needPreferred) {
int port = instanceAddressURL.getPort();
return instanceAddressURL.getMetadataInfo().getMatchedServiceInfos(consumerProtocolServiceKey).stream()
.filter(serviceInfo -> serviceInfo.getPort() <= 0 || serviceInfo.getPort() == port)
// special filter for extra protocols.
.filter(serviceInfo -> {
if (StringUtils.isNotEmpty(
consumerProtocolServiceKey
.getProtocol())) { // if consumer side protocol is specified, use all
// the protocols we got in hand now directly
return true;
} else {
// if consumer side protocol is not specified, choose the preferred or default main protocol
if (needPreferred) {
return serviceInfo.getProtocol().equals(serviceInfo.getParameter(PREFERRED_PROTOCOL));
} else {
return StringUtils.isEmpty(serviceInfo.getParameter(IS_EXTRA));
}
}
})
.map(MetadataInfo.ServiceInfo::getProtocolServiceKey)
.collect(Collectors.toList());
}

private boolean urlChanged(Invoker<T> invoker, InstanceAddressURL newURL, ProtocolServiceKey protocolServiceKey) {
InstanceAddressURL oldURL = (InstanceAddressURL) invoker.getUrl();

Expand Down

0 comments on commit 7c7788d

Please sign in to comment.