gor实现线上HTTP流量复制压测引流

一、使用背景

gor 是一款go语言实现的简单的http流量复制工具,它的主要目的是使你的生产环境HTTP真实流量在测试环境和预发布环境重现。只需要在 代理例如nginx入口服务器上执行一个进程,就可以把生产环境的流量复制到任何地方,

完美解决了HTTP 层实时流量复制和压力测试的问题。常见的HTTP流量copy工具还有另外一款tcpcopy。将机器A上的http请求复制转发到指定机器B上去,

通过线上流量复制引流,通过将真实请求流量放大N倍来进行压测,能对服务有一个较为全面的检验。

二、安装

前往官方下载:https://github.com/buger/goreplay/releases/download/v1.0.0/gor_1.0.0_x64.tar.gz

gor实现线上HTTP流量复制压测引流

例如,现在将测试环境的流量拷贝到我的性能测试环境中

测试环境的:

http://zichan-manage.xxxxx.com:8080

gor实现线上HTTP流量复制压测引流

性能测试环境:

http://dh-manage.xxxx.com:8080

gor实现线上HTTP流量复制压测引流

环境结构说明:

172.20.20.111为性能测试环境的nginx服务器

172.20.20.114为性能测试环境的tomcat网关服务器

172.20.20.115为性能测试环境的app后端服务

在Linux服务器上直接执行以下命令即可:

安装方法很简单:

wget https://github.com/buger/goreplay/releases/download/v0.16.1/gor_0.16.1_x64.tar.gz
tar -xf gor_0.16.1_x64.tar.gz
cp gor /usr/bin/ #将解压后的gor文件复制到/usr/bin下,就可以开始使用了
gor -h

gor实现线上HTTP流量复制压测引流

gor实现线上HTTP流量复制压测引流

gor –input -raw :80 –output -stdout

三、应用场景

3.1 流量实时复制引流(–input-raw 拦截端口配合–output-http输出)

3.1.1 当对服务功能进行了改造,不能直接上线,需要真实请求来做验证,可以用流量复制引流

gor --input-raw :8080 --output-http "http://target_server:8080"
#将本机8080端口的HTTP流量复制到targer_server:8080

gor实现线上HTTP流量复制压测引流

在测试环境执行登录操作:

gor实现线上HTTP流量复制压测引流

在性能测试环境172.20.20.111环境的nginx中日志查看请求日志:

gor实现线上HTTP流量复制压测引流

说明171上的数据已经被copy过来

同时查看111环境后的web tomcat环境的日志:

gor实现线上HTTP流量复制压测引流

说明请求也copy到tomcat层

为了验证的问题更加简明扼要,我只抓取其中一个登录的接口:

首先在功能测试环境开启gor监控

gor实现线上HTTP流量复制压测引流

–http-allow-method POST –http-allow-url /debtoauth2/user/login

然后在tail一下172.20.20.111环境的日志,查看从171上copy过来的日志,是否copy过来

gor实现线上HTTP流量复制压测引流

查看111:

尾号是0006的手机号

gor实现线上HTTP流量复制压测引流

114环境:

gor实现线上HTTP流量复制压测引流

115后端处理成功了

gor实现线上HTTP流量复制压测引流

监控app端登录接口的处理入参和出参:

com.xxx.xxx.debtoauth2.web.controller.UserController.userLogin

gor实现线上HTTP流量复制压测引流

到这里,请求从功能测试环境直接copy到了我的性能测试环境

如果在线上环境开启gor的监控,那么是不是也可以将线上的流量导入到压测环境,这样就可以起到类似压测线上的,对线上的流量预估更加准确

3.1.2 如果目标服务器使用的库与线上机器一样,需要只引流Get方法,不应该复制上行方法(POST等)

gor --<span class="hljs-keyword">input-raw :<span class="hljs-number">80 --http-allow-method GET --<span class="hljs-keyword">output-http <span class="hljs-string">"http://target_server:8080"</span></span></span></span>

3.1.3 当需要对线上服务进行整体性能压测时,可将线上请求扩大N倍,进行引流

