秒杀API设计
1.商品
1.1商品接口表
接口地址 | /goods/to_list |
---|---|
请求类型 | get |
接口描述 | 显示所有参与秒杀活动的商品 |
1.2商品表
字段 | 注释 | 类型 |
---|---|---|
id | 商品号 | int |
goodsName | 商品名称 | String |
goodsImg | 图片地址 | String |
miaoshaPrice | 秒杀价格 | int |
Price | 原价 | int |
stockCount | 商品库存 | int |
1.2秒杀商品表
2.秒杀service接口设计
org.myseckill下创建一个service包用于存放我们的Service接口和其实现类,创建一个exception包用于存放service层出现的异常例如重复秒杀商品异常、秒杀已关闭等允许出现的异常,一个dto包作为数据传输层,dto和entity的区别在于:entity用于业务数据的封装,而dto关注的是web和service层的数据传递。
/**
* 查询所有的秒杀记录
* @return
*/
List<Seckill> getSeckillList();
/**
*查询单个秒杀记录
* @param seckillId
* @return
*/
Seckill getById(long seckillId);
/**
* 秒杀开启时输出秒杀接口地址,
* 否则输出系统时间和秒杀时间
* 防止用户提前拼接出秒杀url通过插件进行秒杀
* @param seckillId
*/
Exposer exportSeckillUrl(long seckillId);
/**
* 执行秒杀操作,如果传入的md5与内部的不相符,说明用户的url被篡改了,此时拒绝执行秒杀
* 有可能失败,有可能成功,所以要抛出我们允许的异常
* @param seckillId
* @param userPhone
* @param md5
*/
SeckillExecution executeSeckill(long seckillId,long userPhone,String md5) throws SeckillException,SeckillCloseException,RepeatKillException;
相应在的dto包中创建Exposer.java,用于封装秒杀的地址信息
//是否开启秒杀
private boolean exposed;
//对秒杀地址加密措施
private String md5;
//id为seckillId的商品的秒杀地址
private long seckillId;
//系统当前时间(毫秒)
private long now;
//秒杀的开启时间
private long start;
//秒杀的结束时间
private long end;
/**
* 不同的构造方法方便对象初始化
* @param exposed
* @param md5
* @param seckillId
*/
public Exposer(boolean exposed, String md5, long seckillId) {
super();
this.exposed = exposed;
this.md5 = md5;
this.seckillId = seckillId;
}
public Exposer(long now, long start, long end) {
super();
this.now = now;
this.start = start;
this.end = end;
}
public Exposer(boolean exposed, long seckillId) {
super();
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 getNow() {
return now;
}
public void setNow(long now) {
this.now = now;
}
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;
}
SeckillExecution.java`
private long seckillId;
//秒杀执行结果的状态
private int state;
//状态的明文标识
private String stateInfo;
//当秒杀成功时,需要传递秒杀成功的对象回去
private SuccessKilled successKilled;
//不同的构造方法,秒杀成功返回所有信息
public SeckillExecution(long seckillId, int state, String stateInfo, SuccessKilled successKilled) {
this.seckillId = seckillId;
this.state = state;
this.stateInfo = stateInfo;
this.successKilled = successKilled;
}
//秒杀失败
public SeckillExecution(long seckillId, int state, String stateInfo) {
this.seckillId = seckillId;
this.state = state;
this.stateInfo = stateInfo;
}
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;
}