当前位置 博文首页 > Codorld:简单读读源码 - dubbo多提供者(provider)配置方法
dubbo:
consumer:
timeout: 300000
protocol:
name: dubbo
port: -1
cloud:
subscribed-services: order-server
# subscribed-services: hello-server,account-server,storage-server,order-server
subscribed-services
如下图:这里是对应的setter方法,上面找到定义地方:
/**
* All services of Dubbo.
*/
public static final String ALL_DUBBO_SERVICES = "*";
/**
* The subscribed services, the default value is "*". The multiple value will use
* comma(",") as the separator.
*
* @see #ALL_DUBBO_SERVICES
*/
private String subscribedServices = ALL_DUBBO_SERVICES;
读一读就知道答案了。默认为*,多个值时候用,
隔开。
接下来,走走最表面的流程,就是看看怎么处理我们输入的数据的,至于是怎么找到提供者的,先不管。
光标点到类DubboCloudProperties
上面,这里ctrl+单击是点不进去的,但是你点一下有提醒:
No usages found in Project Files
Press Ctrl+Alt+F7 again to search in 'Project and Libraries
ubscribedServices
,不要开头的s,不管大写还是小写,Ctrl+F搜出来看看。initSubscribedServices()
方法。读一下,如果ALL_DUBBO_SERVICES
等于我们输入的提供者,就是输出巴拉巴拉。
ALL_DUBBO_SERVICES
是啥,ctrl点,发现又跳回第一个文件了:@ConfigurationProperties(prefix = CONFIG_PROPERTY_PREFIX)
public class DubboCloudProperties {
/**
* All services of Dubbo.
*/
public static final String ALL_DUBBO_SERVICES = "*";
// 这里是默认为*,如果set方法没有执行,那么get时候获得的就是*
private String subscribedServices = ALL_DUBBO_SERVICES;
/* ... */
}
*
或者注释掉。启动消费者。查看日志:
2021-05-27 16:28:24.950 WARN 6564 --- [client.listener] a.c.d.m.r.DubboServiceMetadataRepository : Current application will subscribe all services(size:20) in registry, a lot of memory and CPU cycles may be used, thus it's strongly recommend you using the externalized property 'dubbo.cloud.subscribed-services' to specify the services
与步骤8中一致。
subscribedServices()
后加入到那个集合中newSubscribedServices.addAll(dubboCloudProperties.subscribedServices());
ctrl点subscribedServices()
,然后发现又转到第一个文件了:
再继续读一下:使用commaDelimitedListToStringArray
将我们输入的东西转化了字符串数组,处理返回。
commaDelimitedListToStringArray
怎么处理的,找到导入它的地方:import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
然后ctrl点:
/**
* Convert a comma delimited list (e.g., a row from a CSV file) into an
* array of strings.
* @param str the input {@code String} (potentially {@code null} or empty)
* @return an array of strings, or the empty array in case of empty input
*/
public static String[] commaDelimitedListToStringArray(@Nullable String str) {
return delimitedListToStringArray(str, ",");
}
这里与标题3.中The multiple value will use comma(",") as the separator.
对应。
不写或者*会订阅所有的。
写多就使用,
隔开。