实现线上高性能接口方案nginx负载tornado后端lua数据

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介:

101638437.jpg


最近实现了一个http的接口 ~ http的方式,访问结果为json

nginx做负载,web分离,url转发 ~

tornado做数据操作和异步调用lua的接口 ~

memcached做session的共享 (为啥不用redis做session,一方面是为了不和redis有kv冲突,服务器的中断冗余,更主要的方面是用redis存储session我没有做出来,郁闷呀。。。。)

redis做kv数据库和队列系统

后端的nginx lua是mysql的http方式的json展现接口



nginx 的配置大家都懂的,就是利用upstream负载。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
worker_processes  4 ;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
error_log / var /log/nginx/error.log;
pid / var /run/nginx.pid;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
events {
     worker_connections  30000 ;
     useepoll;
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
http {
     charset utf- 8 ;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     # Enumerate all the Tornado servers here
     upstream frontends {
         server  127.0 . 0.1 : 8000 ;
         server  127.0 . 0.1 : 8001 ;
         server  127.0 . 0.1 : 8002 ;
         server  127.0 . 0.1 : 8003 ;
     }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     include /path/to/nginx.mime.types;
     default_type application/octet-stream;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     access_log / var /log/nginx/access.log;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     keepalive_timeout  65 ;
     proxy_read_timeout  200 ;
     sendfile on;
     tcp_nopush on;
     tcp_nodelay on;
     gzip on;
     gzip_min_length  1000 ;
     gzip_proxied any;
     gzip_types text/plain text/css text/xml
                application/x-javascript application/xml
                application/atom+xml text/javascript;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     # Only retry ifthere was a communication error, not a timeout
     # on the Tornado server (to avoid propagating  "queries of death"
     # to all frontends)
     proxy_next_upstream error;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     server {
         listen  80 ;
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
         location ^~ / static / {
             root /path/to/app;
             if ($query_string) {
                 expires max;
             }
         }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
         location / {
             proxy_pass_header Server;
             proxy_set_header Host $http_host;
             proxy_redirect off;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Scheme $scheme;
             proxy_pass http: //frontends;
         }
     }
}


对于后端节点,监控的检查请用nginx的check模块 ~

模块下载地址:https://github.com/cep21/healthcheck_nginx_upstreams;模块名称:ngx_http_healthcheck_module

1
2
3
4
5
6
7
8
9
10
11
upstream myhttpd {
         server  127.0 . 0.1 :8800weight= 5 ;
         server  127.0 . 0.1 :8801weight= 5 ;
         healthcheck_enabled; 开启健康检查功能
         healthcheck_delay  1000 ;
对同一台后端服务器两次检测之间的时间间隔,单位毫秒,默认为 1000 。                healthcheck_timeout  1000 ;
进行一次健康检测的超时时间,单位为毫秒,默认值 2000
         healthcheck_failcount  3 ;
对一台后端服务器检测成功或失败多少次之后方才确定其为成功或失败,并实现启用或禁用此服务器
         healthcheck_send  "GET/.health HTTP/1.0" ;
对一台后端服务器检测成功或失败多少次之后方才确定其为成功或失败,并实现启用或禁用此服务器,只支持http  1.0 的协议


uwsgi tornado的对比:


有朋友问过我,uwsgi的性能也可以呀。。。

以前我用flask的时候,经常要和uwsgi配合一起用,但是我和群里的人多方面测试压力,uwsgi不是很稳定,经常出现访问错误的情况,看debug也是看不出啥东西来。


这么多的tornado进程该如何开启?

用screen ,用 >/dev/null,用nohup ? 太搓了点吧~

推荐大家用supervisord管理杂乱的tornado服务和别的python服务。

源地址 http://rfyiamcool.blog.51cto.com/1030776/1301410

这里是centos supervisord 的配置


配置supervisor

1
2
yum install supervisor
pip install supervisor


echo_supervisord_conf > /etc/supervisord.conf


vim /etc/supervisord.conf


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[program:tornado8000]
command=python /ops/tornado8000.py
process_name=%(program_name)s
stdout_logfile=/ops/access.log
stderr_logfile=/ops/error.log
[program:tornado8001]
command=python /ops/tornado8001.py
process_name=%(program_name)s
stdout_logfile=/ops/access.log
stderr_logfile=/ops/error.log
[program:tornado8002]
command=python /ops/tornado8002.py
process_name=%(program_name)s
stdout_logfile=/ops/access.log
stderr_logfile=/ops/error.log
[program:tornado8003]
command=python /ops/tornado8003.py
process_name=%(program_name)s
stdout_logfile=/ops/access.log
stderr_logfile=/ops/error.log


保存完配置后,咱们启动下: run~


1
/usr/bin/supervisord -c /etc/supervisord.conf


Supervisord安装完成后有两个可用的命令行supervisor和supervisorctl,命令使用解释如下:

  • supervisord,初始启动Supervisord,启动、管理配置中设置的进程。

  • supervisorctl stop programxxx,停止某一个进程(programxxx),programxxx为[program:chatdemon]里配置的值,这个示例就是chatdemon。

  • supervisorctl start programxxx,启动某个进程

  • supervisorctl restart programxxx,重启某个进程

  • supervisorctl stop groupworker: ,重启所有属于名为groupworker这个分组的进程(start,restart同理)

  • supervisorctl stop all,停止全部进程,注:start、restart、stop都不会载入最新的配置文件。

  • supervisorctl reload,载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程。

  • supervisorctl update,根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启。

  • 注意:显示用stop停止掉的进程,用reload或者update都不会自动重启。


114739607.png

然后用装饰器判断状态。。。

@tornado.web.authenticated


代码在这里 ,大家配置自己的系统搞一下。

https://github.com/mmejia27/tornado-memcached-sessions


关于lua的用法,大家可以看看我以前的博文,对于mysql有详细的用法的。


模块代码:

https://github.com/agentzh/lua-resty-mysql


实例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
upstream nginxbackend
  {
  drizzle_server  127.0 . 0.1 : 3306  dbname=nginx password=youpassword user=yourdbname protocol=mysql charset=utf8;
  drizzle_keepalive max= 100  mode=single overflow=reject;
  }
                                                                                                                                                                                                                                                                                                                                                                  
  location = /getuser
  {
  internal ;
  set_unescape_uri $user $remote_user;
  set_unescape_uri $passwd $remote_passwd;
  set_quote_sql_str $user $user;
  set_quote_sql_str $passwd $passwd;
  drizzle_query  "select count(*) as count from user where user=$user and passwd=$passwd" ;
  drizzle_pass nginxbackend;
  rds_json on;
  }



总之,我们要多方面的测试压力,才能找到适合自己业务的框架。






 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1301410,如需转载请自行联系原作者



相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
26天前
|
负载均衡 应用服务中间件 数据库
Nginx负载过高,加机器就能解决吗?
一个架构难题的剖析:A机器顶不住,加B机器就能解决吗?
35 7
|
4月前
|
消息中间件 负载均衡 应用服务中间件
高并发环境下的Nginx整合方案
【8月更文挑战第20天】在高并发环境下,整合Nginx代理服务器、静态文件服务器、Tomcat集群、Mycat数据库读写分离和消息队列,可以构建一个强大、灵活且可扩展的Web服务架构。
58 1
|
4月前
|
SQL 监控 安全
深入浅出:构建高性能后端服务的关键要素
在数字化浪潮的推动下,后端服务成为了支撑现代软件系统的骨干。本文将通过浅显易懂的语言和生动的比喻,带领读者一探究竟,如何打造一个既稳定又高效的后端系统。从选择合适的技术栈到实施最佳实践,我们将一步步揭开高性能后端服务的神秘面纱。无论你是初学者还是资深开发者,这篇文章都将为你提供宝贵的洞见和实用的技巧。
44 4
|
4月前
|
存储 SQL 缓存
构建高性能后端服务的策略与实践
【8月更文挑战第13天】在数字化时代,后端服务的高效性对于企业至关重要。本文深入探讨了如何通过合理设计、优化数据库操作以及应用缓存技术等策略来提升后端服务性能。同时,也强调了监控和调优的重要性,并指出了团队协作在性能优化中的作用。
65 3
|
5月前
|
负载均衡 网络协议 应用服务中间件
Nginx负载配置
Nginx负载配置
85 8
|
5月前
|
缓存 负载均衡 数据库
构建高性能后端系统的策略与实践
在数字化时代的浪潮中,后端系统作为支撑现代应用程序的核心,其性能的优劣直接影响用户体验和业务发展。本文将深入探讨如何构建一个既高效又可靠的后端系统,通过具体的策略和技术手段,指导读者理解并实施后端优化的最佳实践。我们将一起探索代码优化、数据库设计、缓存应用、异步处理以及负载均衡等关键领域,旨在帮助开发者打造能够应对高并发挑战的后端架构。 【7月更文挑战第27天】
79 5
|
7月前
|
消息中间件 缓存 负载均衡
构建高性能的后端服务:优化策略与实践
在当今互联网时代,构建高性能的后端服务至关重要。本文将深入探讨如何通过优化策略与实践来提升后端服务的性能。我们将从数据库优化、缓存策略、异步处理和负载均衡等方面展开讨论,帮助开发者构建出稳定、高效的后端架构。
251 2
|
7月前
|
消息中间件 API 数据库
构建高性能微服务架构:后端开发的终极指南
【5月更文挑战第27天】随着现代业务需求的不断演进,传统的单体应用架构逐渐显得笨重且难以维护。微服务架构以其灵活性、可扩展性和技术多样性成为企业转型的首选。本文将深入探讨如何构建一个高性能的微服务系统,涵盖关键设计原则、常用技术和实践案例,为后端开发人员提供一份全面的指南。
|
7月前
|
缓存 负载均衡 监控
构建高性能的后端服务架构
在当今快速发展的技术环境下,构建高性能的后端服务架构变得至关重要。本文将探讨如何通过优化数据库设计、采用合适的缓存策略、以及合理使用负载均衡等方法来构建高性能的后端服务架构,以满足用户对于性能和稳定性的需求。
59 0
|
7月前
|
缓存 API 数据库
构建高性能微服务架构:后端开发的新趋势
【5月更文挑战第28天】 在现代软件开发的浪潮中,微服务架构已成为推动技术创新与提升系统可维护性的关键。此文深入剖析了构建高性能微服务架构的策略和实践,揭示了通过容器化、服务网格、持续集成/持续部署(CI/CD)等技术手段来优化后端开发流程的方法。文章不仅探讨了微服务设计原则和性能优化技巧,还提供了对未来后端开发趋势的预见性分析。