【分布式事务系列】Spring Cloud集成Seata 实现分布式事务(五)

211 阅读1分钟

这是我参与11月更文挑战的第15天,活动详情查看:2021最后一次更文挑战

SpringBoot基础上改进为Spring Cloud集成Seata,增加了Greenwich.SR2版本的Spring Cloud 依赖,项目名称如下:

  • spring-cloud-ozx-account
  • spring-cloud-ozx-repo
  • spring-cloud-ozx-order
  • spring-cloud-ozx-rest
集成Spring Cloud Alibaba Seata

在4个服务分别集成Spring Cloud Alibaba Seata

<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-alibaba-seata</artifactId>
	<version>2.1.1.RELEASE</version>
</dependency>

spring-cloud-alibaba-seata不支持yml形式,只能使用file.conf和registry.conf文件夹描述客户端的配置信息,可以直接将${SEATA_HOME}/conf目录下的两个文件复制到项目的resource目录下,如果从配置中心加载这些配置项,在registry.conf中指定配置中心即可,file.conf详细配置如下:

transport {
  type = "TCP"
  server = "NIO"
  ## client和server通信心跳检测开关
  heartbeat = true
  enable-clien-batch-sendR-request = true
  thread-factory {
    boss-thread-prefix = "NettyBoss"
    worker-thread-prefix = "NettyServerNIOWorker"
    server-executor-thread-prefix = "NettyServerBizHandler"
    share-boss-worker = false
    client-selector-thread-prefix = "NettyClientSelector"
    client-selector-thread-size = 1
    client-worker-thread-prefix = "NettyClientWorkerThread"
    boss-thread-size = 1
    worker-thread-size = 8
  }
  shutdown {
    wait = 3
  }
  ## Client和Server通信编解码方式
  serialization = "seata"
  ## Client和Server通信数据压缩方式(none、gzip,默认为none)
  compressor = "none"
}

service{
	vgroup_mapping.${txServiceGroup} = "default"
	default.grouplist = "10.0.100.254:8091"
	enableDegrade = false
	disableGlobalTransaction = false
}

client{
	rm{
		async.commit.buffer.limit = 10000
		lock{
		retry.internal = 10
		retry.times = 30
		retry.policy.branch-rollback-on-conflict = true
		}
		report.retry.count = 5
		table.meta.check.enable = false
		report.success.enable = true
	}
	tm{
	 commit.retry.count = 5
	 rollback.retry.count = 5
	}
	
	undo{
	data.validation = true
	log.serialization = "jackson"
	log.table = "undo_log"
	}
	log{
	exceptionRate = 100
	}
	support{
	spring.datasource.autoproxy = true
	}
}


在上述配置中,vgroup_mapping.txServiceGroup="default"表示事务群组,{txServiceGroup} ="default"表示事务群组,{txServiceGroup}表示事务服务分组,它的值要设置为spring.cloud.alibaba.seata.tx-service-group或者spring.application.name+"seata.tx-service-group"

  • 在spring-cloud-ozx-account、spring-cloud-ozx-repo、spring-cloud-ozx-order三个服务中添加一个配置类SeataAutoConfig,主要实现如下:
  1. 配置数据源代理DataSourceProxy
  2. 初始化GlobalTransactionScanner,装载到Spring IOC容器中。
@Configuration
@EnableConfigurationProperties({SeataProperties.class})
public class SeataAutoConfig{
    @Autowired
    private DataSourceProperties dataSourceProperties;
    private ApplicationContext applicationContext;
    private SeataProperties seataProperties;
    public SeataAutoConfig(SeataProperties seataProperties,ApplicationContext applicationContext){
        this.applicationContext=applicationContext;
        this.seataProperties=seataProperties;
    }
    @Bean
    public DruidDataSource druidDataSource(){
       DruidDataSource druidDataSource= new DruidDataSource();
        druidDataSource.setUrl(dataSourceProperties.getUrl());
        druidDataSource.setUsername(dataSourceProperties.getUsername());
        druidDataSource.setPassword(dataSourceProperties.getPassword());
        druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
        return druidDataSource;
    }
}