1、业务需求

下订单->减库存->扣余额->改(订单)状态

2、新建订单Order-Moudle

1、seata-order-service2001

2、pom

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>cloud2021</artifactId>
  7. <groupId>com.tfjy.springcloud</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>seata-order-service2001</artifactId>
  12. <dependencies>
  13. <!--nacos-->
  14. <dependency>
  15. <groupId>com.alibaba.cloud</groupId>
  16. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  17. </dependency>
  18. <!--seata-->
  19. <dependency>
  20. <groupId>com.alibaba.cloud</groupId>
  21. <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
  22. <exclusions>
  23. <exclusion>
  24. <artifactId>seata-all</artifactId>
  25. <groupId>io.seata</groupId>
  26. </exclusion>
  27. </exclusions>
  28. </dependency>
  29. <dependency>
  30. <groupId>io.seata</groupId>
  31. <artifactId>seata-all</artifactId>
  32. <version>0.9.0</version>
  33. </dependency>
  34. <!--feign-->
  35. <dependency>
  36. <groupId>org.springframework.cloud</groupId>
  37. <artifactId>spring-cloud-starter-openfeign</artifactId>
  38. </dependency>
  39. <!--web-actuator-->
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-web</artifactId>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-actuator</artifactId>
  47. </dependency>
  48. <!--mysql-druid-->
  49. <dependency>
  50. <groupId>mysql</groupId>
  51. <artifactId>mysql-connector-java</artifactId>
  52. <version>5.1.37</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.alibaba</groupId>
  56. <artifactId>druid-spring-boot-starter</artifactId>
  57. <version>1.1.10</version>
  58. </dependency>
  59. <dependency>
  60. <groupId>org.mybatis.spring.boot</groupId>
  61. <artifactId>mybatis-spring-boot-starter</artifactId>
  62. <version>2.0.0</version>
  63. </dependency>
  64. <dependency>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-starter-test</artifactId>
  67. <scope>test</scope>
  68. </dependency>
  69. <dependency>
  70. <groupId>org.projectlombok</groupId>
  71. <artifactId>lombok</artifactId>
  72. <optional>true</optional>
  73. </dependency>
  74. </dependencies>
  75. </project>

3、YML

  1. server:
  2. port: 2001
  3. spring:
  4. application:
  5. name: seata-order-service
  6. cloud:
  7. alibaba:
  8. seata:
  9. #自定义事务组名称需要与seata-server中的对应
  10. tx-service-group: fsp_tx_group
  11. nacos:
  12. discovery:
  13. server-addr: localhost:8848
  14. datasource:
  15. driver-class-name: com.mysql.jdbc.Driver
  16. url: jdbc:mysql://localhost:3306/seata_order
  17. username: root
  18. password: 123456
  19. feign:
  20. hystrix:
  21. enabled: false
  22. logging:
  23. level:
  24. io:
  25. seata: info
  26. mybatis:
  27. mapperLocations: classpath:mapper/*.xml

4、file.conf

image.png

5、registry.conf

6、domain

commonResult

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class CommonResult<T> {
  5. private Integer code;
  6. private String message;
  7. private T data;
  8. public CommonResult(Integer code, String message)
  9. {
  10. this(code,message,null);
  11. }
  12. }

order

  1. @Data
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. public class Order {
  5. private Long id;
  6. private Long userId;
  7. private Long productId;
  8. private Integer count;
  9. private BigDecimal money;
  10. private Integer status; //订单状态:0:创建中;1:已完结
  11. }


7、Dao接口及实现

  1. @Mapper
  2. public interface OrderDao {
  3. //1、新建订单
  4. void create(Order order);
  5. //2、修改订单状态,从0改为1
  6. void update(@Param("userId") Long userId,@Param("status") Integer status);
  7. }

8、Service接口及实现

AccountService

  1. @FeignClient(value = "seata-account-service")
  2. public interface AccountService {
  3. @PostMapping(value = "/account/decrease")
  4. CommonResult decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money );
  5. }

OrderService

  1. public interface OrderService {
  2. void create(Order order);
  3. }

StorageService

  1. @FeignClient(value = "seata-storage-service")
  2. public interface StorageService {
  3. @PostMapping(value = "/storage/decrease")
  4. CommonResult decrease(@RequestParam("productId") Long productId,@RequestParam("count") Integer count );
  5. }

OrderServicelmpl

  1. @Service
  2. @Slf4j
  3. public class OrderServicelmpl implements OrderService {
  4. @Resource
  5. private OrderDao orderDao;
  6. @Resource
  7. private StorageService storageService;
  8. @Resource
  9. private AccountService accountService;
  10. @Override
  11. public void create(Order order) {
  12. log.info("-------->开始新建订单");
  13. //1、新建订单
  14. orderDao.create(order);
  15. log.info("----->订单微服务开始调用库存,做扣减");
  16. //2、扣减库存
  17. storageService.decrease(order.getProductId(),order.getCount());
  18. log.info("----->订单微服务开始调用库存,做扣减end");
  19. log.info("----->订单微服务开始调用账户,做扣减money");
  20. //3、扣减账户
  21. accountService.decrease(order.getUserId(),order.getMoney());
  22. log.info("----->订单微服务开始调用库存,做扣减end");
  23. //4 修改订单状态 从0 到1,1代表已经完成
  24. log.info("---->修改订单状态开始");
  25. orderDao.update(order.getUserId(),0);
  26. log.info("---->修改订单状态结束");
  27. log.info("---->下订单结束了,哈哈哈");
  28. }
  29. }

9、Controller

  1. @RestController
  2. public class OrderController {
  3. @Resource
  4. private OrderService orderService;
  5. @GetMapping("/order/create")
  6. public CommonResult create(Order order){
  7. orderService.create(order);
  8. return new CommonResult(200,"订单创建成功");
  9. }
  10. }

10、Config配置

DataSourceProxyConfig

  1. @Configuration
  2. public class DataSourceProxyConfig {
  3. @Value("${mybatis.mapperLocations}")
  4. private String mapperLocations;
  5. @Bean
  6. @ConfigurationProperties(prefix = "spring.datasource")
  7. public DataSource druidDataSource(){
  8. return new DruidDataSource();
  9. }
  10. @Bean
  11. public DataSourceProxy dataSourceProxy(DataSource dataSource) {
  12. return new DataSourceProxy(dataSource);
  13. }
  14. @Bean
  15. public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
  16. SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
  17. sqlSessionFactoryBean.setDataSource(dataSourceProxy);
  18. sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperLocations));
  19. sqlSessionFactoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
  20. return sqlSessionFactoryBean.getObject();
  21. }
  22. }

MyBatisConfig

  1. @Configuration
  2. @MapperScan({"com.atguigu.springcloud.alibaba.dao"})
  3. public class MyBatisConfig {
  4. }

11、主启动

image.png

3、新建库存Storage-Moudle

4、新建账户Account-Moudle