问题描述
在学习 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
创建
- 如果 orm 框架是 MP(MybatisPlus),使用
@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-all
和 seata-spring-boot-starter
这两个子依赖。再自己手动引入,这里具体的版本号跟你具体的 seata-server 版本以及 spring-cloud-alibaba-dependencies 版本有关,我的版本号分别是0.9.0
和2.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>
至此项目终于能够成功启动!
我的 个人博客 上线啦,欢迎到访~