- 在互联网API接口中,由于网络超时、手动刷新等经常导致客户端重复提交数据到服务端,这就要求在设计API接口时做好幂等控制。尤其是在面向微服务架构的系统中,系统间的调用非常频繁,如果不做好幂等性设置,轻则会导致脏数据入库,重则导致资损。
本例基于Redis实现一个幂等控制框架。主要思路是在调用接口时传入全局唯一的token字段,标识一个请求是否是重复请求。 - 总体思路
1)在调用接口之前先调用获取token的接口生成对应的令牌(token),并存放在redis当中。
2)在调用接口的时候,将第一步得到的token放入请求头中。
3)解析请求头,如果能获取到该令牌,就放行,执行既定的业务逻辑,并从redis中删除该token。
4)如果获取不到该令牌,就返回错误信息(例如:请勿重复提交)
结合AOP技术通过注解的方式实现整个项目入口的幂等控制。
项目下载地址:
https://download.csdn.net/download/zpcandzhj/10571317
Redis基础知识见博文《Redis实战教程》
代码实现
实现基于Springboot。
项目结构:
- pom文件
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zpc.redis</groupId>
<artifactId>my-redis</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<properties>
</properties>
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</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-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--支持jsp的jar包 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<!--springboot插件,支持springboot:run命令启动工程-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.4.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
- application.properties
#页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
#响应页面默认后缀
spring.mvc.view.suffix=.jsp
- applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 开启注解 -->
<context:component-scan base-package="com.zpc.redis"></context:component-scan>
<!--连接池的配置-->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="10"/>
</bean>
<!--分片式连接池的配置-->
<bean class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1">
<list>
<bean class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="127.0.0.1"/>
<constructor-arg index="1" value="6379"/>
</bean>
</list>
</constructor-arg>
</bean>
</beans>
Springboot默认不支持jsp,推荐使用模板引擎。为了支持jsp,需要在main下面新建webapp目录,并建好下列目录与文件:
- WEB-INF/jsp/indexPage.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Insert title here</title>
</head>
<body>
<form action="/addUserPage" method="post">
<input type="hidden" name="token" value="${token}"><span>姓名</span><input type="text" name="name"><br/>
<span>年龄</span><input type="text" name="age"><br/>
<span>性别</span><input type="text" name="sex"><br/>
<input type="submit">
</form>
</body>
</html>
- ExtApiIdempotent.java
package com.zpc.redis.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 标识一个接口是否需要校验token,type取值为head/form
* head:表示客户端token放在请求头中
* form:表示客户端token放在表单中
*/
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtApiIdempotent {
String type();
}
- ExtApiToken.java
package com.zpc.redis.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 标识一个接口是否需要为request自动添加token字段
*/
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtApiToken {
}
- ExtApiIdempotentAop.java
package com.zpc.redis.aop;
import com.zpc.redis.annotation.ExtApiIdempotent;
import com.zpc.redis.annotation.ExtApiToken;
import com.zpc.redis.service.RedisTokenService;
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.util.StringUtils;
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;
/**
* AOP切面
* 完成2个功能:
* 1)判断接口方法是否有ExtApiToken注解,如果有自动在HttpServletRequest中添加token字段值
* 2)判断接口方法是否有ExtApiIdempotent注解,如果有则校验token
*/
@Component
@Aspect
public class ExtApiIdempotentAop {
@Autowired
private RedisTokenService tokenService;
@Pointcut("execution(public * com.zpc.redis.controller.*.*(..))")
public void myAop() {
}
@Before("myAop()")
public void before(JoinPoint point) {
MethodSignature signature = (MethodSignature) point.getSignature();
ExtApiToken annotation = signature.getMethod().getAnnotation(ExtApiToken.class);
if (annotation != null) {
getRequest().setAttribute("token", tokenService.getToken());
}
}
//环绕通知
@Around("myAop()")
public Object doBefore(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//判断方法上是否有ExtApiIdempotent注解
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
ExtApiIdempotent declaredAnnotation = signature.getMethod().getDeclaredAnnotation(ExtApiIdempotent.class);
if (declaredAnnotation != null) {
String type = declaredAnnotation.type();
String token = null;
HttpServletRequest request = getRequest();
if ("head".equals(type)) {
token = request.getHeader("token");
} else {
token = request.getParameter("token");
}
if (StringUtils.isEmpty(token)) {
return "请求参数错误!";
}
boolean tokenOk = tokenService.findToken(token);
if (!tokenOk) {
getResponse("请勿重复提交!");
return null;
}
}
//放行
return proceedingJoinPoint.proceed();
}
public HttpServletRequest getRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
return request;
}
public void getResponse(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();
writer.write(msg);
writer.close();
}
}
- User.java
package com.zpc.redis.bean;
public class User {
private String name;
private Integer age;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
- RedisService.java
package com.zpc.redis.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
@Service
public class RedisService {
@Autowired
private ShardedJedisPool shardedJedisPool;
private <T> T execute(Function<T, ShardedJedis> fun) {
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
return fun.callback(shardedJedis);
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
}
/**
* 执行set操作
*
* @param key
* @param value
* @return
*/
public String set(final String key, final String value) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.set(key, value);
}
});
}
/**
* 执行get操作
*
* @param key
* @return
*/
public String get(final String key) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
return e.get(key);
}
});
}
/**
* 执行删除操作
*
* @param key
* @return
*/
public Long del(final String key) {
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.del(key);
}
});
}
/**
* 设置生存时间,单位为:秒
*
* @param key
* @param seconds
* @return
*/
public Long expire(final String key, final Integer seconds) {
return this.execute(new Function<Long, ShardedJedis>() {
@Override
public Long callback(ShardedJedis e) {
return e.expire(key, seconds);
}
});
}
/**
* 执行set操作并且设置生存时间,单位为:秒
*
* @param key
* @param value
* @return
*/
public String set(final String key, final String value, final Integer seconds) {
return this.execute(new Function<String, ShardedJedis>() {
@Override
public String callback(ShardedJedis e) {
String str = e.set(key, value);
e.expire(key, seconds);
return str;
}
});
}
}
public interface Function<T, E> {
T callback(E e);
}
- RedisTokenService.java
package com.zpc.redis.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.UUID;
/**
* 生成token并且放到redis中
*/
@Service
public class RedisTokenService {
private static final Integer TOKEN_TIMEOUT = 600;
@Autowired
RedisService redisService;
public String getToken() {
String token = "token" + UUID.randomUUID();
redisService.set(token, token, TOKEN_TIMEOUT);
return token;
}
public boolean findToken(String tokenKey){
String token = redisService.get(tokenKey);
if(StringUtils.isEmpty(token)){
return false;
}
redisService.del(tokenKey);
return true;
}
}
- TestController.java
package com.zpc.redis.controller;
import com.zpc.redis.annotation.ExtApiIdempotent;
import com.zpc.redis.bean.User;
import com.zpc.redis.service.RedisTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
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;
@RestController
public class TestController {
@Autowired
private RedisTokenService tokenService;
@RequestMapping(value = "addUser", produces = "application/json;charset=utf-8")
public String addUser(@RequestBody User user, HttpServletRequest request) {
String token = request.getHeader("token");
if (StringUtils.isEmpty(token)) {
return "请求参数错误!";
}
boolean tokenOk = tokenService.findToken(token);
if (!tokenOk) {
return "请勿重复提交!";
}
//执行正常的业务逻辑
System.out.println("user info:" + user);
return "添加成功!";
}
@RequestMapping(value = "getToken")
public String getToken() {
return tokenService.getToken();
}
@ExtApiIdempotent(type = "head")
@RequestMapping(value = "addUser2", produces = "application/json;charset=utf-8")
public String addUser2(@RequestBody User user) {
//执行正常的业务逻辑
System.out.println("user info:" + user);
return "添加成功!!";
}
}
- TestController2.java
package com.zpc.redis.controller;
import com.zpc.redis.annotation.ExtApiIdempotent;
import com.zpc.redis.annotation.ExtApiToken;
import com.zpc.redis.service.RedisTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
@Controller
public class TestController2 {
@Autowired
RedisTokenService tokenService;
@RequestMapping(value = "/indexPage")
@ExtApiToken
public String indexPage(HttpServletRequest request) {
System.out.println("================================");
//加上注解ExtApiToken,使用AOP方式统一设置token
//request.setAttribute("token",tokenService.getToken());
return "indexPage";
}
@RequestMapping(value = "/addUserPage")
@ResponseBody
@ExtApiIdempotent(type = "form")
public String addUserPage(HttpServletRequest request) {
return "添加成功!";
}
}
入口类:
- MyAppication.java
package com.zpc.redis.runner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ComponentScan(basePackages = "com.zpc.redis")
@ImportResource({"classpath*:applicationContext*.xml"})
public class MyAppication {
public static void main(String[] args) {
SpringApplication.run(MyAppication.class, args);
}
}
获取token:
使用postman或者其他接口测试工具发起post请求,注意添加请求头:
Token对表单重复提交的支持:
完整代码:
https://download.csdn.net/download/zpcandzhj/10571317