整合SpringCloud Alibaba + seata + MP,启动报错解决方案

本文指导读者解决在学习Spring Cloud Alibaba Seata时遇到的ClassNotFoundException,涉及配置步骤包括数据源代理、YAML文件设置、依赖调整,并提供详细配置示例。
摘要由CSDN通过智能技术生成

问题描述

在学习 spring cloud alibaba 的 seata 组件的时候,启动总是报错

java.lang.ClassNotFoundException: io.seata.spring.annotation.datasource.SeataDataSourceBeanPostProcessor

解决方法

网上关于这个问题的解决方案很少,自己折腾了一天总算是解决了,如果你也遇到这个问题可以尝试按照以下步骤逐一排查:

1 配置 seata 的数据源代理

新建一个 JavaConfig 配置类,注入 3 个bean

  • DataSource
  • DataSourceProxy
  • SqlSessionFactory
    • 如果 orm 框架是 MP(MybatisPlus),使用MybatisSqlSessionFactoryBean创建
    • 如果是 Mybatis,使用SqlSessionFactoryBean创建
@Configuration
public class DataSourceProxyConfig {

    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    @Bean
    @Primary
    public DataSourceProxy dataSourceProxy(DataSource druidDataSource) {
        return new DataSourceProxy(druidDataSource);
    }

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(dataSourceProxy);
        return factoryBean.getObject();
    }
}

2 application.yml + file.conf

在 application.yml 中,将属性spring.cloud.alibaba.seata.tx-service-group设置为自己的 seata-server 的 file.conf 中的 service.vgroup_mapping.my_test_tx_group属性值。

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    alibaba:
      seata:
        tx-service-group: ambitious_tx_group

在当前模块的 resources 目录下,也要有一个 file.conf 文件,可以直接用我这份(从官方 github 的 sample 中拿来的)。将其中的service.vgroup_mapping.my_test_tx_group属性名修改为service.vgroup_mapping.[刚刚在 application.yml 中配置的组名]

transport {
  # tcp udt unix-domain-socket
  type = "TCP"
  #NIO NATIVE
  server = "NIO"
  #enable heartbeat
  heartbeat = true
  #thread factory for netty
  thread-factory {
    boss-thread-prefix = "NettyBoss"
    worker-thread-prefix = "NettyServerNIOWorker"
    server-executor-thread-prefix = "NettyServerBizHandler"
    share-boss-worker = false
    client-selector-thread-prefix = "NettyClientSelector"
    client-selector-thread-size = 1
    client-worker-thread-prefix = "NettyClientWorkerThread"
    # netty boss thread size,will not be used for UDT
    boss-thread-size = 1
    #auto default pin or 8
    worker-thread-size = 8
  }
  shutdown {
    # when destroy server, wait seconds
    wait = 3
  }
  serialization = "seata"
  compressor = "none"
}
service {
  #vgroup->rgroup
  # !!!这里的属性名一定要与 application.yml 中配置的一样!!!
  # !!!必须是下划线命名,检查自己的是不是"vgroupMapping"!!!
  vgroup_mapping.ambitious_tx_group = "default"
  #only support single node
  default.grouplist = "127.0.0.1:8091"
  #degrade current not support
  enableDegrade = false
  #disable
  disable = false
  #unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
  max.commit.retry.timeout = "-1"
  max.rollback.retry.timeout = "-1"
}

client {
  async.commit.buffer.limit = 10000
  lock {
    retry.internal = 10
    retry.times = 30
  }
  report.retry.count = 5
}

## transaction log store
store {
  ## store mode: file、db
  mode = "db"

  ## file store
  file {
    dir = "sessionStore"

    # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
    max-branch-session-size = 16384
    # globe session size , if exceeded throws exceptions
    max-global-session-size = 512
    # file buffer size , if exceeded allocate new buffer
    file-write-buffer-cache-size = 16384
    # when recover batch read size
    session.reload.read_size = 100
    # async, sync
    flush-disk-mode = async
  }

  ## database store
  db {
    ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp) etc.
    datasource = "dbcp"
    ## mysql/oracle/h2/oceanbase etc.
    db-type = "mysql"
    url = "jdbc:mysql://127.0.0.1:3306/seata"
    user = "root"
    password = "root"
    min-conn = 1
    max-conn = 3
    global.table = "global_table"
    branch.table = "branch_table"
    lock-table = "lock_table"
    query-limit = 100
  }
}
lock {
  ## the lock store mode: local、remote
  mode = "remote"

  local {
    ## store locks in user's database
  }

  remote {
    ## store locks in the seata's server
  }
}
recovery {
  committing-retry-delay = 30
  asyn-committing-retry-delay = 30
  rollbacking-retry-delay = 30
  timeout-retry-delay = 30
}

transaction {
  undo.data.validation = true
  undo.log.serialization = "jackson"
}

## metrics settings
metrics {
  enabled = false
  registry-type = "compact"
  # multi exporters use comma divided
  exporter-list = "prometheus"
  exporter-prometheus-port = 9898
}

3 pom.xml

这一步很关键!我的项目启动报错就是因为依赖版本号冲突导致的
引入依赖spring-cloud-starter-alibaba-seata之后,使用 <exclusions> 标签去掉 seata-allseata-spring-boot-starter 这两个子依赖。再自己手动引入,这里具体的版本号跟你具体的 seata-server 版本以及 spring-cloud-alibaba-dependencies 版本有关,我的版本号分别是0.9.02.1.2.RELEASE
最终引入的效果如下:

<!-- seata -->
<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-spring-boot-starter</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-all</artifactId>
    <version>0.9.0</version>
</dependency>

至此项目终于能够成功启动!


我的 个人博客 上线啦,欢迎到访~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AmbitiousJun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值