创建服务端并调用
创建服务端并调用
syntax = "proto3"; package services; message ProdRequest { int32 prod_id = 1; //传入id } message ProdResponse { int32 prod_stock = 1; //商品库存 } service ProdService { rpc GetProdStock (ProdRequest) returns (ProdResponse); //ProdRequest这里作为参数传进来表示 }
通过protoc --go_out=plugins=grpc:../services Prod.proto 生成grpc中间文件
然后创建一个实现了中间文件中的Server接口和Client接口的结构体
package services import ( "context" ) type ProdService struct { } func (this *ProdService) GetProdStock(ctx context.Context, request *ProdRequest) (*ProdResponse, error) { return &ProdResponse{ProdStock: 20}, nil }
将实现了接口的struct注册到rpcServer中去
package main import ( "google.golang.org/grpc" "grpcpro/services" "net" ) func main() { rpcServer := grpc.NewServer() services.RegisterProdServiceServer(rpcServer, new(services.ProdService)) //将实现的Server注册到rpcServer中区 lis, _ := net.Listen("tcp", ":8081") rpcServer.Serve(lis) }
相关推荐
苦咖啡flask 2020-06-25
mbcsdn 2020-06-05
wanghongsha 2020-04-25
yuanye0 2020-04-18
神龙 2020-02-19
zhujiangtaotaise 2020-01-05
bapinggaitianli 2020-01-01
君小黑 2019-12-19
安之偌素 2019-12-18
Rgenxiao 2011-05-26
xz0mzq 2011-05-26
cxymds 2019-11-04
MrQuinn 2017-08-17
freedomwind00 2018-02-28
瞌睡虫 2015-05-14
davis 2015-05-14
Adolphlwq 2015-05-14