亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐

 更新時(shí)間:2022年07月26日 10:46:02   作者:「已注銷」  
本文主要介紹了springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

各個(gè)框架版本信息

  • springboot: 2.1.3
  • springcloud: Greenwich.RELEASE
  • seata: 1.0.0
  • shardingsphere:4.0.1

maven 依賴

? ? ? ?<dependency>
? ? ? ? <!--<groupId>io.shardingsphere</groupId>-->
? ? ? ? <groupId>org.apache.shardingsphere</groupId>
? ? ? ? <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
? ? </dependency>
? ? <!--sharding-jdbc 4.0.0 以后版本不能加starter 會(huì)導(dǎo)致啟動(dòng)數(shù)據(jù)源沖突-->
? ? <!--<dependency>-->
? ? ? ? <!--<groupId>com.alibaba</groupId>-->
? ? ? ? <!--<artifactId>druid-spring-boot-starter</artifactId>-->
? ? <!--</dependency>-->
? ? <dependency>
? ? ? ? <groupId>com.alibaba</groupId>
? ? ? ? <artifactId>druid</artifactId>
? ? </dependency>

? ? <dependency>
? ? ? ? <groupId>com.alibaba.cloud</groupId>
? ? ? ? <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
? ? ? ? <exclusions>
? ? ? ? ? ? <exclusion>
? ? ? ? ? ? ? ? <groupId>io.seata</groupId>
? ? ? ? ? ? ? ? <artifactId>seata-all</artifactId>
? ? ? ? ? ? </exclusion>
? ? ? ? </exclusions>
? ? </dependency>
? ? <dependency>
? ? ? ? <groupId>io.seata</groupId>
? ? ? ? <artifactId>seata-all</artifactId>
? ? ? ? <version>1.0.0</version>
? ? </dependency>
? ? <dependency>
? ? ? ? <groupId>org.apache.shardingsphere</groupId>
? ? ? ? <artifactId>sharding-transaction-base-seata-at</artifactId>
? ? </dependency>

需要增加的切面類

@Component
@Aspect
@Slf4j
public class SeataTransactionalAspect {
?? ? ? @Before("execution(* com.XXX.dao.*.*(..))")
?? ? ? ?public void before(JoinPoint joinPoint) throws TransactionException {
?? ? ? ? ? ?MethodSignature signature = (MethodSignature)joinPoint.getSignature();
?? ? ? ? ? ?Method method = signature.getMethod();
?? ? ? ? ? ?log.info("攔截到需要分布式事務(wù)的方法," + method.getName());

? ? ? ? if(StringUtils.startsWithAny(method.getName(),"insert","save"
? ? ? ? ? ? ? ? ,"update","delete")){
? ? ? ? ? ? TransactionTypeHolder.set(TransactionType.BASE);
? ? ? ? }
? ? }
}

ProductServiceImpl代碼

@Service
public class ProductServiceImpl implements ProductService {

? ? @Resource
? ? UserFeignClient userFeignClient;

? ? @Resource
? ? ProductDao productDao;
? ??
?? ?@Override
? ? @GlobalTransactional(name="zhjy-product-tx-group",rollbackFor = Exception.class)
? ? public void createProduct(Product product) {
? ? ? ? productDao.insertProduct(product);
? ? ? ? ProductDesc proDesc = new ProductDesc();
? ? ? ? proDesc.setProductDesc(product.getProductDesc());
? ? ? ? proDesc.setStoreId(product.getStoreId());
? ? ? ? proDesc.setProductId(product.getProductId());
? ? ? ? productDao.insertProductDesc(proDesc);
// ? ? ? ?if(product.getProductName().endsWith("5")){
// ? ? ? ? ? ?int i = 1/0;
// ? ? ? ?}
// ? ? ? ?int j = 1/0;
? ? ? ? User user = new User();
? ? ? ? user.setAge(product.getPrice().intValue());
? ? ? ? user.setUserName(product.getProductName());
? ? ? ? user.setRealName(product.getProductName());
? ? ? ? String msg = userFeignClient.saveUser(user);
? ? ? ? //由于開啟了服務(wù)調(diào)用降級(jí),所以需要統(tǒng)一返回錯(cuò)誤碼,根據(jù)錯(cuò)誤碼主動(dòng)拋出異常,讓seata回滾事務(wù)
? ? ? ? if(msg.equals("新增用戶失敗")){
? ? ? ? ? ? ?int i = 1/0;
? ? ? ? }
? ? }
?}

UserFeignClient代碼

@FeignClient(name="service-user",fallbackFactory =UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
? ? @GetMapping("/user/getUserById")
? ? @ResponseBody
? ? String getUserById(@RequestParam("id") Integer id);

? ? @GetMapping("/user/getUserByIdWithPathVariable/{id}")
? ? @ResponseBody
? ? String getUserByIdWithPathVariable(@PathVariable("id") Integer id);

? ? @PostMapping("/user/saveUser")
? ? @ResponseBody
? ? String saveUser(@RequestBody User user );

}

User服務(wù) UserController代碼

