缘由

之前总在想像 http、https 的 get 等接口都是直接可以调用的,能直接生成用例就好了

思路

最近接触到 Nginx,于是了解了一下流量复制,学习了 lua 脚本
通过 nginx 正向代理,对流量数据进行复制,
对指定的请求解析过滤,
将解析的结果丢到 spring 服务进行保存

前置

  1. openresty 安装
  2. lua http 模块安装

主要问题

server {
    listen 7000;

    location / {
        mirror /mirror;
        resolver 8.8.8.8;
        proxy_pass $scheme://$http_host$request_uri;
    }

    location /mirror {
        proxy_pass http://127.0.0.1:7001/;
    }

}

local cjson = require("cjson")

local log_json = {}  
log_json["headers"] = ngx.req.get_headers()  
log_json["uri_args"] = ngx.req.get_uri_args()  
log_json["body"] = ngx.req.read_body()  
log_json["http_version"] = ngx.req.http_version()  
log_json["method"] =ngx.req.get_method() 
log_json["body_data"] = ngx.req.get_body_data()  

local message = cjson.encode(log_json);

local http = require "resty.http"
local httpc = http.new()
local resp, err = httpc:request_uri(
        "http://127.0.0.1:9980/本地spring服务地址",
        {
                method="POST",
                body=messag,
                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"
                }
        }
)

openresty 安装 mac

brew install erlang elixir openssl expat libyaml libiconv libgd sqlite rebar rebar3 automake autoconf 

export LDFLAGS="-L/usr/local/opt/openssl/lib -L/usr/local/lib -L/usr/local/opt/expat/lib"
export CFLAGS="-I/usr/local/opt/openssl/include/ -I/usr/local/include -I/usr/local/opt/expat/include"
export CPPFLAGS="-I/usr/local/opt/openssl/include/ -I/usr/local/include -I/usr/local/opt/expat/include"
brew install openresty

拓展

  1. 线上引流大概也是用的 nginx 吧
  2. 使用消息队列做成异步处理,因为正向请求的数据可能会很多,可能比 spring 服务处理的快

参考

  1. openresty 最佳实践:https://moonbingbing.gitbooks.io/openresty-best-practices


↙↙↙阅读原文可查看相关链接,并与作者交流