增加 seata 示例

This commit is contained in:
Administrator
2020-11-02 15:33:14 +08:00
parent fb945b16d9
commit 002b5a6281
41 changed files with 963 additions and 6 deletions

View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
<artifactId>seata-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.chinaunicom.mall.ebtp.cloud</groupId>
<artifactId>order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order-service</name>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,18 @@
package com.chinaunicom.mall.ebtp.cloud.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Import;
import com.chinaunicom.mall.ebtp.cloud.eureka.starter.EurekaStarterConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients
@Import(EurekaStarterConfiguration.class)
public class OrderSeataApplication {
public static void main(String[] args) {
SpringApplication.run(OrderSeataApplication.class, args);
}
}

View File

@ -0,0 +1,41 @@
package com.chinaunicom.mall.ebtp.cloud.order.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import io.seata.rm.datasource.DataSourceProxy;
@Configuration
public class SeataDatasourceConfiguration {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
@Primary
@Bean("dataSource")
public DataSourceProxy dataSource(DataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}
@Bean
@ConfigurationProperties(prefix = "mybatis")
public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) {
// 这里用 MybatisSqlSessionFactoryBean 代替了 SqlSessionFactoryBean否则 MyBatisPlus 不会生效
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
mybatisSqlSessionFactoryBean.setDataSource(dataSource);
return mybatisSqlSessionFactoryBean;
}
}

View File

@ -0,0 +1,34 @@
package com.chinaunicom.mall.ebtp.cloud.order.controller;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.chinaunicom.mall.ebtp.cloud.order.entity.Order;
import com.chinaunicom.mall.ebtp.cloud.order.service.OrderService;
@RestController
@RequestMapping("order")
public class OrderController {
private @Autowired OrderService orderService;
@GetMapping("create")
public String create(@RequestBody Order order) {
orderService.create(order);
return "Create order success";
}
@RequestMapping("update")
String update(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money,
@RequestParam("status") Integer status) {
orderService.update(userId, money, status);
return "订单状态修改成功";
}
}

View File

@ -0,0 +1,22 @@
package com.chinaunicom.mall.ebtp.cloud.order.entity;
import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
@TableName("order_tbl")
public class Order {
private Long id;
private Long userId;
private Long productId;
private Integer count;
private BigDecimal money;
private Integer status;
}

View File

@ -0,0 +1,15 @@
package com.chinaunicom.mall.ebtp.cloud.order.feign;
import java.math.BigDecimal;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("account-service")
public interface AccountApi {
@RequestMapping("/account/decrease")
String decrease(@RequestParam("userId") Long userId, @RequestParam("money") BigDecimal money);
}

View File

@ -0,0 +1,13 @@
package com.chinaunicom.mall.ebtp.cloud.order.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("storage-service")
public interface StorageApi {
@RequestMapping("/storage/decrease")
String decrease(@RequestParam("productId") Long productId, @RequestParam("count") Integer count);
}

View File

@ -0,0 +1,18 @@
package com.chinaunicom.mall.ebtp.cloud.order.mapper;
import java.math.BigDecimal;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chinaunicom.mall.ebtp.cloud.order.entity.Order;
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
@Update("UPDATE `order_tbl` SET money = money - #{money}, status = 1 WHERE user_id = #{userId} AND status = #{status};")
void update(@Param("userId") Long userId, @Param("money") BigDecimal money, @Param("status") Integer status);
}

View File

@ -0,0 +1,13 @@
package com.chinaunicom.mall.ebtp.cloud.order.service;
import java.math.BigDecimal;
import com.chinaunicom.mall.ebtp.cloud.order.entity.Order;
public interface OrderService {
void create(Order order);
void update(Long userId, BigDecimal money, Integer status);
}

View File

@ -0,0 +1,49 @@
package com.chinaunicom.mall.ebtp.cloud.order.service.impl;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.chinaunicom.mall.ebtp.cloud.order.entity.Order;
import com.chinaunicom.mall.ebtp.cloud.order.feign.AccountApi;
import com.chinaunicom.mall.ebtp.cloud.order.feign.StorageApi;
import com.chinaunicom.mall.ebtp.cloud.order.mapper.OrderMapper;
import com.chinaunicom.mall.ebtp.cloud.order.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
private @Autowired OrderMapper orderMapper;
private @Autowired StorageApi storageApi;
private @Autowired AccountApi accountApi;
@Override
@GlobalTransactional(name = "fsp-create-order", rollbackFor = Exception.class)
public void create(Order order) {
log.info("-------->开始" + order);
orderMapper.insert(order);
// 扣减库存
storageApi.decrease(order.getProductId(), order.getCount());
log.info("-----------> 扣减账户开始");
accountApi.decrease(order.getUserId(), order.getMoney());
log.info("-----------> 扣减账户结束");
log.info("--------> 交易结束");
}
@Override
public void update(Long userId, BigDecimal money, Integer status) {
log.info("修改订单状态入参为userId={},money={},status={}", userId, money, status);
orderMapper.update(userId, money, status);
}
}

View File

@ -0,0 +1,18 @@
logging:
level:
com.chinaunicom.mall.ebtp.cloud: info
server:
port: 8673
spring:
cloud:
alibaba:
seata:
tx-service-group: test_tx_group
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://125.32.114.204:13306/ebtp-cloud?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: mall3-ebtp-dev
password: mall3-ebtp-dev

View File

@ -0,0 +1,5 @@
spring:
profiles:
active: dev
application:
name: order-service

View File

@ -0,0 +1,23 @@
package com.chinaunicom.mall.ebtp.cloud.order;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.chinaunicom.mall.ebtp.cloud.order.entity.Order;
import com.chinaunicom.mall.ebtp.cloud.order.mapper.OrderMapper;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
private @Autowired OrderMapper orderMapper;
// @Test
public void testInsert() {
Order order = new Order();
orderMapper.insert(order);
}
}