我在项目中集成seata1.2.0的时候遇到的一些问题总结:
- seata版本冲突问题,springcloud内部集成了seata的依赖但是和你自己服务器上的seata版本很有可能不一样,往往版本不一样会报如下错误
no available service ‘default‘ found, please make sure registry config correct
基本上这个错误就是版本冲突问题,在maven中去除springcloud内置的seata版本依赖,然后自己重新引入与自己使用的seata版本一致的依赖
<!--阿里巴巴 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>
</dependency>
- yml配置中没有指定nacos中的namespace命名空间ID,我就遇到了这个莫名其妙的错误,我刚开始使用的是默认的命名空间public没有新建seata的配置命名空间ID,然后就会报错一直找不到在nacos中的配置,报如下错误
no available service ‘null‘ found, please make sure registry config correct
后来我新建了一个namespace专门用来放seata的配置文件,明确指定了namespace的ID在yml文件中,然后问题得以解决,成功运行。
我感觉在实际项目开发中也是提倡这种做法的,肯定要专门新建seata配置中心的namespace然后进行操作
- 还有一个问题就是如果我们的项目中使用的持久层框架是Mybatis-Plus,那么一定要注意你的seata数据源配置DataSourceProxyConfig中的SqlSessionFactoryBean要使用mybatis-plus的配置,千万不要用错了,用成mybatis的配置后,那么mybatis-plus的一切base基础函数就用不了了,会报mapper和xml中的方法绑定错误的异常。下面贴上mybatis和mybatis-plus不同的持久层框架下,seata数据源的不同配置:
mybatis:
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSourceProxy);
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
return sqlSessionFactoryBean.getObject();
}
mybatis-plus:
/**
* mybatisPlus项目集成seata的配置
* @param dataSourceProxy
* @return
* @throws Exception
*/
//替换SqlSessionFactory的DataSource
@Bean
public MybatisSqlSessionFactoryBean sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
// 这里必须用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean,否则 MyBatisPlus 不会生效
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(dataSourceProxy);
mybatisSqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath*:/mapper/*.xml"));
return mybatisSqlSessionFactoryBean;
}
最后关于seata集成的一些实操笔记整理在浏览器中,如果各位小伙伴有需要的底下留言发给你们。
集成的操作并不难,但是搞懂底层的undolog表原理任重而道远。