linux搭建seata并使用

news2024/10/5 12:47:39

搭建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: seata
    password: 123456

seata:
  config:
    # support: nacos 、 consul 、 apollo 、 zk  、 etcd3
    type: nacos
    nacos:
      # 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 、 sofa
    type: nacos
    # 配置允许连接的ip地址
    # preferred-networks: 30.240.*
    nacos:
      application: seata-server
      server-addr: 192.168.133.100:8848
      group: SEATA_GROUP
      namespace: seata-id

配置store模块

分类/seata/seata_5.png  0 → 100644

  store:
    # support: file 、 db 、 redis
    mode: redis
    session:
      mode: file
    lock:
      mode: file

分类/seata/seata_4.png  0 → 100644

    redis:
      mode: single
      database: 1
      min-conn: 10
      max-conn: 100
      password:
      max-total: 100
      query-limit: 1000
      # 单个
      single:
        host: 192.168.133.100
        port: 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-order
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.133.100:8848
        namespace: smart-cloud-id
        group: dev
        register-enabled: true
      config:
        server-addr: 192.168.133.100:8848
        namespace: smart-cloud-id
        file-extension: yaml
        shared-configs:
          - dataId: db.yaml
            group: dev
            refresh: true
          - dataId: mp.yaml
            group: dev
            refresh: true
