上期说道:http/2还属于一种不算普及的技术协议,可能目前只适合用于内部系统集成,现在开始大面积介入可能为时尚早。不过有些项目需求不等人,需要使用这项技术,所以研究了一下akka-grpc,写了一篇介绍。本想到此为止,继续其它项目。想想这样做法有点不负责任,像是草草收场。毕竟用akka-grpc做了些事情,想想还是再写这篇跟大家分享使用kka-grpc的过程。
我说过,了解akka-grpc的主要目的还是在protobuf的应用上。这是一种高效率的序列化协议。刚好,公司有这么个项目,是一个图像处理平台:把很多图片拍摄终端的图像传上平台进行商品识别、OCR等图像处理。由于终端数量多、图像处理又特别消耗内存、CPU等计算资源、又要求快速响应,所以第一考虑就是使用akka-cluster把图像处理任务分割到多个节点上并行处理。这里就需要仔细考虑图片在终端到平台、然后集群节点与点actor间的传输效率了。如何在akka系统里使用protobuf格式的数据正是本篇讨论和示范的目的。
akka-grpc应用一般从IDL文件里消息类型和服务函数的定义开始,如下面这个.proto文件示范:
syntax = "proto3";
import "google/protobuf/wrappers.proto";
import "google/protobuf/any.proto";
import "scalapb/scalapb.proto";
option (scalapb.options) = {
// don't append file name to package
flat_package: true
// generate one Scala file for all messages (services still get their own file)
single_file: true
// add imports to generated file
// useful when extending traits or using custom types
// import: "io.ontherocks.hellogrpc.RockingMessage"
// code to put at the top of generated file
// works only with `single_file: true`
//preamble: "sealed trait SomeSealedTrait"
};
package com.datatech.pos.abs;
message UCredential {
string userid = 1;
string password = 2;
}
message JWToken {
string jwt = 1;
}
message Picture {
int32 num = 1;
bytes blob = 2;
}
message Capture {
string ean = 1;
bytes cover1 = 2;
bytes cover2 = 3;
}
message Book {
string ean = 1;
string ver = 2;
string isbn = 3;
string title = 4;
string publisher = 5;
double price = 6;
bytes cover1 = 7;
bytes cover2 = 8;
}
message QueryResult {
int32 sts = 1;
string msg = 2;
Book bookinfo = 3;
}
service Services {
rpc GetAuthToken(UCredential) returns (JWToken) {};
rpc SavePicture(Picture) returns (QueryResult) {};
rpc GetPicture(Picture) returns (Picture) {};
// rpc SaveCapture(Capture) returns (QueryResult) {};
// rpc GetCapture(Capture) returns (Capture) {};
// rpc GetBookInfo(Capture) returns (QueryResult) {};
}
因为这次示范针对的是protobuf的使用,所以就拣了SavePict

最低0.47元/天 解锁文章
733

被折叠的 条评论
为什么被折叠?



