1.nacos搭建
参考官网 https://nacos.io/zh-cn/docs/quick-start-docker.html
下载代码-》mysql执行脚本-》修改 env目录下mysql连接信息-》启动容器docker-compose -f example/standalone-mysql-8.yaml up
2.seata搭建
这个更简单,但是需要多注意
参考官网 https://seata.io/zh-cn/docs/ops/deploy-by-docker.html
2.1先起起来
docker run --privileged -e SEATA_IP="192.168.56.101" -d -p 8091:8091 -p 7091:7091 --name stack-seata seataio/seata-server:1.5.0
2.2把配置文件复制下来
docker cp seata-serve:/seata-server/resources /home/server/stack/seata/config/
2.3修改application.yml配置如下
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${user.home}/logs/seata
extend:
logstash-appender:
destination: 127.0.0.1:4560
kafka-appender:
bootstrap-servers: 127.0.0.1:9092
topic: logback_to_logstash
console:
user:
username: seata
password: seata
seata:
config:
# support: nacos, consul, apollo, zk, etcd3
#type: file
type: nacos
nacos:
server-addr: 192.168.56.101:8848
namespace:
group: SEATA_GROUP
username: nacos
password: nacos
data-id: seataServer.properties
registry:
# support: nacos, eureka, redis, zk, consul, etcd3, sofa
#type: file
type: nacos
nacos:
application: seata-server
server-addr: 192.168.56.101:8848
ip: 192.168.56.101
group: SEATA_GROUP
namespace:
cluster: default
username: nacos
password: nacos
store:
# support: file 、 db 、 redis
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.56.101:3306/stack_study?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
user: root
password: 123456
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
distributed-lock-table: distributed_lock
query-limit: 100
max-wait: 5000
# server:
# service-port: 8091 #If not configured, the default is '${server.port} + 1000'
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
主要是改nacos和db的配置,完整的配置在application.example.yml
2.4初始化数据库,在这里下载脚本https://github.com/seata/seata/tree/develop/script/server/db
2.5在nacos创建配置文件seataServer.properties,内容是https://github.com/seata/seata/tree/develop/script/config-center里面的config.txt
注意这里面的配置service.vgroupMapping.xxx_default_group=default要记住,要server和client一致
2.6再起起来
docker run --privileged -e SEATA_IP="192.168.56.101" -d -p 8091:8091 -p 7091:7091 -v /home/server/stack/seata/config/resources:/seata-server/resources --name stack-seata seataio/seata-server:1.5.0
3.项目中使用(订单项目和库存项目)
3.1库存项目
pom.xml
<!-- 要移除seata核心,保持和server 端一致这样才会配置一致可用 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<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.5.0</version>
</dependency>
一定要注意,否则读不到seataServer.properties
bootstrap.yml
seata:
enabled: true
# 是否自动开启数据源代理
enable-auto-data-source-proxy: true
tx-service-group: stack_default_group
# 数据源代理模式,使用AT模式
data-source-proxy-mode: AT
config:
type: nacos
nacos:
server-addr: 192.168.56.101:8848
group: SEATA_GROUP
namespace:
dataId: seataServer.properties
username: nacos
password: nacos
registry:
type: nacos
nacos:
application: seata-server
server-addr: 192.168.56.101:8848
group: SEATA_GROUP
namespace:
username: nacos
password: nacos
一定要注意这个 tx-service-group: stack_default_group和seataServer.properties里面的service.vgroupMapping对应的。其他的配置保持和server端的一致。
代码
@GlobalLock
@Transactional
public Result seataTest(){
TestData testData=new TestData();
testData.setName("houyong data");
testDataMapper.insert(testData);
return Result.success();
}
被调用端只需要@Transactional。加上@GlobalLock是保证,执行全局事务的时候,本地的事务不会操作到这个方法。保证正在运行的全局事务的原子性。这样比直接加@GlobalTransactional效率高很多。
3.2订单项目
订单项目的配置和库存项目都一样的
代码
@GlobalTransactional
public Result seataTest(){
TestData testData=new TestData();
testData.setName("houyong study");
testDataMapper.insert(testData);
testDataFeign.addTestData();
String a=null;
a.trim();
return Result.success();
}
特意抛了个空指针异常,回滚成功
后台会输出:Branch Rollbacked result: PhaseTwo_Rollbacked