openresty开发系列29--openresty中发起http请求

openresty开发系列29--openresty中发起http请求

有些场景是须要nginx在进行请求转发

用户浏览器请求url访问到nginx服务器,但此请求业务须要再次请求其余业务;
如用户请求订单服务获取订单详情,可订单详情中须要返回商品信息,也就须要再请求商品服务获取商品信息;
这样就须要nginx须要有发起http请求的能力,而不是让用户浏览器再次请求商品信息

nginx服务发起http请求区份内部请求 和 外部请求

图解

下面咱们就介绍一下,openResty中如何发起http请求?

一)内部请求

1)capture请求方法

res = ngx.location.capture(uri,{
    options?
});

options能够传参数和设置请求方式

local res = ngx.location.capture("/product",{
    method = ngx.HTTP_GET,   #请求方式
    args = {a=1,b=2},  #get方式传参数
    body = "c=3&d=4" #post方式传参数
});

res.status --->保存子请求的响应状态码
res.header --->用一个标准 Lua 表储子请求响应的全部头信息。若是是"多值"响应头,
           --->这些值将使用 Lua (数组) 表顺序存储。

res.body   --->保存子请求的响应体数据,它可能被截断。
           --->用户须要检测 res.truncated (截断) 布尔值标记来判断 res.body 是否包含截断的数据。
           --->这种数据截断的缘由只多是由于子请求发生了不可恢复的错误,
           --->例如远端在发送响应体时过早中断了链接,或子请求在接收远端响应体时超时。

res.truncated --->是否截断

-----------------------------------------

编辑nginx.conf配置文件,配置一下路由,定义有个两个服务请求 商品服务请求和订单服务请求

location /product {  #商品服务请求
    echo "商品请求";
}
        
location /order {  #订单服务请求
    content_by_lua_block {
        local res = ngx.location.capture("/product");
        ngx.say(res.status)
        ngx.say(res.body)
    }
}

ngx.location.capture 方法就是发起http的请求,可是它只能请求 内部服务,不能直接请求外部服务

输出结果,http状态为200,返回了 商品服务中的内容

------------------------------------------------

这边有一种状况,这样的定义,用户用浏览器直接请求商品服务也照样请求

可不少时候咱们会要求商品请求 是不对外暴露的,也就是用户没法直接访问商品服务请求。
那咱们只要在内部请求那边加上一个关键字,internal 就能够了
location /product {  #商品服务请求
    internal;
    echo "商品请求";
}
这样直接访问就报404错误了


-----------------post 请求-----------------

location /product {  #商品服务请求
    content_by_lua_block {
        ngx.req.read_body();
        local args = ngx.req.get_post_args()
        ngx.print(tonumber(args.a) + tonumber(args.b))
    }
}
        
location /order {  #订单服务请求
    content_by_lua_block {
        local res = ngx.location.capture("/product",{
            method = ngx.HTTP_POST,  
            args = {a=1,b=2},  
            body = "a=3&b=4"
        });
        ngx.say(res.status)
        ngx.say(res.body)
    }
}



2)capture_multi 并发请求
再以上基础上面增长需求,要得到用户信息
正常逻辑是串行: order request ---> product request ---> user request ----> end
提升性能的方式,发送product请求的同时发起user请求:   order request ---> product request
                                   ---> user request      ----> end


语法:res1,res2, ... = ngx.location.capture_multi({
                                {uri, options?},
                                {uri, options?},
                                ...
                        })

-----并发调用
location = /sum {
    internal;
    content_by_lua_block {
        ngx.sleep(0.1)
        local args = ngx.req.get_uri_args()
        ngx.print(tonumber(args.a) + tonumber(args.b))
    }
}

location = /subduction {
    internal;
    content_by_lua_block {
        ngx.sleep(0.1)
        local args = ngx.req.get_uri_args()
        ngx.print(tonumber(args.a) - tonumber(args.b))
    }
}

location = /app/test_multi {
    content_by_lua_block {
        local start_time = ngx.now()
        local res1, res2 = ngx.location.capture_multi( {
                        {"/sum", {args={a=3, b=8}}},
                        {"/subduction", {args={a=3, b=8}}}
                    })
        ngx.say("status:", res1.status, " response:", res1.body)
        ngx.say("status:", res2.status, " response:", res2.body)
        ngx.say("time used:", ngx.now() - start_time)
    }
}

