当前位置:   article > 正文

分布式--OpenResty+lua+Redis

openresty分布式

前面我们已经使用了nginx,它拥有丰富的模块供我们使用,由于nginx是由c语言编写而成的,所以以前编写模块就必须使用c/c++,后来,有人将lua解释器继承到了nginx中,内建了ngx_lua模块,至此,nginx支持了lua

一、OpenResty

OpenResty是基于nginx开源版本的一个扩展版本,集成了大量的lua库

1. 添加repo
  1. cd /etc/yum.repos.d/
  2. wget https://openresty.org/package/centos/openresty.repo
2. 安装openresty
yum install openresty
3. 启动openresty

openresty默认安装路径为/usr/local/openresty/,其中有个nginx目录,关于openresty的启动和配置都和以前nginx相同

cd /usr/local/openresty/nginx/conf

修改openresty的http模块配置,新增server模块,使用content_by_lua执行一段lua代码:

  1. server{
  2. listen 8080;
  3. location /{
  4. default_type text/html;
  5. content_by_lua 'ngx.say("hello openresty")';
  6. }
  7. }

启动openresty:

  1. cd /usr/local/openresty/nginx/sbin/
  2. ./nginx -p /usr/local/openresty/nginx/

浏览器访问8080端口:

二、http访问Redis

上面完成了nginx执行lua语句,接下来来看nginx如何访问Redis
Redis环境搭建可以看之前的文章:分布式--Redis的安装与数据类型的使用

1. 获取Redis数据

下面是通过nginx获取Redis中key对应的value

1.1 启动Redis

启动一个默认6379端口的Redis即可,下面还有我之前搭建的Redis集群:

1.2 修改nginx配置
  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. server {
  11. listen 80;
  12. server_name localhost;
  13. location / {
  14. default_type text/plain;
  15. # 设置获取redis中key为m的值
  16. set $redis_key "m";
  17. redis_pass 127.0.0.1:6379;
  18. # 如果发生404,就交由@fetch处理
  19. error_page 404 = @fetch;
  20. }
  21. location @fetch {
  22. root html;
  23. }
  24. }
  25. }

nginx重新加载配置:

./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-http.conf  -s reload
1.3 通过http访问
curl http://localhost/1.html

结果:

redis客户端设置m的键值对:

再次访问:

2. 设置Redis数据

上面方式1,只是获取了Redis的数据,那么如何设置Redis键值对呢?如果nginx支持redis的指令就好了,实际上nginx是支持的

2.1 修改nginx配置

可以使用redis2_query后面跟上redis的指令,来设置值,同样也能使用redis指令获取值:

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. server {
  11. listen 80;
  12. server_name localhost;
  13. location /get {
  14. # 设置个变量
  15. set_unescape_uri $key "n";
  16. redis2_query get $key;
  17. redis_pass 127.0.0.1:6379;
  18. # 如果发生404,就交由@fetch处理
  19. error_page 404 = @fetch;
  20. }
  21. location /set {
  22. # 设置个变量
  23. set_unescape_uri $key "n";
  24. redis2_query set $key "hello2";
  25. redis2_pass 127.0.0.1:6379;
  26. }
  27. location @fetch {
  28. root html;
  29. }
  30. }
  31. }

nginx重启:

  1. ./nginx -s stop
  2. ./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-http2.conf
2.2 访问测试
  1. curl http://localhost/get
  2. curl http://localhost/set

结果:

三、lua结合Nginx、Redis

官方介绍:https://github.com/openresty/lua-resty-redis
首先,OpenResty集成lua后,有三种调用lua的方式:

方式语法描述
xxx_by_lua最开始我们已经使用过了,它支持执行一条lua代码
xxx_by_lua_file该语句支持执行一个lua脚本文件,也是用的最多的
xxx_by_lua_block该语句支持执行一个lua代码块,针对一些简单场景使用

下面来使用lua与Redis进行交互

1. lua操作Redis数据

lua操作Redis,步骤是引入Redis模块,连接Redis,然后再操作

1.1 编写lua脚本

