Golang

关注公众号 jb51net

关闭
首页 > 脚本专栏 > Golang > go replay流量重放

go replay流量重放的实现

作者:NPE~

Goreplay 是用 Golang 写的一个 HTTP 实时流量复制工具,支持流量放大缩小、限流、文件记录及ES集成,本文主要介绍了go replay流量重放的实现,感兴趣的可以了解一下

一、介绍

Github地址:https://github.com/buger/goreplay

二、安装

进入官网下载对应操作系统版本,下载后解压即可使用

这里选择最新的v1.3.3为例:https://github.com/buger/goreplay/releases/download/1.3.3/gor_1.3.3_mac.tar.gz

下载完后可直接执行使用

在这里插入图片描述

三、参数讲解

1. 具体参数

# 查看帮助文档
./gor --help

常用参数:

–input-raw :用来捕捉http流量,需要指定ip地址和端口
–input-file :接收流量
–output-file:保存流量的文件
–input-tcp:将多个Goreplay实例获取的流量聚集到一个Goreplay实例
–output-stdout:终端输出
–output-tcp:将获取的流量转移至另外的Goreplay实例
–output-http:流量释放的对象server,需要指定IP地址和端口
–output-file:录制流量时指定的存储文件
–http-disallow-url :不允许正则匹配的URL
–http-allow-header :允许的Header头
–http-disallow-header:不允许的Header头
–http-allow-method:允许的请求方法,传入值为GET,POST,OPTIONS等
–input-file-loop:无限循环,而不是读完这个文件就停止了
–output-http-workers:并发请求数
–stats --out-http-stats 每5秒输出一次TPS数据(查看统计信息)
–split-output true: 按照轮训方式分割流量
–output-http-timeout 30s:http超时30秒时间设置,默认是5秒

2. 案例demo

# --input-file 从文件中获取请求数据,重放的时候 100x 倍速
# --input-file-loop 无限循环,而不是读完这个文件就停止
# --output-http 发送请求到 http://host2.com
# --output-http-workers 并发 100 发请求
# --stats --output-http-stats 每 5 秒输出一次 TPS 数据
./goreplay --input-file 'request.gor|10000%' --input-file-loop --output-http 'http://host2.com' --output-http-workers 100 --stats --output-http-stats
./goreplay --input-raw :80 --http-allow-url '/api/v1' --output-stdout
./goreplay --input-raw :80 --output-file 'request.gor'
sudo ./gor --input-tcp :28020 --output-http "http://staging.com"  --output-http "http://dev.com"
sudo ./gor --input-raw :80 --output-http "http://staging.com"  --output-http "http://dev.com" --split-output true
gor --input-tcp replay.local:28020 --output-http http://staging.com --output-http-timeout 30s
gor --input-file "requests.gor|200%" --output-http "staging.com"
gor --input-tcp :28020 --output-http "http://staging.com|10"
gor --input-raw :80 --output-tcp "replay.local:28020|10%"
gor --input-raw :8080 --output-http staging.com --http-disallow-url /api
gor --input-raw :80 --output-http "http://staging.server" --http-allow-method GET --http-allow-method OPTIONS
gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^1\.0\d
gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
gor --input-raw :8080 --output-http staging.com --http-rewrite-url /v1/user/([^\\/]+)/ping:/v2/user/$1/ping
gor --input-raw :8080 --output-http staging.com --http-set-param api_key=1
gor --input-raw :80 --output-http "http://staging.server" --http-header "User-Agent: Replayed by Gor" --http-header "Enable-Feature-X: true"
./gor --input-raw :8000 --output-http http://staging.com  --output-http-elasticsearch localhost:9200/gor

当基于Header或参数进行限制时,仅支持基于百分比的限制

# Limit based on header value
sudo ./gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-header-limiter "X-API-KEY: 10%"

# Limit based on header value
sudo ./gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-param-limiter "api_key: 10%"

3. 注意点

四、具体使用

前置准备

本地编写http服务并启动,用于模拟线上服务器

