编写proto文件:
Models.proto
syntax="proto3"; package services; import "google/protobuf/timestamp.proto"; message ProdModel{ //商品模型 int32 prod_id=1; string prod_name=2; float prod_price=3; } message OrderMain{ //主订单模型 int32 order_id=1;//订单ID,数字自增 string order_no=2; //订单号 int32 user_id=3; //购买者ID float order_money=4;//商品金额 google.protobuf.Timestamp order_time=5; //下单时间 repeated OrderDetail order_details=6; } //子订单模型 message OrderDetail{ int32 detail_id=1; string order_no=2; int32 prod_id=3; float prod_price=4; int32 prod_num=5; }
Orders.proto
syntax="proto3"; package services; import "google/api/annotations.proto"; import "Models.proto"; message OrderRequest{ OrderMain order_main=1; } message OrderResponse{ string status=1; string message=2; } service OrderSerivce{ rpc NewOrder(OrderRequest) returns (OrderResponse){ option (google.api.http) = { post : "/v1/orders" body : "order_main" }; } }
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){} }
接下来生成pd.go文件
cd pbfiles && protoc --go_out=plugins=grpc:../services Prod.proto protoc --go_out=plugins=grpc:../services Orders.proto protoc --go_out=plugins=grpc:../services Models.proto protoc --grpc-gateway_out=logtostderr=true:../services Prod.proto protoc --grpc-gateway_out=logtostderr=true:../services Orders.proto cd ..
编写服务端Serbices
OrdersService.go
package services import ( "context" "fmt" ) type OrdersService struct { } func(this *OrdersService)NewOrder(ctx context.Context,orderRequest *OrderRequest) (*OrderResponse, error) { fmt.Println(orderRequest.OrderMain) return &OrderResponse{ Status:"OK", Message:"success", },nil }
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 }
最后注册http
启动server和http
httpserver.go
package main import ( "context" "github.com/grpc-ecosystem/grpc-gateway/runtime" "google.golang.org/grpc" "grpcpro/services" "log" "net/http" ) func main() { gwmux:=runtime.NewServeMux() opt := []grpc.DialOption{grpc.WithInsecure()} err:=services.RegisterProdServiceHandlerFromEndpoint(context.Background(), gwmux,"localhost:8081",opt) if err != nil { log.Fatal(err) } err = services.RegisterOrderSerivceHandlerFromEndpoint(context.Background(), gwmux,"localhost:8081",opt) if err != nil { log.Fatal(err) } httpServer:=&http.Server{ Addr:":8080", Handler:gwmux, } httpServer.ListenAndServe() }
server.go
package main import ( "google.golang.org/grpc" "grpcpro/services" "net" ) func main() { rpcServer:=grpc.NewServer() services.RegisterProdServiceServer(rpcServer,new(services.ProdService)) services.RegisterOrderSerivceServer(rpcServer,new(services.OrdersService)) lis,_:=net.Listen("tcp",":8081") rpcServer.Serve(lis) }
go build server.go
go build httpserver.go
完成。。。。。。。
测试