forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YunaiV
committed
Apr 3, 2020
1 parent
9f807d2
commit d6f1ca6
Showing
28 changed files
with
853 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...2/lab-52-seata-at-httpclient-demo/lab-52-seata-at-httpclient-demo-account-service/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-52-seata-at-httpclient-demo-account-service</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 Spring MVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对数据库连接池的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
<dependency> <!-- 本示例,我们使用 MySQL --> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.48</version> | ||
</dependency> | ||
|
||
<!-- 实现对 MyBatis 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.1.2</version> | ||
</dependency> | ||
|
||
<!-- 实现对 Seata 的自动化配置 --> | ||
<dependency> | ||
<groupId>io.seata</groupId> | ||
<artifactId>seata-spring-boot-starter</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
<!-- 实现 Seata 对 HttpClient 的集成支持 --> | ||
<dependency> | ||
<groupId>io.seata</groupId> | ||
<artifactId>seata-http</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
|
||
<!-- Apache HttpClient 依赖 --> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.8</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...e/src/main/java/cn/iocoder/springboot/lab52/accountservice/AccountServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springboot.lab52.accountservice; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class AccountServiceApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AccountServiceApplication.class, args); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...rc/main/java/cn/iocoder/springboot/lab52/accountservice/controller/AccountController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cn.iocoder.springboot.lab52.accountservice.controller; | ||
|
||
import cn.iocoder.springboot.lab52.accountservice.dto.AccountReduceBalanceDTO; | ||
import cn.iocoder.springboot.lab52.accountservice.service.AccountService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/account") | ||
public class AccountController { | ||
|
||
private Logger logger = LoggerFactory.getLogger(AccountController.class); | ||
|
||
@Autowired | ||
private AccountService accountService; | ||
|
||
@PostMapping("/reduce-balance") | ||
public Boolean reduceBalance(@RequestBody AccountReduceBalanceDTO accountReduceBalanceDTO) { | ||
logger.info("[reduceBalance] 收到减少余额请求, 用户:{}, 金额:{}", accountReduceBalanceDTO.getUserId(), | ||
accountReduceBalanceDTO.getPrice()); | ||
try { | ||
accountService.reduceBalance(accountReduceBalanceDTO.getUserId(), accountReduceBalanceDTO.getPrice()); | ||
// 正常扣除余额,返回 true | ||
return true; | ||
} catch (Exception e) { | ||
// 失败扣除余额,返回 false | ||
return false; | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...ount-service/src/main/java/cn/iocoder/springboot/lab52/accountservice/dao/AccountDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cn.iocoder.springboot.lab52.accountservice.dao; | ||
|
||
|
||
import org.apache.ibatis.annotations.Mapper; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.Select; | ||
import org.apache.ibatis.annotations.Update; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Mapper | ||
@Repository | ||
public interface AccountDao { | ||
|
||
/** | ||
* 获取账户余额 | ||
* | ||
* @param userId 用户 ID | ||
* @return 账户余额 | ||
*/ | ||
@Select("SELECT balance FROM account WHERE id = #{userId}") | ||
Integer getBalance(@Param("userId") Long userId); | ||
|
||
/** | ||
* 扣减余额 | ||
* | ||
* @param price 需要扣减的数目 | ||
* @return 影响记录行数 | ||
*/ | ||
@Update("UPDATE account SET balance = balance - #{price} WHERE id = 1 AND balance >= ${price}") | ||
int reduceBalance(@Param("price") Integer price); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...src/main/java/cn/iocoder/springboot/lab52/accountservice/dto/AccountReduceBalanceDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package cn.iocoder.springboot.lab52.accountservice.dto; | ||
|
||
/** | ||
* 账户减少余额 DTO | ||
*/ | ||
public class AccountReduceBalanceDTO { | ||
|
||
/** | ||
* 用户编号 | ||
*/ | ||
private Long userId; | ||
|
||
/** | ||
* 扣减金额 | ||
*/ | ||
private Integer price; | ||
|
||
public Long getUserId() { | ||
return userId; | ||
} | ||
|
||
public AccountReduceBalanceDTO setUserId(Long userId) { | ||
this.userId = userId; | ||
return this; | ||
} | ||
|
||
public Integer getPrice() { | ||
return price; | ||
} | ||
|
||
public AccountReduceBalanceDTO setPrice(Integer price) { | ||
this.price = price; | ||
return this; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...vice/src/main/java/cn/iocoder/springboot/lab52/accountservice/service/AccountService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cn.iocoder.springboot.lab52.accountservice.service; | ||
|
||
/** | ||
* 账户 Service | ||
*/ | ||
public interface AccountService { | ||
|
||
/** | ||
* 扣除余额 | ||
* | ||
* @param userId 用户编号 | ||
* @param price 扣减金额 | ||
* @throws Exception 失败时抛出异常 | ||
*/ | ||
void reduceBalance(Long userId, Integer price) throws Exception; | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
.../src/main/java/cn/iocoder/springboot/lab52/accountservice/service/AccountServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cn.iocoder.springboot.lab52.accountservice.service; | ||
|
||
import cn.iocoder.springboot.lab52.accountservice.dao.AccountDao; | ||
import io.seata.core.context.RootContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
public class AccountServiceImpl implements AccountService { | ||
|
||
private Logger logger = LoggerFactory.getLogger(getClass()); | ||
|
||
@Autowired | ||
private AccountDao accountDao; | ||
|
||
@Override | ||
@Transactional // 开启新事物 | ||
public void reduceBalance(Long userId, Integer price) throws Exception { | ||
logger.info("[reduceBalance] 当前 XID: {}", RootContext.getXID()); | ||
|
||
// 检查余额 | ||
checkBalance(userId, price); | ||
|
||
logger.info("[reduceBalance] 开始扣减用户 {} 余额", userId); | ||
// 扣除余额 | ||
int updateCount = accountDao.reduceBalance(price); | ||
// 扣除成功 | ||
if (updateCount == 0) { | ||
logger.warn("[reduceBalance] 扣除用户 {} 余额失败", userId); | ||
throw new Exception("余额不足"); | ||
} | ||
logger.info("[reduceBalance] 扣除用户 {} 余额成功", userId); | ||
} | ||
|
||
private void checkBalance(Long userId, Integer price) throws Exception { | ||
logger.info("[checkBalance] 检查用户 {} 余额", userId); | ||
Integer balance = accountDao.getBalance(userId); | ||
if (balance < price) { | ||
logger.warn("[checkBalance] 用户 {} 余额不足,当前余额:{}", userId, balance); | ||
throw new Exception("余额不足"); | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...-demo/lab-52-seata-at-httpclient-demo-account-service/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
server: | ||
port: 8083 | ||
|
||
spring: | ||
application: | ||
name: account-service | ||
|
||
datasource: | ||
url: jdbc:mysql://127.0.0.1:3306/seata_account?useSSL=false&useUnicode=true&characterEncoding=UTF-8 | ||
driver-class-name: com.mysql.jdbc.Driver | ||
username: root | ||
password: | ||
|
||
# Seata 配置项,对应 SeataProperties 类 | ||
seata: | ||
application-id: ${spring.application.name} # Seata 应用编号,默认为 ${spring.application.name} | ||
tx-service-group: ${spring.application.name}-group # Seata 事务组编号,用于 TC 集群名 | ||
# 服务配置项,对应 ServiceProperties 类 | ||
service: | ||
# 虚拟组和分组的映射 | ||
vgroup-mapping: | ||
account-service-group: default | ||
# 分组和 Seata 服务的映射 | ||
grouplist: | ||
default: 127.0.0.1:8091 |
61 changes: 61 additions & 0 deletions
61
lab-52/lab-52-seata-at-httpclient-demo/lab-52-seata-at-httpclient-demo-order-service/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.2.2.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-52-seata-at-httpclient-demo-order-service</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 Spring MVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- 实现对数据库连接池的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
<dependency> <!-- 本示例,我们使用 MySQL --> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.48</version> | ||
</dependency> | ||
|
||
<!-- 实现对 MyBatis 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>2.1.2</version> | ||
</dependency> | ||
|
||
<!-- 实现对 Seata 的自动化配置 --> | ||
<dependency> | ||
<groupId>io.seata</groupId> | ||
<artifactId>seata-spring-boot-starter</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
<!-- 实现 Seata 对 HttpClient 的集成支持 --> | ||
<dependency> | ||
<groupId>io.seata</groupId> | ||
<artifactId>seata-http</artifactId> | ||
<version>1.1.0</version> | ||
</dependency> | ||
|
||
<!-- Apache HttpClient 依赖 --> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.8</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...rvice/src/main/java/cn/iocoder/springboot/lab52/orderservice/OrderServiceApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package cn.iocoder.springboot.lab52.orderservice; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class OrderServiceApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(OrderServiceApplication.class, args); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...ce/src/main/java/cn/iocoder/springboot/lab52/orderservice/controller/OrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cn.iocoder.springboot.lab52.orderservice.controller; | ||
|
||
import cn.iocoder.springboot.lab52.orderservice.service.OrderService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/order") | ||
public class OrderController { | ||
|
||
private Logger logger = LoggerFactory.getLogger(OrderController.class); | ||
|
||
@Autowired | ||
private OrderService orderService; | ||
|
||
@PostMapping("/create") | ||
public Integer createOrder(@RequestParam("userId") Long userId, | ||
@RequestParam("productId") Long productId, | ||
@RequestParam("price") Integer price) throws Exception { | ||
logger.info("[createOrder] 收到下单请求,用户:{}, 商品:{}, 价格:{}", userId, productId, price); | ||
return orderService.createOrder(userId, productId, price); | ||
} | ||
|
||
} |
Oops, something went wrong.