初始Seata(四)

目录

1.背景

2.账户服务

1)pom.xml

2)配置文件

3. 测试

1)正常场景

2)异常场景


1.背景

书接上回初始Seata(三),继续账户服务模块的代码。

2.账户服务

目录结构

  • java
    • com.seata.account
      • config
      • controller
      • dao
      • entity
      • service
  • resources
    • mapper(package)
    • application.yml
    • file.conf
    • registry.conf

1)pom.xml

这部分与上回中的服务是一样的,不再赘述。

2)配置文件

*** file.confregistry.conf与上文一致,在此不再赘述

*** application.yml

server:
  port: 8804
spring:
  application:
    name: seata-account
  cloud:
    alibaba:
     seata:
       tx-service-group: my_test_tx_group
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://locahost:3306/seata_account?serverTimezone=GMT%2B8&characterEncoding=utf8
 
eureka:
  client:
    service-url:
      dafaultZone: http://localhost:8801/eureka/
  instance:
    hostname: localhost
    prefer-ip-address: true
logging:
  level:
    io:
      seata: info
mybatis:
  mapperLocations: classpath:mapper/*.xml

*** 启动类

@EnableDiscoveryClient(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClients
public class AccountAppMain8804
{
    public static void main(String[] args){
        SpringApplication.run(AccountAppMain8804.class, args);
    }
}

*** config

**** DataSourceProxyConfig.java

这个文件与前文相同,不再赘述

**** MyBatisConfig.java

@Configuration
@MapperScan({"com.seata.account.dao"})
public class MyBatisConfig{
}

*** entity

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
    private Long id;
    private Long userId;
    private Integer total;
    private Integer used;
    private Integer balance;
}

*** controller

@RequestMapping("/account/")
public class AccountController {
    @Resource
    private AccountService accountService;
 
    @GetMapping("decrease")
    public String decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money){
        accountService.decrease(userId, money);
        return "账户余额扣减成功"
    }
}

*** service & serviceImpl

**** service

public interface AccountService {
    void decrease(Long userid, BigDecimal money);
}
@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao accountDao;
 
    public void decrease(Long userId, BigDecimal money) {
        accountDao.decrease(userId, money);
    }
}

*** dao

@Mapper
public interface AccountDao {
    void decrease(@Param("userId") Long userId, @Param("money") BigDecimal money);
}

*** resources

**** mapper.AccountMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="com.seata.account.dao.AccountDao">
    <resultMap id="BaseResultMap" type="com.seata.account.entity.Account">
        <id column="id" property="id" jdbcType="BIGINT">
        <id column="user_id" property="userId" jdbcType="BIGINT">
        <id column="total" property="total" jdbcType="DECIMAL">
        <id column="used" property="used" jdbcType="DECIMAL">
        <id column="balance" property="balance" jdbcType="DECIMAL">
    </resultMap>
    <update id="decrease">
        update account
        set used = used + #{money}, residue = balance - #{money}
        where user_id=#{userId}
    </update>
</mapper>

3. 测试

1)正常场景

启动注册中心Eureka,然后分别启动三个业务服务。调用创建订单接口,库存和账户正常扣除,订单状态为已支付

2)异常场景

A. 启动注册中心Eureka,订单和库存服务

B. 在账户服务AccountServiceImpl中增加超时,然后启动账户服务

@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao accountDao;
 
    public void decrease(Long userId, BigDecimal money) {
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        accountDao.decrease(userId, money);
    }
}

C.调用订单接口,由于账户服务的超时,该订单并未引起库存减少、账户余额减少、订单状态也是未支付状态。

补充:如果没有订单服务中@GlobalTransactional注册,各位看官可以试试看是怎样的结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值