@feng996 我找到了问题的原因,它与dubbo-spring-boot-starter
我最初想的并不相关。
来源在org.apache.dubbo.config.metadata.ServiceInstanceHostPortCustomizer
,我们来看一下:
@Override
public void customize(ServiceInstance serviceInstance) {
String host = null;
int port = -1;
// ......
Set<URL> urls = writableMetadataService.getExportedServiceURLs();
if (CollectionUtils.isNotEmpty(urls)) {
ApplicationModel applicationModel = serviceInstance.getApplicationModel();
String preferredProtocol = applicationModel.getCurrentConfig().getProtocol();
// The default value of preferredProtocol is dubbo.
if (preferredProtocol != null) {
for (URL exportedURL : urls) {
// The protocol of exportedURL is tri
// because of tri != dubbo, the host and port will not be assigned values.
if (preferredProtocol.equals(exportedURL.getProtocol())) {
host = exportedURL.getHost();
port = exportedURL.getPort();
break;
}
}
} else {
URL url = urls.iterator().next();
host = url.getHost();
port = url.getPort();
}
// If not the port of instace > 0, the application instance will not be registered to RegistryCenter(like Nacos, Zookeeper).
// As a result, the consumer can't find the avaliabe providers when our settging is registry-mode=instance
if (serviceInstance instanceof DefaultServiceInstance) {
DefaultServiceInstance instance = (DefaultServiceInstance) serviceInstance;
instance.setHost(host);
instance.setPort(port);
}
}
}
目前,如果希望消费者和提供者都强制使用Application Discovery
三重协议,只需更改提供者的一行配置,如下所示:
dubbo:
application:
metadata-type: remote
register-mode: instance
protocol: tri <==== HERE, set preferredProtocol as tri
但是,Dubbo 官方为了进一步推广还需要做两件事:
- 尽快完成文档:三方协议迁移指南
- preferredProtocol 的逻辑不够人性化,可能需要优化一下。
与 #8666 相关