Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
Golang使用gRPC进行跨语言服务调用示例_创想鸟

Golang使用gRPC进行跨语言服务调用示例

gRPC基于HTTP/2和Protobuf实现跨语言调用,通过定义统一的proto接口文件,生成Golang服务端和Python客户端代码,确保多语言间高效通信,关键在于接口一致性、高效序列化与版本管理。

golang使用grpc进行跨语言服务调用示例

在微服务架构中,跨语言服务调用是一个常见需求。gRPC 是 Google 开发的高性能、开源的远程过程调用(RPC)框架,基于 HTTP/2 协议,并使用 Protocol Buffers(protobuf)作为接口定义语言(IDL)。Golang 对 gRPC 的支持非常成熟,同时能与其他语言(如 Python、Java、Node.js 等)无缝通信。

1. 定义 Protobuf 接口

要实现跨语言调用,第一步是定义统一的接口和服务消息格式。创建一个 helloworld.proto 文件:

syntax = “proto3”;

package helloworld;

// 定义一个简单的问候服务
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// 请求消息
message HelloRequest {
  string name = 1;
}

// 响应消息
message HelloReply {
  string message = 1;
}

这个 proto 文件定义了一个 Greeter 服务,包含一个方法 SayHello,接收一个名字并返回一条问候语。

2. 生成 Golang 服务端代码

安装必要的工具:

立即学习“go语言免费学习笔记(深入)”;

protoc 编译器Go 插件:go install google.golang.org/protobuf/cmd/protoc-gen-go@latestgRPC 插件:go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

执行命令生成 Go 代码:

protoc –go_out=. –go_opt=paths=source_relative
  –go-grpc_out=. –go-grpc_opt=paths=source_relative
  helloworld/helloworld.proto

会生成两个文件:helloworld.pb.go 和 helloworld_grpc.pb.go。

编写 Golang 服务端实现:

package main

import (
  “context”
  “log”
  “net”

  “google.golang.org/grpc”
  “your-module/helloworld”
)

type server struct {
  helloworld.UnimplementedGreeterServer
}

func (s *server) SayHello(ctx context.Context, req *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
  return &helloworld.HelloReply{
    Message: “Hello ” + req.Name,
  }, nil
}

func main() {
  lis, err := net.Listen(“tcp”, “:50051”)
  if err != nil {
    log.Fatalf(“failed to listen: %v”, err)
  }

  s := grpc.NewServer()
  helloworld.RegisterGreeterServer(s, &server{})
  log.Println(“gRPC server running on :50051”)
  if err := s.Serve(lis); err != nil {
    log.Fatalf(“failed to serve: %v”, err)
  }
}

启动后,该服务将在 50051 端口监听 gRPC 请求。

3. 使用 Python 调用 Golang 服务

Python 可以通过生成对应的 protobuf 代码来调用 Go 实现的服务。

使用相同的 helloworld.proto 文件生成 Python 代码:

python -m grpc_tools.protoc -I. –python_out=. –grpc_python_out=. helloworld/helloworld.proto

编写 Python 客户端:

import grpc
import helloworld_pb2
import helloworld_pb2_grpc

def run():
   with grpc.insecure_channel(‘localhost:50051’) as channel:
     stub = helloworld_pb2_grpc.GreeterStub(channel)
     response = stub.SayHello(helloworld_pb2.HelloRequest(name=’Alice’))
     print(“Response:”, response.message)

if __name__ == ‘__main__’:
   run()

运行前确保已安装依赖:

pip install grpcio grpcio-tools

执行 Python 脚本,将输出:Hello Alice,说明成功调用了 Go 编写的 gRPC 服务。

4. 跨语言通信的关键点

Protobuf 是桥梁:所有语言共享同一份 .proto 文件,保证接口一致性。数据序列化高效:Protobuf 二进制编码比 JSON 更小更快。HTTP/2 支持多路复用:提升连接效率,降低延迟。强类型接口:编译时检查字段和类型,减少运行时错误。注意命名空间和包路径:不同语言对 package 的处理方式略有差异,需正确配置导入路径。

基本上就这些。只要定义好 proto 接口,Golang 服务端和其他语言客户端就能轻松实现跨语言通信。不复杂但容易忽略的是保持 proto 文件同步和版本管理。

以上就是Golang使用gRPC进行跨语言服务调用示例的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1406639.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
Golangstrconv数字与字符串转换实践
上一篇 2025年12月15日 22:25:50
Golang反射与标签解析结合使用实例
下一篇 2025年12月15日 22:26:01

相关推荐

发表回复

登录后才能评论
关注微信