seata:
  enabled: true
  tx-service-group: default_tx_group
  service:
    vgroup-mapping:
      default_tx_group: default
    grouplist:
      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 {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IStoreFeign iStoreFeign;

    @GlobalTransactional
    @Override
    public 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-store
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.133.100:8848
        namespace: smart-cloud-id
        group: dev
        register-enabled: true
      config:
        server-addr: 192.168.133.100:8848
        namespace: smart-cloud-id
        file-extension: yaml
        shared-configs:
          - dataId: db.yaml
            group: dev
            refresh: true
          - dataId: mp.yaml
            group: dev
            refresh: 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.coloradmin.cn/o/1304068.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Linux】free命令使用

free命令 ​free是指查看当前系统内存的使用情况&#xff0c;它显示系统中剩余及已用的物理内存和交换内存&#xff0c;以及共享内存和被核心使用的缓冲区。 作者 作者&#xff1a;Brian Edmonds。 语法 free [参数] free 命令 -Linux手册页 命令选项及作用 执行令 &am…

ArcGIS无法绘制一个或多个图层

背景&#xff1a;在导入一份数据时候&#xff0c;arcmap出现无法绘制一个或多个图层的错误&#xff0c;...点数少于要素所要求的的数量&#xff0c;查阅了半天资料发现是制作数据时候拓扑关系错误造成&#xff0c;现将处理方法详细记录如下&#xff1a; 1.原数据&#xff1a; …

如何使用GaussDB创建外表(FOREIGN TABLE)

目录 一、前言 二、创建外表的特点 二、GaussDB创建外表访问外部数据库表&#xff08;示例&#xff09; 1、创建外表 2、FAQ&#xff1a;CREATE USER MAPPING错误 三、GaussDB创建外表映射数据文件&#xff08;示例&#xff09; 1、创建数据文件 2、创建外表 3、FAQ&a…

使用torch解决线性回归问题

数据处理 import torch import numpy as np import pandas as pd import matplotlib.pyplot as pltdatapd.read_csv(./datasets/Income1.csv) #数据准备data.head(5)#展示数据 #以上所有的代码都是用jupyter notebook写&#xff0c;形成了阶段性的结果展示 查看数据信息 dat…

Qt的坐标系系统 - 3个坐标系,2个变换

参考&#xff1a; https://zhuanlan.zhihu.com/p/584048811https://www.zhihu.com/tardis/zm/art/634951149?source_id1005 小谈Qt的坐标系系统 Qt中有三个坐标系 设备坐标系窗口坐标系逻辑坐标系 设备坐标系: 即Device坐标系。也是物理坐标系。即真实的的物理坐标系。 …

Ubuntu部署EMQX开源版MQTT服务器-Orange Pi部署-服务器部署

一、前言 作为全球最具扩展性的 MQTT 消息服务器&#xff0c;EMQX 提供了高效可靠海量物联网设备连接&#xff0c;能够高性能实时移动与处理消息和事件流数据&#xff0c;本文将介绍如何在Ubuntu 22.04上部署MQTT服务器。我们本次选择开源版&#xff0c;使用离线安装方式部署。…

ArcGIS pro与SuperMap根据属性自动填充颜色步骤

GIS项目经常会接触到控规CAD数据&#xff0c;想要把数据转换成GIS图层并发布&#xff0c;需要进行专题配图。研究了一下ArcGIS pro和SuperMap iDesktop的配图&#xff0c;整理一下用到的一些技术思路。 1、Excel表格根据RGB值添加单元格填充颜色 要实现如上效果图&#xff0c;…

【NR技术】NR NG-RAN整体架构 -网络接口以及无线协议框架(四)

1 引言 本博文介绍NR NG-RAN的网络节点间的接口以及无线协议框架。网络接口介绍包括RAN和NGC之间的NG接口&#xff1b;无线协议框架包括用户面和控制面协议。 2 NG接口 2.1 NG用户面接口 NG-U (user plane interface)是NG-RAN节点与UPF之间的接口。NG接口的用户平面协议栈如图…

UE4 透明物体不渲染显示??

问题描述&#xff1a;半透明特效在背景&#xff08;半透明材质模型&#xff09;前&#xff0c;当半透明特效开始移动的时候&#xff0c;随着速度的加快会逐渐不渲染&#xff01; 解决办法&#xff1a; 1.设置透明度排序 2.如果还没效果&#xff0c;修改半透明背景模型以下材质…

基于KEDA的Kubernetes自动缩放机制

KEDA以事件驱动的方式实现Kubernetes Pod的动态自动扩容机制&#xff0c;以满足不同的负载需求&#xff0c;从而提高应用可伸缩性和弹性。原文: Dynamic Scaling with Kubernetes Event-driven Autoscaling (KEDA) Kubernetes是容器编排平台的事实标准&#xff0c;已经彻底改变…

vp与相机连接

1.网线 2.相机电源线 1.相机电源线 接头 &#xff08;使用红色和黑色 线头要串联&#xff09; 1.光源连接口 2.光源控制器 开关 3.光源控制器通电接口 4.光源自动感应接口 &#xff08;一般用于自动控制光源 开关&#xff09; 1.两种不同类型的光源 1.光源亮度控制 2.切…

Leetcode—209.长度最小的子数组【中等】

2023每日刷题&#xff08;五十六&#xff09; Leetcode—209.长度最小的子数组 实现代码 class Solution { public:int minSubArrayLen(int target, vector<int>& nums) {int left 0, right 0;int ans nums.size() 1, s 0;for(; right < nums.size(); righ…

基于单片机的自动售货机(论文+源码)

1.系统设计 本设计以这样的工作流程开始自动售货机的自动售货过程&#xff1a; 启动系统&#xff0c;开始待机&#xff1b;顾客通过按键选择商品的种类以及数量并确认&#xff1b; 售货机检查是否有足够的货物并通过LCD提示等待顾客投币&#xff1b;顾客投入货币&#xff0c;…

使用yum/dnf管理软件包

本章主要介绍使用 yum 对软件包进行管理。 yum 的介绍搭建yum源创建私有仓库yum客户端的配置yum的基本使用使用第三方yum源 使用rpm安装包时经常会遇到一个问题就是包依赖&#xff0c;如下所示。 [rootrhel03 ~]# rpm -ivh /mnt/AppStream/Packages/httpd-2.4.37-41.modulee…

【漏洞复现】云时空社会化商业ERP系统gpy文件上传

漏洞描述 用友软件的先进管理理念,汇集各医药企业特色管理需求,通过规范各个流通环节从而提高企业竞争力、降低人员成本,最终实现全面服务于医药批发、零售连锁企业的信息化建设的目标,是一款全面贴合最新GSP要求的医药流通行业一站式管理系统。 时空云社会化商业ERP gpy…

GNSS - PPP软件 - GAMP 在VS2019/2022下完成调试、跑通程序(超详细!)

目录 一、前期准备 二、调试详细步骤 1.VS中新建项目 2.复制源码至项目文件夹 3.将源码中“.c”文件和“.h”文件添加至项目指定位置 4.修改项目属性&#xff1a; (1)【 配置属性 -> C/C ->预处理器 ->预处理器定义】添加如下 (2)【配置属性->链接器->调…

产品固件烧写方案

1、前言 一成熟的量产的嵌入式产品&#xff0c;软件一般分为BootLoader和App&#xff0c;BootLoader用于启动校验、App升级、App版本回滚等功能&#xff0c;BootLoader在cpu上电第一阶段中运行&#xff0c;之后跳转至App地址执行应用程序。 因此&#xff0c;在发布固件的时候&a…

12.11 C++ 作业

完善对话框&#xff0c;点击登录对话框&#xff0c;如果账号和密码匹配&#xff0c;则弹出信息对话框&#xff0c;给出提示”登录成功“&#xff0c;提供一个Ok按钮&#xff0c;用户点击Ok后&#xff0c;关闭登录界面&#xff0c;跳转到其他界面 如果账号和密码不匹配&#xf…

[UNILM]论文实现:Unified Language Model Pre-training for Natural Language.........

文章目录 一、完整代码二、论文解读2.1 介绍2.2 架构2.3 输入端2.4 结果 三、过程实现四、整体总结 论文&#xff1a;Unified Language Model Pre-training for Natural Language Understanding and Generation 作者&#xff1a;Li Dong, Nan Yang, Wenhui Wang, Furu Wei, Xia…

编程实战:自己编写HTTP服务器(系列4:查看文件、下载等一般功能)

系列入口&#xff1a;编程实战&#xff1a;自己编写HTTP服务器&#xff08;系列1&#xff1a;概述和应答&#xff09;-CSDN博客 本文介绍各种功能的实现。大部分是特定内置入口。 目录 一、默认页 二、查看文件 三、关闭服务 四、下载页面 一、默认页 前面在已经介绍过重定…