location = /app/test_queue {
    content_by_lua_block {
        local start_time = ngx.now()
        local res1 = ngx.location.capture("/sum", {
                        args={a=3, b=8}
                    })
        local res2 = ngx.location.capture("/subduction", {
                        args={a=3, b=8}
                    })
        ngx.say("status:", res1.status, " response:", res1.body)
        ngx.say("status:", res2.status, " response:", res2.body)
        ngx.say("time used:", ngx.now() - start_time)
    }
}

访问:http://10.11.0.215/app/test_queue
返回:status:200 response:11 status:200 response:-5 time used:0.22399997711182

访问:http://10.11.0.215/app/test_multi
返回:status:200 response:11 status:200 response:-5 time used:0.10199999809265

从处理的时间长度看multi并发比queue队列要快一倍左右

二)外部请求

如何发起外部请求呢?
由于ngx.location.capture不能直接发起外部请求,咱们须要经过内部请求中用反向代理请求发起外部请求

请求tmall和淘宝搜索的时候直接经过浏览器能够搜索,可是使用反向代理后没法正常返回内容,天猫和淘宝作了反爬虫处理
    location /tmall {
                internal;
                proxy_pass "https://list.tmall.com/search_product.htm?q=ipone";
        }

        location /ordertmall {
                content_by_lua_block {
                        local res = ngx.location.capture("/tmall");
                        ngx.say(res.status)
                        ngx.say(res.body)
                }
        }


相似的例子:

    location /baidu {
        internal;
        # 防止返回乱码
        proxy_set_header Accept-Encoding   ' ';
        proxy_pass "https://www.baidu.com/s?wd=iphone";
        #proxy_pass "https://www.baidu.com";
    }

    location /orderbaidu {
        content_by_lua_block {
            local res = ngx.location.capture("/baidu");
            ngx.say(res.status)
            ngx.say(res.body)
        }
    }


在商品服务那边用的proxy_pass 请求外部http请求,这样就达到了请求外部http的目的。

请求返回了302,表示请求成功了;

但发现都是乱码,这个是什么缘由呢?
一开始想到的是字符编码的问题,须要把虚拟主机的server模块配置一个字符编码
charset UTF-8;   设置为utf-8。重启nginx从新访问
仍是乱码,这是为何呢,编码不是改了吗?这个是由于baidu这个web服务器加了gzip压缩,
他返回给咱们的结果是通过压缩的,咱们再接受过来的时候须要解压才行,那怎么办?
咱们可让taobao服务返回不须要压缩的数据吗?  咱们能够在请求外部连接那边设置
proxy_set_header Accept-Encoding   ' ';#让后端不要返回压缩(gzip或deflate)的内容
最终
    location /baidu {
        internal;
        # 防止返回乱码
        proxy_set_header Accept-Encoding   ' ';
        proxy_pass "https://www.baidu.com/s?wd=iphone";
        #proxy_pass "https://www.baidu.com";
    }
重启nginx,再次访问,结果输出

以上咱们介绍了 内部访问 和  外部访问

三)动态变量

刚才咱们请求外部请求,是写死了wd=iphone,那咱们用capture传参

    location /baidu02 {
        internal;
        resolver 8.8.8.8;
        proxy_set_header Accept-Encoding ' ';
        proxy_pass "https://www.baidu.com/s?wd=$arg_wd";
    }

    location /orderbaidu02 {
        content_by_lua_block {
            local get_args = ngx.req.get_uri_args();
            local res = ngx.location.capture("/baidu02",{
                method = ngx.HTTP_GET,
                args = {wd=get_args["wd"]}
            });
            ngx.say(res.status)
            ngx.say(res.body)
        }
    }

访问:http://192.168.10.164/orderbaidu02?wd=huawei
添加搜索参数 wd=huawei就会返回华为手机的结果

注意:在proxy_pass 中使用变量,须要使用resolver指令解析变量中的域名

# google 域名解析
resolver 8.8.8.8;


这样咱们请求传wd参数的值,随便由用户决定查询什么值。
咱们这边就发现了请求外部服务的时候发现比较复杂,咱们能够借用第三方的库 resty.http ,
从可实现外部请求,并且使用很方便,下篇文章咱们就介绍resty.http模块nginx

相关文章
相关标签/搜索