java分布式事務(wù)seata的使用方式
更新時間:2024年04月27日 16:45:36 作者:jjw_zyfx
這篇文章主要介紹了java分布式事務(wù)seata的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
首先創(chuàng)建一個seata的springboot模塊
并引入seata的起步依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> </dependency>
模塊的目錄結(jié)構(gòu)
如下:
- seata.yaml中的內(nèi)容為:
seata: tx-service-group: seata-toutiao service: vgroup-mapping: # 事務(wù)組與cluster的映射關(guān)系 seata-toutiao: DEFAULT grouplist: DEFAULT: 192.168.211.136:8091
- spring.factories中的內(nèi)容為:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.jjw.core.seata.SeataHeimaAutoConfiguration org.springframework.boot.env.EnvironmentPostProcessor=\ com.jjw.core.seata.MyEnvironmentPostProcessor
- MyEnvironmentPostProcessor中的內(nèi)容為:
package com.jjw.core.seata; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertySource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import java.io.IOException; import java.util.List; /** * 自定義環(huán)境處理,在運行SpringApplication之前加載任意配置文件到Environment環(huán)境中 */ public class MyEnvironmentPostProcessor implements EnvironmentPostProcessor { //Properties對象 @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { //自定義配置文件 String[] profiles = { "seata.yaml" }; //循環(huán)添加 for (String profile : profiles) { //從classpath路徑下面查找文件 Resource resource = new ClassPathResource(profile); //加載成PropertySource對象,并添加到Environment環(huán)境中 environment.getPropertySources().addFirst(loadProfiles(resource)); } } //加載單個配置文件 private PropertySource<?> loadProfiles(Resource resource) { if (!resource.exists()) { throw new IllegalArgumentException("資源" + resource + "不存在"); } try { YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader(); List<PropertySource<?>> resources = yamlPropertySourceLoader.load(resource.getFilename(), resource); return resources.get(0); } catch (IOException ex) { throw new IllegalStateException("加載配置文件失敗" + resource, ex); } } }
- SeataHeimaAutoConfiguration中的內(nèi)容為:
package com.jjw.core.seata; import io.seata.rm.datasource.DataSourceProxy; import io.seata.spring.boot.autoconfigure.properties.SeataProperties; import io.seata.spring.boot.autoconfigure.properties.client.ServiceProperties; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.HashMap; import java.util.Map; /** * --加載文件properties * https://www.cnblogs.com/huanzi-qch/p/11122107.html * --加載yaml * https://blog.csdn.net/baidu_28523317/article/details/108701391 * * --順序 * https://blog.csdn.net/f641385712/article/details/105596178 * * --可能存在的問題 * https://segmentfault.com/q/1010000040364236 */ @Configuration @ConditionalOnClass(DataSourceProxy.class) public class SeataHeimaAutoConfiguration { //僅僅用作測試 @Bean public Map<String,String> seatax(SeataProperties seataProperties, ServiceProperties serviceProperties){ String txServiceGroup = seataProperties.getTxServiceGroup(); System.out.println(txServiceGroup); System.out.println(serviceProperties.getGrouplist()); return new HashMap<>(); } }
在需要用到分布式事務(wù)的微服務(wù)中添加依賴
(用到幾個微服務(wù)就給相應(yīng)的微服務(wù)都添加分布式事務(wù)依賴)、且對這幾個微服務(wù)所在的庫都添加一個undolog表:
-- auto-generated definition create table undo_log ( id bigint auto_increment primary key, branch_id bigint not null, xid varchar(100) not null, context varchar(128) not null, rollback_info longblob not null, log_status int not null, log_created datetime not null, log_modified datetime not null, ext varchar(100) null, constraint ux_undo_log unique (xid, branch_id) ) charset = utf8;
在最開始使用分布式事務(wù)的那個微服務(wù)上的方法上添加事務(wù)注解
@GlobalTransactional // 全局事務(wù) @Transactional // 本地事務(wù) @Override public void pass(Integer id) {
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中基于推、拉模式的sentinel規(guī)則持久化詳解
這篇文章主要介紹了Java中基于推、拉模式的sentinel規(guī)則持久化詳解,推模式是sentinelDashboard?把規(guī)則推給Nacos,Nacos監(jiān)聽規(guī)則的變化推給微服務(wù),拉模式是sentinelDashboard?把規(guī)則直接給微服務(wù),?Nacos定時的同步微服務(wù)端的規(guī)則,需要的朋友可以參考下2023-09-09解析Java中所有錯誤和異常的父類java.lang.Throwable
這篇文章主要介紹了Java中所有錯誤和異常的父類java.lang.Throwable,文章中簡單地分析了其源碼,說明在代碼注釋中,需要的朋友可以參考下2016-03-03關(guān)于maven install報錯原因揭秘:parent.relativePath指向錯誤的本地POM文件
在使用Maven進(jìn)行項目構(gòu)建時,如果遇到'parent.relativePath'指向錯誤的本地POM文件的問題,可能會導(dǎo)致構(gòu)建失敗,這通常是由于父項目POM文件的相對路徑設(shè)置錯誤、本地POM文件與父項目POM文件版本或內(nèi)容不一致所致,解決方法包括檢查并修正父項目POM文件中的相對路徑設(shè)置2024-09-09MyBatis 實現(xiàn)數(shù)據(jù)的批量新增和刪除的操作
這篇文章主要介紹了MyBatis 實現(xiàn)數(shù)據(jù)的批量新增和刪除的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02