勾选goland启动多实例选项,分别启动两个服务:localhost:8888、localhost:9999

选中main.go,鼠标右击配置

在这里插入图片描述

勾选允许多实例运行

在这里插入图片描述

先执行main.go运行实例1

在这里插入图片描述

修改暴露的端口,然后再执行main.go,运行实例2

在这里插入图片描述

最终效果

在这里插入图片描述

或者将port端口 暴露为参数,将main.go编译为可执行文件,然后分别运行两次。当然也可以直接编写两份一样的main函数,然后改下端口

用法一:本地录制流量保存到文件并回放到指定服务器

#将端口 8888 流量保存到本地的文件
sudo ./gor --input-raw :8888 --output-file=requests.gor
1 f83d22b80000000169dd18f1 1736750112736464000 0
GET /getInfo HTTP/1.1
User-Agent: Apifox/1.0.0 (https://apifox.com)
Accept: */*
Host: localhost:8888
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
./gor --input-file requests_0.gor --output-http="http://localhost:9999"

用法二:将实时流量输出到控制台

# 将服务一的实时流量输出到控制台
sudo ./gor --input-raw :8888 --output-stdout

用法三:将实时流量转发到服务二

# 将服务一的实时流量转发到服务二
sudo ./gor --input-raw "localhost:8888" --output-http="http://localhost:9999"

用法四:压测(流量放大或缩小)

goreplay支持将捕获到的生产实际请求流量减少或者放大重播以用于测试环境的压力测试.压力测试一般针对 Input 流量减少或者放大。

# 将流量放大为200%,按照两倍速率去回放,比如:之前两个请求间的间隔为2s,我放大两倍后,请求间隔就变为了1s,相当于qps翻倍
# 如果“input-flie”是多个文件,可以用正则去匹配,如“request*.gor|200%”
# 除了input-file可以限流,--output-http也可以限流,详细文章后半部分
sudo ./gor --input-file "requests*.gor|1%" --output-http="http://localhost:9999"

goreplay限流详细解释

goreplay支持将捕获到的生产实际请求流量减少或者放大重放以用于测试环境的压力测试,压力测试一般以对input的流量减少或者放大。例如:

# Replay from file on 2x speed
#将请求流量以2倍的速度放大重放;当然也支持10%,20%等缩小请求流量
gor --input-file "requests.gor|200%" --output-http "staging.com"

如果受限于测试环境的服务器的资源压力,只想重放一部分流量到测试环境中,而不需要所有的实际生产流量,那么就可以用限速功能。两种策略实现限流:

①随机丢弃请求流量:

input和output两端都支持限速,有两种限速算法,百分比或者绝对值

  • 百分比:input端支持缩小或者放大请求流量,基于指定的策略随机丢弃请求流量
  • 绝对值:如果单位时间(秒)内达到临界值,则丢弃剩余的请求流量,下一秒临界值还原

详细用法:

在output终端使用”|”运算符指定限速阈值,例如:

# staging.server will not get more than ten requests per second
#staging服务每秒只接收10个请求
gor --input-tcp :28020 --output-http "http://localhost:8082|10"
# replay server will not get more than 10% of requests 
# useful for high-load environments
gor --input-raw :80 --output-tcp "replay.local:28020|10%"

②基于Header或者URL参数限速

如果header或者URL参数中有唯一值,例如(API key),则可以转发指定百分比的流量到后端,例如:

# Limit based on header value
gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-header-limiter "X-API-KEY: 10%"

# Limit based on URL param value
gor --input-raw :80 --output-tcp "replay.local:28020|10%" --http-param-limiter "api_key: 10%"

五、代码地址

教程代码地址

Github:https://github.com/ziyifast/ziyifast-code_instruction/tree/main/go-demo/go-replay

参考文档:
https://juejin.cn/post/6999586008698208263
https://blog.csdn.net/qq_40093255/article/details/117227229

到此这篇关于go replay流量重放的实现的文章就介绍到这了,更多相关go replay流量重放内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文