1. 程式人生 > >基於redis的API介面冪等設計

基於redis的API介面冪等設計

  • 在網際網路API介面中,由於網路超時、手動重新整理等經常導致客戶端重複提交資料到服務端,這就要求在設計API介面時做好冪等控制。尤其是在面向微服務架構的系統中,系統間的呼叫非常頻繁,如果不做好冪等性設定,輕則會導致髒資料入庫,重則導致資損。
    本例基於Redis實現一個冪等控制框架。主要思路是在呼叫介面時傳入全域性唯一的token欄位,標識一個請求是否是重複請求。
  • 總體思路
    1)在呼叫介面之前先呼叫獲取token的介面生成對應的令牌(token),並存放在redis當中。
    2)在呼叫介面的時候,將第一步得到的token放入請求頭中。
    3)解析請求頭,如果能獲取到該令牌,就放行,執行既定的業務邏輯,並從redis中刪除該token。
    4)如果獲取不到該令牌,就返回錯誤資訊(例如:請勿重複提交)

結合AOP技術通過註解的方式實現整個專案入口的冪等控制。

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對錶單重複提交的支援:
這裡寫圖片描述