OpenResty上各种测试用例实操(1),openresty用例


下面记录了我阅读《OpenResty最佳实践》过程中对一些例子的实践操作示例,记录以作备忘。

一.Location的组合使用

演示一个location只作为内部调用接口,被另一个location调用

reload配置文件后,在终端输入

更复杂一些的情况:

有多个子请求来并行或串行调用,它们的执行时间的比较,下面是改造后的nginx.conf

worker_processes 1;
error_log logs/error.log;
events {
	worker_connections 1024;
}

http {
	server{
		listen 6699;
                location = /sum {
                        internal;
                        content_by_lua_block{
				ngx.sleep(0.1)
				local args = ngx.req.get_uri_args()
				ngx.say(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.say(tonumber(args.a) - tonumber(args.b))
			}
		}

                location = /app/test_parallels {
			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_multi({
						{"/sum", {args={a=3, b=8}}}
                                })
				local res2 = ngx.location.capture_multi({
                                                {"/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 / {
			default_type text/html;
          
                        content_by_lua_block {
				ngx.say("Hello World!")
                        }
		}
	}
}

检查nginx.conf语法正确后,热加载nginx


在命令行输入如下命令进行对比测试,发现当两个子请求没有依赖关系时,并行比串行效率高一倍

另一个场景是,外部重定向,可以跨域名的。例如从 A 网站跳转到 B 网站是绝对允许的。在 CDN 场景的大量下载应用中,一般分为调度、存储两个重要环节。调度就是通过根据请求方 IP 、下载文件等信息寻找最近、最快节点,应答跳转给请求方完成下载。

下面是配置文件内容


语法检查无误后,重新加载生效

在命令行输入如下两个命令测试

参考文献

[1].https://moonbingbing.gitbooks.io/openresty-best-practices/content/openresty/work_with_location.html

相关内容

    暂无相关文章