创建个目录存放lua脚本:

  1. mkdir lua
  2. vi control_redis.lua

内容为:

  1. -- 引入redis模块
  2. local redis = require("resty.redis")
  3. -- 创建个redis对象
  4. local red = redis:new()
  5. -- 1. 连接redis
  6. -- 多参数返回
  7. local ok,err = red:connect("127.0.0.1",6379)
  8. if not ok then
  9. ngx.say("connect failed:",err)
  10. return
  11. end
  12. -- 2. 设置redis的键值对
  13. ok,err = red:set("luaKey","luaValue")
  14. if not ok then
  15. ngx.say("set faild:",err)
  16. return
  17. end
  18. -- 3. 读取redis的键值对
  19. ret = red:get("luaKey")
  20. ngx.say("read luaKey value:",ret)
  21. return
1.2 修改nginx配置

使用content_by_lua_file指定lua脚本的绝对路径:

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. sendfile on;
  9. keepalive_timeout 65;
  10. server {
  11. listen 8090;
  12. server_name localhost;
  13. location / {
  14. default_type text/html;
  15. content_by_lua_file /usr/local/openresty/nginx/lua/control_redis.lua;
  16. }
  17. }
  18. }

重启nginx:

  1. ./nginx -s stop
  2. ./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf
1.3 测试
2. lua获取get请求参数
2.1 编写lua脚本
vi http_get.lua

使用ngx.req.get_uri_args()获取,内容为:

  1. -- 返回的是一个table类型
  2. local args = ngx.req.get_uri_args()
  3. for k,v in pairs(args) do
  4. ngx.say("key:"..k.."value:"..v)
  5. end
2.2 修改nginx配置

新增端口监听:

  1. server {
  2. listen 8091;
  3. server_name localhost;
  4. location /get {
  5. default_type text/html;
  6. content_by_lua_file /usr/local/openresty/nginx/lua/http_get.lua;
  7. }
  8. }

重新加载配置文件:

./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload
2.3 测试
http://192.168.42.4:8091/get?name=张三&age=19
3. lua获取post请求参数

post请求有两种:body键值对和body请求体。后者对应现在流行的json格式

3.1 编写lua脚本

post请求参数的获取都需要先调用ngx.req.read_body()方法

键值对:

vi http_post_kv.lua

使用ngx.req.get_post_args()获取,内容为:

  1. -- 先读取下
  2. ngx.req.read_body()
  3. -- 再获取
  4. local params = ngx.req.get_post_args()
  5. for k,v in pairs(params) do
  6. ngx.say("key:"..k.." value:"..v)
  7. end

请求体:

vi http_post_body.lua

使用ngx.req.get_body_data()获取,内容为:

  1. -- 先读取下
  2. ngx.req.read_body()
  3. -- 再获取请求体
  4. local body = ngx.req.get_body_data();
  5. ngx.say(body)
3.2 修改nginx配置
  1. server {
  2. listen 8092;
  3. server_name localhost;
  4. location /post_kv {
  5. default_type text/html;
  6. content_by_lua_file /usr/local/openresty/nginx/lua/http_post_kv.lua;
  7. }
  8. location /post_body {
  9. default_type text/html;
  10. content_by_lua_file /usr/local/openresty/nginx/lua/http_post_body.lua;
  11. }
  12. }

重新加载nginx配置:

./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload
3.3 测试

键值对:


请求体:

4. lua获取请求头
4.1 编写lua脚本
vi http_headers.lua

请求头通过ngx.req.get_headers()获取,内容为:

  1. local headers = ngx.req.get_headers()
  2. for k,v in pairs(headers) do
  3. ngx.say("key:"..k.." value:"..v)
  4. end
4.2 修改nginx配置
  1. server {
  2. listen 8093;
  3. server_name localhost;
  4. location /headers {
  5. default_type text/html;
  6. content_by_lua_file /usr/local/openresty/nginx/lua/http_headers.lua;
  7. }
  8. }

重新加载配置文件:

./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload
4.3 测试

以上就是openresty+lua+redis的基本使用

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号