- Goreplay 是用 Golang 写的一个 HTTP 实时流量复制工具。功能更强大,支持流量的放大、缩小,频率限制,还支持把请求记录到文件,方便回放和分析,也支持和 ElasticSearch 集成,将流量存入 ES 进行实时分析。
- GoReplay 不是代理,而是监听网络接口上的流量,不需要更改生产基础架构,而是在与服务相同的计算机上运行 GoReplay 守护程序。
- 特点:简单易用
- 地址:https://github.com/buger/goreplay
- stars:14.1k
与 TCPCopy 相比它的架构更简单,只有一个 gor 组件,如下:
只需要在生产服务器上启动一个 gor 进程,它负责所有的工作包括监听、过滤和转发。它的设计遵循 Unix 设计哲学:一切都是由管道组成的,各种输入将数据复用为输出。
首先安装Golang及相关依赖环境 在官网上下载安装包或者go的一些中文网站上下载安装包
https://golang.org/dl/
https://studygolang.com/dl
我下载的是: go1.15.5.linux-amd64.tar.gz
tar -C /usr/local -zxvf go1.14.4.linux-amd64.tar.gz3.配置环境变量
# 打开 vim /etc/profile # 添加 export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin # 编译生效 source /etc/profile4.验证 go env
[root@vm ~]# go version go version go1.15.5 linux/amd645.Goreplay 安装
从https://github.com/buger/gor/releases 下载最新的 Gor 二进制文件(提供 Windows、Linux x64 和 Mac OS 的预编译二进制文件),也可以自己编译 。
下载二进制包:
[root@vm ~]# curl -L -O https://github.com/buger/goreplay/releases/download/v1.3.1/gor_1.3.1_x64.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 626 100 626 0 0 741 0 --:--:-- --:--:-- --:--:-- 741
100 10.5M 100 10.5M 0 0 3258k 0 0:00:03 0:00:03 --:--:-- 5951k
[root@vm-jmeter ~]# tar xvzf gor_1.3.1_x64.tar.gz
gor
解压缩包后,您可以从当前目录运行 Gor,或者您可能希望将二进制文件复制到您的 PATH(对于 Linux 和 Mac OS,它可以是 /usr/local/bin)
3.Goreplay 使用示例 3.1.接口服务环境部署准备一个RESTful API 接口部署到两个地址服务器上,模拟生产环境和测试环境。
3.2.捕获生产环境流量保存到本地文件- 在终端运行这个命令:
#将端口 8082 流量保存到本地的文件 sudo ./gor --input-raw :8082 --output-file=requests.gor
[root@vm-1 ~]# sudo ./gor --input-raw :8082 --output-file=requests.gor Interface: eth0 . BPF Filter: ((tcp dst port 8082) and (dst host 172.16.106.149 or dst host fe80::f599:6e65:3dc9:b84a)) Interface: lo . BPF Filter: ((tcp dst port 8082) and (dst host 127.0.0.1 or dst host ::1)) Interface: virbr0 . BPF Filter: ((tcp dst port 8082) and (dst host 192.168.124.1)) 2021/08/17 14:26:41 [PPID 17654 and PID 17655] Version:1.3.0
注意:使用–output-file-append 选项可以将捕获流量保存为一个单独的文件 使用sudo并要求输入密码:为了分析网络,Gor 需要只有超级用户才能使用的权限。但是,可以为非 root 用户配置运行Gor 。
- 发送请求
通过接口请求工具向服务器接口发送请求,模拟用户操作行为。
- 查看生产服务日志
看到生产环境产生了日志,gor运行正常。
Hibernate: select person0_.id as id1_0_, person0_.age as age2_0_, person0_.name as name3_0_ from person person0_ Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: insert into person (age, name, id) values (?, ?, ?) Hibernate: insert into person (age, name, id) values (?, ?, ?)
- 停掉gor
我们停掉在服务器上运行的 Gor,可以看到已经生成了录制的请求文件
[root@vm-1 ~]# ls -l | grep "requests" -rw-r----- 1 root root 300 8月 17 14:33 requests_0.gor
- 请求文件的内容
[root@vm-1 ~]# cat requests_0.gor 1 04481f92ac106afb42ce55ca 1629182282437724065 0 GET /person HTTP/1.1 User-Agent: PostmanRuntime/7.28.2 Accept: ** Cache-Control: no-cache Postman-Token: 4922ce23-5706-42c5-8a54-ade5d5aee785 Host: 172.16.106.149:8082 Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Length: 03.5.将 http 的请求实时转发到测试服务
sudo ./gor --input-raw :8082 --output-http="http://172.16.106.237:8082"
[root@vm-1 ~]# sudo ./gor --input-raw :8082 --output-http="http://172.16.106.237:8082" Interface: eth0 . BPF Filter: ((tcp dst port 8082) and (dst host 172.16.106.149 or dst host fe80::f599:6e65:3dc9:b84a)) Interface: lo . BPF Filter: ((tcp dst port 8082) and (dst host 127.0.0.1 or dst host ::1)) Interface: virbr0 . BPF Filter: ((tcp dst port 8082) and (dst host 192.168.124.1)) 2021/08/17 14:59:49 [PPID 18118 and PID 18119] Version:1.3.03.6.压力测试
GOR 支持将捕获到的生产实际请求流量减少或者放大回放以用于各种环境的压力测试。
录制的请求文件:
[root@vm-1 ~]# cat requests.gor 1 040f1f92ac106afb7e8d5679 1629183084044026882 0 POST /person/two HTTP/1.1 User-Agent: PostmanRuntime/7.28.2 Accept: */* Cache-Control: no-cache Postman-Token: a8600dd1-28c1-4825-b6a2-68c42659942f Host: 172.16.106.149:8082 Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Length: 0
运行以下命令,将流量从文件回放到测试服务,并放大两倍:
[root@vm-1 ~]./gor --input-file "requests.gor|200%" --output-http="http://172.16.106.237:8082" 2021/08/17 15:03:58 [PPID 12356 and PID 18187] Version:1.3.0 [DEBUG][elapsed 1.361742ms]: [INPUT-FILE] FileInput: end of file 'requests.gor'
查看下测试服务日志的处理情况
Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: select next_val as id_val from hibernate_sequence for update Hibernate: update hibernate_sequence set next_val= ? where next_val=? Hibernate: insert into person (age, name, id) values (?, ?, ?) Hibernate: insert into person (age, name, id) values (?, ?, ?)
注意,当然也也支持请求流量 10%,20% 等缩小。
4.总结今天简单介绍 Gor 的常见玩法,事实上远不止上述提到的这些,更多的玩法有时间再写。
文件源码:
https://github.com/zuozewei/blog-example/tree/master/Performance-testing/04-full-link/springboot-demo



