syntax = "proto3";
package services;
import "google/protobuf/timestamp.proto"; //引入timestamp的proto文件
//商品模型
message ProdModel {
int32 prod_id = 1;
string prod_name = 2;
float prod_price = 3;
}
message OrderMain {
int32 order_id = 1; //订单id,数字
int32 user_id = 3; //购买者id
float order_money = 4; //商品金额
google.protobuf.Timestamp order_time = 5; //定义时间戳字段
}
syntax = "proto3";
package services;
import "google/api/annotations.proto";
import "Models.proto";
message OrderResponse {
string status = 1;
string message = 2;
}
service OrderService {
rpc NewOrder (OrderMain) returns (OrderResponse) {}
}
package main
import (
"context"
"fmt"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/grpc"
"grpccli/helper"
"grpccli/services"
"log"
"time"
)
func main() {
//creds, err := credentials.NewClientTLSFromFile("keys/server.crt", "localhost")
//if err != nil {
// log.Fatal(err)+
//}
creds := helper.GetClientCreds()
conn, err := grpc.Dial(":8081", grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatal(err)
}
defer conn.Close()
orderClient := services.NewOrderServiceClient(conn)
t := timestamp.Timestamp{Seconds: time.Now().Unix()}
response, err := orderClient.NewOrder(context.Background(), &services.OrderMain{OrderId: 20, UserId: 30, OrderMoney: 2000, OrderTime: &t})
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
}
原文:https://www.cnblogs.com/hualou/p/12070461.html