目录
- 前言
- goreplay介绍与安装
- 使用示例
- 流量放大、缩小
- 流量写入到ElastichSearch
- goreplay基本实现原理
- 总结
前言
哈喽,大家好,我是asong。
今天给大家推荐一款使用Go语言编写的流量回放工具 -- goreplay;工作中你一定遇到过需要在服务器上抓包的场景,有了这个工具就可以助你一臂之力,goreplay的功能十分强大,支持流量的放大、缩小,并且集成了ElasticSearch,将流量存入ES进行实时分析;
废话不多,我们接下来来看一看这个工具;
goreplay介绍与安装
项目地址:https://github.com/buger/goreplay
goreplay是一个开源网络监控工具,可以实时记录TCP/HTTP流量,支持把流量记录到文件或者elasticSearch实时分析,也支持流量的放大、缩小,还支持频率限制;goreplay不是代理,无需任何代码入侵,只需要在服务相同的机器上运行goreplay守护程序,其会在后台侦听网络接口上的流量,goreplay的设计遵循 Unix 设计哲学:一切都是由管道组成的,各种输入将数据复用为输出;可以看一下官网画的架构图:

goreplay的安装也比较简单,只需要在https://github.com/buger/goreplay/releases 下载对应操作系统的二进制文件即可,我的电脑是MAC的:

解压缩后就是一个二进制文件gor,将其添加到您的环境变量中,方便我们后续的操作;
使用示例
实时流量转发
首先我们要准备一个Web服务,最简单的就是用Gin 快速实现一个helloworld,替大家实现好了:https://github.com/asong2020/golang_Dream/tree/master/code_demo/gin_demo;
import( "flag" "github.com/gin开发者_JAVA学习-gonic/gin" ) varPortstring funcinit(){ flag.StringVar(&Port,"port","8081","InputYourPort") } funcRyFOHmain(){ flag.Parse() r:=gin.Default() r.Use() r1:=r.Group("/api") { r1.GET("/ping",func(c*gin.Context){ c.jsON(200,gin.H{ "message":"pong", }) }) } r.Run("localhost:"+Port) }
因为资源有限,这里我用一台电脑起两个进程来模拟流量转发,分别启动两个web服务分别监控端口号8081、8082:
$gorun.--port="8081" $gorun.--port="8082"

服务弄好了,现在我们来开启gor守护进程进行流量监听与转发,将8081端口的流量转发到8082端口上:
$sudogor--input-raw:8081--output-http="http://127.0.0.1:8082"

现在我们请求8081端口:
$curl--location--requestGET'http://127.0.0.1:8081/api/ping'
可以看到8082端口同样被请求了:

流量放大、缩小
goreplay支持将捕获的流量存储到文件中,实际工作中我们可以使用捕获的流量做压力测试,首先我们需要将捕获的流量保存到本地文件,然后利用该文件进行流量回放RyFOH;
还是上面的Web程序,我们将端口8081的流量保存到本地文件:
$sudogor--input-raw:8081--output-file./requests.gor
我们对8081端口执行了5次请求:

