Search in sources :

Example 1 with NacosConfiguration

use of io.seata.config.nacos.NacosConfiguration in project chao-cloud by chaojunzi.

the class TxSeataImportSelector method selectImports.

@Override
public String[] selectImports(AnnotationMetadata metadata) {
    Class<Object> className = ClassUtil.loadClass(metadata.getClassName());
    // 排除 DataSourceAutoConfiguration 自动注入
    SpringBootApplication boot = AnnotationUtil.getAnnotation(className, SpringBootApplication.class);
    // 修改值
    Class<?>[] exclude = boot.exclude();
    if (!ArrayUtil.contains(exclude, DataSourceAutoConfiguration.class)) {
        Class<? extends Object>[] excludeClass = ArrayUtil.append(exclude, DataSourceAutoConfiguration.class);
        EntityUtil.putAnnotationValue(boot, "exclude", excludeClass);
    }
    // 获取注解值
    EnableTxSeata txSeata = AnnotationUtil.getAnnotation(className, EnableTxSeata.class);
    // 加载相关配置
    ConfigType type = txSeata.value();
    switch(type) {
        case // 0.7.1 只支持nacos
        Nacos:
            // 整合seata-nacos
            Properties properties = this.getNacosProp();
            // 获取配置资源
            try {
                ConfigService configService = NacosFactory.createConfigService(properties);
                // NacosConfiguration
                EntityUtil.setPrivateFinalField(NacosConfiguration.class, "configService", configService);
                // 
                NacosConfiguration configuration = NacosConfiguration.getInstance();
                // ConfigurationFactory
                EntityUtil.setPrivateFinalField(ConfigurationFactory.class, "instance", configuration);
                // 修改注册-nacos
                // EntityUtil.setPrivateFinalField(ConfigurationFactory.class,
                // "DEFAULT_FILE_INSTANCE", configuration);
                EntityUtil.setPrivateFinalField(ConfigurationFactory.class, "CURRENT_FILE_INSTANCE", configuration);
                // 修改服务发现
                NamingService namingService = NamingFactory.createNamingService(properties);
                EntityUtil.setPrivateFinalField(NacosRegistryServiceImpl.class, "naming", namingService);
            } catch (Exception e) {
                log.error("{}", e);
                throw new BusinessException("Nacos-Seata 初始化失败");
            }
            return new String[] { TxSeataConfig.class.getName() };
        default:
            break;
    }
    return null;
}
Also used : Properties(java.util.Properties) DataSourceAutoConfiguration(org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration) BusinessException(com.chao.cloud.common.exception.BusinessException) SpringBootApplication(org.springframework.boot.autoconfigure.SpringBootApplication) NacosConfiguration(io.seata.config.nacos.NacosConfiguration) BusinessException(com.chao.cloud.common.exception.BusinessException) ConfigService(com.alibaba.nacos.api.config.ConfigService) NamingService(com.alibaba.nacos.api.naming.NamingService) ConfigType(io.seata.config.ConfigType)

Aggregations

ConfigService (com.alibaba.nacos.api.config.ConfigService)1 NamingService (com.alibaba.nacos.api.naming.NamingService)1 BusinessException (com.chao.cloud.common.exception.BusinessException)1 ConfigType (io.seata.config.ConfigType)1 NacosConfiguration (io.seata.config.nacos.NacosConfiguration)1 Properties (java.util.Properties)1 SpringBootApplication (org.springframework.boot.autoconfigure.SpringBootApplication)1 DataSourceAutoConfiguration (org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration)1