欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > Seata源码笔记(二)

Seata源码笔记(二)

2025/8/23 8:36:53 来源:https://blog.csdn.net/qq_36514197/article/details/143690652  浏览:    关键词:Seata源码笔记(二)

Seata源码笔记(二)

  • 配置相关的
  • ConfigurationFactory
    • 静态代码块
      • load():融入spring获取value的方式
        • Configuration的get方法拦截后,value取值优先级
          • ObjectHolder
          • PROPERTY_BEAN_MAP
    • getInstance
      • buildConfiguration
    • reload

基于incubartor-seata-2.x

配置相关的

ConfigurationFactory

静态代码块

initOriginConfiguration和maybeNeedOriginFileInstance逻辑较为简单,主要看看load

private static final String REGISTRY_CONF_DEFAULT = "registry";
private static final String ENV_SYSTEM_KEY = "SEATA_ENV";
public static final String ENV_PROPERTY_KEY = "seataEnv";private static final String SYSTEM_PROPERTY_SEATA_CONFIG_NAME = "seata.config.name";private static final String ENV_SEATA_CONFIG_NAME = "SEATA_CONFIG_NAME";public static volatile Configuration CURRENT_FILE_INSTANCE;public static volatile FileConfiguration ORIGIN_FILE_INSTANCE_REGISTRY;public static volatile FileConfiguration ORIGIN_FILE_INSTANCE = null;static {// 负责初始化ORIGIN_FILE_INSTANCE_REGISTRY,通过读取registry.conf(<configName>-<envName>.conf)initOriginConfiguration();// 初始化CURRENT_FILE_INSTANCE(判断是否有extConfiguration,没有就用ORIGIN_FILE_INSTANCE_REGISTRY,本来FileConfiguration就是Configuration的实现)// ext默认有个SpringBootConfigurationProvider实现,返回一个代理对象(seata-spring-autoconfigure-core里)load();// 如果CURRENT_FILE_INSTANCE是.conf的(也就是load用的还是registry.conf)// 那原配置文件ORIGIN_FILE_INSTANCE就用的registry.conf里的config.file.name(test中给的file.conf)maybeNeedOriginFileInstance();
}

load():融入spring获取value的方式

对应上述静态代码块里load部分(主要是通过SPI去找ExtConfigurationProvider的实现然后调用provide方法,目前唯一提供的实现SpringBootConfigurationProvider,它是通过动态代理创建一个Configuration的代理对象,来拦截并设置get方法获取的值)

public class SpringBootConfigurationProvider implements ExtConfigurationProvider {private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootConfigurationProvider.class);private static final String INTERCEPT_METHOD_PREFIX = "get";private static final Map<String, Object> PROPERTY_BEAN_INSTANCE_MAP = new ConcurrentHashMap<>(64);@Overridepublic Configuration provide(Configuration originalConfiguration) {return (Configuration)Enhancer.create(originalConfiguration.getClass(),(MethodInterceptor)(proxy, method, args, methodProxy) -> {if (method.getName().startsWith(INTERCEPT_METHOD_PREFIX) && args.length > 0) {Object result;String rawDataId = (String)args[0];Class<?> dataType = ReflectionUtil.getWrappedClass(method.getReturnType());// 1. Get config value from the system propertyresult = originalConfiguration.getConfigFromSys(rawDataId);if (result == null) {String dataId = convertDataId(rawDataId);// 2. Get config value from the springboot environmentresult = getConfigFromEnvironment(dataId, dataType);if (result != null) {return result;}// 3. Get config defaultValue from the argumentsif (args.length > 1) {result = args[1];if (result != null) {// See Configuration#getConfig(String dataId, long timeoutMills)if (dataType.isAssignableFrom(result.getClass())) {return result;} else {result = null;}}}// 4. Get config defaultValue from the property objecttry {result = getDefaultValueFromPropertyObject(dataId);} catch (Throwable t) {LOGGER.error("Get config '{}' default value from the property object failed:", dataId, t);}}if (result != null) {if (dataType.isAssignableFrom(result.getClass())) {return result;}// Convert typereturn this.convertType(result, dataType);}}return method.invoke(originalConfiguration, args);});}...
}
Configuration的get方法拦截后,value取值优先级

result,也就是get方法的结果读取优先级:

环境变量(步骤1里getenv和getProperty)
ApplicationContext(步骤2里的ObjectHolder,很贴心的做了各种变换去找,下面会去看ObjectHolder怎么来的)
Configuration.getConfig(String dataId, String defaultValue…)(步骤3取默认值)
从PROPERTY_BEAN_MAP获取(步骤4,下面会去看PROPERTY_BEAN_MAP怎么来的)

ObjectHolder

seata-spring-autoconfigure-core的spring.factories配了个自动配置项SeataCoreAutoConfiguration
在这里插入图片描述在这里插入图片描述在这里插入图片描述

PROPERTY_BEAN_MAP

同上,seata-spring-autoconfigure-core的spring.factories配了个自动配置项SeataCoreEnvironmentPostProcessor,并且client和server包里也配置了SeataClientEnvironmentPostProcessor和SeataServerEnvironmentPostProcessor
在这里插入图片描述

getInstance

buildConfiguration

reload

工作去了,晚点更

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词