然后我们对8082端口进行流量缩小测试,缩小一倍:
gor--input-file"requests_0.gor"--output-http="http://127.0.0.1:8082|50%"
调整百分比就是进行流量放大、缩小,这里我们缩小了一倍,可以看到只有2次请求到了8082端口;我们可以调整流量回放的速度,比如我们调整流量以10倍速度进行重播:
$gor--input-file"requests_0.gor|1000%"--output-http="http://127.0.0.1:8082|50%"#1000%就是放大10倍
流量写入到ElastichSearch
goreplay可以将捕获的流量导出到Es中,只需要执行如下命令:
$gor--input-raw:8000--output-httphttp://staging.cm--output-http-elasticsearchlocalhost:9200/gor
我们不需要提前创建索引结构,他将自动创建,具体结构如下:
typeESRequestResponsestruct{
ReqURLstring`json:"Req_URL"`
ReqMethodstring`json:"Req_Method"`
ReqUserAgentstring`json:"Req_User-Agent"`
ReqAcceptLanguagestring`json:"Req_Accept-Language,omitempty"`
ReqAcceptstring`json:"Req_Accept,omitempty"`
ReqAcceptEncodingstring`json:"Req_Accept-Encoding,omitempty"`
ReqIfModifiedSincestring`json:"Req_If-Modified-Since,omitempty"`
ReqConnectionstring`json:"Req_Conne编程客栈ction,omitempty"`
ReqCookiesstring`json:"Req_Cookies,omitempty"`
RespStatusstring`json:"Resp_Status"`
RespStatusCodestring`json:"Resp_Status-Code"`
RespProtostring`json:"Resp_Proto,omitempty"`
RespContentLengthstring`json:"Resp_Content-Length,omitempty"`
RespContentTypestring`json:"Resp_Content-Type,omitempty"`
RespTransferEncodingstring`json:"Resp_Transfer-Encoding,omitempty"`
RespContentEncodingstring`json:"Resp_Content-Encoding,omitempty"`
RespExpiresstring`json:"Resp_Expires,omitempty"`
RespCacheControlstring`json:"Resp_Cache-Control,omitempty"`
RespVarystring`json:"Resp_Vary,omitempty"`
RespSetCookiestring`json:"Resp_Set-Cookie,omitempty"`
Rttint64`json:"RTT"`
Timestamptime.Time
}
goreplay提供了太多的功能,就不一一介绍了,可以通过执行help命令查看其他高级用法,每个命令都提供了例子,入手很快;
$gor-h GorisasimplehttptrafficreplicationtoolwritteninGo.Itsmaingoalistoreplaytrafficfromproductionserverstostaginganddevenvironments. Projectpage:https://github.com/buger/gor Author:<LeonidBugaev>leonsbox@gmail.com CurrentVersion:v1.3.0 -copy-buffer-sizevalue Setthebuffersizeforanindividualrequest(default5MB) -cpuprofilestring writecpuprofiletofile -exit-afterduration exitafterspecifiedduration -http-allow-headervalue AregexptomatchASPecificheaderagainst.Requestswithnon-matchingheaderswillbedropped: gor--input-raw:8080--output-httpstaging.com--http-allow-headerapi-version:^v1 -http-allow-methodvalue WhitelistofHTTPmethodstoreplay.Anythingelsewillbedropped: gor--input-raw:8080--output-httpstaging.com--http-allow-methodGET--http-allow-methodOPTIONS -http-allow-urlvalue Aregexptomatchrequestsagainst.Filtergetmatchedagainstfullurlwithdomain.Anythingelsewillbedropped: gor--input-raw:8080--output-httpstaging.com--http-allow-url^www. -http-basic-auth-filtervalue Aregexptomatchthedecodedbasicauthstringagainst.Requestswithnon-matchingheaderswillbedropped: gor--input-raw:8080--output-httpstaging.com--http-basic-auth-filter"^customer[0-9].www.devze.com*" -http-disallow-headervalue Aregexptomatchaspecificheaderagainst.Requestswithmatchingheaderswillbedropped: gor--input-raw:8080--output-httpstaging.com--http-disallow-header"User-Agent:ReplayedbyGor" ..........省略
goreplay基本实现原理
goreplay底层也是调用Libpcap,Libpcap即数据包捕获函数库,tcpdump也是基于这个库实现的,Libpcap是C语言写的,Go语言不能直接调用C语言,需要使用CGo,所以goreplay可以直接使用谷歌的包github.com/google/gopacket,提供了更方便的操作接口,基于goreplay封装了input、output,在启动的时候通过命令行参数解析指定的input、output,input读取数据写入到output中,默认是一个input复制多份,写多个output,多个input之前是并行的,但是单个intput到多个output是串行的,所以input-file会有性能瓶颈,压测的时候需要开多个进程同时跑来达到压测需求;
goreplay的源码有点多,就不在这里分析了,大家感兴趣哪一部分可以从gor.go的main函数入手,看自己感兴趣的部分就可以了;
总结
goreplay提供的玩法非常丰富,合理的改造可以做成回归工具帮助我们确保服务的稳定性,别放过这个自我展现的机会~。编程客栈
以上就是利用Go语言实现流量回放工具的示例代码的详细内容,更多关于Go语言流量回放工具的资料请关注我们其它相关文章!
加载中,请稍侯......
精彩评论