栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

初始Seata(四)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

初始Seata(四)

目录

1.背景

2.账户服务

1)pom.xml

2)配置文件

3. 测试

1)正常场景

2)异常场景


1.背景

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

2.账户服务

目录结构

java

com.seata.account

configcontrollerdaoentityserviceresources

mapper(package)application.ymlfile.confregistry.conf

1)pom.xml

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

2)配置文件

*** file.conf 和 registry.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



 

    
        
        
        
        
        
    
    
        update account
        set used = used + #{money}, residue = balance - #{money}
        where user_id=#{userId}
    

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注册,各位看官可以试试看是怎样的结果。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/763416.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号