jojocookx commented on issue #8726:
URL: https://github.com/apache/dubbo/issues/8726#issuecomment-1875068203
使用了2.7.x新版,这里提供个没有办法的办法:
思路是:
ServiceConfig里面有一个扩展点可以用,ConfiguratorFactory这个扩展点执行在port之后,并且可以对URL进行重置,我们实现扩展点,来对port进行重置
`
String host = findConfigedHosts(protocolConfig, registryURLs, map);
Integer port = findConfigedPorts(protocolConfig, name, map,
protocolConfigNum);
URL url = new URL(name, host, port,
getContextPath(protocolConfig).map(p -> p + "/" + path).orElse(path), map);
// You can customize Configurator to append extra parameters
if (ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class)
.hasExtension(url.getProtocol())) {
url =
ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class)
.getExtension(url.getProtocol()).getConfigurator(url).configure(url);
}
`
实现扩展点
`
public class BindPortFixConfiguratorFactory implements ConfiguratorFactory {
@Override
public Configurator getConfigurator(URL url) {
return new Configurator() {
@Override
public URL getUrl() {
return url;
}
@Override
public URL configure(URL url) {
URL finalURL = checkAndFixRegistryPort(url);
finalURL = checkAndFixBindPort(finalURL);
return finalURL;
}
};
}
private URL checkAndFixRegistryPort(URL url) {
Integer portToRegistryFromEnv = getEnvPortToRegistry();
if(portToRegistryFromEnv != null) {
int portToRegistryFromUrl = url.getPort();
if(portToRegistryFromEnv != portToRegistryFromUrl) {
return url.setPort(portToRegistryFromEnv);
}
}
return url;
}
private URL checkAndFixBindPort(URL url) {
Integer portToBindFromEnv = getEnvPortToBind();
if(portToBindFromEnv != null) {
String bindPort = url.getParameter("bind.port");
if(!portToBindFromEnv.toString().equals(bindPort)) {
return url.addParameter("bind.port",
String.valueOf(portToBindFromEnv));
}
}
return url;
}
private Integer getEnvPortToBind() {
String portStr = getValueFromConfig(DUBBO_PORT_TO_BIND);
return parsePort(portStr);
}
private Integer getEnvPortToRegistry() {
String portStr = getValueFromConfig(DUBBO_PORT_TO_REGISTRY);
return parsePort(portStr);
}
private String getValueFromConfig(String key) {
String value = ConfigUtils.getSystemProperty(key);
if (StringUtils.isEmpty(value)) {
value = ConfigUtils.getSystemProperty(key);
}
return value;
}
private Integer parsePort(String configPort) {
Integer port = null;
if (configPort != null && configPort.length() > 0) {
try {
Integer intPort = Integer.parseInt(configPort);
if (isInvalidPort(intPort)) {
throw new
IllegalArgumentException("Specified invalid port from env value:" + configPort);
}
port = intPort;
} catch (Exception e) {
throw new IllegalArgumentException("Specified
invalid port from env value:" + configPort);
}
}
return port;
}
}`
创建文件spi文件
resources/META-INF/dubbo/hbec.platform.commons.dubbo.spi.BindPortFixConfiguratorFactory
文件内容如下
`dubbo=hbec.platform.commons.dubbo.spi.BindPortFixConfiguratorFactory`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]