企业网站销售,省级建设主管部门网站,opencart做的网站,网站建设服务费一年多少钱目录
1、前言
2、接口流复制
2.1、方式一#xff1a;使用mirror指令
2.1.1、nginx配置
2.1.2、配置说明
2.1.3、测试结果
2.1.4、注意事项
2.2、方式二#xff1a;使用Lua
2.2.1、安装Openresty
2.2.2、nginx配置
2.2.3、配置说明
2.2.4、测试结果
3、小结 1、前…目录
1、前言
2、接口流复制
2.1、方式一使用mirror指令
2.1.1、nginx配置
2.1.2、配置说明
2.1.3、测试结果
2.1.4、注意事项
2.2、方式二使用Lua
2.2.1、安装Openresty
2.2.2、nginx配置
2.2.3、配置说明
2.2.4、测试结果
3、小结 1、前言
项目中通常会遇到一个中转服务需要往多个不同的系统推送同一份数据传统做法是需要在Java代码侧中调用多个API接口进行发送。其实Nginx作为一个请求代理转发中间件必然具备类似的功能常见就有mirror指令进行流的镜像复制。
2、接口流复制
2.1、方式一使用mirror指令 注意要使用nginx的mirror指令需要nginx安装ngx_http_mirror_module模块。可以通过nginx -V命令查看。nginx 1.13.4及后续版本已经内置了ngx_http_mirror_module模块之前的版本需要手动编译安装。 示例场景
发送一个主请求端口8080nginx同时转发到8081服务和8082服务8081和8082各自access log都有访问记录且状态正常。
开撸我这里准备的nginx版本为1.20.1。
2.1.1、nginx配置
server {listen 8080;index index.php index.html;server_name localhost;location / {mirror /mirror;proxy_pass http://localhostServer;}location /mirror {internal;proxy_pass http://mirrorServer$request_uri;}
}upstream localhostServer {server localhost:8081;
}upstream mirrorServer {server localhost:8082;
}server {listen 8081;server_name localhost;access_log /var/log/nginx/8081-access.log;root html/local;
}
server {listen 8082;server_name localhost;access_log /var/log/nginx/8082-access.log;root html/mirror;
}
接着在nginx的html目录下创建local目录和mirror目录并创建文件test.html。test.html内容随便填写
mkdir -p local mirror
touch test.html
echo local.test--- test.html test.html
echo mirror.test--- test.html test.html
2.1.2、配置说明
主请求/ 会将请求转发到 localhostServer。镜像请求配置了 mirror 指令将请求同时复制一份转发到 /mirror。/mirror 使用了 internal防止客户端直接访问。镜像服务/mirror 会将请求转发到 mirrorServer。
2.1.3、测试结果
由于8081和8082服务都配置了access log因此当我们访问http://localhost:8080时预期的结果是访问请求能得到正常的回显并且8081和8082服务的access log都有相应的访问日志。 8081-access.log 8082-access.log 这样就完成了nginx实现接口复制的功能。Nginx 实现接口复制的需求通常用于在接收到请求后将请求数据转发到多个后端服务器例如用于日志记录、监控或者负载分摊。
2.1.4、注意事项
镜像请求是非阻塞的Nginx 不会等待镜像请求的响应。镜像功能仅支持 HTTP 请求不支持 WebSocket 或其他非 HTTP 协议。
2.2、方式二使用Lua
Nginx支持Lua脚本有2种方式
手动编译nginx加入ngx_http_lua_module模块安装openresty使用该组件的Nginx OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。 2.2.1、安装Openresty
下载最新版本的openrestyOpenResty - 下载。这里下载的是1.27.1.1版本。 这里下的是源码包需要重新编译。 注这里直接编译会使用QAT硬件加速你需要手动安装QAT_Engine依赖。 这里直接禁用硬件加速编译时排除qat
./configure --without-http_qat_module
# 安装
make
sudo make install# 这里还需要安装resty.http模块/usr/local/openresty/bin/opm get ledgetech/lua-resty-http
安装完成后启动openresty的nginx
/usr/local/openresty/nginx/sbin/nginx
2.2.2、nginx配置
server {listen 8090;location / {content_by_lua_block {local http require resty.httplocal res ngx.location.capture(/localServer)local httpc http.new()httpc:set_timeout(2000)local ok, err httpc:request_uri(http://127.0.0.1:8092/test.html, {method ngx.var.request_method,body ngx.var.request_body,headers ngx.req.get_headers(),follow_redirects true,})if not ok thenngx.log(ngx.ERR, Mirror request failed: , err)endngx.say(res.body)}}location /localServer {proxy_pass http://127.0.0.1:8091;}}server {listen 8091;server_name 127.0.0.1;access_log /usr/local/openresty/nginx/logs/8091-access.log;root html/local;}server {listen 8092;server_name 127.0.0.1;access_log /usr/local/openresty/nginx/logs/8092-access.log;root html/mirror;}
2.2.3、配置说明
主请求/ 会先处理主服务的请求通过 ngx.location.capture 调用 /localServer。镜像请求使用 Lua 的 resty.http 库手动发起 HTTP 请求将数据复制到镜像服务器。返回响应将主请求的结果返回给客户端。
2.2.4、测试结果
直接访问请求curl http://localhost:8090/test.html
8901-access.log 8902-access.log 3、小结
实时请求复制推荐使用 ngx_http_mirror_module简单易用。高级控制如果需要复杂逻辑使用 ngx_http_lua_module 配合 Lua 脚本。离线分析通过日志记录请求数据然后离线处理。