SpringCloud整合seata1.5.2【遇到的各种问题 & 如何详细解决】

目录

前言

1 安装 nacos2.1.0

1.1 找到2.1.0版本,点击下载 

1.2 进行解压

1.3  创建数据库

1.4 更改配置

1.5 启动

2 安装 seata1.5.2

2.1 找到1.5.2版本,点击下载

2.2 进行解压

2.3  创建数据库

2.4 更改配置

2.5 创建配置

2.6 启动 

3 整合SpringCloud

4 各种问题

 问题一

问题二

结语


前言

注:因版本不同,对应的配置也有所区别,请参照下面链接

https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

1 安装 nacos2.1.0

官网下载:https://github.com/alibaba/nacos/releases

百度网盘链接: https://pan.baidu.com/s/1J2SLS9M_P-B9tbiNrhXRhw 
提取码:yyds


1.1 找到2.1.0版本,点击下载 

1.2 进行解压

1.3  创建数据库

创建 nacos 数据库,进到 conf 目录下,打开 nacos-mysql.sql 文件,创建表

1.4 更改配置

进到 bin 目录下,编辑 startup.cmd ,把 cluster 改为 standalone

进到 conf 目录下,打开 application.properties 文件

打开数据库的注释,更改数据库配置

1.5 启动

进入 bin 目录下,双击 startup.cmd ,启动 nacos

输入账号和密码,至此 nacos 安装成功

2 安装 seata1.5.2

官网下载:https://github.com/apache/incubator-seata/tags

百度网盘链接:https://pan.baidu.com/s/1OD50fKb-0tF3lPoZ1tA47Q 
提取码:yyds

2.1 找到1.5.2版本,点击下载


2.2 进行解压

2.3  创建数据库

创建 seata 数据库,进入 script\server\db 目录下,打开 mysql.sql 文件,创建表


 为自己项目的数据库创建 undo_log 表 

