最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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);
? ? ? ? //由于開(kāi)啟了服務(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)文章

最新評(píng)論

科技| 泾源县| 平阳县| 宝鸡市| 基隆市| 牙克石市| 客服| 缙云县| 秀山| 天台县| 辰溪县| 天津市| 天峨县| 伊宁市| 银川市| 永吉县| 定陶县| 德州市| 天柱县| 宜宾市| 三门峡市| 通江县| 华阴市| 加查县| 弋阳县| 海林市| 文昌市| 平遥县| 遂昌县| 榆林市| 乾安县| 奉节县| 威海市| 柳河县| 沾益县| 花莲市| 平遥县| 安福县| 青岛市| 荥阳市| 河北省|