Centos7 openresty使用Lua连接Redis集群(包括单机版redis)

短命女 2023-02-12 08:25 68阅读 0赞

" class="reference-link">watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIyMDQ5Nzcz_size_16_color_FFFFFF_t_70

1、新增 lua-resty-redis-cluster 模块

连接redis集群需要用到 lua-resty-redis-cluster模块

github地址: https://github.com/cuiweixie/lua-resty-redis-cluster

网盘:我的代码工程—》jar包—》lua-resty-redis-cluster-master

下载完成之后,只需要用到包中的2个文件rediscluster.lua和redis_slot.c

复制包中的 redis_slot.c 到openresty安装目录的lualib下,rediscluster.lua到lualib下的resty下

.c文件无法在Nginx配置文件中引入,需要编译成**.so**文件,编译命令

  1. # 安装gcc、c++编译器以及内核文件
  2. yum -y install gcc gcc-c++ kernel-devel
  3. # centos自带lua需要执行此命令再编译,自己安装过lua不需要
  4. yum install lua-devel
  5. #编译命令
  6. gcc redis_slot.c -fPIC -shared -o libredis_slot.so
  7. #查看结果
  8. ll

编辑结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIyMDQ5Nzcz_size_16_color_FFFFFF_t_70 1

2、编写redis-lua脚本

结构

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzIyMDQ5Nzcz_size_16_color_FFFFFF_t_70 2

nginx.conf配置文件中http下添加

  1. #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
  2. lua_package_path "/opt/openresty/lualib/?.lua;;/opt/openresty/nginx/conf/openResty/?.lua;;"; #lua 模块
  3. lua_package_cpath "/opt/openresty/lualib/?.so;";
  4. include /opt/openresty/nginx/conf/openResty/openResty.conf;

nginx.conf完成内容

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #日志格式
  14. log_format my_format '$remote_addr^A$msec^A$http_host^A$request_uri';
  15. #access_log logs/access.log main;
  16. sendfile on;
  17. #tcp_nopush on;
  18. #keepalive_timeout 0;
  19. keepalive_timeout 65;
  20. upstream lua.redis.com{
  21. server 192.168.164.25:8090;
  22. }
  23. #gzip on;
  24. #lua模块路径,多个之间”;”分隔,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找
  25. lua_package_path "/opt/openresty/lualib/?.lua;;/opt/openresty/nginx/conf/openResty/?.lua;;"; #lua 模块
  26. lua_package_cpath "/opt/openresty/lualib/?.so;";
  27. include /opt/openresty/nginx/conf/openResty/openResty.conf;
  28. }

/opt/openresty/nginx/conf/openResty目录下新增openResty.conf

  1. server {
  2. listen 80;
  3. server_name lua.redis.com;
  4. #单机版redis
  5. location /lua_redis_single {
  6. default_type 'text/html';
  7. charset utf-8;
  8. lua_code_cache off;
  9. content_by_lua_file conf/openResty/lua/redis/redis-util.lua;
  10. }
  11. #集群版redis
  12. location /lua_redis {
  13. default_type 'text/html';
  14. charset utf-8;
  15. lua_code_cache off;
  16. content_by_lua_file conf/openResty/lua/redis/redis-cluster.lua;
  17. }
  18. #redis中k为空则将请求转发到当前后台服务器进行查询
  19. location /redis {
  20. default_type 'text/plain';
  21. proxy_pass http://lua.redis.com;
  22. }
  23. }

