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这里作为参数传进来表示
}
package services
import (
"context"
)
type ProdService struct {
}
func (this *ProdService) GetProdStock(ctx context.Context, request *ProdRequest) (*ProdResponse, error) {
return &ProdResponse{ProdStock: 20}, nil
}
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)
}
原文:https://www.cnblogs.com/hualou/p/12070536.html