前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >纯Lua模拟出oenrestry+lua下Hash一致性闭环代理分发

纯Lua模拟出oenrestry+lua下Hash一致性闭环代理分发

作者头像
Parker
发布2020-07-21 14:45:07
7680
发布2020-07-21 14:45:07
举报
文章被收录于专栏:在码圈在码圈

原理

Hash一致性闭环算法 - ( 适用于Redis扩容、Nginx多级缓存 等等 ) Github 开源地址

Lua模拟脚本

1. 配置文件
代码语言:javascript
复制
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by parker.
--- DateTime: 2020/5/6 11:19 下午
--- 配置类
---
server = {}

server.address = {
    "192.0.0.1",
    "192.0.0.2",
    "192.0.0.3",
    "192.0.0.4",
    "192.0.0.5"
}

return server
2. Hash一致性闭环分发算法
代码语言:javascript
复制
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by parker.
--- DateTime: 2020/5/6 11:17 下午
--- 纯Lua模拟出oenrestry+lua下Hash一致性闭环代理分发
---
local hashRequestInfo = {}

--- 初始化
hashRequestInfo.init = function()
    local that = hashRequestInfo

    --- 获得配置服务器地址
    local serverArr = require("config.ServerLocal")

    --- 保存服务器数组
    that.ipAddress = serverArr.address

    --- 模拟执行 用户访问
    that.requestMode()
end

--- 获得服务器索引 取模
hashRequestInfo.getServerIndex = function(args)
    local that = hashRequestInfo
    local count = #that.ipAddress
    local index = args % count + 1
    return index
end

--- 获得对应服务器映射地址
hashRequestInfo.getServerAddress = function(args)
    local that = hashRequestInfo
    local index = that.getServerIndex(args)
    local serverIpAddress = that.ipAddress[index]
    return serverIpAddress
end

--- 模拟执行
hashRequestInfo.requestMode = function()
    local that = hashRequestInfo
    
    --- 模拟 1000 个 用户访问
    for i = 1, 1000 do
        local randomNum = math.random(1000,100000)
        local serverAddress = that.getServerAddress(randomNum)
        print("访问服务器地址:http://"..serverAddress)
    end

end

--- 执行
hashRequestInfo.init()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-06-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原理
  • Lua模拟脚本
    • 1. 配置文件
      • 2. Hash一致性闭环分发算法
      相关产品与服务
      云数据库 Redis
      腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档