DROP TABLE IF EXISTS `undo_log`;
CREATE TABLE `undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `branch_id` bigint(20) NOT NULL,
  `xid` varchar(100) NOT NULL,
  `context` varchar(128) NOT NULL,
  `rollback_info` longblob NOT NULL,
  `log_status` int(11) NOT NULL,
  `log_created` datetime NOT NULL,
  `log_modified` datetime NOT NULL,
  `ext` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.4 更改配置

进到 conf 目录下,打开 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: nacos
    nacos:
      server-addr: http://127.0.0.1:8848
      # namespace: 7392baed-d98b-48a4-8676-34e1b38eade6
      namespace:
      group: SEATA_GROUP
      username: nacos
      password: nacos
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
      data-id: seataServer.properties
  registry:
    # support: nacos 、 eureka 、 redis 、 zk  、 consul 、 etcd3 、 sofa
    type: nacos
    preferred-networks: 30.240.*
    nacos:
      application: seata-server
      server-addr: http://127.0.0.1:8848
      group: SEATA_GROUP
      namespace:
      # namespace: 7392baed-d98b-48a4-8676-34e1b38eade6
      cluster: default
      username: nacos
      password: nacos
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #access-key: ""
      #secret-key: ""
  security:
    secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
    tokenValidityInMilliseconds: 1800000
    ignore:
      urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

2.5 创建配置

进入 nacos

  • Data ID:seataServer.properties
  • Group:SEATA_GROUP
  • 配置格式:properties
  • 配置内容:
#Transaction storage configuration, only for the server. The file, DB, and redis configuration values are optional.
store.mode=db
store.lock.mode=db
store.session.mode=db
#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://1.14.165.93:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000
#Transaction routing rules configuration, only for the client
#注意:一定要配置【超级大坑】  mall_tx_group这个可以自己命名
service.vgroupMapping.mall_tx_group=default

2.6 启动 

进入 bin 目录下,双击 seata-server.bat ,启动 seata

查看 nacos 服务列表,至此 seata 启动成功

3 SpringCloud整合

注:以下版本必须完全相同

SpringBoot 2.3.12.RELEASE

SpringCloud Hoxton.SR12

SpringCloud Alibaba 2.2.9.RELEASE

nacos 2.1.0.RELEASE

seata 1.5.2

        <!-- 注意一定要引入对版本,要引入spring-cloud版本seata,而不是springboot版本的seata-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
            <!-- 排除掉springcloud默认的seata版本,以免版本不一致出现问题-->
            <exclusions>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-spring-boot-starter</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.seata</groupId>
                    <artifactId>seata-all</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 上面排除掉了springcloud默认色seata版本,此处引入和seata-server版本对应的seata包-->
        <dependency>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
            <version>1.5.2</version>
        </dependency>

4 各种问题

yml文件配置 


 问题一

ERROR 14668 --- [  restartedMain] i.s.c.r.netty.NettyClientChannelManager  : can not get cluster name in registry config 'service.vgroupMapping.default_tx_group', please make sure registry config correct

配置以下解决,mall_tx_group 就是 2.5 那个超级大坑,自己配的什么名字,就用自己的

把 mall_tx_group 换成自己的就行了,其它不用变

seata:
  tx-service-group: mall_tx_group
  service:
    vgroup-mapping:
      mall_tx_group: default
    grouplist:
      default: 127.0.0.1:8091

问题二

配了上面还是项目还是启动失败,报了很多错

Caused by: java.lang.IllegalAccessError: class com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper$$EnhancerBySpringCGLIB$$94ab1087 cannot access its superclass com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceWrapper

在启动类上配置

exclude = DruidDataSourceAutoConfigure.class

结语

至此SpringCloud整合seata成功!!!

解决各种bug花了一天,制作不易

最后本文章如果对您有一点点帮助的话,求赞 求收藏 求关注,您的支持是我创作的最大动力!

文章粗浅,希望对大家有帮助!

  • 43
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring CloudSeata都是分布式系统开发中常用的框架,它们可以一起使用来实现分布式事务管理。下面是Spring Cloud整合Seata的步骤: 1. 引入Seata的依赖 在Spring Boot项目的pom.xml文件中,添加Seata的依赖: ```xml <dependency> <groupId>io.seata</groupId> <artifactId>seata-all</artifactId> <version>${seata.version}</version> </dependency> ``` 其中${seata.version}需要根据实际情况进行替换。 2. 配置Seata的配置文件 在Spring Boot项目的resources目录下,创建一个名为file.conf的文件,并配置Seata的全局配置信息。示例如下: ```ini transport.type=TCP transport.server=NIO transport.heartbeat=true transport.enableClientBatchSendRequest=false transport.threadFactory.bossThreadPrefix=NettyBoss transport.threadFactory.workerThreadPrefix=NettyServerNIOWorker transport.threadFactory.serverExecutorThreadPrefix=NettyServerBizHandler transport.threadFactory.shareBossWorker=false transport.threadFactory.clientSelectorThreadPrefix=NettyClientSelector transport.threadFactory.clientSelectorThreadSize=1 transport.threadFactory.clientWorkerThreadPrefix=NettyClientWorkerThread transport.threadFactory.bossThreadSize=1 transport.threadFactory.workerThreadSize=default transport.shutdown.wait=3 service.vgroupMapping.my_test_tx_group=default service.seata.grouplist[default]=localhost:8091 client.rm.async.commit.buffer.limit=10000 client.rm.lock.retryInterval=10 client.rm.lock.retryTimes=30 client.rm.report.retryCount=5 client.rm.report.success.enable=false client.rm.table.meta.check.enable=false client.rm.table.undo.data.validation=false ``` 其中,service.vgroupMapping.my_test_tx_group是指将分布式事务组名为my_test_tx_group的应用程序映射到default分组,service.seata.grouplist[default]是指定Seata Server的地址和端口号。 3. 配置Spring Cloud的数据源 在Spring Boot项目中,需要将数据源配置为Seata的代理数据源。在application.properties或application.yml文件中,添加以下配置: ```yaml spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false spring.datasource.username=root spring.datasource.password=root # Seata DataSourceProxy spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group spring.cloud.alibaba.seata.enable-auto-data-source-proxy=true ``` 其中,spring.datasource.*是指定应用程序使用的数据源,spring.cloud.alibaba.seata.*是指定使用Seata代理数据源。 4. 配置Spring Cloud的Feign客户端 如果应用程序中使用了Feign客户端,则需要配置Feign客户端的拦截器,以便在进行远程调用时启用Seata的分布式事务管理。在Spring Boot项目中,需要定义一个名为FeignConfiguration的类,并在其中添加以下配置: ```java @Configuration public class FeignConfiguration { @Bean public RequestInterceptor requestInterceptor() { return new SeataFeignClientInterceptor(); } } ``` 其中,Seata

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值