首页 > 其他 > 详细

日志类型,创建主订单模型

时间:2019-12-19 23:55:13      阅读:161      评论:0      收藏:0      [点我收藏+]

技术分享图片

定义Models.proto

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; //定义时间戳字段
}

定义Orders.proto

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!