秒杀Servlce接口设计

Posted sinceForever

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了秒杀Servlce接口设计相关的知识,希望对你有一定的参考价值。

秒杀Servlce接口设计

1、创建service包,创建SecKillServlce业务接口

SecKillServlce.Java

package org.secKill.service;
/*
* 业务接口:站在“开发者”的角度设计接口
* 三个方面:方法定义粒度,参数,返回类型(return 类型/异常)*/

import org.secKill.dto.Exposer;
import org.secKill.dto.SecKillExecution;
import org.secKill.entity.SecKill;
import org.secKill.exception.RepeatKillException;
import org.secKill.exception.SecKillCloseException;
import org.secKill.exception.SecKillException;

import java.util.List;

/**
 * Created by 谭雪娇 on 2017/5/5.
 */
public interface SecKillService {
    /*
    * 查询所有秒杀记录
    * @return
    * */
    List<SecKill> getSecKillList();
    /*
    * c查询单个秒杀记录
    * @Param SecKillId
    * @return
    * */
    SecKill getById(long secKillId);
    /*
    * 秒杀开启时输出秒杀接口地址,
    * 否则输出系统时间
    * @param secKillId
    * */
    Exposer exportSecKillUrl(long secKillId);
    /*
    * 执行秒杀操作
    * @param secKillId
    * @param userPhone
    * @param md5*/
    SecKillExecution executeZSecKill(long secKillId, long userPhone, String md5) throws SecKillException,RepeatKillException,SecKillCloseException;
}

 

 

建立dto包,创建暴露秒杀接口dto(数据传输层)

Exposer.java

package org.secKill.dto;

/**
 * Created by 谭雪娇 on 2017/5/5.
 * 暴露秒杀接口DTO
 */
public class Exposer {
    //是否开启秒杀
    private boolean exposed;
    //一种加密措施
    private String md5;
    //id
    private long secKillId;
    //开始时间
    private long start;
    //结束时间
    private long end;

    public Exposer(boolean exposed, String md5, long secKillId) {
        this.exposed = exposed;
        this.md5 = md5;
        this.secKillId = secKillId;
    }

    public Exposer(boolean exposed, String md5, long start, long secKillId, long end) {
        this.exposed = exposed;
        this.md5 = md5;
        this.start = start;
        this.secKillId = secKillId;
        this.end = end;
    }

    public Exposer(boolean exposed, long secKillId) {
        this.exposed = exposed;
        this.secKillId = secKillId;
    }

    public boolean isExposed() {
        return exposed;
    }

    public void setExposed(boolean exposed) {
        this.exposed = exposed;
    }

    public String getMd5() {
        return md5;
    }

    public void setMd5(String md5) {
        this.md5 = md5;
    }

    public long getSecKillId() {
        return secKillId;
    }

    public void setSecKillId(long secKillId) {
        this.secKillId = secKillId;
    }

    public long getStart() {
        return start;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public long getEnd() {
        return end;
    }

    public void setEnd(long end) {
        this.end = end;
    }

    @Override
    public String toString() {
        return "Exposer{" +
                "exposed=" + exposed +
                ", md5=‘" + md5 + \‘+
                ", secKillId=" + secKillId +
                ", start=" + start +
                ", end=" + end +
                ‘}‘;
    }
}

 

创建包enums,创建枚举类型SecKillStateEnum.java使用枚举表述数据常量字典

SecKillStateEnum.java

package org.secKill.enums;

/**
 * 使用枚举表述常量数据字典
 *
 * @author yan
 */
public enum SecKillStateEnum {

    SUCCESS(1, "秒杀成功"), END(0, "秒杀结束"),

    REPEAT_KILL(-1, "重复秒杀"),

    INNER_ERROR(-2, "系统异常"),

    DATA_REWRITE(-3, "数据篡改");

    private int state;

    private String stateInfo;

    private SecKillStateEnum(int state, String stateInfo) {
        this.state = state;
        this.stateInfo = stateInfo;
    }

    public int getState() {
        return state;
    }

    public String getStateInfo() {
        return stateInfo;
    }

    public static SecKillStateEnum stateOf(int index) {
        for (SecKillStateEnum state : values()) {
            if (state.getState() == index) {
                return state;
            }
        }
        return null;
    }

}

 

 

dto包内创建封装秒杀执行后结果类SecKillExecution.java

package org.secKill.dto;

import org.secKill.entity.SuccessKilled;
import org.secKill.enums.SecKillStateEnum;

/**
 * Created by 谭雪娇 on 2017/5/5.
 * 封装秒杀执行后结果
 */
public class SecKillExecution {
    private long secKillId;
    //秒杀执行结果状态
    private int state;
    //状态标识
    private String stateInfo;
    //秒杀成功对象
    private SuccessKilled successKilled;

    public SecKillExecution(long secKillId, SecKillStateEnum stateEnum , SuccessKilled successKilled) {
        this.secKillId = secKillId;
        this.state = stateEnum.getState();
        this.stateInfo=stateEnum.getStateInfo();
        this.successKilled=successKilled;

    }

    public SecKillExecution(long secKillId,SecKillStateEnum stateEnum) {
        this.secKillId = secKillId;
        this.state =stateEnum.getState();
        this.stateInfo = stateEnum.getStateInfo();
    }

    public long getSecKillId() {
        return secKillId;
    }

    public void setSecKillId(long secKillId) {
        this.secKillId = secKillId;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getStateInfo() {
        return stateInfo;
    }

    public void setStateInfo(String stateInfo) {
        this.stateInfo = stateInfo;
    }

    public SuccessKilled getSuccessKilled() {
        return successKilled;
    }

    public void setSuccessKilled(SuccessKilled successKilled) {
        this.successKilled = successKilled;
    }
}

 

创建包exception创建三个异常

创建SecKillException.java 秒杀执行异常

package org.secKill.exception;

/**
 * Created by 谭雪娇 on 2017/5/5.
 */
public class SecKillException extends RuntimeException{
    public SecKillException(String message){super(message);}
    public SecKillException(String message,Throwable cause){super(message,cause);}
}

 

 

重复秒杀异常RepeatKillException.继承SecKillException.java

package org.secKill.exception;

import org.secKill.dto.SecKillExecution;
import org.secKill.entity.SuccessKilled;
import org.secKill.enums.SecKillStateEnum;

/**
 * Created by 谭雪娇 on 2017/5/5.
 */
public class RepeatKillException extends SecKillException{
    public RepeatKillException(String message) {
        super(message);
    }

    public RepeatKillException(String message, Throwable cause) {
        super(message, cause);
    }
}

 

创建SecKillException.java,表示秒杀关闭异常

package org.secKill.exception;

/**
 * Created by 谭雪娇 on 2017/5/5.
 * 秒杀关闭异常
 */
public class SecKillCloseException extends SecKillException {
    public SecKillCloseException(String message) {
        super(message);
    }

    public SecKillCloseException(String message, Throwable cause) {
        super(message, cause);
    }
}

 

 

 

 

 

以上是关于秒杀Servlce接口设计的主要内容,如果未能解决你的问题,请参考以下文章

秒杀系统的设计与实现接口限流方案

如何设计秒杀系统?

Java高并发秒杀API之web层

高并发秒杀系统--课程总结与思考

Restful接口设计

针对秒杀项目做的一些优化