/opt/openresty/nginx/conf/openResty/lua/redis目录下新增redis-cluster.lua

  1. local config = {
  2. name = "test",
  3. serv_list = {
  4. {ip="192.168.164.24", port = 7000},
  5. {ip="192.168.164.24", port = 7001},
  6. {ip="192.168.164.24", port = 7002},
  7. {ip="192.168.164.24", port = 7003},
  8. {ip="192.168.164.24", port = 7004},
  9. {ip="192.168.164.24", port = 7005},
  10. }
  11. }
  12. -- 获取连接
  13. local redis_cluster = require "resty.rediscluster"
  14. local red = redis_cluster:new(config)
  15. --Nginx服务器中使用lua获取getpost参数
  16. local request_method = ngx.var.request_method
  17. local args = nil
  18. local param = nil
  19. --获取url参数的值
  20. if "GET" == request_method then
  21. args = ngx.req.get_uri_args()
  22. elseif "POST" == request_method then
  23. ngx.req.read_body()
  24. args = ngx.req.get_post_args()
  25. end
  26. param = args["k"]
  27. -- 使用pipeline操作业务
  28. -- red:init_pipeline()
  29. local result=red:get(param)
  30. -- local results = red:commit_pipeline()
  31. local cjson = require "cjson"
  32. if result == ngx.null then
  33. -- 转发到后台tomcat服务器去查询并且缓存
  34. local resp = ngx.location.capture("/redis/getV?k="..param)
  35. result = resp.body
  36. end
  37. ngx.say(cjson.encode(result))
  38. red:close()

redis springboot工程

  1. /**
  2. * 用户测试nginx-lua-redis,当前工程作为后端都服务器工程使用
  3. * @param id
  4. * @return
  5. */
  6. @RequestMapping("/redis/getV")
  7. public String getRedis(String k){
  8. String v = (String)redisService.get(k);
  9. if(StringUtils.isBlank(v)) {
  10. v=k+"东哥";
  11. redisService.set(k, v);
  12. System.out.println("----------tomcat-web-project----------------:"+v);
  13. }
  14. return v;
  15. }

测试:

访问 http://lua.redis.com/lua_redis?k=myName 请求lua_redis

  1. [http://lua.redis.com/redis/getV?k=myName][http_lua.redis.com_redis_getV_k_myName] 跳过lua\_redis,直接访问redis springboot工程

首先,访问lua_redis

20200525180501227.png

去redis库中查找该key

20200525181105395.png

访问,redis springboot工程,说明后台工程时通的

20200525180635251.png

将springboot工程 kill掉,再访问lua_redis,此时证明lua可以从redis直接get数据

20200525180750539.png

从reids数据库中删除掉 myName这个key

20200525181134499.png

再次访问lua_redis

20200525181203295.png

启动springboot工程,再次访问redis springboot工程,如果没有该值会set该值

补充:连接单机版redis

redis-util.lua

  1. local function close_redes( red )
  2. if not red then
  3. return
  4. end
  5. -- 释放连接(连接池实现)
  6. local pool_max_idle_time = 10000 -- 毫秒
  7. local pool_size = 100 --连接池大小
  8. local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
  9. if not ok then
  10. ngx.say("set keepalive error : ", err)
  11. end
  12. end
  13. local redis = require("resty.redis")
  14. -- 创建实例
  15. local red = redis:new()
  16. -- 设置超时(毫秒)
  17. red:set_timeout(2000)
  18. -- 建立连接
  19. local ip = "192.168.2.110"
  20. local port = 6379
  21. local ok, err = red:connect(ip, port)
  22. if not ok then
  23. return
  24. end
  25. local res, err = red:auth("jinku_redis")
  26. if not res then
  27. ngx.say("connect to redis error : ", err)
  28. return
  29. end
  30. -- 选择db 3
  31. ok, err = red:select(3)
  32. if not ok then
  33. ngx.say("failed to select db: ", err)
  34. return
  35. end
  36. red:init_pipeline()
  37. red:set("msg1", "hello1")
  38. red:set("msg2", "hello2")
  39. red:get("msg1")
  40. red:get("msg2")
  41. local respTable, err = red:commit_pipeline()
  42. -- 得到数据为空处理
  43. if respTable == ngx.null then
  44. respTable = {}
  45. end
  46. -- 结果是按照执行顺序返回的一个table
  47. for i, v in ipairs(respTable) do
  48. ngx.say("msg : ", v, "<br/>")
  49. end
  50. close_redes(red)

发表评论

表情:
评论列表 (有 0 条评论,68人围观)

还没有评论,来说两句吧...

相关阅读