gumingyaotangwei 2019-12-22
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
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 }
服务端:
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) }