Derek Eskens
@snekse
Dev 20+ years, Consulting 8+ years
Java, JS, Groovy
'Foodie for Life' is tattooed across my tummy
Star Wars > Star Trek
gRPC Remote Procedure Calls 🙄
"A high performance, open source universal RPC framework"
Binary Transport over HTTP/2
Didn't we abandon RPC?
JSON + HTTP Verbs ≠REST
-ish
Public?
Volume?
Message Size?
Streaming?
Polyglot?
CRUD?
Bandwidth?
Devices?
Technically gRPC is format agnostic
ns/ops
750
1500
2250
syntax = "proto3";
import "google/protobuf/timestamp.proto";
message StockQuoteRequest {
string symbol = 1;
}
message Price {
int32 amount = 1; //Thanks NASDAQ 🙄
}
message BidAsk {
Price bid = 1;
Price ask = 2;
}
message StockQuoteResponse {
string symbol = 1;
Price last = 2;
BidAsk bidAsk = 3;
google.protobuf.Timestamp date = 4;
}
A Brief Protobuf Syntax Intro
syntax = "proto3";
import "opi/brokerage/financial-instruments/pricing.proto";
service QuoteService {
// Get the current price
rpc GetQuote (StockQuoteRequest)
returns (StockQuoteResponse);
// Get level II streaming quotes
rpc GetQuoteStream (StockQuoteRequest)
returns (stream StockQuoteResponse);
}
protoc
public class QuoteServiceImpl extends QuoteServiceImplBase {
@Override
public void getQuote(StockQuoteRequest req, StreamObserver<StockQuoteResponse> rspObserver) {
try {
// Get the quote from a 3rd party provider
RealTimeQuote rtq = rtQuoteProvider.getRealTimeQuote(req.getSymbol());
// Build our response
StockQuoteResponse stockQuoteResponse = toResponse(req.getSymbol(), rtq);
// send the response
rspObserver.onNext(stockQuoteResponse);
} catch (Exception e) {
rspObserver.onError(Status.UNKNOWN
.withDescription("Failed to get quote for " + req.getSymbol())
.augmentDescription(e.getMessage())
.asRuntimeException()
);
} finally {
rspObserver.onCompleted(); // mark RPC call as complete
}
}
// ...
}
private StockQuoteResponse toResponse(String ticker, RealTimeQuote rtq) {
return StockQuoteResponse.newBuilder()
.setSymbol(ticker)
.setLast(asPrice(rtq.last))
.setBidAsk(asBidAsk(rtq.bid, rtq.ask))
.setDate(getTimestamp())
.build();
}
private Price asPrice(int value) {
return Price.newBuilder().setAmount(value).build();
}
private BidAsk asBidAsk(int bid, int ask) {
return BidAsk.newBuilder()
.setBid(asPrice(bid))
.setAsk(asPrice(ask))
.build();
}
private Timestamp getTimestamp() {
// ...
}