SpringCloud Alibaba Seata 的安装与配置

x33g5p2x  于2022-05-05 转载在 Spring  
字(6.4k)|赞(0)|评价(0)|浏览(205)

Seata安装

Windows下安装

下载并解压缩:http://seata.io/zh-cn/blog/download.html

修改conf/file.conf文件

  • mode="file"改为mode="db" , 并配置数据库相关信息
store {
  ## store mode: file、db、redis
  ## 这里直接改为db,默认是file
  mode = "db"

  ## file store property
  file {
    ## store location dir
    dir = "sessionStore"
    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    maxBranchSessionSize = 16384
    # globe session size , if exceeded throws exceptions
    maxGlobalSessionSize = 512
    # file buffer size , if exceeded allocate new buffer
    fileWriteBufferCacheSize = 16384
    # when recover batch read size
    sessionReloadReadSize = 100
    # async, sync
    flushDiskMode = async
  }

  ## database store
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    driver-class-name = "com.mysql.jdbc.Driver"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "root"
    password = "123456"
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}

创建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`)
);

修改registry.conf文件

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  # 设置nacos作为注册中心
  type = "nacos"

  nacos {
 	 # 设置nacos地址、命名空间等
    serverAddr = "localhost:8848"
    namespace = "public"
    cluster = "default"
  }
  eureka {
    serviceUrl = "http://localhost:8761/eureka"
    application = "default"
    weight = "1"
  }
  redis {
    serverAddr = "localhost:6379"
    db = "0"
  }
  zk {
    cluster = "default"
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  consul {
    cluster = "default"
    serverAddr = "127.0.0.1:8500"
  }
  etcd3 {
    cluster = "default"
    serverAddr = "http://localhost:2379"
  }
  sofa {
    serverAddr = "127.0.0.1:9603"
    application = "default"
    region = "DEFAULT_ZONE"
    datacenter = "DefaultDataCenter"
    cluster = "default"
    group = "SEATA_GROUP"
    addressWaitTime = "3000"
  }
  file {
    name = "file.conf"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
    # 这里可以选择使用nacos的配置中心,这里是个demo,这里不在使用,但是方法在下面
  type = "file"

  nacos {
    serverAddr = "localhost:8848"
    # 这里可以在nacos中创建命名空间
    namespace = ""
  }
  consul {
    serverAddr = "127.0.0.1:8500"
  }
  apollo {
    app.id = "seata-server"
    apollo.meta = "http://192.168.1.204:8801"
  }
  zk {
    serverAddr = "127.0.0.1:2181"
    session.timeout = 6000
    connect.timeout = 2000
  }
  etcd3 {
    serverAddr = "http://localhost:2379"
  }
  file {
    name = "file.conf"
  }
}

修改conf/logback.xml

${user.home}改为具体的seata目录,我这里是D:\soft\seata-0.9,那么配置如下(在第19行),需要自己创建logs文件夹

<property name="LOG_HOME" value="D://soft//seata-0.9//logs"/>

双击启动bin/seata-server.bat文件启动服务

Docker安装Seata

# 1. 获取镜像
docker pull seataio/seata-server:1.3.0

# 2. 运行容器
docker run --name seata-server -p 8091:8091 -d  seataio/seata-server:1.3.0

# 3. 将容器中的配置拷贝到/usr/local/seata-1.3.0
docker cp seata-server:/seata-server /usr/local/seata-1.3.0

# 4. 完成后就会在/usr/local/seata-1.3.0出现容器的配置,我们现在可以将原来容器停止并删除
docker stop seata-server
docker rm seata-server

# 5. 进入目录/usr/local/seata-1.3.0/resources中修改file.conf和registry.conf中的内容

修改file.conf中的 mode="db", 以及mysql的相关内容,同上面windows一样
修改registry.conf中的 registry下的type="nacos" 和config下的type="nacos" 并 配置nacos相关信息

# 6. 启动seata
docker run 
-d --restart always  
--name  seata-server 
-p 8091:8091  
-v /usr/local/seata-1.3.0:/seata-server 
-e SEATA_IP=192.168.1.23 
-e SEATA_PORT=8091 
seataio/seata-server:latest

使用Seata实现事务控制

初始化数据表

在我们项目下的数据库中加入一张undo_log表,这是Seata记录事务日志要用到的表

-- the table to store seata xid data
-- 0.7.0+ add context
-- you must to init this sql for you business databese. the seata server not need it.
-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库)
-- 注意此处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;

添加配置

在需要进行分布式控制的微服务中进行下面几项配置:

添加依赖:

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

DataSourceProxyConfig

Seata 是通过代理数据源实现事务分支的,所以需要配置 io.seata.rm.datasource.DataSourceProxy 的 Bean,且是 @Primary默认的数据源,否则事务不会回滚,无法实现分布式事务

@Configuration
public class DataSourceProxyConfig {
    
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }
    
    @Primary
    @Bean
    public DataSourceProxy dataSource(DruidDataSource druidDataSource) {
        return new DataSourceProxy(druidDataSource);
    }
}

bootstrap.yaml

spring:
	application:
		name: service-product
	cloud:
		nacos:
			config:
				server-addr: localhost:8848 # nacos的服务端地址
					namespace: public
						group: SEATA_GROUP
		alibaba:
			seata:
				tx-service-group: ${spring.application.name}

在中微服务中消费方开启全局事务

@GlobalTransactional//全局事务控制
public Order createOrder(Integer pid) {}

测试

相关文章

微信公众号

最新文章

更多