背景

在秒杀活动功能点上,后端服务并不是单机完成,有多个服务集群部署,解决大量请求,前端统一请求到ngixn,有nginx做代理转发,负载均衡,默认轮询策略。

秒杀主要使用技术

nginx + springboot + redis + lua + LinkedBlockingDeque + 锁 + mysql

初始化:定时任务 LTS 初始化数据到redis中
秒杀:lua 脚本 +redis 保证预减库存的原子性 以及快速响应用户
队列:秒杀成功的记录 在缓存中 以及队列中 进行异步处理 ,加锁保证DB数据库减库存以及秒杀成功日志下单成功 一致性

详见代码:https://gitee.com/Sir-yuChen/backstage_ant.git

nginx配置文件

  1. #简单配置
  2. #user nobody;
  3. worker_processes 1;
  4. events {
  5. worker_connections 1024;
  6. }
  7. http {
  8. include mime.types;
  9. default_type application/octet-stream;
  10. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  11. # '$status $body_bytes_sent "$http_referer" '
  12. # '"$http_user_agent" "$http_x_forwarded_for"';
  13. #access_log logs/access.log main;
  14. sendfile on;
  15. #tcp_nopush on;
  16. #keepalive_timeout 0;
  17. keepalive_timeout 65;
  18. ##
  19. # Gzip 压缩配置
  20. ##
  21. #是告诉nginx采用gzip压缩的形式发送数据。这将会减少我们发送的数据量。
  22. #gzip on;
  23. #为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
  24. #gzip_disable "msie6";
  25. # 命名不要用下划线 400异常
  26. upstream backstage-ant {
  27. #weight=3 权重 max_fails=5 重试次数 fail_timeout=10s 超时时间
  28. server localhost:8083; # 后端本地服务及端口
  29. server localhost:8082;
  30. server localhost:8080;
  31. }
  32. server {
  33. listen 8088;
  34. server_name backstage_ant.com;
  35. location / {
  36. # 转发的地址 upstream 的命名一致
  37. proxy_pass http://backstage-ant;
  38. #Proxy Settings
  39. proxy_http_version 1.1;
  40. proxy_set_header X-Client-IP $remote_addr;
  41. proxy_set_header Upgrade $http_upgrade;
  42. proxy_set_header Connection "upgrade";
  43. proxy_set_header X-Real-IP $remote_addr;
  44. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  45. client_max_body_size 10m;
  46. client_body_buffer_size 128k;
  47. proxy_connect_timeout 90;
  48. proxy_send_timeout 90;
  49. proxy_read_timeout 90;
  50. proxy_buffer_size 4k;
  51. proxy_buffers 4 32k;
  52. proxy_busy_buffers_size 64k;
  53. proxy_temp_file_write_size 64k;
  54. }
  55. error_page 500 502 503 504 /50x.html;
  56. location = /50x.html {
  57. root html;
  58. }
  59. }
  60. }

秒杀功能压测

压测工具:jmeter
测重点:

后端服务为集群部署

是否能保证秒杀高并发请求

是否能避免超卖问题

队列是否处理秒杀成功记录

数据库是否生成订单

以及秒杀成功记录,减库存

秒杀大致流程图(只考虑了部分问题)

nginx 转发配置 - 图1

springboot 本地启动多个相同服务

工具:idea

nginx 转发配置 - 图2