隐藏秒杀接口地址

<script>
function getMiaoshaPath(){
$.ajax({
url: '/miaosha/generate_path', // 调用后台接口获取秒杀接口的入口地址
type: "GET",
data: {//post提交表单的时候,url参数可以放在data中
goodsId: $("#goodsId").val()
},
success:function(data){
if(data.code == 0){
doMiaosha(data.data);//相当于是产生的uuid( 获取到的秒杀接口的入口地址
}else{

}
},
error:function(){
layer.msg("客户端请求有误");
}
});

}

function doMiaosha(uuidPath){//秒杀的时候,需要向服务端传递参数
$.ajax({
url: "/miaosha/" + uuidPath + "/do_miaosha", //进行秒杀
type: "POST",
data: {//post提交表单的时候,url参数可以放在data中
goodsId: $("#goodsId").val()
},
success:function(data){//成功的时候会有一个回调,data是返回的数据,该数据应该是一个对象类型,或字典类型
if(data.code == 0){
/* render(data.data);//这里需要一个渲染页面的函数 render */
//window.location.href="/order_detail.htm?orderId=" + data.data.id;
getMiaoshaResult($("#goodsId").val());
}else{
layer.msg(data.msg);
}
},
error:function(){

layer.msg("客户端请求有误");

}
});

}


后台接口:

@RequestMapping(value="/generate_path", method=RequestMethod.GET)

@ResponseBody

    public Result<String> generate_path(MiaoshaUser user, @RequestParam("goodsId")long goodsId) {
if(user == null){
return Result.error(CodeMsg.SESSION_ERROR);
}
String genPath = miaoshaService. createPath(user, goodsId);//产生并存入到redis数据库中
return Result.success(genPath);

}


@RequestMapping(value="/{uuidPath}/do_miaosha", method=RequestMethod.POST)
@ResponseBody
    public Result<Integer> miaosha(MiaoshaUser user, @RequestParam("goodsId")long goodsId,@PathVariable("uuidPath")String path) {
//RequestParam可以把保单隐含提交的input标签对应name属性的元素取出
if(user == null){
return Result.error(CodeMsg.SESSION_ERROR);
}
boolean pass = miaoshaService.verifyPath(user, goodsId, path);
if(!pass){
return Result.error(CodeMsg.REQUEST_ILLEGAL);
}
//判断内存标记
boolean over = isOverMap.get(goodsId);
if(over){
return Result.error(CodeMsg.MIAO_SHA_OVER);

}

MiaoshaService:

public String createPath(MiaoshaUser user, long goodsId) {
String str = MD5Util.md5(UUIDUtil.uuid() + "123456");
redisService.set(MiaoshaKey.getMiaoshaPath, "" + user.getId() + "_" + goodsId, str);
return str;
}
public boolean verifyPath(MiaoshaUser user, long goodsId, String path) {
if(user == null || path == null){
return false;
}
String uuidRedisPath = redisService.get(MiaoshaKey.getMiaoshaPath,  "" + user.getId() + "_" + goodsId, String.class);
if(uuidRedisPath.equals(path)){
return true;
}
return false;
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
基于SpringBoot和Mybatis的秒杀系统是一个利用SpringBoot和Mybatis框架构建的商城秒杀系统。该系统还使用了MySQL数据库、Redis缓存中间件和RabbitMQ消息中间件等中间件来提高系统性能和可靠性。 该系统的开发工具和版本如下:JDK 1.8、MySQL 5.7、Spring Boot 1.5.10、Mybatis 3.2.2、Redis 3.2和RabbitMQ 1.8。 该系统的主要功能和优化措施包括: 1. 页面缓存、商品详情静态化和订单静态化,可以将页面内容缓存在Redis中,提高系统响应速度。 2. 引入消息队列RabbitMQ,对秒杀接口进行优化,减轻系统负载压力。 3. 隐藏秒杀接口地址,增加系统安全性。 4. 接口限流防刷,限制用户对秒杀接口的请求频率,防止刷单行为。 5. 解决超卖问题,通过合理的库存处理和事务控制机制,避免商品超卖的情况发生。 这些优化措施可以提高系统的并发处理能力、降低系统负载,并提升用户体验。通过使用SpringBoot和Mybatis框架,开发者可以更快速地搭建和开发高性能的秒杀系统。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Seckill:基于SpringBoot + Mybatis + Redis + RabbitMQ秒杀系统](https://download.csdn.net/download/weixin_42168745/15727325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [基于SpringBoot+Mybatis+Mysql+中间件构建的商城秒杀系统源码.zip](https://download.csdn.net/download/weixin_47367099/85324382)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [基于 SpringBoot+Mybatis+Redis+RabbitMQ 秒杀系统 ](https://download.csdn.net/download/bala5569/85490405)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值