3)方法1.1,AbstractInterfaceConfig类#startConfigCenter()方法
void startConfigCenter() {if (configCenter == null) {ConfigManager.getInstance().getConfigCenter().ifPresent(cc -> this.configCenter = cc);}// 如果配置了ConfigCenterif (this.configCenter != null) {// 从其他位置获取配置中心的相关属性信息,比如配置中心地址// TODO there may have duplicate refreshthis.configCenter.refresh();// 属性更新后,从远程配置中心获取数据(应用配置,全局配置)prepareEnvironment();}// 从配置中心取到配置数据后,刷新所有的XxConfig中的属性ConfigManager.getInstance().refreshAll();}private void prepareEnvironment() {if (configCenter.isValid()) {if (!configCenter.checkOrUpdateInited()) {return;}// 动态配置中心,管理台上的配置中心DynamicConfiguration dynamicConfiguration = getDynamicConfiguration(configCenter.toUrl());// 如果是zookeeper,获取的就是/dubbo/config/dubbo/dubbo.properties节点中的内容String configContent = dynamicConfiguration.getProperties(configCenter.getConfigFile(), configCenter.getGroup());String appGroup = application != null ? application.getName() : null;String appConfigContent = null;if (StringUtils.isNotEmpty(appGroup)) {// 获取的就是/dubbo/config/dubbo-demo-consumer-application/dubbo.properties节点中的内容// 这里有bugappConfigContent = dynamicConfiguration.getProperties (StringUtils.isNotEmpty(configCenter.getAppConfigFile()) ? configCenter.getAppConfigFile() : configCenter.getConfigFile(), appGroup );}try {Environment.getInstance().setConfigCenterFirst(configCenter.isHighestPriority());Environment.getInstance().updateExternalConfigurationMap(parseProperties(configContent));Environment.getInstance().updateAppExternalConfigurationMap(parseProperties(appConfigContent));} catch (IOException e) {throw new IllegalStateException(...);}}}4)方法1.2,AbstractInterfaceConfig类#refresh()方法
// 刷新XxConfig// 一个XxConfig对象的属性可能是有值的,也可能是没有值的,这时需要从其他位置获取属性值,来进行属性的覆盖// 覆盖的优先级,从大到小为系统变量->配置中心应用配置->配置中心全局配置->注解或xml中定义->dubbo.properties文件// 以ServiceConfig为例,ServiceConfig中包括很多属性,比如timeout// 但是在定义一个Service时,如果在注解上没有配置timeout,那么就会其他地方获取timeout的配置// 比如可以从系统变量->配置中心应用配置->配置中心全局配置->注解或xml中定义->dubbo.properties文件// refresh是刷新,将当前ServiceConfig上的set方法所对应的属性更新为优先级最高的值public void refresh() {try {CompositeConfiguration compositeConfiguration = Environment.getInstance().getConfiguration(getPrefix(), getId());// 表示XxConfig对象本身- AbstractConfigConfiguration config = new ConfigConfigurationAdapter(this);//设置顺序,if (Environment.getInstance().isConfigCenterFirst()) {// The sequence would be: SystemConfiguration -> AppExternalConfiguration -> ExternalConfiguration -> AbstractConfig -> PropertiesConfigurationcompositeConfiguration.addConfiguration(4, config);} else {// The sequence would be: SystemConfiguration -> AbstractConfig -> AppExternalConfiguration -> ExternalConfiguration -> PropertiesConfigurationcompositeConfiguration.addConfiguration(2, config);}// loop methods, get override value and set the new value back to method//Method[] methods = getClass().getMethods();for (Method method : methods) {// 是不是setXX()方法if (MethodUtils.isSetter(method)) {// 获取xx配置项的valueString value = https://www.huyubaike.com/biancheng/StringUtils.trim(compositeConfiguration.getString(extractPropertyName(getClass(), method)));// isTypeMatch() is called to avoid duplicate and incorrect update, for example, we have two'setGeneric' methods in ReferenceConfig.if (StringUtils.isNotEmpty(value) && ClassUtils.isTypeMatch(method.getParameterTypes()[0], value)) {method.invoke(this, ClassUtils.convertPrimitive(method.getParameterTypes()[0], value));}// 是不是setParameters()方法} else if (isParametersSetter(method)) {// 获取parameter配置项的valueString value = https://www.huyubaike.com/biancheng/StringUtils.trim(compositeConfiguration.getString(extractPropertyName(getClass(), method)));if (StringUtils.isNotEmpty(value)) {Map
经验总结扩展阅读
- 四 SpringBoot - 整合Mybatis,逆向工程,JPA
- 属鸡2023年多大年龄了 属鸡人2023全年运势详解
- 华为手环6怎么样_华为手环6功能详解
- logback.xml详解
- iptables使用详解
- 一篇文章带你掌握主流办公框架——SpringBoot
- RAID5 IO处理之条带读代码详解
- RAID5 IO处理之写请求代码详解
- RAID5 IO处理之重构代码详解
- RAID5 IO处理之replace代码详解
