springcloud + mybatis + seate集成示例
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.1.1.RELEASE</version></dependency> 2.编写配置类
@Configurationpublic class DataSourceConfig { @Bean @ConfigurationProperties(prefix = 'spring.datasource') public DataSource druidDataSource() {DruidDataSource druidDataSource = new DruidDataSource();return druidDataSource; } @Primary @Bean('dataSource') public DataSourceProxy dataSource(DataSource druidDataSource) {return new DataSourceProxy(druidDataSource); } @Bean public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();factoryBean.setDataSource(dataSourceProxy);factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources('classpath*:/mapper/*.xml'));return factoryBean.getObject(); }} 3.修改启动类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) 4.在需要开启分布式事物的方法上添加注解
@GlobalTransactional 二.服务端1.下载seata-server
[seata-server]{https://github.com/seata/seata/releases}
2.修改配置文件(本文以nacos为样例,store记录为mysql)修改conf下的registry.conf
registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type = 'nacos' nacos { serverAddr = 'localhost' namespace = 'piblic' cluster = 'default' }}config { # file、nacos 、apollo、zk、consul、etcd3 type = 'nacos' nacos { serverAddr = 'localhost' #namespace为空,否则后面会报找不到store.db.driver-class-name找不到 namespace = '' }}
修改registry下的type为nacos,配置nacos的serverAddr为你nacos服务的地址,不要带http和端口号。它会默认去连你的8858端口,如需修改端口,请修改nacos-config.sh文件中的端口拷贝registry.conf到每个使用seata服务的resources下修改conf下nacos-config.txt
store.mode=dbstore.db.datasource=dbcpstore.db.db-type=mysqlstore.db.driver-class-name=com.mysql.jdbc.Driverstore.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=truestore.db.user=mysqlstore.db.password=mysql
修改store.mode为db,修改对应db配置文件为你自己的mysql。修改service.vgroup_mapping.my_test_tx_group=default属性0.9.0.1版本后:my_test_tx_group修改为自己的服务名(spring.application.name)+'-seata-service-group'0.9.0.1版本前:my_test_tx_group修改为自己的服务名(spring.application.name)+'-fescar-service-group'可以spring-cloud-alibaba-seata项目下的GlobalTransactionAutoConfiguration累中看到
有多少个服务需要使用seata就要添加多少个ervice.vgroup_mapping执行nacos-config.sh Nacos-Server-IP(?tex-Nacos-Server-IP( Nacos-Server-IP为你nacos服务的ip地址),在一步骤是把nacos-config.txt中的配置更新到nacos上,如果nacos-config.txt重新修改,就要重新执行该命令。或者登陆到nacos的管理界面直接进行修改。之后添加,删除,修改服务,在nacos配置上作对应修改即可
3.创建表在上一步骤中store.db.url的database中执行建表语句(conf下db_store.sql)在每一个业务库中执行建表语句(conf下db_undo_log.sql)
本文链接:https://gudepeng.github.io/note/2019/11/29/seate/demo样例:https://github.com/gudepeng/demoproject/tree/master/seata-servicea
到此这篇关于springcloud + mybatis + seate集成示例的文章就介绍到这了,更多相关springcloud mybatis seate内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!
相关文章: