分布式事务-seate。

1.参考链接:https://github.com/seata/seata-samples/tree/master/springcloud-eureka-feign-mybatis-seata;

分布式事务-seate。_第1张图片

2.代码链接:https://github.com/kickTec/springCloudDemo/tree/transaction-seate

3.关键组件:

  eureka、seate-0.8.0服务器、微服务1(hello)、微服务2(feign-consumer)

spring boot:2.1.8.RELEASE

spring cloud:Greenwich.SR2

4.seate服务器参数(主要指定host,多个网卡自动绑定IP有时不对):

    @Parameter(names = "--help", help = true)
    private boolean help;
    @Parameter(names = {"--host", "-h"}, description = "The ip to register to registry center.", order = 1)
    private String host;
    @Parameter(names = {"--port", "-p"}, description = "The port to listen.", order = 2)
    private int port = SERVER_DEFAULT_PORT;
    @Parameter(names = {"--storeMode", "-m"}, description = "log store mode : file, db", order = 3)
    private String storeMode;
    @Parameter(names = {"--serverNode", "-n"}, description = "server node id, such as 1, 2, 3.it will be generated according to the snowflake by default", order = 4)
    private Long serverNode;
    @Parameter(names = {"--seataEnv", "-e"}, description = "The name used for multi-configuration isolation.",
        order = 5)

windows启动seate服务器:seata-server.bat -h 192.168.0.18

5.微服务配置:

maven配置:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.8.RELEASE
		 
	
	com.kenick
	feign-consumer
	0.0.1-SNAPSHOT
	feign-consumer
	feign-consumer

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-web
		

		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.0.0
		

		
			mysql
			mysql-connector-java
			runtime
		

		
		
			com.github.pagehelper
			pagehelper
			4.0.0
		

		
		
			com.alibaba
			druid-spring-boot-starter
			1.1.10
			
				
					slf4j-api
					org.slf4j
				
			
		

		
		
			org.projectlombok
			lombok
			1.18.8
		

		
		
			com.alibaba.cloud
			spring-cloud-alibaba-seata
			2.1.0.RELEASE
			
				
					seata-all
					io.seata
				
			
		
		
			io.seata
			seata-all
			1.2.0
		

		
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		

		
		
			org.springframework.cloud
			spring-cloud-starter-openfeign
		
	

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.SR2
                pom
                import
            
        
    

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


seate特殊配置(seate服务器和微服务指定相同的tx-service-group):

spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group

file.conf文件中设置:

  #transaction service group mapping
  vgroupMapping.my_test_tx_group = "default"
  #only support when registry.type=file, please don't set multiple addresses
  default.grouplist = "192.168.0.18:8091"

register.conf中指定使用eureka:

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "eureka"

  nacos {
    serverAddr = "localhost"
    namespace = "public"
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://192.168.0.105:8888/eureka"
    application = "default"
    weight = "1"
  }

由于seate AT模式是通过代理数据库连接方式实现的,在每个service服务中都需要进行如下配置:

/**
 * 数据源代理
 */
@Configuration
public class DataSourceConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    @Primary
    @Bean("dataSource")
    public DataSourceProxy dataSource(DataSource druidDataSource){
        return new DataSourceProxy(druidDataSource);
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy)throws Exception{
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSourceProxy);
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mybatis/mapping/*Mapper.xml"));
        sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
        return sqlSessionFactoryBean.getObject();
    }

}

全局事务使用,在最开始的service上添加注释

@GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)

其它的请参考项目代码配置。

6、依次启动eurekak、seate服务器、微服务。

正常调用feign-consumer接口(192.168.0.18:4001/addUser),先在当前服务添加用户,再调用另一个微服务hello添加用户。

分布式事务-seate。_第2张图片

分布式事务-seate。_第3张图片

异常流程:两次用户都保存成功后,service异常。

分布式事务-seate。_第4张图片

分布式事务-seate。_第5张图片

你可能感兴趣的:(分布式事务-seate。)