语言版本: English | 简体中文
提示:本页面由机器翻译生成,内容可能存在不准确或不完整之处。请以英文原文
README.md为准。
用于测试与开发的最快、最可靠的 gRPC Mock 服务器。
GripMock 可以根据你的 .proto 文件或编译后的 .pb 描述文件创建 Mock 服务器,让 gRPC 测试变得简单且高效。非常适合端到端测试、开发环境和 CI/CD 流水线。

.proto、编译后的 .pb、BSR 模块或 gRPC reflection 加载 API.pb 服务加载 - 可通过 API 在运行时加载编译后的 protobuf 描述文件,无需重启equals、contains、matches、glob、headers、priority 和 match limitsAny)与响应时延google.protobuf.*、google.type.*)faker.*)otelgrpc + otelhttp)/metrics) - 暴露运行时/进程指标(go_*、process_*)以及 GripMock 指标proxy、replay、capture 模式,用于从真实上游服务渐进迁移到本地 mocks完整文档 - 带示例的完整指南
/api/descriptors):运行时加载编译后的 proto 描述文件(.pb),附带可验证的 curl 工作流:文档proxy、replay、capture,包含实用的上线迁移指导:文档sdk.By(fullMethod) 助手以及带上下文的远程校验:文档/metrics 说明:文档GripMock 最初是 tokopedia/gripmock 的一个 fork,随后演进为独立且完全重写的项目。
当前 GripMock 聚焦于实用测试工作流:
架构细节与基准方法见:Performance Comparison

