前段时间看到开源项目中使用到grpc streaming接口,趁此机会学习一下,找了个很久之前的官方demo,RouteGuide,将其代码适配到最新grpc版本,这个小项目非常的齐全,涉及到:
业务场景
单向流写操作
单向流读操作
双向流读写操作
json模拟数据DB
业务场景为:地理位置信息和路线导航服务。
本篇文章首发于星球,相关源码已上传至星球,感兴趣加入即可。
数据存储以json文件存储,里面包含了多个经纬度信息,具体字段如下:
{
"location": {
"latitude": 412567807,
"longitude": -741058078
},
"name": "New York State Reference Route 987E, Southfields, NY 10975, USA"
}
随后我们便可以写一个json解析工具将其读取,服务端/客户端便可以基于这份数据进行RPC接口的通信。
GetFeature接口
获取给定位置的地理特征,如果在给定位置没有地理特征,则返回一个空名称的特征。
rpc GetFeature(Point) returns (Feature) {}
ListFeatures接口
服务器到客户端的流式 RPC。
获取给定矩形范围内可用的地理特征。结果是以流的方式传送,而不是一次性返回。
rpc ListFeatures(Rectangle) returns (stream Feature) {}
RecordRoute
客户端到服务器的流式 RPC。接受一系列正在遍历的路线上的点流,并在遍历完成时返回 RouteSummary。
rpc RecordRoute(stream Point) returns (RouteSummary) {}
RouteChat
双向流式 RPC。接受在遍历路线时发送的一系列 RouteNotes 消息,同时接收其他 RouteNotes(例如,来自其他用户)。
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
获取给定位置的地理特征
询问服务端这份数据是否存在:
point = MakePoint(409146138, -746188906);
输出:
-------------- GetFeature --------------
Found feature called BerkshireValleyManagementAreaTrail,Jefferson,NJ,USA at 40.9146, -74.6189
Found no feature at 0, 0
查询矩阵内的地理信息
rect.mutable_lo()->set_latitude(400000000);
rect.mutable_lo()->set_longitude(-750000000);
rect.mutable_hi()->set_latitude(420000000);
rect.mutable_hi()->set_longitude(-730000000);
std::cout << "Looking for features between 40, -75 and 42, -73"
<< std::endl;
输出:
-------------- ListFeatures --------------
Looking for features between 40, -75 and 42, -73
Found feature called PatriotsPath,Mendham,NJ07945,USA at 40.7838, -74.6144
Found feature called 101NewJersey10,Whippany,NJ07981,USA at 40.8123, -74.3999
Found feature called U.S.6,Shohola,PA18458,USA at 41.3628, -74.9016
Found feature called 5ConnersRoad,Kingston,NY12401,USA at 42, -74.0371
Found feature called MidHudsonPsychiatricCenter,NewHampton,NY10958,USA at 41.4008, -74.3951
Found feature called 287FlugertownRoad,LivingstonManor,NY12758,USA at 41.9611, -74.6525
Found feature called 4001TremleyPointRoad,Linden,NJ07036,USA at 40.611, -74.2187
Found feature called 352SouthMountainRoad,Wallkill,NY12589,USA at 41.6802, -74.237
Found feature called BaileyTurnRoad,Harriman,NY10926,USA at 41.295, -74.1077
Found feature called 193-199WawayandaRoad,Hewitt,NJ07421,USA at 41.2145, -74.395
Found feature called 406-496WardAvenue,PineBush,NY12566,USA at 41.5737, -74.2848
Found feature called 162MerrillRoad,HighlandMills,NY10930,USA at 41.3844, -74.0502
Found feature called ClintonRoad,WestMilford,NJ07480,USA at 41.0873, -74.4459
...
输出:
-------------- RecordRoute --------------
Visiting point 41.9611, -74.6525
Visiting point 40.7587, -74.167
Visiting point 41.5302, -74.8416
Visiting point 40.8032, -74.8645
Visiting point 40.0273, -74.1221
Visiting point 41.5302, -74.8416
Visiting point 41.2856, -74.5149
Visiting point 40.9224, -74.8287
Visiting point 40.1263, -74.7964
Visiting point 40.9533, -74.2201
Finished trip with 10 points
Passed 7 features
Travelled 887071 meters
It took 11 seconds
ClientReaderWriter + ServerReaderWriter
-------------- RouteChat --------------
Sending message First message at 0, 0
Sending message Second message at 0, 1
Sending message Third message at 1, 0
Sending message Fourth message at 0, 1
Got message Second message at 0, 1