SpringCloud整合Nacos + Seata学习笔记
一、seata1.4.1配置
1、seata-server下载
链接: github seata下载.
下载seata1.4.1,将zip压缩包放到相应地方,直接解压就可以用了。
2、修改seata1.4.1的file.conf和registry.conf配置文件
2.1、连接Oracle数据库配置
file.conf配置文件
。
store {
## store mode: file、db、redis
mode = "db"
## 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 = "oracle"
driverClassName = "oracle.jdbc.driver.OracleDriver"
url = "jdbc:oracle:thin:localhost:1521:ORCL"
user = "***"
password = "***"
minConn = 5
maxConn = 100
globalTable = "GLOBAL_TABLE"
branchTable = "BRANCH_TABLE"
lockTable = "LOCK_TABLE"
queryLimit = 100
maxWait = 5000
}
registry.conf配置文件
。
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
}
2.2、连接MySQL数据库配置
file.conf配置文件
。
store {
## store mode: file、db、redis
mode = "db"
## 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 = "***"
password = "***"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
}
}
registry.conf配置文件
。
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
loadBalance = "RandomLoadBalance"
loadBalanceVirtualNodes = 10
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "127.0.0.1:8848"
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
}
3、seata安装目录的lib目录增加oracle驱动包
seata默认是MySQL连接的,如果是MySQL数据库连接就不需要导包;但是没有Oracle安装驱动的,如果要使用Oracle数据库进行数据连接,需要导入Oracle驱动包,我这里导入的是oracle6.jar。
4、数据库表创建
4.1、Oracle数据库表创建
1、新建表 UNDO_LOG、GLOBAL_TABLE、BRANCH_TABLE、LOCK_TABLE
-- Create table
create table UNDO_LOG
(
id NUMBER(20) not null,
branch_id NUMBER(20) not null,
xid VARCHAR2(100) not null,
context VARCHAR2(128) not null,
rollback_info BLOB,
log_status INTEGER,
log_created DATE,
log_modified DATE
);
-- Create/Recreate indexes
create index IDX_XRID on UNDO_LOG (BRANCH_ID, XID);
-- Create/Recreate primary, unique and foreign key constraints
alter table UNDO_LOG
add constraint PK_UNDO primary key (ID);
create sequence UNDO_LOG_SEQ
minvalue 1
maxvalue 9999999999999999999999999
start with 1
increment by 1
cache 20;
create table GLOBAL_TABLE (
xid varchar2(128) not null,
transaction_id NUMBER(20),
status NUMBER(10) not null,
application_id varchar2(64),
transaction_service_group varchar2(64),
transaction_name varchar2(128),
timeout NUMBER(10),
begin_time NUMBER(20),
application_data varchar2(2000),
gmt_create DATE,
gmt_modified DATE
);
create table BRANCH_TABLE (
branch_id NUMBER(20) not null,
xid varchar2(128) not null,
transaction_id NUMBER(20) ,
resource_group_id varchar2(128),
resource_id varchar2(256) ,
lock_key varchar2(256) ,
branch_type varchar2(8) ,
status NUMBER(10),
client_id varchar2(64),
application_data varchar2(2000),
gmt_create DATE,
gmt_modified DATE
);
create table LOCK_TABLE (
row_key varchar2(128) not null,
xid varchar2(128),
transaction_id NUMBER(20) ,
branch_id NUMBER(20),
resource_id varchar2(256) ,
table_name varchar2(64) ,
pk varchar2(128) ,
gmt_create DATE ,
gmt_modified DATE
);
4.2、MySQL数据库表创建
-- MySQL 数据库新建库 seata,在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`)
);
当配置完这些就离成功只差一步了,接下来启动nacos服务,启动完之后再启动seata服务,之后你会发现在nacos服务中会发现seata-service服务。如图所示:
接下来就是往自己的项目中集成了。
二、springboot项目集成
1、在pom.xml文件引入依赖
pom.xml 依赖引入
。
<dependencies>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
2、配置application.yaml
application.yaml
server:
port: 9999
spring:
profiles:
active: dev
3、配置bootstrap.properties
bootstrap.properties
spring.application.name=demo-test
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=yaml
4、打开nacos配置项目的配置文件
加入seata连接信息等yaml
#seata 配置, 代替file.conf和registry.conf配置
seata:
enabled: true
# 事务群组(可以每个应用独立取名,也可以使用相同的名字)
tx-service-group: my_test_tx_group
client:
rm-report-success-enable: true
# 异步提交缓存队列长度(默认10000)
rm-async-commit-buffer-limit: 1000
# 一阶段全局提交结果上报TC重试次数(默认1次,建议大于1)
tm-commit-retry-count: 3
# 一阶段全局回滚结果上报TC重试次数(默认1次,建议大于1)
tm-rollback-retry-count: 3
support:
# 数据源自动代理开关(默认false关闭)
spring-datasource-autoproxy: true
service:
vgroup-mapping:
# TC 集群(必须与seata-server保持一致)
my_test_tx_group: seata
# grouplist配置不生效,不需要配置,只要将seata注册到相同的eureka即可
grouplist:
default: 127.0.0.1:8095
application-id: demo-teat
registry:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
5、使用
@GlobalTransactional
6、启动项目进行测试
测试 seata
。
package com.example.demo.contraller;
import com.example.demo.pojo.Commodity;
import com.example.demo.service.CommodityService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/demo/commodity",produces = "application/json;charset=UTF-8")
public class CommodityContraller {
@Autowired
private CommodityService commodityService;
@GlobalTransactional
@GetMapping(value = "/updateCommodity")
public String UpdateCommodity(@RequestParam(value = "id") Integer id, @RequestParam(value = "num") Integer num){
Commodity commodity = new Commodity();
commodity.setId(id);
commodity.setNum(num);
Integer integer = commodityService.updateCommodity(commodity);
System.out.println(integer);
//int i = 10/0;
return Integer.toString(integer);
}
@GetMapping(value = "/test")
public String test(){
return "hello wrold!";
}
}
seata分布式事务要么一起成功,要么一起失败,必须是一个整体性的事务。