• 53_部署分发层nginx以及基于lua完成基于商品id的定向流量分发策略



    大家可以自己按照上一讲讲解的内容,基于OpenResty在另外两台机器上都部署一下nginx+lua的开发环境

    我已经在eshop-cache02和eshop-cache03上都部署好了

    我这边的话呢,是打算用eshop-cache01和eshop-cache02作为应用层nginx服务器,用eshop-cache03作为分发层nginx

    在eshop-cache03,也就是分发层nginx中,编写lua脚本,完成基于商品id的流量分发策略

    当然了,我们这里主要会简化策略,简化业务逻辑,实际上在你的公司中,你可以随意根据自己的业务逻辑和场景,去制定自己的流量分发策略

    1、获取请求参数,比如productId
    2、对productId进行hash
    3、hash值对应用服务器数量取模,获取到一个应用服务器
    4、利用http发送请求到应用层nginx
    5、获取响应后返回

    这个就是基于商品id的定向流量分发的策略,lua脚本来编写和实现

    我们作为一个流量分发的nginx,会发送http请求到后端的应用nginx上面去,所以要先引入lua http lib包

    cd /usr/hello/lualib/resty/
    wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua
    wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua


    keepalive=false,必须要加,不然会报错
    代码:

    local uri_args = ngx.req.get_uri_args()
    local productId = uri_args["productId"]

    local host = {"192.168.31.19", "192.168.31.187"}
    local hash = ngx.crc32_long(productId)
    hash = (hash % 2) + 1
    backend = "http://"..host[hash]

    local method = uri_args["method"]
    local requestBody = "/"..method.."?productId="..productId

    local http = require("resty.http")
    local httpc = http.new()

    local resp, err = httpc:request_uri(backend, {
    method = "GET",
    path = requestBody,
    keepalive=false
    })

    if not resp then
    ngx.say("request error :", err)
    return
    end

    ngx.say(resp.body)

    httpc:close()

    /usr/servers/nginx/sbin/nginx -s reload

    http://192.168.194.133/hello?requestPath=hello&productId=1
    基于商品id的定向流量分发策略的lua脚本就开发完了,而且也测试过了

    我们就可以看到,如果你请求的是固定的某一个商品,那么就一定会将流量打到固定的一个应用nginx上面去

  • 相关阅读:
    java实现模拟登陆
    springboot整合kafka(转)
    springboot使用缓存(三)
    springboot使用缓存(二)
    springboot使用缓存(一)
    springboot日期入参出参格式化注解:@DateTimeFormat 和 @JsonFormat (转)
    H5调用android相机拍照
    时间复杂度
    java guide面试突击版
    java面试手册-复制内容
  • 原文地址:https://www.cnblogs.com/hg-super-man/p/12783059.html
Copyright © 2020-2023  润新知