1. 程式人生 > 實用技巧 >openresty + gor+minio 整合

openresty + gor+minio 整合

以前有寫過簡單的容器整合,以下是關於s3整合的,主要是測試資料的捕捉以及回放

參考流程


簡單說明
gor 與openresty 部署在一起,為了方便基於supervisord 管理,預設已經開啟請求捕捉(80埠),同時supervisord集成了管理,可以通過ui操作是否捕捉
對於捕捉的資料儲存在minio s3中,如果需要回放操作,我們直接可以基於gor tools 將儲存的資料回放給需要測試的服務

環境準備

  • docker-compose 檔案
version: "3"
services: 
 minio: 
  image: minio/minio
  command: server /data
  volumes: 
  - "./data:/data"
  ports: 
  - "9000:9000"
  environment:
   - "MINIO_ACCESS_KEY=minio"
   - "MINIO_SECRET_KEY=minio123"
 api:
  image: dalongrong/openresty-gor:s3
  build: 
   dockerfile: ./Dockerfile-openresty
   context: ./
  privileged: true
  cap_add: 
  - ALL
  volumes:
  - "./supervisor.conf:/etc/supervisord.conf"
  - "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  - "./demoapps:/opt/demoapps"
  - "./capture:/app/capture"
  - "./logs/access-test.log:/usr/local/openresty/nginx/logs/access-test.log"
  ports:
  - "80:80"
  - "8080:8080"
  - "9001:9001"
  • supervisord.conf 配置
[program:website]
command = /usr/local/openresty/bin/openresty
[inet_http_server]
port = :9001
[program:gor]
# 此處需要指定關於s3 的配置
environment=AWS_ACCESS_KEY_ID="minio",AWS_SECRET_ACCESS_KEY="minio123",AWS_REGION="demo",AWS_ENDPOINT_URL="http://minio:9000",AWS_DEBUG="true"
stdout_logfile = gor.log, /dev/stdout
command = gor --input-raw :80 --output-file s3://logs/%Y-%m-%d-%H-%M.gz
  • openresty+gor dockerfile
    添加了時區以及gor+supervisord的整合
FROM openresty/openresty:alpine
ENV PATH=$PATH:/app
RUN /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
ENV TZ Asia/Shanghai
RUN apk add --update --no-cache \
  tzdata && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY --from=dalongrong/gor /app/gor /app/gor
COPY --from=ochinchina/supervisord:latest /usr/local/bin/supervisord /usr/local/bin/supervisord
EXPOSE 9001 8080 80
COPY supervisor.conf /etc/supervisor.conf
CMD ["/usr/local/bin/supervisord"]
  • 啟動
docker-compose up -d
  • s3 bucket 建立
    需要建立logs(和自己使用gor 的配置有關係)

測試效果

  • supervisord 管理介面
    我們可以通過此管理介面控制gor 是否進行資料捕捉

  • minio 儲存

  • 回放

    我使用了本地工具,同時注意需要配置關於s3的資訊

export AWS_ACCESS_KEY_ID=minio
export AWS_REGION=demo
export AWS_SECRET_ACCESS_KEY=minio123
export AWS_ENDPOINT_URL=http://127.0.0.1:9000
export AWS_DEBUG=true
./gor-mac --input-file s3://logs/2020-07-20-10-19_13.gz  --output-http "http://localhost"

對於回放效果的檢視可以通過檢視openresty 的log

類似可以整合的工具

mountebank(可以mock http,https,tcp,smtp),diffy (請求比較,主要是http)
hoverfly 一個不錯的流量處理工具,nginx 的mirrror也是一個不錯的選擇

說明

以上只是一個簡單的整合試用,實際如果我們需要基於捕捉的資料進行業務操作回放,還有好多事情需要做

參考資料

https://github.com/buger/goreplay
https://github.com/opendiffy/diffy
https://github.com/bbyars/mountebank
https://github.com/spectolabs/hoverfly
https://github.com/rongfengliang/openresty_rewrite_static/tree/gor