概述
‘SEATA’ 全称 ‘Simple Extensible Autonomous Transaction Architecture’,简易可扩展的自治式分布式事务管理框架,其前身是fescar。阿里巴巴GTS的开源版实现,是一种分布式事务的解决方案。
seata主要由三个重要组件组成:
Transaction Coordinator(TC):管理全局的分支事务的状态,用于全局性事务的提交和回滚。
Transaction Manager™:事务管理器,用于开启全局事务、提交或者回滚全局事务,是全局事务的开启者。
Resource Manager(RM):资源管理器,用于分支事务上的资源管理,向TC注册分支事务,上报分支事务的状态,接受TC的命令来提交或者回滚分支事务。
中文文档平台参考地址,源代码参考地址
前期注意事项
JDK版本必须是64位的
首先要确保你的JDK的版本是64位的,若是32位的将会导致启动seata的服务的时候内存溢出或者无法正常启动。
seata-server 1.2.0
下载
github: seata-server-1.2.0
网盘:seata-server-1.2.0(提取码:eufg)
创建seata-server所需表(这边是postgresql)
- the table to store GlobalSession data
DROP TABLE IF EXISTS "global_table";
CREATE TABLE "global_table" (
"xid" VARCHAR (128) NOT NULL,
"transaction_id" INT8,
"status" INT4 NOT NULL,
"application_id" VARCHAR (32),
"transaction_service_group" VARCHAR (32),
"transaction_name" VARCHAR (64),
"timeout" INT4,
"begin_time" INT8,
"application_data" text,
"gmt_create" TIMESTAMP,
"gmt_modified" TIMESTAMP,
PRIMARY KEY ("xid")
);
CREATE INDEX ON "global_table" ("gmt_modified", "status");
CREATE INDEX ON "global_table" ("transaction_id");
-- the table to store BranchSession data
DROP TABLE IF EXISTS "branch_table";
CREATE TABLE "branch_table" (
"branch_id" INT8 NOT NULL,
"xid" VARCHAR (128) NOT NULL,
"transaction_id" INT8,
"resource_group_id" VARCHAR (32),
"resource_id" VARCHAR (256),
"lock_key" VARCHAR (128),
"branch_type" VARCHAR (8),
"status" INT4,
"client_id" VARCHAR (64),
"application_data" text,
"gmt_create" TIMESTAMP,
"gmt_modified" TIMESTAMP,
PRIMARY KEY ("branch_id")
);
CREATE INDEX ON "branch_table" ("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" INT8,
"branch_id" INT8,
"resource_id" VARCHAR (256),
"table_name" VARCHAR (32),
"pk" VARCHAR (32),
"gmt_create" TIMESTAMP,
"gmt_modified" TIMESTAMP,
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.fsp_tx_group = "default"
#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"
## 采用数据库存储:postgresql
db {
## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "postgresql"
driverClassName = "org.postgresql.Driver"
url = "jdbc:postgresql://10.16.30.36:5432/seata_server"
user = "postgres"
password = "tuxin2018"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
registry.conf (用于指定注册中心和配置文件)
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
# 这边采用 eureka
type = "eureka"
eureka {
serviceUrl = "http://10.16.30.31:8886/eureka/"
application = "default"
weight = "1"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
file {
name = "file.conf"
}
}
file.conf和registry.conf配置完成后,启动seata-server服务,点击执行‘/bin/seata-server.bat’
客户端
先假设有四个服务
eureka-server、scene-server、short-server、base-server
其中eureka-server为注册中心,scene-server 调用 short-server 和 base-server
eureka-server 不需要与seata相关的配置
scene-server、short-server、base-server每个服务下面创建日志表
-- the sequence of undo_log
CREATE SEQUENCE undo_log_seq
START 1
INCREMENT 1;
DROP TABLE IF EXISTS "undo_log";
CREATE TABLE "undo_log" (
"id" INT8 NOT NULL DEFAULT nextval('undo_log_seq'),
"branch_id" INT8 NOT NULL,
"xid" VARCHAR (100) NOT NULL,
"context" VARCHAR (128) NOT NULL,
"rollback_info" BYTEA NOT NULL,
"log_status" INT4 NOT NULL,
"log_created" TIMESTAMP,
"log_modified" TIMESTAMP,
"ext" VARCHAR (100) DEFAULT NULL,
PRIMARY KEY ("id"),
UNIQUE ("branch_id", "xid")
);
scene-server 配置
不需要file.conf 和 registry.conf 这两个文件 (之前老版本的是需要的)
依赖
<!-- 分布式事务支持 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
bootstrap.yml配置
server:
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 30
session-timeout: 30
port: 8901
servlet:
context-path: /sceneServer
session:
cookie:
name: scene-server-session
connection-timeout: 30000
compression:
enabled: true
mime-types: textml,text/xml,text/plain,text/css,application/javascript,image/png,text/json,application/json,text/html
min-response-size: 1024
excluded-user-agents: gozilla,traviata
eureka:
instance:
leaseRenewalIntervalInSeconds: 10 #表示eureka client发送心跳给server端的频率,默认为30秒
health-check-url-path: ${server.servlet.context-path}/actuator/health #健康检查的地址(依赖spring-boot-starter-actuator)
prefer-ip-address: true #以IP地址注册到服务中心
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: http://10.16.30.31:8886/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
spring:
application:
name: scene-server
profiles:
active: dev
cloud:
config:
enabled: false
fail-fast: true
name: ${spring.application.name}
profile: ${spring.profiles.active}
label: master
discovery:
enabled: true
serviceId: tx-config
datasource:
platform: postgresql
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000 #配置获取连接等待超时的时间
timeBetweenEvictionRunsMillis: 60000 #配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
minEvictableIdleTimeMillis: 300000 #配置一个连接在池中最小生存的时间,单位是毫秒
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: true
testOnReturn: true
poolPreparedStatements: true #打开PSCache,并且指定每个连接上PSCache的大小
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,log4j #配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #通过connectProperties属性来打开mergeSql功能;慢SQL记录
initialize: false
url: jdbc:postgresql://10.16.30.36:5432/tx_short_server
username: postgres
password: tuxin2018
driverClassName: org.postgresql.Driver
druid:
stat-view-servlet:
login-password: 123456
login-username: root
jpa:
database-platform: com.tuxin.project.base.url.server.config.dialect.JsonbPostgresDialect
use_jdbc_metadata_defaults: false
show-sql: false
hibernate:
ddl-auto: update
dialect: org.hibernate.dialect.PostgreSQLDialect
properties:
hibernate:
current_session_context_class: org.springframework.orm.hibernate5.SpringSessionContext
temp:
use_jdbc_metadata_defaults: false
jdbc:
lob:
non_contextual_creation: true
database: postgresql
aop:
proxy-target-class: true
mybatis-plus:
mapper-locations: classpath*:/mapper/*.xml
global-config:
db-config:
id-type: id_worker
#逻辑删除配置
logic-delete-value: 1
logic-not-delete-value: 0
db-type: postgresql
# sql-injector: com.baomidou.mybatisplus.extension.injector.LogicSqlInjector
configuration:
map-underscore-to-camel-case: true #配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)
cache-enabled: false
call-setters-on-nulls: true
sys:
ignoreAuths:
web:
allowedOrigins: "*"
seata:
application-id: ${spring.application.name} # Seata 应用名称,默认使用 ${spring.application.name}
tx-service-group: fsp_tx_group # Seata 事务组, 值为seata-server的file.conf中vgroupMapping后面跟的值,此处为fsp_tx_group
client:
support:
spring:
datasource-autoproxy: true
# 服务配置项
service:
# 新版本好像将vgroup-mapping 改为 vgroupMapping
vgroupMapping:
# 重点注意:此处Key对应 tx-service-group 的 Value, value 默认 default
fsp_tx_group: default
# 是否开启本地事务
disable-global-transaction: false
registry:
eureka:
# 注册中心地址
service-url: ${eureka.client.serviceUrl.defaultZone}
type: eureka
# 日志
logging:
level:
io:
seata: info
重点是后面的seata相关配置
启动类注解
@EnableAutoConfiguration(exclude = {PageHelperAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.tuxin.project.base.scene.server.service.feign", "com.tuxin.auth.client.feign"})
@ComponentScan
@MapperScan(basePackages = {"com.tuxin.project.base.scene.server.mapper"})
@SpringCloudApplication
public class SceneApplication {
public static void main(String[] args) {
SpringApplication.run(SceneApplication.class, args);
}
}
调用其他服务的方法上需要添加
@GlobalTransactional(rollbackFor = Exception.class)
short-server和base-server配置相同
依赖
<!-- 分布式事务支持 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
在bootstrap.yml添加以下内容
seata:
application-id: ${spring.application.name} # Seata 应用名称,默认使用 ${spring.application.name}
tx-service-group: fsp_tx_group # Seata 事务组, 值为seata-server的file.conf中vgroupMapping后面跟的值,此处为fsp_tx_group
client:
support:
spring:
datasource-autoproxy: true
# 服务配置项
service:
# 新版本好像将vgroup-mapping 改为 vgroupMapping
vgroupMapping:
# 重点注意:此处Key对应 tx-service-group 的 Value, value 默认 default
fsp_tx_group: default
# 是否开启本地事务
disable-global-transaction: false
registry:
eureka:
# 注册中心地址
service-url: ${eureka.client.serviceUrl.defaultZone}
type: eureka
# 日志
logging:
level:
io:
seata: info
剩下的启动服务就好了
摸索了两天,还不是很全面,一起交流呀!!!
最后
以上就是感性刺猬为你收集整理的seata 1.2.0 + springcloud + eureka + feign + postgresql的全部内容,希望文章能够帮你解决seata 1.2.0 + springcloud + eureka + feign + postgresql所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复