首页
/ gRPC-Gateway 示例项目教程

gRPC-Gateway 示例项目教程

2024-09-16 20:01:47作者:谭伦延

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.modgo.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 示例项目,并根据需要进行扩展和修改。

登录后查看全文
热门项目推荐

热门内容推荐

最新内容推荐

项目优选

收起
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
178
262
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
867
513
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
129
183
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
265
305
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
398
371
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.07 K
0
ShopXO开源商城ShopXO开源商城
🔥🔥🔥ShopXO企业级免费开源商城系统,可视化DIY拖拽装修、包含PC、H5、多端小程序(微信+支付宝+百度+头条&抖音+QQ+快手)、APP、多仓库、多商户、多门店、IM客服、进销存,遵循MIT开源协议发布、基于ThinkPHP8框架研发
JavaScript
93
15
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
598
57
GitNextGitNext
基于可以运行在OpenHarmony的git,提供git客户端操作能力
ArkTS
10
3