我是靠谱客的博主 等待音响,最近开发中收集的这篇文章主要介绍seata高可用集群部署客户端配置,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

版本:seata1.2.0

参考官方文档:https://github.com/seata/seata-samples/tree/master/ha

注意:在store mode是file的模式下,是不支持集群部署

db模式

在1.3及以上的版本中支持redis的模式来实现集群部署

数据可配置

创建seata_server数据库及表结构,sql如下:

-- 创建数据库
create database seata_server;
-- the table to store GlobalSession data
drop table `global_table`;
create table `global_table` (
`xid` varchar(128)
not null,
`transaction_id` bigint,
`status` tinyint not null,
`application_id` varchar(64),
`transaction_service_group` varchar(64),
`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 `branch_table`;
create table `branch_table` (
`branch_id` bigint not null,
`xid` varchar(128) not null,
`transaction_id` bigint ,
`resource_group_id` varchar(128),
`resource_id` varchar(256) ,
`lock_key` varchar(256) ,
`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 `lock_table`;
create table `lock_table` (
`row_key` varchar(128) not null,
`xid` varchar(128),
`transaction_id` long ,
`branch_id` long,
`resource_id` varchar(256) ,
`table_name` varchar(64) ,
`pk` varchar(128) ,
`gmt_create` datetime ,
`gmt_modified` datetime,
primary key(`row_key`)
);

配置文件修改

配置文件路径:seata-server-1.2.0/conf

涉及到修改的配置文件:file.conf、registry.conf

file.conf

service {
#transaction service group mapping
# 注意:1.2.0 版本(或更早的版本) 已经将 ‘vgroup_mapping’ 改为 ‘vgroupMapping’
vgroupMapping.fescar-service-group = "seata-server"
#only support when registry.type=file, please don't set multiple addresses
default.grouplist = "127.0.0.1:8091"
#degrade, current not support 降级处理
enableDegrade = false
#disable seata 是否开启本地事务
disableGlobalTransaction = false
}
## transaction log store, only used in seata-server
store {
## store mode: file、db
mode = "db"
## 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 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:3306/seata_server"
user = "XXXX"
password = "XXXXXX"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
}
}

1.添加db的配置项,db连接的数据库就是上面创建的seata_server数据库。

2.store mode选择db

registry.conf

registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
eureka {
serviceUrl = "http://127.0.0.1:8001/eureka/,http://127.0.0.1:8002/eureka/"
application = "seata-server"
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
type = "file"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
app.id = "seata-server"
apollo.meta = "http://127.0.0.1:8801"
}
zk {
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}

1.修改注册中心选择eureka

2.eureka的serviceUrl配置两个注册中心

客户端配置

引入pom

<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</dependency>

创建GlobalTransactionScanner

创建GlobalTransactionScanner bean,该类的作用是扫描@GlobalTransaction注解,对代理方法进行拦截增强事务

@Configuration
public class SeataConfig {
@Value("${spring.application.name}")
private String applicationId;
@Bean
public GlobalTransactionScanner globalTransactionScanner() {
GlobalTransactionScanner globalTransactionScanner = new GlobalTransactionScanner(applicationId,
"fescar-service-group");
return globalTransactionScanner;
}
}

配置数据源

@Configuration
public class SeataDataSourceConfig {
@Bean(destroyMethod = "close", initMethod = "init")
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource druidDataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
@ConfigurationProperties(prefix = "spring.datasource")
@Primary
@Bean("dataSource")
public DataSource dataSource(DruidDataSource druidDataSource) {
DataSourceProxy dataSourceProxy = new DataSourceProxy(druidDataSource);
return dataSourceProxy;
}
}

配置文件

客户端在resource文件下面,添加3个文件,分别为file.conf、registry.conf、seata.conf

其中file.conf和registry.conf文件的内容和seata-server的配置文件有所不同。

registry.conf

registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
eureka {
serviceUrl = "http://127.0.0.1:8001/eureka"
application = "seata-server"
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"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
consul {
serverAddr = "127.0.0.1:8500"
}
zk {
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
file {
name = "file.conf"
}
}

file.conf

上述registry的config.type=file,所以需要配置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 = true
#thread factory for netty
threadFactory {
bossThreadPrefix = "NettyBoss"
workerThreadPrefix = "NettyServerNIOWorker"
serverExecutorThread-prefix = "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"
}
service {
#vgroup->rgroup
#此处的vgroupMapping.xxx配置后面的xxx要与yml配置中的spring.cloud.alibaba.seata.tx-service-group一致,
#配置的为seata-server eurake应用名,值同时要与regiester.conf里的registry节点下的eureka下的application保持一致。
vgroupMapping.fescar-service-group = "seata-server"
#only support single node
#当注册类型为file文件时配置有效,不要输入多个地址,此处的地址为seata server的监听地址(seata server也有相同配置)
default.grouplist = "127.0.0.1:8091"
#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 {
rm {
asyncCommitBufferLimit = 10000
lock {
retryInterval = 10
retryTimes = 30
retryPolicyBranchRollbackOnConflict = true
}
reportRetryCount = 5
tableMetaCheckEnable = false
reportSuccessEnable = false
}
tm {
commitRetryCount = 5
rollbackRetryCount = 5
}
undo {
dataValidation = true
logSerialization = "jackson"
# undo表名称,SEATA AT模式需要UNDO_LOG表
logTable = "undo_log"
}
log {
exceptionRate = 100
}
}

启动类

启动类@SpringBootApplication的注解中,排除 DataSourceAutoConfiguration.class(手动进行数据源配置,不需要自动配置)

@SpringBootApplication( exclude = { DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients
public class TestSeataApplication {
public static void main(String[] args) {
SpringApplication.run(TestSeataApplication.class, args);
}
}

最后

以上就是等待音响为你收集整理的seata高可用集群部署客户端配置的全部内容,希望文章能够帮你解决seata高可用集群部署客户端配置所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部