1 API
接口冪等產生的原因
-
表單重複提交問題
-
RPC
遠程調用的時候,網絡發生延遲導致重試
2 API
接口冪等解決方案
使用 Token(令牌)
:保證令牌的唯一性和零時性
分佈式
session
解決方案:Redis
+Token
處理流程:
數據提交前要向服務的申請 token
,token
放到 redis
或 jvm
內存,token
有效時間,提交後後臺校驗 token
,同時刪除 token
,生成新的 token
返回。
token
特點:要申請,一次有效性,可以限流。
3 項目中解決 API
接口冪等
3.1 環境搭建
1 數據庫
-- ----------------------------
-- Table structure for order_info
-- ----------------------------
DROP TABLE IF EXISTS `order_info`;
CREATE TABLE `order_info` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`orderName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
`orderDes` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of order_info
-- ----------------------------
INSERT INTO `order_info` VALUES (1, '口罩', '防護用品');
2 依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- SpringBoot web 核心組件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<!-- servlet 依賴 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL 標籤庫 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- tomcat 的支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- SpringBoot 外部tomcat支持 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- mysql 數據庫驅動. -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring-boot mybatis依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- SpringBoot 對lombok 支持 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- springboot-aop 技術 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3 配置文件
spring:
datasource:
url: jdbc:mysql://120.78.134.111:3306/springboot?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
redis:
database: 1
host: 120.78.134.111
port: 6379
password:
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 10000
mybatis:
type-aliases-package: com.snow.entity # mybatis 別名掃描
4 啓動項
package com.snow;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@MapperScan("com.snow.mapper")
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
5 實體類
package com.snow.entity;
import lombok.Data;
import lombok.ToString;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.entity
* @Description : Order
* @date :2020/4/16 11:44
*/
@Data
@ToString
public class Order {
private int id;
private String orderName;
private String orderDes;
}
6
OrderMapper
package com.snow.mapper;
import com.snow.entity.Order;
import org.apache.ibatis.annotations.Insert;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.mapper
* @Description : UserMapper
* @date :2020/4/16 11:46
*/
public interface OrderMapper {
@Insert("insert order_info values (null, #{orderName}, #{orderDes})")
public int addOrder(Order order);
}
7
indexPage.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/addOrderPage" method="post">
<input type="hidden" name="token" value="${token}">
<span>訂單名稱</span> <input type="text" name="orderName"><br>
<span>訂單描述</span> <input type="text" name="orderDes"><br>
<input type="submit">
</form>
</body>
</html>
8
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
成功!
</body>
</html>
9
fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
請不要重複提交!
</body>
</html>
3.2 BaseRedisService
封裝 Redis
package com.snow.utils;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.utils
* @Description : Redis服務封裝工具類
* @date :2020/4/16 14:45
*/
@Component
public class BaseRedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setString(String key, Object data, Long timeout) {
if (data instanceof String) {
String value = (String) data;
stringRedisTemplate.opsForValue().set(key, value);
}
if (timeout != null) {
stringRedisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
}
public Object getString(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
public void delKey(String key) {
stringRedisTemplate.delete(key);
}
}
3.3 RedisToken
工具類
package com.snow.utils;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.utils
* @Description : 生成 token 工具類
* @date :2020/4/16 14:47
*/
@Component
public class RedisToken {
@Autowired
private BaseRedisService baseRedisService;
private static final long TOKENTIMEOUT = 60 * 60;
public String getToken() {
// 生成token 規則保證 臨時且唯一 不支持分佈式場景 分佈式全局ID生成規則
String token = "token" + UUID.randomUUID();
// 如何保證token臨時 (緩存)使用redis 實現緩存
baseRedisService.setString(token, token, TOKENTIMEOUT);
return token;
}
// 1.在調用接口之前生成對應的令牌(Token), 存放在Redis
// 2.調用接口的時候,將該令牌放入的請求頭中
// 3.接口獲取對應的令牌,如果能夠獲取該令牌(將當前令牌刪除掉) 就直接執行該訪問的業務邏輯
// 4.接口獲取對應的令牌,如果獲取不到該令牌 直接返回請勿重複提交
public synchronized boolean findToken(String tokenKey) {
// 3.接口獲取對應的令牌,如果能夠獲取該(從redis獲取令牌)令牌(將當前令牌刪除掉) 就直接執行該訪問的業務邏輯
String tokenValue = (String) baseRedisService.getString(tokenKey);
if (StringUtils.isEmpty(tokenValue)) {
return false;
}
// 保證每個接口對應的token 只能訪問一次,保證接口冪等性問題
baseRedisService.delKey(tokenValue);
return true;
}
}
工具類
package com.snow.utils;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.utils
* @Description : token所處位置
* @date :2020/4/16 15:41
*/
public interface ConstantUtils {
static final String EXTAPIHEAD = "head";
static final String EXTAPIFROM = "from";
}
3.4 自定義 Api
冪等註解和切面
package com.snow.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.annotation
* @Description : 解決接口冪等性 支持網絡延遲和表單重複提交
* @date :2020/4/16 14:51
*/
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtApiIdempotent {
String type();
}
package com.snow.aop;
import com.snow.annotation.ExtApiIdempotent;
import com.snow.utils.ConstantUtils;
import com.snow.utils.RedisToken;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.aop
* @Description : `Api` 冪等切面
* @date :2020/4/16 14:52
*/
@Aspect
@Component
public class ExtApiAopIdempotent {
@Autowired
private RedisToken redisToken;
// 1.使用AOP環繞通知攔截所有訪問(controller)
@Pointcut("execution(public * com.snow.controller.*.*(..))")
public void rlAop() {
}
// 前置通知
@Before("rlAop()")
public void before(JoinPoint point) {
}
// 環繞通知
@Around("rlAop()")
public Object doBefore(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
// 2.判斷方法上是否有加ExtApiIdempotent
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
ExtApiIdempotent declaredAnnotation = methodSignature.getMethod().getDeclaredAnnotation(ExtApiIdempotent.class);
// 3.如何方法上有加上ExtApiIdempotent
if (declaredAnnotation != null) {
String type = declaredAnnotation.type();
// 如何使用Token 解決冪等性
// 步驟:
String token = null;
HttpServletRequest request = getRequest();
if (type.equals(ConstantUtils.EXTAPIHEAD)) {
token = request.getHeader("token");
} else {
token = request.getParameter("token");
}
if (StringUtils.isEmpty(token)) {
return "參數錯誤";
}
// 3.接口獲取對應的令牌,如果能夠獲取該(從redis獲取令牌)令牌(將當前令牌刪除掉) 就直接執行該訪問的業務邏輯
boolean isToken = redisToken.findToken(token);
// 4.接口獲取對應的令牌,如果獲取不到該令牌 直接返回請勿重複提交
if (!isToken) {
response("請勿重複提交!");
// 後面方法不在繼續執行
return null;
}
}
// 放行
Object proceed = proceedingJoinPoint.proceed();
return proceed;
}
public HttpServletRequest getRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
return request;
}
public void response(String msg) throws IOException {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = attributes.getResponse();
response.setHeader("Content-type", "text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
try {
writer.println(msg);
} catch (Exception e) {
} finally {
writer.close();
}
}
}
3.5 冪等註解使用
package com.snow.controller;
import com.snow.annotation.ExtApiIdempotent;
import com.snow.entity.Order;
import com.snow.mapper.OrderMapper;
import com.snow.utils.ConstantUtils;
import com.snow.utils.RedisToken;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.controller
* @Description : OrderController
* @date :2020/4/8 22:25
*/
@RestController
public class OrderController {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RedisToken redisToken;
/**
* 從redis中獲取Token
*
* @return
*/
@RequestMapping("/redisToken")
public String RedisToken() {
return redisToken.getToken();
}
/**
* 驗證Token
*
* @param order
* @param request
* @return
*/
@RequestMapping(value = "/addOrderExtApiIdempotent", produces = "application/json; charset=utf-8")
@ExtApiIdempotent(type = ConstantUtils.EXTAPIHEAD)
public String addOrderExtApiIdempotent(@RequestBody Order order, HttpServletRequest request) {
int result = orderMapper.addOrder(order);
return result > 0 ? "添加成功" : "添加失敗" + "";
}
}
3.6 封裝生成 token
註解
package com.snow.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.annotation
* @Description : 執行該請求的時候 需要生成令牌 轉發到頁面進行展示
* @date :2020/4/16 15:45
*/
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtApiToken {
}
3.7 改造 ExtApiAopIdempotent
package com.snow.aop;
import com.snow.annotation.ExtApiIdempotent;
import com.snow.annotation.ExtApiToken;
import com.snow.utils.ConstantUtils;
import com.snow.utils.RedisToken;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.aop
* @Description : `Api` 冪等切面
* @date :2020/4/16 14:52
*/
@Aspect
@Component
public class ExtApiAopIdempotent {
@Autowired
private RedisToken redisToken;
// 1.使用AOP環繞通知攔截所有訪問(controller)
@Pointcut("execution(public * com.snow.controller.*.*(..))")
public void rlAop() {
}
/**
* 前置通知轉發Token參數
*
* @param point
*/
@Before("rlAop()")
public void before(JoinPoint point) {
MethodSignature signature = (MethodSignature) point.getSignature();
ExtApiToken extApiToken = signature.getMethod().getDeclaredAnnotation(ExtApiToken.class);
if (extApiToken != null) {
// 可以放入到AOP代碼 前置通知
getRequest().setAttribute("token", redisToken.getToken());
}
}
/**
* 環繞通知驗證參數
*
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around("rlAop()")
public Object doBefore(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
ExtApiIdempotent extApiIdempotent = signature.getMethod().getDeclaredAnnotation(ExtApiIdempotent.class);
if (extApiIdempotent != null) {
return extApiIdempotent(proceedingJoinPoint, signature);
}
// 放行
Object proceed = proceedingJoinPoint.proceed();
return proceed;
}
// 驗證Token
public Object extApiIdempotent(ProceedingJoinPoint proceedingJoinPoint, MethodSignature signature) throws Throwable {
ExtApiIdempotent extApiIdempotent = signature.getMethod().getDeclaredAnnotation(ExtApiIdempotent.class);
if (extApiIdempotent == null) {
// 直接執行程序
Object proceed = proceedingJoinPoint.proceed();
return proceed;
}
// 代碼步驟:
// 1.獲取令牌 存放在請求頭中
HttpServletRequest request = getRequest();
String valueType = extApiIdempotent.type();
if (StringUtils.isEmpty(valueType)) {
response("參數錯誤!");
return null;
}
String token = null;
if (valueType.equals(ConstantUtils.EXTAPIHEAD)) {
token = request.getHeader("token");
} else {
token = request.getParameter("token");
}
if (StringUtils.isEmpty(token)) {
response("參數錯誤!");
return null;
}
if (!redisToken.findToken(token)) {
response("請勿重複提交!");
return null;
}
Object proceed = proceedingJoinPoint.proceed();
return proceed;
}
public HttpServletRequest getRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
return request;
}
public void response(String msg) throws IOException {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = attributes.getResponse();
response.setHeader("Content-type", "text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
try {
writer.println(msg);
} catch (Exception e) {
} finally {
writer.close();
}
}
}
3.8 API
接口保證冪等性
package com.snow.controller;
import com.snow.annotation.ExtApiIdempotent;
import com.snow.entity.Order;
import com.snow.mapper.OrderMapper;
import com.snow.utils.ConstantUtils;
import com.snow.utils.RedisToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.controller
* @Description : OrderController
* @date :2020/4/8 22:25
*/
@RestController
public class OrderController {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RedisToken redisToken;
/**
* 從redis中獲取Token
*
* @return
*/
@RequestMapping("/redisToken")
public String RedisToken() {
return redisToken.getToken();
}
/**
* 驗證Token
*
* @param order
* @param request
* @return
*/
@RequestMapping(value = "/addOrderExtApiIdempotent", produces = "application/json; charset=utf-8")
@ExtApiIdempotent(type = ConstantUtils.EXTAPIHEAD)
public String addOrderExtApiIdempotent(@RequestBody Order order, HttpServletRequest request) {
int result = orderMapper.addOrder(order);
return result > 0 ? "添加成功" : "添加失敗" + "";
}
}
3.9 頁面防止重複提交
package com.snow.controller;
import com.snow.annotation.ExtApiIdempotent;
import com.snow.annotation.ExtApiToken;
import com.snow.entity.Order;
import com.snow.mapper.OrderMapper;
import com.snow.utils.ConstantUtils;
import com.snow.utils.RedisToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
/**
* @author :yang-windows
* @Title : springboot
* @Package :com.snow.controller
* @Description : 頁面跳轉
* @date :2020/4/16 13:58
*/
@Controller
public class OrderPageController {
@Autowired
private OrderMapper orderMapper;
@Autowired
private RedisToken redisToken;
@RequestMapping("/indexPage")
@ExtApiToken
public String indexPage(HttpServletRequest req) {
return "indexPage";
}
@RequestMapping("/addOrderPage")
@ExtApiIdempotent(type = ConstantUtils.EXTAPIFROM)
public String addOrder(Order order) {
int addOrder = orderMapper.addOrder(order);
return addOrder > 0 ? "success" : "fail";
}
}
測試
1 瀏覽器輸入:http://127.0.0.1:8080/redisToken
2 添加商品:瀏覽器訪問 http://127.0.0.1:8080/indexPage
提交兩次報下面錯誤