访问 http://localhost:4771/ 可在 Web 控制台中可视化管理 stubs。
选择你偏好的安装方式:
brew tap gripmock/tap
brew install --cask gripmock
curl -s https://raw.githubusercontent.com/bavix/gripmock/refs/heads/master/setup.sh | sh -s
irm https://raw.githubusercontent.com/bavix/gripmock/refs/heads/master/setup.ps1 | iex
docker pull bavix/gripmock
如需构建插件,请使用配套 builder 镜像:
docker pull bavix/gripmock:v3.7.1-builder
go install github.com/bavix/gripmock/v3@latest
使用 .proto 文件启动:
gripmock service.proto
添加静态 stubs:
gripmock --stub stubs/ service.proto
直接从 Buf Schema Registry(BSR)加载 API:
gripmock --stub third_party/bsr/eliza buf.build/connectrpc/eliza
从在线 gRPC 服务 reflection 加载 API:
gripmock grpc://localhost:50051
gripmock grpcs://api.company.local:443
可选参数:
gripmock grpc://localhost:50051?timeout=10s
gripmock grpcs://10.0.0.5:8443?serverName=api.company.local
gripmock grpc://localhost:50051?bearer=<token>
在 reflection 基础上使用 upstream modes(实验性):
# 通过 GripMock 进行纯反向代理
gripmock grpc+proxy://localhost:50051
# 本地 stubs 优先,matcher 未命中时回退上游
gripmock grpc+replay://localhost:50051
# replay + 将上游未命中自动录制为 GripMock stubs
gripmock grpc+capture://localhost:50051
私有 BSR 模块:
BSR_BUF_TOKEN=<token> gripmock --stub stubs/ buf.build/acme/private-api
自托管 BSR:
BSR_SELF_BASE_URL=https://bsr.company.local \
BSR_SELF_TOKEN=<token> \
gripmock --stub stubs/ bsr.company.local/team/payments
使用 Docker:
docker run -p 4770:4770 -p 4771:4771 \
-v $(pwd)/stubs:/stubs \
-v $(pwd)/proto:/proto \
bavix/gripmock --stub=/stubs /proto/service.proto
OTEL_ENABLED=true \
OTEL_EXPORTER_OTLP_ENDPOINT=localhost:4317 \
OTEL_EXPORTER_OTLP_INSECURE=true \
gripmock --stub stubs/ service.proto
GET /metrics 始终可用OTEL_ENABLED=true 时启用追踪导出在 CI 流水线中使用官方 Action bavix/gripmock-action 运行 GripMock。
name: test
on: [push, pull_request]
jobs:
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Start GripMock
uses: bavix/gripmock-action@v1
with:
source: proto/service.proto
stub: stubs
- name: Run tests
run: go test ./...
该 Action 会:
latest 或固定 version)/api/health/readiness)grpc-addr、http-addr)供后续测试步骤使用更多示例与完整 inputs/outputs 见:GitHub Actions 指南。
在 examples 目录查看完整示例:
Stub(通用):
# yaml-language-server: $schema=https://bavix.github.io/gripmock/schema/stub.json
# examples/projects/greeter/stub_say_hello.yaml
- service: helloworld.Greeter
method: SayHello
input:
matches:
name: ".+"
output:
data:
message: "Hello, {{.Request.name}}!" # dynamic template lives in output
说明:
- 动态模板只放在 output 中(例如 data、headers、stream)。
- 保持 input 匹配为静态(equals/contains/matches 中不要使用 {{ ... }})。
# 启动服务
go run main.go examples/projects/greeter/service.proto --stub examples/projects/greeter
# 通过 grpcurl 调用
grpcurl -plaintext -d '{"name":"Alex"}' localhost:4770 helloworld.Greeter/SayHello
期望响应:
{
"message": "Hello, Alex!"
}
service: Greeter
method: SayHello
input:
equals:
name: "gripmock"
output:
data:
message: "Hello GripMock!"
优先级系统:
- service: UserService
method: GetUser
priority: 100 # Higher priority
input:
equals:
id: "admin"
output:
data:
role: "administrator"
- service: UserService
method: GetUser
priority: 1 # Lower priority (fallback)
input:
contains:
id: "user"
output:
data:
role: "user"
流式支持:
service: TrackService
method: StreamData
input:
equals:
sensor_id: "GPS001"
output:
stream:
- position: {"lat": 40.7128, "lng": -74.0060}
timestamp: "2024-01-01T12:00:00Z"
- position: {"lat": 40.7130, "lng": -74.0062}
timestamp: "2024-01-01T12:00:05Z"
GripMock 在 output 区域支持 Go text/template 语法的动态模板。
{{.Request.field}}{{.Headers.header_name}}{{.Requests}}(接收消息切片)、{{len .Requests}}、{{(index .Requests 0).field}}{{.MessageIndex}} 给出当前消息索引(从 0 开始)sum、avg、mul、min、max、add、sub、divjson、split、join、upper、lower、title、sprintf、int、int64、float、round、floor、ceilfaker.Person.*、faker.Contact.*、faker.Geo.*、faker.Network.*、faker.Identity.*重要规则:
- 不要在 input.equals、input.contains 或 input.matches 中使用动态模板(匹配必须是静态的)
- 对于服务端流,如果同时设置了 output.stream 与 output.error/output.code,会先发送消息再返回错误;如果 output.stream 为空,会立即返回错误
Header 匹配:
service: AuthService
method: ValidateToken
headers:
equals:
authorization: "Bearer valid-token"
input:
equals:
token: "abc123"
output:
data:
valid: true
user_id: "user123"
GripMock 提供四种强大的匹配策略:
equals)input:
equals:
name: "gripmock"
age: 25
active: true
contains)input:
contains:
name: "grip" # Matches "gripmock", "gripster", etc.
matches)input:
matches:
email: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
phone: "^\\+?[1-9]\\d{1,14}$"
glob)input:
glob:
filename: "*.txt"
path: "/usr/local/*"
GET /api/stubs - 列出所有 stubsPOST /api/descriptors - 在运行时加载 protobuf descriptor set(FileDescriptorSet)POST /api/stubs - 添加新 stubPOST /api/stubs/search - 查找匹配的 stubDELETE /api/stubs - 清空所有 stubsGET /api/health/liveness - 健康检查GET /api/health/readiness - 就绪检查# Add a stub
curl -X POST http://localhost:4771/api/stubs \
-H "Content-Type: application/json" \
-d '{
"service": "Greeter",
"method": "SayHello",
"input": {"equals": {"name": "world"}},
"output": {"data": {"message": "Hello World!"}}
}'
# Search for matching stub
curl -X POST http://localhost:4771/api/stubs/search \
-H "Content-Type: application/json" \
-d '{
"service": "Greeter",
"method": "SayHello",
"data": {"name": "world"}
}'
为 stub 文件添加 schema 校验以获得 IDE 支持:
JSON 文件:
{
"$schema": "https://bavix.github.io/gripmock/schema/stub.json",
"service": "MyService",
"method": "MyMethod"
}
YAML 文件:
# yaml-language-server: $schema=https://bavix.github.io/gripmock/schema/stub.json
service: MyService
method: MyMethod
GripMock 支持与 Buf Schema Registry 的简化集成:
# Public BSR (default)
BSR_BUF_BASE_URL=https://buf.build
BSR_BUF_TOKEN=<token>
# Self-hosted BSR
BSR_SELF_BASE_URL=https://bsr.company.local
BSR_SELF_TOKEN=<token>
# Public module
gripmock buf.build/connectrpc/eliza
# Self-hosted module
gripmock bsr.company.local/team/payments:main
# With stubs
gripmock --stub stubs/ bsr.company.local/team/payments
GripMock 会自动路由模块:
- buf.build/owner/repo → 使用 Buf profile
- bsr.company.local/owner/repo → 使用 Self profile
详情见 BSR Documentation。
GripMock 支持通过以下端点 scheme 从 gRPC reflection 加载 descriptors:
grpc://host:port(不安全)grpcs://host:port(TLS)支持的 query 参数:
timeout(默认 5s)bearer(Authorization token)serverName(TLS SNI override)示例:
gripmock grpc://localhost:50051
gripmock grpcs://api.company.local:443
gripmock grpcs://10.0.0.5:8443?serverName=api.company.local
完整指南:gRPC Reflection Source。
⚠️ 实验性功能:Upstream modes 可能在未提前通知的情况下变更。
Upstream modes 叠加在 reflection sources 之上,定义运行时行为:
proxy - 纯反向代理replay - 本地优先 + 上游回退capture - replay + 从上游自动录制 stub模式指南:
欢迎贡献!详情请见我们的 Contributing Guide(中文版:CONTRIBUTING.zh-CN.md)。
本项目基于 MIT License,详见 LICENSE 文件。
由 GripMock 社区用 ❤️ 打造
$ claude mcp add gripmock \
-- python -m otcore.mcp_server <graph>