博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloudAlibaba--Seata简单案例(二)
阅读量:2444 次
发布时间:2019-05-10

本文共 9149 字,大约阅读时间需要 30 分钟。

新建库存Storage-Module

  1. seata-storage-service2002

  2. 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
  1. application.yml
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
  1. file.conf
    与之前一致
  2. registry.conf
    与之前一致
  3. domain
@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); }}
  1. Dao接口及实现
@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}
  1. Service接口及实现
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中扣减库存结束"); }}
  1. Controller
@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,"扣减库存成功!"); }}
  1. Config配置
    与之前一致。
  2. 主启动类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)@EnableDiscoveryClient@EnableFeignClientspublic class SeataStorageServiceApplication2002{
public static void main(String[] args) {
SpringApplication.run(SeataStorageServiceApplication2002.class, args); }}

新建账户Account-module

  1. seata-account-service2003
  2. 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
  1. application.yml
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
  1. file.conf
    与之前一致
  2. registry.conf
    与之前一致
  3. domain
@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); }}
  1. Dao接口及实现
@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};
  1. Service接口及实现
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中扣减账户余额结束"); }}
  1. Controller
@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,"扣减账户余额成功!"); }}
  1. Config配置
    与之前一致。
  2. 主启动类
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)@EnableDiscoveryClient@EnableFeignClientspublic class SeataAccountMainApp2003{
public static void main(String[] args) {
SpringApplication.run(SeataAccountMainApp2003.class, args); }}

转载地址:http://zvpqb.baihongyu.com/

你可能感兴趣的文章
outlook 加载配置项_如何禁用Outlook加载项进行故障排除
查看>>
如何导出或删除Outlook.com搜索历史记录
查看>>
dd-wrt固件_如何使用DD-WRT优先安排网络流量
查看>>
如何将您的计算机变成带有病态胡须的增压TiVo
查看>>
如何在Facebook Messenger中启用暗模式
查看>>
如何远程锁定或擦除iOS 5设备
查看>>
如何重命名您的AirPods
查看>>
powerpoint预览_如何添加,删除和重新排列PowerPoint幻灯片
查看>>
您的Mac正在High Sierra中跟踪您的位置,这就是原因(以及如何禁用它)
查看>>
在Windows 7下最大限度地发挥多显示器的魔力
查看>>
如何使Ubuntu中的Gnome面板完全透明
查看>>
电子修补程序入门:购物清单
查看>>
chrome插件 备份书签_如何在本地备份和还原您的Chrome书签
查看>>
在Firefox的“关于”页面上找到隐藏功能和复活节彩蛋
查看>>
word中将空格替换为_如何在Word 2010中将英寸更改为厘米
查看>>
如何在Google文档中创建连字符,连字符和Em连字符
查看>>
如何为Windows Home Server设置电子邮件通知
查看>>
spark fold_每日新闻摘要:三星Galaxy Fold将于9月发布
查看>>
gpt分区 添加vhd引导_如何在不进行重新分区的情况下双重引导Windows 7和8(使用VHD)...
查看>>
如何在iPhone上将GIF设置为动态壁纸
查看>>