grpc之protobuf常用语法速学
1,语法速学(1):返回商品”数组”、repeated修饰符
Repeated:是一个修饰符,返回字段可以重复任意多次(包括0次) 可以认为就是一个数组(切片)
服务端:
创建protobuf文件
syntax="proto3"; package services; import "google/api/annotations.proto"; message ProdRequest { int32 prod_id =1; //传入的商品ID } message ProdResponse{ int32 prod_stock=1;//商品库存 } message QuerySize{ int32 size = 1;//页尺寸 } //返回 一堆商品库存,使用了repeated修饰符 message ProdResponseList{ repeated ProdResponse prodres=1; } service ProdService { rpc GetProdStock (ProdRequest) returns (ProdResponse){ option (google.api.http) = { get: "/v1/prod/{prod_id}" }; } rpc GetProdStocks(QuerySize) returns (ProdResponseList){ } }
生成Prod.pb.go文件
cd pbfiles && protoc --go_out=plugins=grpc:../services Prod.proto
创建ProdSerbice.go文件
package services import ( "context" ) type ProdService struct { } func(this *ProdService) GetProdStock(ctx context.Context, request *ProdRequest) (*ProdResponse, error) { return &ProdResponse{ProdStock:20},nil } func (this *ProdService) GetProdStocks(ctx context.Context,size *QuerySize) (*ProdResponseList, error){ var Prodres []*ProdResponse Prodres = make([]*ProdResponse,0,3) Prodres = append(Prodres,&ProdResponse{ProdStock:28,}) Prodres = append(Prodres,&ProdResponse{ProdStock:29,}) Prodres = append(Prodres,&ProdResponse{ProdStock:30,}) return &ProdResponseList{ Prodres:Prodres, },nil }
server.go
package main import ( "google.golang.org/grpc" "grpcpro/services" "net" ) func main() { rpcServer:=grpc.NewServer() services.RegisterProdServiceServer(rpcServer,new(services.ProdService)) lis,_:=net.Listen("tcp",":8081") rpcServer.Serve(lis) }
启动服务
go run server.go
客户端:
拷贝服务端的生成的文件Prod.pb.go到客户端目录下
mian.go
package main import ( "context" "fmt" "google.golang.org/grpc" "gprccli/services" "log" ) func main(){ conn,err:=grpc.Dial(":8081",grpc.WithInsecure()) if err!=nil{ log.Fatal(err) } defer conn.Close() prodClient:=services.NewProdServiceClient(conn) ctx:=context.Background() //prodRes,err:=prodClient.GetProdStock(context.Background(), // &services.ProdRequest{ProdId:12}) //if err!=nil{ // log.Fatal(err) //} //fmt.Println(prodRes.ProdStock) response,err := prodClient.GetProdStocks(ctx,&services.QuerySize{Size:10}) if err!=nil{ log.Fatal(err) } fmt.Println(response.Prodres[2].ProdStock) }
启动客户端client 调用服务端gprc服务
go run main.go
2,语法速学(2): 使用枚举、获取分区商品库存
1、传入一个商品ID 获取一个商品库存。
2、根据size获取一堆商品的库存列表
3,创建枚举类型,支持分区枚举参数
enum ProdAreas{ A=0; B=1; C=2; }
ProdService.go
package services import ( "context" ) type ProdService struct {} func(this *ProdService) GetProdStock(ctx context.Context, request *ProdRequest) (*ProdResponse, error) { var stock int32=0 if request.ProdArea==ProdAreas_A{ stock=30 }else if request.ProdArea==ProdAreas_B{ stock=31 }else{ stock=50 } return &ProdResponse{ProdStock:stock},nil } func(this *ProdService) GetProdStocks(ctx context.Context,size *QuerySize) (*ProdResponseList, error) { Prodres:= []*ProdResponse{ &ProdResponse{ProdStock:28}, &ProdResponse{ProdStock:29}, &ProdResponse{ProdStock:30}, &ProdResponse{ProdStock:31}, } return &ProdResponseList{ Prodres:Prodres, },nil }
语法速学(3): 导入外部Proto、获取商品信息
服务端:
Models.proto
syntax="proto3"; package services; message ProdModel{ //商品模型 int32 prod_id=1; string prod_name=2; float prod_price=3; }
Prod.proto
syntax="proto3"; package services; import "google/api/annotations.proto"; import "Models.proto"; enum ProdAreas{ A=0; B=1; C=2; } message ProdRequest { int32 prod_id =1; //传入的商品ID } message ProdResponse{ int32 prod_stock=1;//商品库存 } message QuerySize{ int32 size = 1;//页尺寸 } //返回 一堆商品库存,使用了repeated修饰符 message ProdResponseList{ repeated ProdResponse prodres=1; } service ProdService { rpc GetProdStock (ProdRequest) returns (ProdResponse){ option (google.api.http) = { get: "/v1/prod/{prod_id}" }; } rpc GetProdStocks(QuerySize) returns (ProdResponseList){ } rpc GetProdInfo(ProdRequest) returns(ProdModel){} }
生成pb.go文件
protoc --go_out=plugins=grpc:../services Prod.proto
protoc --go_out=plugins=grpc:../services Models.proto
ProdService.go
package services import ( "context" ) type ProdService struct { } func(this *ProdService) GetProdStock(ctx context.Context, request *ProdRequest) (*ProdResponse, error) { return &ProdResponse{ProdStock:20},nil } func (this *ProdService) GetProdStocks(ctx context.Context,size *QuerySize) (*ProdResponseList, error){ var Prodres []*ProdResponse Prodres = make([]*ProdResponse,0,3) Prodres = append(Prodres,&ProdResponse{ProdStock:28,}) Prodres = append(Prodres,&ProdResponse{ProdStock:29,}) Prodres = append(Prodres,&ProdResponse{ProdStock:30,}) return &ProdResponseList{ Prodres:Prodres, },nil } func (this *ProdService) GetProdInfo(ctx context.Context, in *ProdRequest) (*ProdModel, error){ ret:=ProdModel{ ProdId:101, ProdName:"测试商品", ProdPrice:20.5, } return &ret,nil }
客户端:
拷贝服务端 Models.pb.go和Prod.pb.go到客户端下
package main import ( "context" "fmt" "google.golang.org/grpc" "gprccli/services" "log" ) func main(){ conn,err:=grpc.Dial(":8081",grpc.WithInsecure()) if err!=nil{ log.Fatal(err) } defer conn.Close() prodClient:=services.NewProdServiceClient(conn) ctx:=context.Background() //prodRes,err:=prodClient.GetProdStock(context.Background(), // &services.ProdRequest{ProdId:12}) //if err!=nil{ // log.Fatal(err) //} //fmt.Println(prodRes.ProdStock) //response,err := prodClient.GetProdStocks(ctx,&services.QuerySize{Size:10}) // //if err!=nil{ // log.Fatal(err) //} //fmt.Println(response.Prodres[2].ProdStock) prod,err :=prodClient.GetProdInfo(ctx,&services.ProdRequest{ProdId:12}) if err!=nil{ log.Fatal(err) } fmt.Println(prod) }
相关推荐
小小书童 2020-05-20
极品小肥羊 2020-04-23
小小书童 2019-12-28
comhaqs 2019-12-28
Erick 2019-12-18
极品小肥羊 2018-03-17
darren0zdc 2020-10-16
ltstud 2020-07-24
Erick 2020-07-18
wujingsheng0 2020-06-16
xmwang0 2020-06-14
极品小肥羊 2020-05-31
小小书童 2020-04-10
君小黑 2020-03-04
naiwenw 2020-02-18
小小书童 2020-02-22
dabaoge 2020-02-16
小小书童 2019-12-23