本文共 9149 字,大约阅读时间需要 30 分钟。
seata-storage-service2002
pom.xml
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-seata seata-all io.seata io.seata seata-all 0.9.0 org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java 5.1.37 com.alibaba druid-spring-boot-starter 1.1.10 org.projectlombok lombok true
server: port: 2002spring: application: name: seata-storage-service cloud: alibaba: seata: tx-service-group: fsp_tx_group nacos: discovery: server-addr: localhost:8848 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/seata_storage username: root password: 123456logging: level: io: seata: infomybatis: mapperLocations: classpath:mapper/*.xml
@Datapublic class Storage { private Long id; /** * 产品id */ private Long productId; /** * 总库存 */ private Integer total; /** * 已用库存 */ private Integer used; /** * 剩余库存 */ private Integer residue;}
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult{ private Integer code; private String message; private T data; public CommonResult(Integer code, String message) { this(code,message,null); }}
@Mapperpublic interface StorageDao { //扣减库存 void decrease(@Param("productId") Long productId, @Param("count") Integer count);}
UPDATE t_storage SET used = used + #{count},residue = residue - #{count} WHERE product_id = #{productId}
public interface StorageService { /** * 扣减库存 */ void decrease(Long productId, Integer count);}
@Servicepublic class StorageServiceImpl implements StorageService { private static final Logger LOGGER = LoggerFactory.getLogger(StorageServiceImpl.class); @Resource private StorageDao storageDao; /** * 扣减库存 */ @Override public void decrease(Long productId, Integer count) { LOGGER.info("------->storage-service中扣减库存开始"); storageDao.decrease(productId,count); LOGGER.info("------->storage-service中扣减库存结束"); }}
@RestControllerpublic class StorageController { @Autowired private StorageService storageService; /** * 扣减库存 */ @RequestMapping("/storage/decrease") public CommonResult decrease(Long productId, Integer count) { storageService.decrease(productId, count); return new CommonResult(200,"扣减库存成功!"); }}
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)@EnableDiscoveryClient@EnableFeignClientspublic class SeataStorageServiceApplication2002{ public static void main(String[] args) { SpringApplication.run(SeataStorageServiceApplication2002.class, args); }}
com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-seata seata-all io.seata io.seata seata-all 0.9.0 org.springframework.cloud spring-cloud-starter-openfeign org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.0 mysql mysql-connector-java 5.1.37 com.alibaba druid-spring-boot-starter 1.1.10 org.projectlombok lombok true
server: port: 2003spring: application: name: seata-account-service cloud: alibaba: seata: tx-service-group: fsp_tx_group nacos: discovery: server-addr: localhost:8848 datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/seata_account username: root password: 123456feign: hystrix: enabled: falselogging: level: io: seata: infomybatis: mapperLocations: classpath:mapper/*.xml
@Data@AllArgsConstructor@NoArgsConstructorpublic class Account { private Long id; /** * 用户id */ private Long userId; /** * 总额度 */ private BigDecimal total; /** * 已用额度 */ private BigDecimal used; /** * 剩余额度 */ private BigDecimal residue;}
@Data@AllArgsConstructor@NoArgsConstructorpublic class CommonResult{ private Integer code; private String message; private T data; public CommonResult(Integer code, String message) { this(code,message,null); }}
@Mapperpublic interface AccountDao { /** * 扣减账户余额 */ void decrease(@Param("userId") Long userId, @Param("money") BigDecimal money);}
UPDATE t_account SET residue = residue - #{money},used = used + #{money} WHERE user_id = #{userId};
public interface AccountService { /** * 扣减账户余额 * @param userId 用户id * @param money 金额 */ void decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money);}
@Servicepublic class AccountServiceImpl implements AccountService { private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class); @Resource AccountDao accountDao; /** * 扣减账户余额 */ @Override public void decrease(Long userId, BigDecimal money) { LOGGER.info("------->account-service中扣减账户余额开始"); //模拟超时异常,全局事务回滚 //暂停几秒钟线程 //try { TimeUnit.SECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } accountDao.decrease(userId,money); LOGGER.info("------->account-service中扣减账户余额结束"); }}
@RestControllerpublic class AccountController { @Resource AccountService accountService; /** * 扣减账户余额 */ @RequestMapping("/account/decrease") public CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money){ accountService.decrease(userId,money); return new CommonResult(200,"扣减账户余额成功!"); }}
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)@EnableDiscoveryClient@EnableFeignClientspublic class SeataAccountMainApp2003{ public static void main(String[] args) { SpringApplication.run(SeataAccountMainApp2003.class, args); }}
转载地址:http://zvpqb.baihongyu.com/