linux搭建seata并使用

搭建seata

官网

在linux下搭建

下载1.6.1版本:地址
新建文件夹、上传压缩包并解压
[root@hao ~]# cd /usr/local/software/
[root@hao /usr/local/software]# ls
canal  docker  elk  gitlab  jdk  mysql  nacos  nexus  nginx  rabbitmq  redis  redis_sentinel  xxl-job
[root@hao /usr/local/software]# mkdir seata
[root@hao /usr/local/software]# ls
canal  docker  elk  gitlab  jdk  mysql  nacos  nexus  nginx  rabbitmq  redis  redis_sentinel  seata  xxl-job
[root@hao /usr/local/software]# cd seata/
[root@hao /usr/local/software/seata]# ls
[root@hao /usr/local/software/seata]# tar -zxvf seata-server-1.6.1.tar.gz
seata/Dockerfile
seata/LICENSE
seata/target/seata-server.jar
seata/conf/application.yml
seata/conf/application.example.yml
seata/conf/logback-spring.xml
seata/bin/seata-server.sh
[root@hao /usr/local/software/seata]# ls
seata  seata-server-1.6.1.tar.gz
在nacos中创建命名空间

分类/seata/seata_1.png  0 → 100644

在数据库创建表【seata官方给出的步骤】【更改格式:时间戳】
CREATE TABLE `undo_log` (`branch_id` bigint(20) NOT NULL COMMENT 'branch transaction id',`xid` varchar(128) NOT NULL COMMENT 'global transaction id',`context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',`rollback_info` longblob NOT NULL COMMENT 'rollback info',`log_status` int(11) NOT NULL COMMENT '0:normal status,1:defense status',`log_created` datetime(6) NOT NULL COMMENT 'create datetime',`log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AT transaction mode undo table';

备份原application.yml文件,将application.example.yml文件改为application.yml

[root@hao /usr/local/software/seata/seata]# cd conf/
[root@hao /usr/local/software/seata/seata/conf]# ls
application.example.yml  application.yml  logback  logback-spring.xml
[root@hao /usr/local/software/seata/seata/conf]# mv application.yml application.yml.bak
[root@hao /usr/local/software/seata/seata/conf]# ls
application.example.yml  application.yml.bak  logback  logback-spring.xml
[root@hao /usr/local/software/seata/seata/conf]# mv application.example.yml application.yml
[root@hao /usr/local/software/seata/seata/conf]# ls
application.yml  application.yml.bak  logback  logback-spring.xml
配置application.yml文件

配置config模块

分类/seata/seata_2.png  0 → 100644

console:user:username: seatapassword: 123456seata:config:# support: nacos 、 consul 、 apollo 、 zk  、 etcd3type: nacosnacos:# nacos服务器的地址server-addr: 192.168.133.100:8848# nacos中配置的seata的命名空间namespace: seata-id# namespace下组的命名group: SEATA_GROUP

注册中心registry模块

分类/seata/seata_3.png  0 → 100644

  registry:# support: nacos 、 eureka 、 redis 、 zk  、 consul 、 etcd3 、 sofatype: nacos# 配置允许连接的ip地址# preferred-networks: 30.240.*nacos:application: seata-serverserver-addr: 192.168.133.100:8848group: SEATA_GROUPnamespace: seata-id

配置store模块

分类/seata/seata_5.png  0 → 100644

  store:# support: file 、 db 、 redismode: redissession:mode: filelock:mode: file

分类/seata/seata_4.png  0 → 100644

    redis:mode: singledatabase: 1min-conn: 10max-conn: 100password:max-total: 100query-limit: 1000# 单个single:host: 192.168.133.100port: 6390# 哨兵模式sentinel:master-name:sentinel-hosts:

配置security模块

分类/seata/seata_6.png  0 → 100644

seata:#新添加内容,不加会报错security:secretKey: "securityKey"tokenValidityInMilliseconds: 1000000000
配置脚本文件script
[root@hao /usr/local/software/seata/seata/conf]# cd ..
[root@hao /usr/local/software/seata/seata]# ls
bin  conf  Dockerfile  ext  lib  LICENSE  logs  script  target
[root@hao /usr/local/software/seata/seata]# cd script/
[root@hao /usr/local/software/seata/seata/script]# ls
config-center  logstash  server
[root@hao /usr/local/software/seata/seata/script]# cd config-center/
[root@hao /usr/local/software/seata/seata/script/config-center]# ls
apollo  config.txt  consul  etcd3  nacos  README.md  zk
[root@hao /usr/local/software/seata/seata/script/config-center]# cd nacos/
[root@hao /usr/local/software/seata/seata/script/config-center/nacos]# ls
nacos-config-interactive.py  nacos-config-interactive.sh  nacos-config.py  nacos-config.sh

编辑nacos-config.sh,【如果运行了nginx,修改端口为7777:自己的代理端口】

分类/seata/seata_7.png

运行
sh nacos-config.sh -h 192.168.133.100

分类/seata/seata_8.png  0 → 100644

进入nacos查看,共103个配置文件

分类/seata/seata_9.png  0 → 100644

启动seata服务

进入bin文件夹进行启动

[root@hao /usr/local/software/seata/seata/script/config-center/nacos]# cd ..
[root@hao /usr/local/software/seata/seata/script/config-center]# cd ..
[root@hao /usr/local/software/seata/seata/script]# cd ..
[root@hao /usr/local/software/seata/seata]# ls
bin  conf  Dockerfile  ext  lib  LICENSE  logs  script  target
[root@hao /usr/local/software/seata/seata]# cd bin/
[root@hao /usr/local/software/seata/seata/bin]# ./seata-server.sh
apm-skywalking not enabled
seata-server is starting, you can check the /usr/local/software/seata/seata/logs/start.out

查看日志

分类/seata/seata_11.png  0 → 100644

进入nacos网页查看服务列表,查看seata-server服务

分类/seata/seata_10.png  0 → 100644

开启8091端口

在业务中使用

基于seata的订单业务

订单模块

CREATE TABLE `order` (`order_id` bigint NOT NULL AUTO_INCREMENT,`order_product` bigint DEFAULT NULL,`order_num` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '订单号',`order_quantity` int DEFAULT NULL,`order_create_by` varchar(32) DEFAULT NULL,`order_create_time` datetime DEFAULT NULL,`order_update_time` datetime DEFAULT NULL,PRIMARY KEY (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

maven

        <dependencies><dependency><groupId>com.wnhz.smart</groupId><artifactId>smart-cloud-store-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.wnhz.smart</groupId><artifactId>smart-cloud-common</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId><exclusions><exclusion><groupId>io.seata</groupId><artifactId>seata-all</artifactId></exclusion><exclusion><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId></exclusion></exclusions></dependency><dependency><groupId>io.seata</groupId><artifactId>seata-all</artifactId><version>1.6.1</version></dependency><dependency><groupId>io.seata</groupId><artifactId>seata-spring-boot-starter</artifactId><version>1.6.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

bootstrap.yml

spring:application:name: smart-cloud-ordercloud:nacos:discovery:server-addr: 192.168.133.100:8848namespace: smart-cloud-idgroup: devregister-enabled: trueconfig:server-addr: 192.168.133.100:8848namespace: smart-cloud-idfile-extension: yamlshared-configs:- dataId: db.yamlgroup: devrefresh: true- dataId: mp.yamlgroup: devrefresh: true
seata:enabled: truetx-service-group: default_tx_groupservice:vgroup-mapping:default_tx_group: defaultgrouplist:default: 192.168.133.100:8091

启动类

package com.wnhz.smart.cloud.order;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** @author Hao* @date 2023-12-11 16:43*/
@SpringBootApplication
@MapperScan("com.wnhz.smart.cloud.domain.mapper")
@EnableDiscoveryClient
@EnableFeignClients("com.wnhz.smart.cloud.store.feign")
public class SmartOrderApp {public static void main(String[] args) {SpringApplication.run(SmartOrderApp.class);}
}

分布式事务使用

package com.wnhz.smart.cloud.order.service.impl;import cn.hutool.core.util.IdUtil;
import com.wnhz.smart.cloud.domain.entity.Order;
import com.wnhz.smart.cloud.domain.mapper.OrderMapper;
import com.wnhz.smart.cloud.order.service.IOrderService;
import com.wnhz.smart.cloud.store.feign.IStoreFeign;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.Date;/*** @author Hao* @date 2023-12-11 16:53*/
@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {@Autowiredprivate OrderMapper orderMapper;@Autowiredprivate IStoreFeign iStoreFeign;@GlobalTransactional@Overridepublic String generateOrder(Long productId, Integer quantity) {Order order = new Order().setOrderProduct(productId).setOrderQuantity(quantity).setOrderCreateBy("Linda").setOrderCreateTime(new Date()).setOrderUpdateTime(new Date());log.debug("产生订单对象:{},准备调用库存组件...", order);orderMapper.insert(order);iStoreFeign.decr(productId, quantity);String orderNum = IdUtil.getSnowflakeNextIdStr();log.debug("去库存成功,完善订单产生订单号:{}", orderNum);order.setOrderNum(orderNum);orderMapper.updateById(order);log.debug("订单生成成功:{}", order);return orderNum;}
}

在业务中捕获异常【因为正常情况下在这里调用的是openFeign的接口,所以异常是openFeign的异常,这里是想要知道seata的异常】

package com.wnhz.smart.cloud.order.aop;import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransactionContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;/*** @author Hao* @date 2023-12-11 19:26*/
@Aspect
@Component
@Slf4j
public class SeataExceptionAop {@Pointcut("@annotation(io.seata.spring.annotation.GlobalTransactional)")private void transactionPointCut() {}@AfterThrowing(throwing = "e", pointcut = "transactionPointCut()")public void globalTransactionalException(Throwable e) throws TransactionException {log.debug("分布式事务异常:{}", e.getMessage());String xid = RootContext.getXID();if (StringUtils.hasText(xid)) {log.debug("XID:{},执行回滚操作", xid);GlobalTransactionContext.reload(xid).rollback();log.debug("事务:{}回滚完成", xid);// throw new TransactionException("事务处理失败,回滚完成........");}}
}
库存模块

CREATE TABLE `store` (`store_id` bigint NOT NULL AUTO_INCREMENT,`store_product` bigint DEFAULT NULL,`store_quantity` int DEFAULT NULL,`store_create_by` varchar(32) DEFAULT NULL,`store_create_time` datetime DEFAULT NULL,`store_update_by` datetime DEFAULT NULL,PRIMARY KEY (`store_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

bootstrap.yml

spring:application:name: smart-cloud-storecloud:nacos:discovery:server-addr: 192.168.133.100:8848namespace: smart-cloud-idgroup: devregister-enabled: trueconfig:server-addr: 192.168.133.100:8848namespace: smart-cloud-idfile-extension: yamlshared-configs:- dataId: db.yamlgroup: devrefresh: true- dataId: mp.yamlgroup: devrefresh: true

api-feign接口

@FeignClient(value = "smart-cloud-store")
public interface IStoreFeign {@GetMapping("/api/store/decr")ResponseResult<Void> decr(@RequestParam("productId") Long productId,@RequestParam("quantity") Integer quantity);
}

order端

分类/seata/seata_13.png  0 → 100644

store端

分类/seata/seata_12.png  0 → 100644

完成

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/267728.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[IDEA] 写代码时没有类型推断的解决方法

本示例使用scala, 其他语言同理 使用 .var 时会自动生成变量 使用快捷键 CtrlAtlv 一样 val abc "abc"但是这个变量没有显式表现类型 期望 val abc: String "abc" 解决方法

[Unity]关于Unity接入Appsflyer并且打点支付

首先需要去官方下载Appsflyer的UnityPackage 链接在这afPackage 然后导入 导入完成 引入此段代码 using AppsFlyerSDK; using System.Collections; using System.Collections.Generic; using UnityEngine;public class AppflysManager : MonoBehaviour {public static App…

高效利用内存资源之动态内存管理详解

目录 一、为什么存在动态内存分配 二、动态内存函数的介绍 2.1malloc 2.2free 2.3calloc 2.4realloc 三、常见的动态内存错误 3.1对NULL指针的解引用操作 3.2对动态开辟空间的越界访问 3.3对非动态开辟内存使用free释放 3.4使用free释放一块动态开辟内存的一部分 3.…

UE4/UE5 修改/还原场景所有Actor的材质

使用蓝图方法&#xff1a; 1.修改场景所有Actor 材质&#xff1a; Wirframe&#xff1a;一个材质类 MatList&#xff1a;获取到的所有模型的全部材质 的列表 TempAllClass&#xff1a;场景中所有获取的 Actor 的列表 功能方法如下&#xff1a; 蓝图代码可复制在&#xff1a…

adb命令学习记录

1、 adb ( android debug bridge)安卓调试桥&#xff0c;用于完成电脑和手机之间的通信控制。 xcode来完成对于ios设备的操控&#xff0c;前提是有个mac电脑。 安卓系统是基于linux内核来进行开发的。 2、adb的安装: 本身 adb是 android SDK 其中自带的工具&#xff0c;用于完…

web Speech Synthesis 文字语音播报,Audio 播放base64提示音

SpeechSynthesisUtterance基本介绍 SpeechSynthesisUtterance是HTML5中新增的API,用于将指定文字合成为对应的语音.也包含一些配置项,指定如何去阅读(语言,音量,音调)等 SpeechSynthesisUtterance基本属性 SpeechSynthesisUtterance.lang 获取并设置话语的语言SpeechSynthesisU…

打包CSS

接上一个打包HTML继续进行CSS的打包 1.在之前的文件夹里的src文件夹创建一个css文件 2.在浏览器打开webpack——>中文文档——>指南——>管理资源——>加载CSS 3.复制第一句代码到终端 4.复制下图代码到webpack.config.js脚本的plugins&#xff1a;[.....]内容下…

内存cache大量使用问题导致应用异常问题

概述 28s应用崩溃查看内存使用有大量cache。 分析 查看free 信息平时的确存在大量cache使用的情况查看dmes信息发现filesendserver崩溃 崩溃信息为系统调用 查看到page allocation failure:order 5 同时也看到系统内存使用情况 查看到系统实际还有部分内存为空闲内存&am…

【原创分享】Altium Designer 23全新PCB模块复用方法教程

"Reuse Blocks"功能即“复用块”功能是Altium Designer 23设计环境中的一项强大工具&#xff0c;它允许用户将先前创建的设计模块存储在一个可访问的库中&#xff0c;并在需要时将其插入到新的设计中。通过"Reuse Blocks"&#xff0c;设计师可以节省大量时…

Scratch题库:6547网助力编程学习之路

随着科技的不断发展&#xff0c;编程已经成为了当今社会的一项重要技能。越来越多的家长和学校开始重视孩子们的编程教育&#xff0c;而Scratch作为一款适合儿童学习的编程语言&#xff0c;受到了广泛的关注。然而&#xff0c;面对琳琅满目的Scratch教程和题库&#xff0c;如何…

移液器吸头材质选择——PFA吸头在半导体化工行业的应用

PFA吸头是一种高性能移液器配件&#xff0c;这种材料具有优异的耐化学品、耐热和电绝缘性能&#xff0c;使得PFA吸头在应用中表现出色。那么它有哪些特点呢&#xff1f; 首先&#xff0c;PFA吸头具有卓越的耐化学腐蚀性能。无论是酸性溶液、碱性溶液还是有机溶剂&#xff0c;P…

python——第十七天

方法重写(overwrite) 、方法覆盖(override )&#xff1a;在继承的基础上&#xff0c;子类继承了父类的方法&#xff0c;如果不能满足自己使用&#xff0c;我们就可以重写或覆盖该方法 函数重载(overload)&#xff1a; 在强数据类型的编程语言中(如Java、C、C等等): 函数名称…