gRPC入门教程(grpc实战)
haoteby 2025-05-08 18:30 28 浏览
Overview
gRPC 是一个现代开源的高性能远程过程调用 (RPC) 框架,可以在任何环境中运行。它可以通过对负载平衡、跟踪、健康检查和身份验证的可插拔支持有效地连接数据中心内和跨数据中心的服务。它也是适用于分布式计算的最后一英里,将设备、移动应用程序和浏览器连接到后端服务。
gRPC使用protocol buffers作为Interface Definition Language (IDL),client端使用方法stub,server端有同样方法实现.
数据处理流程
优点
gRPC一个高性能、开源的通用 RPC 框架,主要特点:
- 服务定义简单:仅需使用Protocol Buffers就完成service定义
- 启动快速可扩展:只需一行即可安装运行和开发环境,还可以使用该框架扩展到每秒数百万个 RPC
- 跨语言和平台:自动化生成各种语言和平台服务客户端和服务器存根
- 双向流式且集成身份验证:双向流式传输和完全集成的可插拔身份验证以及基于 HTTP/2 的传输
Protocol Buffer编译器安装
- 在线安装
//Linux
apt install -y protobuf-compiler
protoc --version
//Mac
brew install protobuf
protoc --version # Ensure compiler version is 3+
- 二进制文件安装
PB_REL="https://github.com/protocolbuffers/protobuf/releases"
curl -LO $PB_REL/download/v3.15.8/protoc-3.15.8-linux-x86_64.zip
unzip protoc-3.15.8-linux-x86_64.zip -d $HOME/.local
export PATH="$PATH:$HOME/.local/bin"
Go plugins安装
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
.proto文件中定义Service
Protocol buffer结构化数据使用 messages 定义,其中每条message都是一个小的信息逻辑记录,包含一系列称为字段的name-value值对。如 hello.proto
syntax = "proto3";
option go_package = "github.com/gs-samples/grpc/helloworld";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
生成gRPC代码
指定proto文件路径,执行protoc命令
protoc --go_out=. --go_opt=paths=source_relative \ 10514 16:49:50
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
helloworld/hello.proto
执行后生成
helloworld/helloworld.pb.go 和
helloworld/helloworld_grpc.pb.go
生成pb代码中,依赖
google.golang.org/grpc v1.36.0
google.golang.org/protobuf v1.28.0
Server与Client测试
package grpc
import (
"context"
"fmt"
pb "go-samples/rpc/grpc/helloworld"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
"net"
"testing"
"time"
)
const (
addr = "localhost:50051"
port = "50051"
)
func TestClientCall(t *testing.T) {
go startServer()
time.Sleep(time.Second * 2)
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
// Contact the server and print out its response.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: "world1"})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
}
func startServer() {
lis, err := net.Listen("tcp", fmt.Sprintf(":%v", port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterGreeterServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
// server is used to implement helloworld.GreeterServer.
type server struct {
pb.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
return &pb.HelloReply{Message: "Server reply--->Hello " + in.GetName()}, nil
}
相关推荐
- Chrome OS 41 用 Freon 取代 X11_chrome os atom
-
在刚发布的ChromeOS41里,除了常规的Wi-Fi稳定性提升(几乎所有系统的changelog里都会包含这一项)、访客模式壁纸等之外,还存在底层改变。这一更新中Google移除...
- 苹果iPad Pro再曝光 有望今年六月发布
-
自进入2015年以后,有关大屏iPad的消息便一直不绝于耳,之前就有不少媒体猜想这款全新的平板电脑将会在三月发布,不过可惜的是我么只在那次发布会上看到了MacBookPro。近日@Ubuntu团队便...
- 雷卯针对香橙派Orange Pi 5 Max开发板防雷防静电方案
-
一、应用场景高端平板、边缘计算、人工智能、云计算、AR/VR、智能安防、智能家居、Linux桌面计算机、Linux网络服务器、Android平板、Android游戏机...
- Ubuntu Server无法更新问题解决_ubuntu server not found
-
上周老家的一台运行UbuntuServer的盒子无法连接上了,中秋这两天回来打开,顺手更新一下发现更新报错。提示`E:Releasefileforhttps://mirrors.aliyun...
- 虚幻引擎5正式版发布:古墓丽影&巫师新作采用、新一代实时渲染
-
机器之心报道编辑:杜伟、陈萍虚幻引擎5的目标是「助力各种规模的团队在视觉领域和互动领域挑战极限,施展无限潜能」。...
- AMD Milan-X双路霄龙7773X平台基准测试曝光 CPU缓存总量超1.5GB
-
OpenBenchmarking基准测试数据库刚刚曝光了AMDMilan-X双路霄龙7773X平台的跑分成绩,虽然很快就被撤下,但我们还是知晓了高达1.6GB的总CPU缓存。早些时...
- 全网最新的Dify(1.7.2)私有化离线部署教程(ARM架构)
-
Hello,大家好!近期工作中有涉及到Dify私有化离线部署,特别是针对于一些国产设备。因此特别整理了该教程,实测有效!有需要的小伙伴可以参考下!本文主要针对Dify1.7.2最新版本+国产操作系...
- 在ubuntu下新建asp.net core项目_创建ubuntu
-
本文一步步讲述在ubuntu下用visualstudiocode创建asp.netcore项目的过程。step1:环境操作系统:virtualbox下安装的lubuntu。请不要开启“硬件...
-
- 在晶晨A311D2处理器上进行Linux硬件视频编码
-
在KhadasVIM4AmogicA311D2SBC上,我更多的时间是在使用Ubuntu22.04。它的总体性能还不错,只不过缺少3D图形加速和硬件视...
-
2025-08-26 17:22 haoteby
- Nacos3.0重磅来袭!全面拥抱AI,单机及集群模式安装详细教程!
-
之前和大家分享过JDK17的多版本管理及详细安装过程,然后在项目升级完jdk17后又发现之前的注册和配置中心nacos又用不了,原因是之前的nacos1.3版本的,版本太老了,已经无法适配当前新的JD...
- 电影质量级渲染来了!虚幻引擎5.3正式发布:已开放下载
-
快科技9月8日消息,日前,Unrealengine正式发布了虚幻引擎5.3,带来了大量全方位的改进。...
- 2025如何选购办公电脑?极摩客mini主机英特尔系列选购指南
-
当下,迷你主机的性能越来越强,品类也越来越多。但是CPU是不变的,基本都是AMD和英特尔的。有一个小伙伴在评论区提问,我应该如何在众多机器中选购一台符合自己的迷你主机呢?那今天我们优先把我们的系列,分...
- ubuntu 20.04+RTX4060 Ti+CUDA 11.7+cudnn
-
ububtu添加国内源sudocp/etc/apt/sources.list/etc/apt/sources.list.backupsudovim/etc/apt/sources.lis...
- Linux Mint 18将重新基于Ubuntu 16.04 带来更好硬件支持
-
项目负责人ClementLefebvre在本月6日披露了关于LinuxMint18“Sarah”操作系统的大量信息,包括带来全新扁平化体验的Mint-Y主题。而现在,这款将于年底之前上线的操作...