初学阿里分布式事务seata分享

框架使用的是nacons+seata

Eureka官宣2.x版本不再开源,因此学习了nacos,顺便也讲一下nacos。
nacos的下载地址: https://github.com/alibaba/nacos/tags
seata的下载地址: http://seata.io/zh-cn/blog/download.html

我的环境

Windows10
JDK8
Nacos-server:1.2.0
seata:0.9.0

nacos下载

直接去官网下载zip的,就可以了,建议将下载链接复制到迅雷进行下载,不然网页下载实在是太慢了。下载完成之后看到文件夹下面
在这里插入图片描述
然后在bin文件夹里面运行startup.cmd就把nacos服务跑起来了,访问localhost:8848就可以看到界面:
在这里插入图片描述
服务启动成功!

微服务部署

1.创建一个微服务后,导入对应maven

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

2.主启动类加上注解@EnableDiscoveryClient
3.配置yml文件 :可以先不用配置seata,先直接配置下面的nacos。
在这里插入图片描述
4.启动服务,然后访问一次请求,就可以在nacos中看见对应的服务了
在这里插入图片描述

seata介绍

在这里插入图片描述
seata主要是“三角关系”
1.TM向TC申请一个全局事务,并且生成一个XID;
2.XID在微服务调用中进行传播
3.RM向TC注册分支微服务,并把自己交给XID
4.TM向TC发起回滚
5.TC调度XID下的分支事务完成回滚

seata配置

seata下载链接再最上方,直接进官网下载,我这里用的是0.9.0版本
官网下载完后,进入文件夹可以看到
在这里插入图片描述
1、首先配置数据库,创建一个seata库,然后在bin文件下,有一个db_store.sql文件夹(1.0.0没有,可以去官网找sql),然后复制内容直接创建三个表到seata库。

– the table to store GlobalSession data
drop table if exists global_table;
create table global_table (
xid varchar(128) not null,
transaction_id bigint,
status tinyint not null,
application_id varchar(32),
transaction_service_group varchar(32),
transaction_name varchar(128),
timeout int,
begin_time bigint,
application_data varchar(2000),
gmt_create datetime,
gmt_modified datetime,
primary key (xid),
key idx_gmt_modified_status (gmt_modified, status),
key idx_transaction_id (transaction_id)
);

– the table to store BranchSession data
drop table if exists branch_table;
create table branch_table (
branch_id bigint not null,
xid varchar(128) not null,
transaction_id bigint ,
resource_group_id varchar(32),
resource_id varchar(256) ,
lock_key varchar(128) ,
branch_type varchar(8) ,
status tinyint,
client_id varchar(64),
application_data varchar(2000),
gmt_create datetime,
gmt_modified datetime,
primary key (branch_id),
key idx_xid (xid)
);

– the table to store lock data
drop table if exists lock_table;
create table lock_table (
row_key varchar(128) not null,
xid varchar(96),
transaction_id long ,
branch_id long,
resource_id varchar(256) ,
table_name varchar(32) ,
pk varchar(36) ,
gmt_create datetime ,
gmt_modified datetime,
primary key(row_key)
);

2、在所有涉及到的库中添加一个undo_log表,是用于回滚事务的

– 注意此处0.3.0+ 增加唯一索引 ux_undo_log
drop table undo_log;
CREATE TABLE undo_log (
id bigint(20) NOT NULL AUTO_INCREMENT,
branch_id bigint(20) NOT NULL,
xid varchar(100) NOT NULL,
context varchar(128) NOT NULL,
rollback_info longblob NOT NULL,
log_status int(11) NOT NULL,
log_created datetime NOT NULL,
log_modified datetime NOT NULL,
ext varchar(100) DEFAULT NULL,
PRIMARY KEY (id),
UNIQUE KEY ux_undo_log (xid,branch_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

到此数据库配置完成

3、配置seata的file.conf文件
修改server下的my_test_tx_group,我这里改成hyq_group
在这里插入图片描述
然后修改store下的mode属性,这里改成db,之后配置下方的db属性。
在这里插入图片描述
4、配置seata的registry.conf文件
我服务注册用的是nacos,所以最上方的type改成nacos就好
在这里插入图片描述
到这里seata的代码以外的配置已经完成

5、创建微服务,导入maven,修改yml配置

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>seata-all</artifactId>
                <groupId>io.seata</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.seata</groupId>
        <artifactId>seata-all</artifactId>
        <version>0.9.0</version>
    </dependency>

在这里插入图片描述
在这里插入图片描述
6、在resource下创建file.conf和registry.conf
在这里插入图片描述
其中file.conf的内容和seata文件夹下的file.conf有一处不一样(好好比较一下我上面配置的seata下的file.conf你就会看到了)
在这里插入图片描述
然后registry.conf文件就和seata文件里面的一样了
7、配置DataSourceProxyConfig,使用seata对数据源进行代理(在之前要先配置mybatis,这里就不介绍了)

@Configuration
public class DataSourceProxyConfig {
@Value("${mybatis.mapper-locations}")
private String mapperLocations;

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource(){
    return new DruidDataSource();
}
@Bean
public DataSourceProxy dataSourceProxy(DataSource dataSource) {
    return new DataSourceProxy(dataSource);
}
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSourceProxy);
    sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
    sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
    return sqlSessionFactoryBean.getObject();
}

} 额,记得别忘记这个括号了

如果你的代码之前用了druid的话,可以这样改
在这里插入图片描述
8、不要忘记在主启动类添加(exclude = DataSourceAutoConfiguration.class)取消数据源的自动配置
9、使用@GlobalTransactional回滚
(每个system.out.println下方都有使用feign或者openfeign调用其他微服务的方法噢,我这里就不详细写了)
在这里插入图片描述
seata配置到这里就结束了!

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值