-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[AI-8th] fix: respect virtual host and port in dubbo provider bootstrap #1556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||
| package com.alipay.sofa.rpc.bootstrap.dubbo; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| import com.alipay.sofa.rpc.common.annotation.VisibleForTesting; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.dubbo.config.ProtocolConfig; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.dubbo.config.ServiceConfig; | ||||||||||||||||||||||||||||||||||||||||||||||||
| import com.alipay.sofa.rpc.bootstrap.ProviderBootstrap; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -97,27 +98,33 @@ private void copyServers(ProviderConfig<T> providerConfig, ServiceConfig service | |||||||||||||||||||||||||||||||||||||||||||||||
| if (CommonUtils.isNotEmpty(serverConfigs)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| List<ProtocolConfig> dubboProtocolConfigs = new ArrayList<ProtocolConfig>(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| for (ServerConfig serverConfig : serverConfigs) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // 生成并丢到缓存里 | ||||||||||||||||||||||||||||||||||||||||||||||||
| ProtocolConfig protocolConfig = DubboSingleton.SERVER_MAP.get(serverConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (protocolConfig == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig = new ProtocolConfig(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| copyServerFields(serverConfig, protocolConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| ProtocolConfig old = DubboSingleton.SERVER_MAP.putIfAbsent(serverConfig, protocolConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (old != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig = old; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| ProtocolConfig protocolConfig = getOrCreateProtocolConfig(serverConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| dubboProtocolConfigs.add(protocolConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| serviceConfig.setProtocols(dubboProtocolConfigs); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private void copyServerFields(ServerConfig serverConfig, ProtocolConfig protocolConfig) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| ProtocolConfig getOrCreateProtocolConfig(ServerConfig serverConfig) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| String serverCacheKey = buildServerCacheKey(serverConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| ProtocolConfig protocolConfig = DubboSingleton.SERVER_MAP.get(serverCacheKey); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (protocolConfig == null) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig = new ProtocolConfig(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| copyServerFields(serverConfig, protocolConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| ProtocolConfig old = DubboSingleton.SERVER_MAP.putIfAbsent(serverCacheKey, protocolConfig); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (old != null) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig = old; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| return protocolConfig; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| @VisibleForTesting | ||||||||||||||||||||||||||||||||||||||||||||||||
| void copyServerFields(ServerConfig serverConfig, ProtocolConfig protocolConfig) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setId(serverConfig.getId()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setName(serverConfig.getProtocol()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setHost(serverConfig.getHost()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setPort(serverConfig.getPort()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setHost(resolveHost(serverConfig)); | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setPort(resolvePort(serverConfig)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+123
to
+127
|
||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setAccepts(serverConfig.getAccepts()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setSerialization(serverConfig.getSerialization()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| if (!StringUtils.CONTEXT_SEP.equals(serverConfig.getContextPath())) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -132,6 +139,25 @@ private void copyServerFields(ServerConfig serverConfig, ProtocolConfig protocol | |||||||||||||||||||||||||||||||||||||||||||||||
| protocolConfig.setParameters(serverConfig.getParameters()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private String resolveHost(ServerConfig serverConfig) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return StringUtils.isNotBlank(serverConfig.getVirtualHost()) ? serverConfig.getVirtualHost() | ||||||||||||||||||||||||||||||||||||||||||||||||
| : serverConfig.getHost(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| private Integer resolvePort(ServerConfig serverConfig) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| return serverConfig.getVirtualPort() != null ? serverConfig.getVirtualPort() : serverConfig.getPort(); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| String buildServerCacheKey(ServerConfig serverConfig) { | ||||||||||||||||||||||||||||||||||||||||||||||||
| StringBuilder sb = new StringBuilder(64); | ||||||||||||||||||||||||||||||||||||||||||||||||
| sb.append(serverConfig.getProtocol()).append(':') | ||||||||||||||||||||||||||||||||||||||||||||||||
| .append(serverConfig.getHost()).append(':') | ||||||||||||||||||||||||||||||||||||||||||||||||
| .append(serverConfig.getPort()).append(':') | ||||||||||||||||||||||||||||||||||||||||||||||||
| .append(resolveHost(serverConfig)).append(':') | ||||||||||||||||||||||||||||||||||||||||||||||||
| .append(resolvePort(serverConfig)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+151
to
+157
|
||||||||||||||||||||||||||||||||||||||||||||||||
| String buildServerCacheKey(ServerConfig serverConfig) { | |
| StringBuilder sb = new StringBuilder(64); | |
| sb.append(serverConfig.getProtocol()).append(':') | |
| .append(serverConfig.getHost()).append(':') | |
| .append(serverConfig.getPort()).append(':') | |
| .append(resolveHost(serverConfig)).append(':') | |
| .append(resolvePort(serverConfig)); | |
| /** | |
| * Append a single part into the server cache key in an unambiguous way. | |
| * Uses a simple length-prefixed encoding: <length>#<value>. | |
| */ | |
| private void appendKeyPart(StringBuilder sb, Object part) { | |
| String value = part == null ? "" : String.valueOf(part); | |
| sb.append(value.length()).append('#').append(value); | |
| } | |
| String buildServerCacheKey(ServerConfig serverConfig) { | |
| StringBuilder sb = new StringBuilder(64); | |
| appendKeyPart(sb, serverConfig.getProtocol()); | |
| appendKeyPart(sb, serverConfig.getHost()); | |
| appendKeyPart(sb, serverConfig.getPort()); | |
| appendKeyPart(sb, resolveHost(serverConfig)); | |
| appendKeyPart(sb, resolvePort(serverConfig)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copyServerFields was changed from private to package-private for test access; the repo commonly annotates such methods with
@VisibleForTestingto document intent and discourage production callers from depending on it. Consider adding the annotation (and import) here.