慕课网-java高并发秒杀api之高并发优化-总结

本文介绍如何在Spring框架中配置和使用声明式事务管理,包括事务管理器的设置、基于注解的事务配置及事务注解的使用技巧。同时探讨了通过存储过程解决行级锁和网络延迟问题,并提到了集群部署和负载均衡。

1.架构优化

2.spring声明式事务

声明式事务:http://www.open-open.com/lib/view/open1414310646012.html

 

配置并使用Spring声明式事务

  1. 在spring-service.xml中添加上配置事务管理器
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <!--注入数据库连接池-->
      <property name="dataSource" ref="dataSource"/>
    </bean>
  2. 在spring-service.xml中添加上配置基于注解的声明式事务
    <tx:annotation-driven transaction-manager="transactionManager"/>
  3. 在业务类的executeSecKill方法中添加上@Transactional事务注解
  4. 学习点:使用注解控制事务方法的优点
    • 开发团队达到一致约定,明确标注事务方法的编程风格
    • 保证事务方法的执行时间尽可能短,不要穿插其他网络操作RPC/HTTP请求,或者剥离到事务方法外部
    • 不是所有的方法都需要事务,如只有一条修改操作,只读操作就不需要事务控制

 

3.通过sql procedure解决行级锁和网络延迟

 

4.集群化部署,nginx负载均衡

java实现秒杀系统@Controller @RequestMapping("seckill")//url:/模块/资源/{id}/细分 /seckill/list public class SeckillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SeckillService seckillService; @RequestMapping(value="/list",method = RequestMethod.GET) public String list(Model model){ //获取列表页 List list=seckillService.getSeckillList(); model.addAttribute("list",list); //list.jsp+model = ModelAndView return "list";//WEB-INF/jsp/"list".jsp } @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model){ if (seckillId == null){ return "redirect:/seckill/list"; } Seckill seckill = seckillService.getById(seckillId); if (seckill == null){ return "forward:/seckill/list"; } model.addAttribute("seckill",seckill); return "detail"; } //ajax json @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public SeckillResult exposer(@PathVariable("seckillId") Long seckillId){ SeckillResult result; try { Exposer exposer =seckillService.exportSeckillUrl(seckillId); result = new SeckillResult(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); result = new SeckillResult(false,e.getMessage()); } return result; } @RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"} ) @ResponseBody public SeckillResult execute(@PathVariable("seckillId")Long seckillId,
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值