1 简介

  https://github.com/ledgetech/lua-resty-http

  在lua中操作http请求有两种方式

  第一种方式:使用通过ngx.location.capture 去方式实现

  第二种方式:lua-resty-http,是用于访问外部 Http 资源,外部 web 服务,RESTFul 等的轻量级 http 库。因为openresty默认没有引入lua-resty-http,所以需要自行下载。

2 下载安装

2.1 下载解压

  https://github.com/ledgetech/lua-resty-http

 

2.2 上传

  将解压后的下面三个文件上传到openresty/lualib/resty目录下

 

3 http使用示例

3.1 修改nginx配置文件

  

  在http下加一行配置

resolver 8.8.8.8;

  在server加一个location配置

location /http {
default_type text/html;
content_by_lua_file lua/lua-resty-http.lua;
}

  

3.2 添加文件lua-resty-http.lua

内容

local http = require("resty.http")  

local httpc = http.new()  

local resp, err = httpc:request_uri("http://www.sogou.com", {
method = "GET",
path = "/web?query=resty.http",
headers = {
["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
}
}) if not resp then
ngx.say("request error :", err)
return
end ngx.status = resp.status for k, v in pairs(resp.headers) do
if k ~= "Transfer-Encoding" and k ~= "Connection" then
ngx.header[k] = v
end
end ngx.say(resp.body) httpc:close()

3.3 重启后访问

4 lua-resty-http实现一致性hash负载均衡简要示例

  现在有两个服务可以访问,分别为192.168.28.111,192.168.28.112

4.1 修改ngxin配置文件

  在server下加一个location

4.2 添加文件lua-resty-http-hash-lb.lua

内容

local http = require("resty.http")
local httpc = http.new()
-- 服务ip
local hosts = {"192.168.28.111","192.168.28.112"}
-- 获取请求参数id
local item_id= ngx.var.id
-- 获取id的hash值
local id_hash = ngx.crc32_long(item_id)
-- 取余
local index = (id_hash % 2) +1
-- 发起请求,根据余数确定向哪个服务发起请求
local resp, err = httpc:request_uri("http://"..hosts[index], {
method = "GET",
path = "/sogou?query=resty.http",
headers = {
["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
}
}) if not resp then
ngx.say("request error :", err)
return
end ngx.say(resp.body) httpc:close()

4.3 重启后访问

  http://192.168.28.110:8099/hashlb?id=1

  http://192.168.28.110:8099/hashlb?id=2

  http://192.168.28.110:8099/hashlb?id=3

  http://192.168.28.110:8099/hashlb?id=4

  http://192.168.28.110:8099/hashlb?id=5

  分别看实际访问的哪个服务

最新文章

  1. swift-字符和字符串
  2. js键盘事件和焦点事件
  3. CentOS 6 minimal 网络配置
  4. dsu + lca
  5. 最新samba.tar.gz安装方法
  6. 苹果dock效果
  7. mybatis设置callSettersOnNulls解决返回字段不全的问题
  8. Python 回溯算法
  9. mac中更改xampp的根目录
  10. gitlab 之 cicd
  11. 《高性能JavaScript》--读书笔记
  12. Bootloader升级方式一————擦、写flash在RAM中运行
  13. 洛谷T31039 九尾狐吃棉花糖
  14. IntelliJ IDEA载入JDBC驱动包
  15. $(function() {....}) ,(function($){...})(jQuery)
  16. LoadRunner的函数
  17. EntityFramework的linq扩展where
  18. plsql oracle 使用教程
  19. Android-Window(一)——初识Window
  20. 【翻译】What is State Machine Diagram(什么是状态机图)?

热门文章

  1. tool1
  2. 关于li标签的相关css属性
  3. 第2-4-5章 规则引擎Drools高级语法-业务规则管理系统-组件化-中台
  4. SSH(六)hibernate持久层模板于事务管理
  5. 根据经纬度算UTM带号
  6. JavaEE Day02MySQL
  7. java并发数据结构之CopyOnWriteArrayList
  8. 帮你短时间拿下Git,Git详细教程(浓缩的都是精华)
  9. Spring IOC源码(二):IOC容器之 刷新前的准备
  10. JavaScript:变量:声明和赋值变量时,内存结构是什么样的?