@RestController
@Slf4j
@RefreshScope
public class UserController {
? ? @Autowired
? ? UserService userService;

? ? @PostMapping("/user/saveUser")
? ? @ResponseBody
? ? public String saveUser(@RequestBody User user) {
? ? ? ? userService.saveUser(user.getUserName(),user.getRealName(),user.getAge());

// ? ? ? ?if(user.getAge()>5){
? ? ? ? ? ? int i = 1/0;
// ? ? ? ?}
? ? ? ? return "sucess";
? ? }
}

UserServiceImpl代碼

@Service
public class UserServiceImpl implements UserService {?? ?
? ? @Resource
? ? UserDao userDao;

? ? @Override
? ? public void saveUser(String userName, String realName, Integer age) {
? ? ? ? userDao.insertUser(userName,realName,age);
? ? }

}

到此這篇關(guān)于springboot整合shardingsphere和seata實(shí)現(xiàn)分布式事務(wù)的實(shí)踐的文章就介紹到這了,更多相關(guān)springboot 分布式事務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解spring cloud config整合gitlab搭建分布式的配置中心

    詳解spring cloud config整合gitlab搭建分布式的配置中心

    這篇文章主要介紹了詳解spring cloud config整合gitlab搭建分布式的配置中心,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)實(shí)例

    JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)實(shí)例

    這篇文章主要給大家介紹了關(guān)于JAVA8獲取list集合中重復(fù)的元素與獲取去重?cái)?shù)據(jù)的相關(guān)資料,在實(shí)際開發(fā)中經(jīng)常會(huì)遇到需要找出(刪除)一個(gè)list中某些元素的屬性相同的元素,需要的朋友可以參考下
    2023-07-07
  • Spring Boot中進(jìn)行 文件上傳和 文件下載功能實(shí)現(xiàn)

    Spring Boot中進(jìn)行 文件上傳和 文件下載功能實(shí)現(xiàn)

    開發(fā)Wb應(yīng)用時(shí),文件上傳是很常見(jiàn)的一個(gè)需求,瀏覽器 通過(guò) 表單形式 將 文件 以 流的形式傳遞 給 服務(wù)器,服務(wù)器再對(duì)上傳的數(shù)據(jù)解析處理,下面將通過(guò)一個(gè)案例講解使用 SpringBoot 實(shí)現(xiàn) 文件上傳,感興趣的朋友一起看看吧
    2024-07-07
  • Java Socket+mysql實(shí)現(xiàn)簡(jiǎn)易文件上傳器的代碼

    Java Socket+mysql實(shí)現(xiàn)簡(jiǎn)易文件上傳器的代碼

    最近在做一個(gè)小項(xiàng)目,項(xiàng)目主要需求是實(shí)現(xiàn)一個(gè)文件上傳器,通過(guò)客戶端的登陸,把本地文件上傳到服務(wù)器的數(shù)據(jù)庫(kù)(本地的)。下面通過(guò)本文給大家分享下實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧
    2016-10-10
  • tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例

    tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例

    這篇文章主要為大家介紹了tio-boot整合hotswap-classloader實(shí)現(xiàn)熱加載方法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java8新特性-Lambda表達(dá)式詳解

    Java8新特性-Lambda表達(dá)式詳解

    Java 8 (又稱為 jdk 1.8) 是 Java 語(yǔ)言開發(fā)的一個(gè)主要版本。Lambda 表達(dá)式,也可稱為閉包,它是推動(dòng) Java 8 發(fā)布的最重要新特性。本文通過(guò)詳細(xì)的代碼示例介紹了Java8新特性感興趣的朋友可以參考一下
    2023-04-04
  • Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例

    Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例

    這篇文章主要介紹了Java使用設(shè)計(jì)模式中迭代器模式構(gòu)建項(xiàng)目的代碼結(jié)構(gòu)示例,迭代器模式能夠?qū)υL問(wèn)者隱藏對(duì)象的內(nèi)部細(xì)節(jié),需要的朋友可以參考下
    2016-05-05
  • 使用jpa之動(dòng)態(tài)插入與修改(重寫save)

    使用jpa之動(dòng)態(tài)插入與修改(重寫save)

    這篇文章主要介紹了使用jpa之動(dòng)態(tài)插入與修改(重寫save),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring Boot 中的 SockJS原理及使用方法

    Spring Boot 中的 SockJS原理及使用方法

    SockJS 的主要作用是提供一種 WebSocket 的兼容性解決方案,使得不支持 WebSocket 的瀏覽器也可以使用 WebSocket,本文介紹了Spring Boot中的SockJS,包括SockJS的原理,使用方法和示例代碼,感興趣的朋友跟隨小編一起看看吧
    2023-07-07
  • Java 認(rèn)識(shí)異常并掌握使用

    Java 認(rèn)識(shí)異常并掌握使用

    所謂異常是指程序在運(yùn)行時(shí)出現(xiàn)錯(cuò)誤時(shí)提示調(diào)用者的機(jī)制,異常的種類有很多,不同種類的異常有不同的含義,也有不同的處理方式,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-09-09

最新評(píng)論