proto grpc

2023-12-25  本文已影响0人  hehehehe

python
./pip3 install grpcio grpcio-tools protobuf -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

 python -m grpc_tools.protoc --proto_path=proto --python_out=. --grpc_python_out=. ./proto/*/*.proto
import grpc
from services import roadserver_pb2, roadserver_pb2_grpc
host = '123.4.5.6'
port = '342'
from lane import lane_pb2

def run():
    conn = grpc.insecure_channel(f"{host}:{port}")  # 监听频道
    client = roadserver_pb2_grpc.RoadServiceStub(channel=conn)  # 客户端使用Stub类发送请求,参数为频道,为了绑定链接
    response = client.DownloadTile(
        roadserver_pb2.TileDownloadRequest(tile_id=[329613331], branch='prod_road_task_ctf_merge_1699542325_91',
                                           need_type=[5, 6]))  # 返回的结果就是proto中定义的类
    print(response.tile_info)
    for tile_info in response.tile_info:
        for feat_list in tile_info.feat_list:
            for feat in feat_list.feats:
                data = feat.data
                print(data)
                group = lane_pb2.LaneGroup()
                group.ParseFromString(data)
                print(group.id)
                print(f"{group.preds=}")


if __name__ == '__main__':
    run()


if __name__ == '__main__':
    run()

golang

protoc --proto_path=./hdmap_server/proto --go_out=hdmap_server --go-grpc_out=hdmap_server ./*/*/*/*.proto
syntax = "proto2";

package RoadPB;

option java_package = "com.aaa.hdmap_server.proto";
option java_outer_classname = "common";
option go_package = "./common";
package main

import (
    "context"
    "fmt"
    "geotool/hdmap_server/lane"
    "geotool/hdmap_server/services"
    "github.com/golang/protobuf/proto"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    "log"
    "time"
)

func main() {
    conn, err := grpc.Dial("127.0.0.1:3452", grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()
    serviceClient := services.NewRoadServiceClient(conn)
    ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    defer cancel()
    tileids := []int32{329613331}
    needType := []int32{5}
    branch := "prod_road_1699542325_91"
    tileDownloadRequest := services.TileDownloadRequest{
        TileId: tileids, Branch: &branch, NeedType: needType}
    tile, err := serviceClient.DownloadTile(ctx, &tileDownloadRequest)
    if err != nil {
        panic(err)
    }
    tileInfo := tile.TileInfo
    //fmt.Println(len(tileInfo))
    for _, tile := range tileInfo {
        //fmt.Println(*tile.TileId)
        //fmt.Println(*tile.Version)
        featList := tile.FeatList
        for _, featlist := range featList {
            feats := featlist.Feats
            for _, feat := range feats {
                fmt.Println(*feat.Id.Type)
                fmt.Println(*feat.Id.Version)
                //data := feat.Data
                data := feat.GetData()
                group := lane.LaneGroup{}
                err := proto.Unmarshal(data, &group)
                //err := json.Unmarshal(data, &group)
                if err != nil {
                    panic(err)
                }
                fmt.Println(group.Id)
                fmt.Println(group.RightBoundarys)

            }
        }
    }
    fmt.Println()
}

上一篇下一篇

猜你喜欢

热点阅读