前言
ElasticSearch
文章转载自公众号「Golang梦工厂」,喜欢内容,推荐关注。
goreplay介绍与安装
项目地址:https://github.com/buger/goreplay
elasticSearchgoreplaygoreplay
goreplaymac
gor
使用示例
实时流量转发
WebGin helloworld
import (
"flag"
"github.com/gin-gonic/gin"
)
var Port string
func init() {
flag.StringVar(&Port, "port", "8081", "Input Your Port")
}
func main() {
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)
}
80818082
$ go run . --port="8081"
$ go run . --port="8082"
gor80818082
$ sudo gor --input-raw :8081 --output-http="http://127.0.0.1:8082"
8081
$ curl --location --request GET 'http://127.0.0.1:8081/api/ping'
8082
流量放大、缩小
goreplay
Web8081
$ sudo gor --input-raw :8081 --output-file ./requests.gor
8081
8082
gor --input-file "requests_0.gor" --output-http="http://127.0.0.1:8082|50%"
8082
$ gor --input-file "requests_0.gor|1000%" --output-http="http://127.0.0.1:8082|50%" # 1000%就是放大10倍
ElastichSearch
goreplayEs
$ gor --input-raw :8000 --output-http http://staging.cm --output-http-elasticsearch localhost:9200/gor
我们不需要提前创建索引结构,他将自动创建,具体结构如下:
type ESRequestResponse struct {
ReqURL string `json:"Req_URL"`
ReqMethod string `json:"Req_Method"`
ReqUserAgent string `json:"Req_User-Agent"`
ReqAcceptLanguage string `json:"Req_Accept-Language,omitempty"`
ReqAccept string `json:"Req_Accept,omitempty"`
ReqAcceptEncoding string `json:"Req_Accept-Encoding,omitempty"`
ReqIfModifiedSince string `json:"Req_If-Modified-Since,omitempty"`
ReqConnection string `json:"Req_Connection,omitempty"`
ReqCookies string `json:"Req_Cookies,omitempty"`
RespStatus string `json:"Resp_Status"`
RespStatusCode string `json:"Resp_Status-Code"`
RespProto string `json:"Resp_Proto,omitempty"`
RespContentLength string `json:"Resp_Content-Length,omitempty"`
RespContentType string `json:"Resp_Content-Type,omitempty"`
RespTransferEncoding string `json:"Resp_Transfer-Encoding,omitempty"`
RespContentEncoding string `json:"Resp_Content-Encoding,omitempty"`
RespExpires string `json:"Resp_Expires,omitempty"`
RespCacheControl string `json:"Resp_Cache-Control,omitempty"`
RespVary string `json:"Resp_Vary,omitempty"`
RespSetCookie string `json:"Resp_Set-Cookie,omitempty"`
Rtt int64 `json:"RTT"`
Timestamp time.Time
}
goreplayhelp
$ gor -h
Gor is a simple http traffic replication tool written in Go. Its main goal is to replay traffic from production servers to staging and dev environments.
Project page: https://github.com/buger/gor
Author: <Leonid Bugaev> leonsbox@gmail.com
Current Version: v1.3.0
-copy-buffer-size value
Set the buffer size for an individual request (default 5MB)
-cpuprofile string
write cpu profile to file
-exit-after duration
exit after specified duration
-http-allow-header value
A regexp to match a specific header against. Requests with non-matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-header api-version:^v1
-http-allow-method value
Whitelist of HTTP methods to replay. Anything else will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-method GET --http-allow-method OPTIONS
-http-allow-url value
A regexp to match requests against. Filter get matched against full url with domain. Anything else will be dropped:
gor --input-raw :8080 --output-http staging.com --http-allow-url ^www.
-http-basic-auth-filter value
A regexp to match the decoded basic auth string against. Requests with non-matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-basic-auth-filter "^customer[0-9].*"
-http-disallow-header value
A regexp to match a specific header against. Requests with matching headers will be dropped:
gor --input-raw :8080 --output-http staging.com --http-disallow-header "User-Agent: Replayed by Gor"
..........省略
goreplay
goreplayLibpcapLibpcaptcpdumpLibpcapCGoCCGogoreplaygoreplayinputoutputinputoutputinputoutputinputoutputinputintputoutputinput-file
goreplaygor.gomain
总结
goreplay
- END -
扫码关注公众号「网管叨bi叨」
给网管个星标,第一时间吸我的知识 👆
网管整理了一本《Go 开发参考书》收集了70多条开发实践。去公众号回复【gocookbook】领取!还有一本《k8s 入门实践》讲解了常用软件在K8s上的部署过程,公众号回复【k8s】即可领取!
觉得有用就点个在看 👇👇👇