seata执行闪退_Seata 1.2.0的配置以及踩坑记录
首先Seata1.2.0版本不在自帶sql,且在file.conf文件中沒有了vgroup_mapping.fsp_tx_group =“default” 這項配置
數據庫SQL
seata數據庫 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`)
);
在其它使用seata的數據庫中建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;
修改file.conf
將mode改為db
修改數據庫為自己的地址和用戶名密碼
## transaction log store, only used in seata-server
store {
## store mode: file、db
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 property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata"
user = "root"
password = "root"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
修改file.conf.example
更改的內容與file.conf一致
transport {
# tcp udt unix-domain-socket
type = "TCP"
#NIO NATIVE
server = "NIO"
#enable heartbeat
heartbeat = true
# the client batch send request enable
enableClientBatchSendRequest = false
#thread factory for netty
threadFactory {
bossThreadPrefix = "NettyBoss"
workerThreadPrefix = "NettyServerNIOWorker"
serverExecutorThreadPrefix = "NettyServerBizHandler"
shareBossWorker = false
clientSelectorThreadPrefix = "NettyClientSelector"
clientSelectorThreadSize = 1
clientWorkerThreadPrefix = "NettyClientWorkerThread"
# netty boss thread size,will not be used for UDT
bossThreadSize = 1
#auto default pin or 8
workerThreadSize = "default"
}
shutdown {
# when destroy server, wait seconds
wait = 3
}
serialization = "seata"
compressor = "none"
}
## transaction log store, only used in server side
store {
## store mode: file、db
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 property
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://127.0.0.1:3306/seata"
user = "root"
password = "root"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
}
}
## server configuration, only used in server side
server {
recovery {
#schedule committing retry period in milliseconds
committingRetryPeriod = 1000
#schedule asyn committing retry period in milliseconds
asynCommittingRetryPeriod = 1000
#schedule rollbacking retry period in milliseconds
rollbackingRetryPeriod = 1000
#schedule timeout retry period in milliseconds
timeoutRetryPeriod = 1000
}
undo {
logSaveDays = 7
#schedule delete expired undo_log in milliseconds
logDeletePeriod = 86400000
}
#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
maxCommitRetryTimeout = "-1"
maxRollbackRetryTimeout = "-1"
rollbackRetryTimeoutUnlockEnable = false
}
## metrics configuration, only used in server side
metrics {
enabled = false
registryType = "compact"
# multi exporters use comma divided
exporterList = "prometheus"
exporterPrometheusPort = 9898
}
修改registry.conf
將type修改為nacos
修改serverAddr為localhost:8848;默認為localhsot
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "localhost:8848"
namespace = ""
cluster = "default"
username = ""
password = ""
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = 0
password = ""
cluster = "default"
timeout = 0
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
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
type = "file"
nacos {
serverAddr = "localhost"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
啟動Seata(之前得先啟動Nacos)
直接閃退
原因:找不到logback.xml配置log文件或目錄
解決: 修改logback.xml文件
將${user.home}寫為固定地址
修改前:
修改后:
報錯:java.sql.SQLException: Could not retrieve transation read-only status server
解決
一、查看mysql的事務隔離級別 SHOW VARIABLES LIKE '%iso%';
二、將隔離級別改為READ-COMMITTED SET GLOBAL transaction_isolation='READ-COMMITTED';
再次啟動Seata 報錯:
ERROR[main]com.alibaba.druid.pool.DruidDataSource.init:878 -init datasource error, url: jdbc:mysql://127.0.0.1:3306/seata java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
解決:修改file.conf 和 file.conf.example 在url后要加上serverTimezone
修改前: url = "jdbc:mysql://127.0.0.1:3306/seata"
修改后: url = "jdbc:mysql://127.0.0.1:3306/seata?serverTimezone=GMT%2B8"
注意:如果使用的是mysql8版本以上,還需將修改driverClassName
修改前: driverClassName = "com.mysql.jdbc.Driver"
修改后: driverClassName = "com.mysql.cj.jdbc.Driver"
環境準備完成,開擼
POM(版本信息在父POM已經定義好,都是目前位置最新版本)
com.alibaba.cloudgroupId>
spring-cloud-starter-alibaba-seataartifactId>
io.seatagroupId>
seata-allartifactId>
exclusion>
io.seatagroupId>
seata-spring-boot-starterartifactId>
exclusion>
exclusions>
dependency>
io.seatagroupId>
seata-spring-boot-starterartifactId>
1.2.0version>
dependency>
com.alibabagroupId>
druidartifactId>
1.1.22version>
dependency>
com.alibaba.cloudgroupId>
spring-cloud-alibaba-nacos-discoveryartifactId>
dependency>
org.springframework.cloudgroupId>
spring-cloud-starter-openfeignartifactId>
dependency>
org.springframework.bootgroupId>
spring-boot-starter-webartifactId>
dependency>
org.springframework.bootgroupId>
spring-boot-starter-actuatorartifactId>
dependency>
org.springframework.bootgroupId>
spring-boot-devtoolsartifactId>
dependency>
org.springframework.bootgroupId>
spring-boot-starter-data-jdbcartifactId>
dependency>
org.mybatis.spring.bootgroupId>
mybatis-spring-boot-starterartifactId>
dependency>
mysqlgroupId>
mysql-connector-javaartifactId>
dependency>
org.projectlombokgroupId>
lombokartifactId>
dependency>
dependencies>
YAML
server:
port: 9001
spring:
application:
name: cloud-seata-order
cloud:
nacos:
discovery:
server-addr: localhost:8848
datasource:
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/order?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
management:
endpoints:
web:
exposure:
include: '*'
mybatis:
mapper-locations: classpath:mapper/*.xml
seata:
application-id: ${spring.application.name}
tx-service-group: default
service:
vgroupMapping:
default: default
grouplist:
default: 127.0.0.1:8091
ribbon:
ReadTimeout: 6000
ConnectTimout: 6000
主啟動
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class OrderSeataMain9001 {
public static void main(String[] args) {
SpringApplication.run(OrderSeataMain9001.class,args);
}
}
目錄結構
其它的微服務模塊和代碼就不再贅述,order --> storage --> account {訂單 庫存 賬戶(余額)}
啟動Order應用:
報錯:no available service ‘null’ found, please make sure registry config correct
將依賴的seata-spring-boot-starter排除(前面的POM已經是修改過后的)
再導入seata-spring-boot-starter
com.alibaba.cloudgroupId>
spring-cloud-starter-alibaba-seataartifactId>
io.seatagroupId>
seata-allartifactId>
exclusion>
io.seatagroupId>
seata-spring-boot-starterartifactId>
exclusion>
exclusions>
dependency>
io.seatagroupId>
seata-spring-boot-starterartifactId>
1.2.0version>
dependency>
且YAML為(前面的YAML已經是修改后的,這里只是闡述問題)
在新版中vgroup-mappging改為了vgroupMapping駝峰命名
且此處Key對應 tx-service-group 的 Value, 此處 value 默認 default
如果不指定grouplist
報如下錯:java.lang.IllegalArgumentException: defalut.grouplist is required
seata:
application-id: ${spring.application.name}
tx-service-group: default
service:
vgroupMapping:
default: default
grouplist:
default: 127.0.0.1:8091
第二個問題:
Cause: java.sql.SQLException: com.alibaba.fescar.core.exception.TransactionException: RPC Timeout
問題描述:之前我是連的遠程阿里云服務器的mysql,在調用的出現RPC Timeout異常
@Service
public class OrderServiceImpl implements OrderService {
@Resource
private OrderDao orderDao;
@Resource
private AccountService accountService;
@Resource
private StorageService storageService;
@Override
@GlobalTransactional(name = "default",rollbackFor = Exception.class)
public void create(@RequestParam Order order) {
orderDao.create(order);
accountService.accountDecease(order.getUserId(),order.getMoney()*order.getCount());
int i = 10/0;
storageService.storageDecease(order.getCommodityCode(),order.getCount());
}
}
這里寫了int i = 10 / 0;的一個異常調用時出現RPC Timeout異常,而去掉int i = 10/0后 調用正常;
這里的解決方法就是YAML為ribbon設置超時時間后訪問成功by /zero,且事務回滾正常:
ribbon:
ReadTimeout: 6000
ConnectTimout: 6000
而這個問題比較奇怪的是,我把mysql切換到本地的mysql后,就沒有RPC Timeout異常;
且我的阿里云服務器訪問正常,不存在網絡差的情況
最后附上使用AT模式的注意事項(來源:官網)
使用 AT 模式需要的注意事項有哪些 ?
1.必須使用代理數據源,有 3 種形式可以代理數據源:
依賴 seata-spring-boot-starter 時,自動代理數據源,無需額外處理。
依賴 seata-all 時,使用 @EnableAutoDataSourceProxy (since 1.1.0) 注解,注解參數可選擇 jdk 代理或者 cglib 代理。
依賴 seata-all 時,也可以手動使用 DatasourceProxy 來包裝 DataSource。
2.配置 GlobalTransactionScanner,使用 seata-all 時需要手動配置,使用 seata-spring-boot-starter 時無需額外處理。
3.業務表中必須包含單列主鍵,若存在復合主鍵,請參考問題 13 。
4.每個業務庫中必須包含 undo_log 表,若與分庫分表組件聯用,分庫不分表。
5.跨微服務鏈路的事務需要對相應 RPC 框架支持,目前 seata-all 中已經支持:Apache Dubbo、Alibaba Dubbo、sofa-RPC、Motan、gRpc、httpClient,對于 Spring Cloud 的支持,請大家引用 spring-cloud-alibaba-seata。其他自研框架、異步模型、消息消費事務模型請結合 API 自行支持。
6.目前AT模式支持的數據庫有:MySQL、Oracle、PostgreSQL和 TiDB。
7.使用注解開啟分布式事務時,若默認服務 provider 端加入 consumer 端的事務,provider 可不標注注解。但是,provider 同樣需要相應的依賴和配置,僅可省略注解。
8.使用注解開啟分布式事務時,若要求事務回滾,必須將異常拋出到事務的發起方,被事務發起方的 @GlobalTransactional 注解感知到。provide 直接拋出異常 或 定義錯誤碼由 consumer 判斷再拋出異常。
總結
以上是生活随笔為你收集整理的seata执行闪退_Seata 1.2.0的配置以及踩坑记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php无嵌套遍历多维数组,不递归怎么遍历
- 下一篇: xsslabs靶机解题_web 攻击靶机