grpc-POST提交主订单数据(gateway实现http api)
2021-01-20 13:12
标签:uil rpc gate 启动 hid opened https cos print 编写proto文件: Models.proto Orders.proto Prod.proto 接下来生成pd.go文件 编写服务端Serbices OrdersService.go 最后注册http 启动server和http httpserver.go server.go go build server.go go build httpserver.go 完成。。。。。。。 测试 grpc-POST提交主订单数据(gateway实现http api) 标签:uil rpc gate 启动 hid opened https cos print 原文地址:https://www.cnblogs.com/sunlong88/p/12128687.htmlsyntax="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;
}
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"
};
}
}
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){}
}
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 ..
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
}
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()
}
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)
}
上一篇:C# 简单易懂之反射
下一篇:C#委托的最简单入门和理解
文章标题:grpc-POST提交主订单数据(gateway实现http api)
文章链接:http://soscw.com/index.php/essay/44548.html