当前位置: 首页 > news >正文

go-zero开发入门-API网关开发示例

开发一个 API 网关,代理 https://blog.csdn.net/Aquester/article/details/134856271 中的 RPC 服务。

网关完整源代码

// file: main.go
package mainimport ("flag""fmt""github.com/zeromicro/go-zero/core/conf""github.com/zeromicro/go-zero/gateway"
)var configFile = flag.String("f", "etc/gateway.yaml", "the config file")func main() {var c gateway.GatewayConfflag.Parse()conf.MustLoad(*configFile, &c)server := gateway.MustNewServer(c)defer server.Stop()fmt.Printf("Starting gateway on %s:%d...\n", c.Host, c.Port)server.Start()
}

上述代码可以使用 go-zero 的脚手架工具 goctl 自动生成,比如:

goctl gateway -dir gateway

同时会生成网关配置文件 gateway.yaml:

# cat etc/gateway.yaml 
Name: gateway-example # gateway name
Host: localhost # gateway host
Port: 8888 # gateway port
Upstreams: # upstreams- Grpc: # grpc upstreamTarget: 0.0.0.0:8080 # grpc target,the direct grpc server address,for only one node
#      Endpoints: [0.0.0.0:8080,192.168.120.1:8080] # grpc endpoints, the grpc server address list, for multiple nodes
#      Etcd: # etcd config, if you want to use etcd to discover the grpc server address
#        Hosts: [127.0.0.1:2378,127.0.0.1:2379] # etcd hosts
#        Key: greet.grpc # the discovery key# protoset modeProtoSets:- hello.pb# Mappings can also be written in proto options
#    Mappings: # routes mapping
#      - Method: get
#        Path: /ping
#        RpcPath: hello.Hello/Ping

编译网关源码生成可执行程序文件

# cat Makefile 
all: gateway
gateway: main.gogo build -o gateway main.go clean:rm -f gateway

生成被代理 RPC 服务的 pb 文件

protoc --descriptor_set_out=add.pb add.proto

add.pb 是一个二进制文件。

编辑网关配置文件 gateway.yaml

# cat etc/gateway.yaml 
Name: go-zero-gateway # 网关名
Host: 0.0.0.0 # 网关的服务地址
Port: 8888 # 网关的服务端口
Upstreams: # 被网关代理的上游服务- Grpc: # gRPC 服务列表Etcd:Hosts:- 127.0.0.1:2379 # etcd 服务地址和端口(Endpoints)Key: add.rpcProtoSets: # 这里为被代理 RPC 服务的 pb 文件列表- /root/go-zero/gateway/proto/add.pbMappings: # Mappings can also be written in proto options- Method: get # HTTP 方法Path: /add # HTTP 路径RpcPath: add.Adder/add # 对应的 RPC 路径(第一个 add 为包名,Adder 为 service 名,后一个 add 为 service 中的方法名)

启动网关

./gateway

通过网关访问 RPC 服务

# curl -i '127.0.0.1:8888/add'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-a53f71dd9eb638c5c2af03eb633a56be-dad8426820c63b1d-00
Date: Fri, 08 Dec 2023 02:08:39 GMT
Content-Length: 9{"sum":0}# curl -i '127.0.0.1:8888/add?a=1&b=2'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Traceparent: 00-0df2bbd651938b704c532a01bb2f16e3-27c5267777c8cc06-00
Date: Fri, 08 Dec 2023 02:08:47 GMT
Content-Length: 9{"sum":3}
http://www.lryc.cn/news/255187.html

相关文章:

  • TCP一对一通信
  • laravel DB::connection 报错 Database connection [{$name}] not configured
  • 快捷支付是什么?快捷支付好申请吗?
  • 如何在Spring Boot中集成RabbitMQ
  • 【Spring Boot 源码学习】ApplicationContextInitializer 详解
  • 软考2018下午第六题改编逻辑(状态模式)
  • 基于深度学习的典型目标跟踪算法
  • docker搭建nginx实现负载均衡
  • Android蓝牙协议栈fluoride(二) - 软件框架
  • IDEA中的Postman!
  • el-tooltip (element-plus)修改长度
  • Verilog学习 | 用initial语句写出固定的波形
  • 使用arcpy移除遥感影像云层
  • 编程应用实例,商超进销存管理系统软件,支持扫描条形码也可以自编码
  • 第二证券:十字星买入法?
  • 【C++】如何优雅地把二维数组初始化为0
  • 8 个顶级的 PDF 转 Word 转换器
  • 计算机网络——习题
  • Linux 线程——信号量
  • 网页设计中增强现实的兴起
  • Android7.0新特性
  • visual studio 2022中使用vcpkg包管理器
  • C语言-链表_基础
  • Java第二十一章总结
  • 【keil备忘录】2. stm32 keil仿真时的时间测量功能
  • 图的存储(邻接矩阵,边集数组,邻接表,链式前向星)
  • Linux 基础知识整理(二)
  • 2024年值得关注的8个未来数据库
  • C++新经典模板与泛型编程:将trait类模板用作模板参数
  • BUUCTF-[GYCTF2020]FlaskApp flask爆破pin