gRPC-Gateway 示例项目教程
2024-09-16 09:14:40作者:谭伦延
1. 项目目录结构及介绍
grpc-gateway-example/
├── Makefile
├── README.md
├── api/
│ ├── annotations.proto
│ ├── echo_service.proto
│ ├── http.proto
│ └── http.pb.gw.go
├── cmd/
│ └── server/
│ └── main.go
├── go.mod
├── go.sum
└── third_party/
└── google/
└── api/
├── annotations.proto
└── http.proto
目录结构说明
-
api/: 包含项目的 Protobuf 文件和生成的 Go 代码。
annotations.proto: 定义了 gRPC-Gateway 所需的注解。echo_service.proto: 定义了 Echo 服务的接口。http.proto: 定义了 HTTP 相关的注解。http.pb.gw.go: 生成的 gRPC-Gateway 代码。
-
cmd/server/: 包含项目的启动文件。
main.go: 项目的入口文件,负责启动 gRPC 和 HTTP 服务。
-
third_party/: 包含第三方依赖的 Protobuf 文件。
google/api/: 包含 Google API 的注解定义。
-
Makefile: 项目构建和生成代码的脚本。
-
go.mod 和 go.sum: Go 模块依赖管理文件。
2. 项目的启动文件介绍
cmd/server/main.go
package main
import (
"context"
"flag"
"net/http"
"github.com/golang/glog"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"google.golang.org/grpc"
gw "github.com/philips/grpc-gateway-example/api"
)
var (
echoEndpoint = flag.String("echo_endpoint", "localhost:9090", "endpoint of YourService")
)
func run() error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithInsecure()}
err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux, *echoEndpoint, opts)
if err != nil {
return err
}
return http.ListenAndServe(":8080", mux)
}
func main() {
flag.Parse()
defer glog.Flush()
if err := run(); err != nil {
glog.Fatal(err)
}
}
启动文件说明
main.go: 项目的入口文件,负责启动 gRPC 和 HTTP 服务。run(): 函数负责初始化 gRPC-Gateway 的 HTTP 服务,并将其绑定到指定的端口(默认 8080)。main(): 主函数,解析命令行参数并启动服务。
3. 项目的配置文件介绍
Makefile
all: generate
generate:
protoc -I/usr/local/include -I. \
-I$(GOPATH)/src \
-I$(GOPATH)/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=plugins=grpc:. \
--grpc-gateway_out=logtostderr=true:. \
api/echo_service.proto
clean:
rm -f api/*.pb.go
rm -f api/*.pb.gw.go
配置文件说明
Makefile: 用于生成 Protobuf 文件的 Go 代码和 gRPC-Gateway 代码。generate: 目标用于生成代码,使用protoc工具生成 gRPC 和 gRPC-Gateway 代码。clean: 目标用于清理生成的代码文件。
通过以上步骤,您可以了解如何使用 gRPC-Gateway 示例项目,并根据需要进行扩展和修改。
登录后查看全文
热门项目推荐
相关项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0213
cann-learning-hubCANN 学习中心仓,支持在线互动运行、边学边练,提供教程、示例与优化方案,一站式助力昇腾开发者快速上手。Jupyter Notebook0137
JoyAI-EchoJoyAI-Echo,这是一个独立的、仅用于推理的版本,旨在实现分钟级多镜头音视频生成。它采用了经过蒸馏的DMD生成器、配对的跨模态记忆以及故事级别的一致性。其性能的核心在于,一个跨模态视听记忆库能够在长达五分钟的视频中保持角色外观和语音音色的一致性。同时,一个训练后处理流程将基于记忆的强化学习与分布匹配蒸馏相结合,实现了7.5倍的速度提升,显著增强了视觉质量和对齐效果。00
GLM-5.2智谱开源 GLM-5.2,这是针对长文本任务的最新旗舰模型。相较于前代产品 GLM-5.1,它在长文本任务处理能力上实现了显著飞跃,并且首次在稳定的 100 万 token 上下文中提供这一能力。Jinja00
SwanLab⚡️SwanLab - an open-source, modern-design AI training tracking and visualization tool. Supports Cloud / Self-hosted use. Integrated with PyTorch / Transformers / LLaMA Factory / veRL/ Swift / Ultralytics / MMEngine / Keras etc.Python00
tiny-universe《大模型白盒子构建指南》:一个全手搓的Tiny-UniverseJupyter Notebook03
热门内容推荐
最新内容推荐
项目优选
收起
deepin linux kernel
C
32
16
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
468
461
暂无描述
Dockerfile
776
5.07 K
Ascend Extension for PyTorch
Python
756
961
本项目是CANN提供的transformer类大模型算子库,实现网络在NPU上加速计算。
C++
872
2.01 K
本项目是CANN提供的神经网络类计算算子库,实现网络在NPU上加速计算。
C++
696
1.4 K
昇腾LLM分布式训练框架
Python
183
230
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
1.1 K
1.14 K
本仓库是 Flutter SDK 与 Flutter Engine 的 OpenHarmony 适配版本,由 CPF-Flutter 团队维护。开发者可使用熟悉的 Flutter 技术栈开发 OpenHarmony 应用,3.35.7 及以后的适配版本可基于本仓库源码构建支持 OpenHarmony 的 Flutter Engine。
Dart
1.04 K
271
Oohos_react_native
React Native鸿蒙化仓库
C++
361
430