gor --<span class="hljs-keyword">input-raw :<span class="hljs-number">80 --http-allow-method GET --<span class="hljs-keyword">output-http <span class="hljs-string">"http://target_server:8080|200%" #&#x5C06;&#x8BF7;&#x6C42;&#x6269;&#x5927;<span class="hljs-number">1&#x500D;&#xFF0C;&#x4E5F;&#x53EF;&#x7F29;&#x5C0F;&#xFF0C;&#x8C03;&#x6574;<span class="hljs-string">"|"&#x540E;&#x9762;&#x7684;&#x767E;&#x5206;&#x6BD4;&#x5373;&#x53EF;</span></span></span></span></span></span>

3.1.4 只复制某个URL请求,–http-allow-url参数

<span class="hljs-attribute">gor --input-raw :<span class="hljs-number">8080 --http-allow-method GET --output-http <span class="hljs-string">"http://target_server:8080" --http-allow-url <span class="hljs-regexp">mall.*hotword</span></span></span></span>

3.1.5 多目标服务器的流量复制引流,有点类似nginx的mirror

gor --<span class="hljs-keyword">input-raw :<span class="hljs-number">80 --<span class="hljs-keyword">output-http <span class="hljs-string">"http://target_server:8080" --<span class="hljs-keyword">output-http <span class="hljs-string">"http://target_server2:8080"</span></span></span></span></span></span>

其他参数:

gor实现线上HTTP流量复制压测引流

gor –input-raw :80 –output-http ‘http://192.168.0.100:8080’ –http-rewrite-url /v1/user/([^\/]+)/ping:/v2/user/$1/ping

3.2 录制与回放

适合在夜深人静的时候,偷摸干一些和谐的事(≧∀≦)
3.2.1 录制,只是gor输出目标改为本地文件,使用–output-file参数

gor --<span class="hljs-keyword">input-raw :<span class="hljs-number">80 --<span class="hljs-keyword">output-file gor_request<span class="hljs-variable">.log</span></span></span></span>

3.2.2 回放

gor --<span class="hljs-keyword">input-file <span class="hljs-string">"gor_request.log|200%" --<span class="hljs-keyword">output-http <span class="hljs-string">"http://target_server:8080"</span></span></span></span>

四、最后

通过案例发现,gor目前的只能使用较为简单的无鉴权的操作,同时要求线下环境的数据要和线上的数据保持一致,不然会出现线上的请求参数,copy到测试环境出现空指针等异常

2。对于类似APP端的接口,基本都存在API鉴权操作,即请求头都会携带唯一标识

如果从从线上环境的请求,header中的token是123456,那么这个token的值也会被copy到线下测试环境,但是这个用户没有做登录操作,所以也没有token,这个时候拿线上的token肯定操作失败:

gor实现线上HTTP流量复制压测引流

其实这些登录已失效,都是因为copy过来的请求,都是带的线上的token,肯定在测试环境不行

如果将这个token替换到测试环境的toekn:

gor实现线上HTTP流量复制压测引流

gor –input-raw :8080 –http-allow-method POST –output-http “http://172.20.20.111:8080” –input-raw-realip-header “X-Real-IP” –http-set-header “Host: dh-manage.xiaoniu88.com:8080” –http-set-header “Origin: dh-manage.xiaoniu88.com:8080” –http-set-header “accessToken: c7d7646b45194c34982d4de2a7bc505f”

gor实现线上HTTP流量复制压测引流

所以,gor适用于没有任何token鉴权验证的,同时要求线上和线下数据一致的环境

参考文档:

https://cloud.tencent.com/developer/article/1491793

https://www.cnblogs.com/playboysnow/articles/9759366.html

https://www.cnblogs.com/mithrandirw/p/8468910.html

https://blog.csdn.net/weixin_33825683/article/details/92585462

https://blog.csdn.net/weixin_34345560/article/details/87986675

https://www.houyang.org/detail/292465674.html

Original: https://www.cnblogs.com/qmfsun/p/11598763.html
Author: Agoly
Title: gor实现线上HTTP流量复制压测引流

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/549054/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球