seata的安装和使用
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash
免费下载资源
·
seata是干什么的,有什么用,这里就不废话了,主要讲讲安装和使用.
本文介绍在linux中安装启动seata.
1.首先linux系统要配置java环境,参考我的上一篇博客.
2.下载steata包.地址https://github.com/seata/seata 本文使用的是seata-server-0.9.0.tar.gz
3.上传到linux的/usr/local目录后解压
tar -xvf seata-server-0.9.0.tar.gz
cd seata
ls
有4个文件 bin conf lib LICENSE
4.修改registry.conf文件
cd conf
vi registry.conf
修改后的内容如下: (本文使用nacos作为注册中心)
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos" # 这里修改为nacos
nacos {
serverAddr = "192.168.0.199:8848" # 这里填你的nacos地址
namespace = ""
cluster = "default"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
name = "file.conf"
}
}
5.启动.进入bin目录启动
cd ../bin
nohup sh seata-server.sh -p 8091 -h 192.168.0.199 -m file &> seata.log &
- -p 指定启动
seata server
的端口号。 - -h 指定
seata server
所绑定的主机
。 - -m 事务日志、事务执行信息存储的方式,目前支持
file
(文件方式)、db
(数据库方式,建表语句请查看config/db_store.sql
、config/db_undo_log.sql
)
6.查看启动日志
tail -1000f seata.log
当我们看到-Server started
时并未发现其他错误信息,我们的seata server
已经启动成功。
在项目中的使用.
1.添加依赖
<spring-cloud-alibaba-seata.version>2.0.0.RELEASE</spring-cloud-alibaba-seata.version>
<seata-all.version>0.9.0</seata-all.version>
<!-- seata -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>${spring-cloud-alibaba-seata.version}</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata-all.version}</version>
</dependency>
2.在resources目录添加配置文件registry.conf和file.conf (注意文中标出的要修改的地方)
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos" # 修改为nacos
nacos {
serverAddr = "192.168.0.199:8848" # 填写nacos的地址
namespace = ""
cluster = "default"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
name = "file.conf"
}
}
transport {
# tcp udt unix-domain-socket
type = "TCP"
#NIO NATIVE
server = "NIO"
#enable heartbeat
heartbeat = true
#thread factory for netty
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"
# netty boss thread size,will not be used for UDT
boss-thread-size = 1
#auto default pin or 8
worker-thread-size = 8
}
shutdown {
# when destroy server, wait seconds
wait = 3
}
serialization = "seata"
compressor = "none"
}
service {
#vgroup->rgroup
vgroup_mapping.pms-server-fescar-service-group = "default" # pms-server需要修改为你的微服务的spring.application.name,后面的-fescar-service-group保留
#only support single node
default.grouplist = "192.168.0.199:8091" # 修改为你的seata的地址
#degrade current not support
enableDegrade = false
#disable
disable = false
#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
max.commit.retry.timeout = "-1"
max.rollback.retry.timeout = "-1"
}
client {
async.commit.buffer.limit = 10000
lock {
retry.internal = 10
retry.times = 30
}
report.retry.count = 5
tm.commit.retry.count = 1
tm.rollback.retry.count = 1
}
## transaction log store
store {
## store mode: file、db
mode = "file"
## file store
file {
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
max-branch-session-size = 16384
# globe session size , if exceeded throws exceptions
max-global-session-size = 512
# file buffer size , if exceeded allocate new buffer
file-write-buffer-cache-size = 16384
# when recover batch read size
session.reload.read_size = 100
# async, sync
flush-disk-mode = 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 = "mysql"
password = "mysql"
min-conn = 1
max-conn = 3
global.table = "global_table"
branch.table = "branch_table"
lock-table = "lock_table"
query-limit = 100
}
}
lock {
## the lock store mode: local、remote
mode = "remote"
local {
## store locks in user's database
}
remote {
## store locks in the seata's server
}
}
recovery {
#schedule committing retry period in milliseconds
committing-retry-period = 1000
#schedule asyn committing retry period in milliseconds
asyn-committing-retry-period = 1000
#schedule rollbacking retry period in milliseconds
rollbacking-retry-period = 1000
#schedule timeout retry period in milliseconds
timeout-retry-period = 1000
}
transaction {
undo.data.validation = true
undo.log.serialization = "jackson"
undo.log.save.days = 7
#schedule delete expired undo_log in milliseconds
undo.log.delete.period = 86400000
undo.log.table = "undo_log"
}
## metrics settings
metrics {
enabled = false
registry-type = "compact"
# multi exporters use comma divided
exporter-list = "prometheus"
exporter-prometheus-port = 9898
}
support {
## spring
spring {
# auto proxy the DataSource bean
datasource.autoproxy = false
}
}
3.使用DataSourceProxy代理数据源 (本文使用默认的Hikari连接池,使用Druid同理)
package com.java.gmall.wms.config;
import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
/**
* 需要将 DataSourceProxy 设置为主数据源,否则事务无法回滚
*/
@Primary
@Bean("dataSource")
public DataSource dataSource(@Value("${spring.datasource.url}")String url,@Value("${spring.datasource.driver-class-name}")String driverClassName,
@Value("${spring.datasource.username}")String username,@Value("${spring.datasource.password}")String password) {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(url);
hikariDataSource.setDriverClassName(driverClassName);
hikariDataSource.setUsername(username);
hikariDataSource.setPassword(password);
return new DataSourceProxy(hikariDataSource);
}
}
4.在你需要使用feign进行微服务间调到的方法上添加注解@GlobalTransactional
@Override
@GlobalTransactional
public void saveSpuInfoVO(SpuInfoVO spuInfoVO) {
// ...
// 远程调用其他微服务的方法
this.skuSaleFeign.saveSkuSaleInfo(skuSaleDTO);
int i = 1/0;
}
5.每个涉及到分布式事务的微服务的数据库都要有一张undo_log日志表,sql如下
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for undo_log
-- ----------------------------
DROP TABLE IF EXISTS `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=2 DEFAULT CHARSET=utf8;
6.在需要微服务调用的微服务中进行以上相同配置,除了@GlobalTransactional注解外.
配置完毕进行测试.在@GlobalTransactional注解的方法中抛出异常,查看feign调用的微服务是否会回滚.
PS:当你配置完毕启动项目的时候.seata的日志会有提示,通过提示也能发现你是否配置成功
GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e
added ecosystem file for PM2 4 年前
5def40a3
Add host customization support for the NodeJS version 4 年前
更多推荐
已为社区贡献10条内容
所有评论(0)