Centos7 openresty使用Lua连接Redis集群

悠悠 2023-07-11 05:46 53阅读 0赞

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

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

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

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

  1. gcc SOURCE_FILES -fPIC -shared -o TARGET
  2. # centos自带lua需要执行此命令再编译,自己安装过lua不需要
  3. yum install lua-devel
  4. #编译命令
  5. gcc redis_slot.c -fPIC -shared -o libredis_slot.so
  6. [hadoop@slave2 lualib]$ ls
  7. cjson.so libredis_slot.so lua2redis.lua ngx rds redis redis_slot.c resty

如下是连接redis集群代码:

  1. local config = {
  2. name = "test",
  3. serv_list = {
  4. {ip="192.168.28.100", port = 7001},
  5. {ip="192.168.28.100", port = 7002},
  6. {ip="192.168.28.100", port = 7003},
  7. {ip="192.168.28.100", port = 7004},
  8. {ip="192.168.28.100", port = 7005},
  9. {ip="192.168.28.100", port = 7006},
  10. },
  11. }
  12. local redis_cluster = require "resty.rediscluster"
  13. local red = redis_cluster:new(config)
  14. red:set("s1", "1000")
  15. red:get("s1")
  16. red:close()

发表评论

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

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

相关阅读

    相关 centos7 搭建redis

    简单说明: 我们的集群结构: 我们的集群有三个主节点,每个主节点有一个从节点,一共有6个节点。都是在一台机器上创建多个redis实例来实现的。 1.集群环境的安装 1.

    相关 CentOS7搭建redis

    客户是个关系户...  开设好centos7虚拟机的同时还得搭建好redis集群。安装过程很顺利,没有遇到什么坑。 环境介绍:两台centos7系统+redis 4.0.11