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

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
简介:

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
相关文章
|
17天前
|
JavaScript API 开发工具
<大厂实战场景> ~ Flutter&鸿蒙next 解析后端返回的 HTML 数据详解
本文介绍了如何在 Flutter 中解析后端返回的 HTML 数据。首先解释了 HTML 解析的概念,然后详细介绍了使用 `http` 和 `html` 库的步骤,包括添加依赖、获取 HTML 数据、解析 HTML 内容和在 Flutter UI 中显示解析结果。通过具体的代码示例,展示了如何从 URL 获取 HTML 并提取特定信息,如链接列表。希望本文能帮助你在 Flutter 应用中更好地处理 HTML 数据。
99 1
|
1月前
|
JSON 前端开发 Java
震惊!图文并茂——Java后端如何响应不同格式的数据给前端(带源码)
文章介绍了Java后端如何使用Spring Boot框架响应不同格式的数据给前端,包括返回静态页面、数据、HTML代码片段、JSON对象、设置状态码和响应的Header。
132 1
震惊!图文并茂——Java后端如何响应不同格式的数据给前端(带源码)
|
17天前
|
JSON Dart 数据格式
<大厂实战场景> ~ flutter&鸿蒙next处理后端返回来的数据的转义问题
在 Flutter 应用开发中,处理后端返回的数据是常见任务,尤其涉及转义字符时。本文详细探讨了如何使用 Dart 的 `dart:convert` 库解析包含转义字符的 JSON 数据,并提供了示例代码和常见问题的解决方案,帮助开发者有效处理数据转义问题。
112 0
|
1月前
|
JavaScript 前端开发
vue3教程,如何手动获取后端数据(入门到精通3,新人必学篇)
本文提供了一个Vue 3教程,讲解了如何使用axios库手动从后端获取数据,包括安装axios、配置后端访问地址、编写路由地址、发起HTTP请求以及在组件中读取和打印响应数据的步骤。
304 0
vue3教程,如何手动获取后端数据(入门到精通3,新人必学篇)
|
2月前
|
SQL JSON Java
springboot 如何编写增删改查后端接口,小白极速入门,附完整代码
本文为Spring Boot增删改查接口的小白入门教程,介绍了项目的构建、配置YML文件、代码编写(包括实体类、Mapper接口、Mapper.xml、Service和Controller)以及使用Postman进行接口测试的方法。同时提供了SQL代码和完整代码的下载链接。
springboot 如何编写增删改查后端接口,小白极速入门,附完整代码
|
2月前
|
JSON 数据格式
Blob格式转json格式,拿到后端返回的json数据
文章介绍了如何将后端返回的Blob格式数据转换为JSON格式,并处理文件下载和错误提示。
86 0
Blob格式转json格式,拿到后端返回的json数据
|
1月前
|
前端开发 Java 数据库
springBoot:template engine&自定义一个mvc&后端给前端传数据&增删改查 (三)
本文介绍了如何自定义一个 MVC 框架,包括后端向前端传递数据、前后端代理配置、实现增删改查功能以及分页查询。详细展示了代码示例,从配置文件到控制器、服务层和数据访问层的实现,帮助开发者快速理解和应用。
|
3月前
|
前端开发 JavaScript
这篇文章介绍了如何使用form表单结合Bootstrap格式将前端数据通过action属性提交到后端的servlet,包括前端表单的创建、数据的一级和二级验证,以及后端servlet的注解和参数获取。
这篇文章介绍了使用AJAX技术将前端页面中表单接收的多个参数快速便捷地传输到后端servlet的方法,并通过示例代码展示了前端JavaScript中的AJAX调用和后端servlet的接收处理。
这篇文章介绍了如何使用form表单结合Bootstrap格式将前端数据通过action属性提交到后端的servlet,包括前端表单的创建、数据的一级和二级验证,以及后端servlet的注解和参数获取。
|
3月前
|
负载均衡 应用服务中间件 网络安全
Django后端架构开发:Nginx服务优化实践
Django后端架构开发:Nginx服务优化实践
58 2
|
3月前
|
负载均衡 算法 应用服务中间件
揭秘!nginx的神秘力量——如何单枪匹马驾驭Linux中的流量洪流与后端军团?
【8月更文挑战第20天】在Linux环境下,Nginx是一款高效HTTP与反向代理服务器,还具备负载均衡功能。本文采用问答形式解析Nginx的反向代理机制:接收并转发客户端请求至后端服务器,增强安全性和性能。示例配置展示了如何设置上游服务器与代理传递规则。同时介绍了Nginx负载均衡原理及配置,利用upstream模块与不同调度算法(如轮询、最少连接、IP哈希)分发请求,提升系统可靠性和处理能力。
49 2

热门文章

最新文章