Below you will find pages that utilize the taxonomy term “GRPC”
Rust Tonic `serde::Serialize`'ing types including Google's WKTs
I’m increasing using Rust in addition to Golang to write code. tonic is really excellent but I’d been struggling with coupling its generated types with serde_json because, by default, tonic doesn’t generate types with serde::Serialize annotations.
For what follows, the Protobuf sources are in Google’s googleapis repo, specifically google.firestore.v1.
(JSON) serializing generated types
For example, I’d like to do the following:
use google::firestore::v1::ListenRequest;
async fn mmain() -> Result<(), Box<dyn std::error::Error>> {
...
let rqst = ListenRequest { ... };
let json_string = serde_json::to_string(&rqst)?;
dbg!(json_string);
}
Yields:
rust-analyzer and tonic
Solution: https://github.com/rust-analyzer/rust-analyzer/issues/5799
References: https://jen20.dev/post/completion-of-generated-code-in-intellij-rust/
build.rs:
fn main() -> Result<(), Box<dyn std::error::Error>> {
// gRPC Healthcheck
tonic_build::compile_protos("proto/grpc_health_v1.proto")?;
Ok(())
}
But, because this compiles the proto(s) at build time, the imports aren’t available to Visual Studio Code and rust-analyzer
pub mod grpc_health_v1 {
tonic::include_proto!("grpc.health.v1");
}
// These imports would be unavailable and error
use grpc_health_v1::{
health_check_response::ServingStatus,
health_server::{Health, HealthServer},
HealthCheckRequest, HealthCheckResponse,
};
However,
"rust-analyzer.cargo.loadOutDirsFromCheck": true,
Using tonic
Microsoft’s akri uses tonic to provide gRPC.
For the gRPC Health-checking Protocol proto:
./proto/grpc_health_v1.proto:
syntax = "proto3";
package grpc.health.v1;
service Health {
rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse);
}
message HealthCheckRequest {
string service = 1;
}
message HealthCheckResponse {
enum ServingStatus {
UNKNOWN = 0;
SERVING = 1;
NOT_SERVING = 2;
SERVICE_UNKNOWN = 3; // Used only by the Watch method.
}
ServingStatus status = 1;
}
Tonic supports compiling protos using build.rs:
fn main() -> Result<(), Box<dyn std::error::Error>> {
// compile refers to the path
tonic_build::compile_protos("proto/grpc_health_v1.proto")?;
Ok(())
}
And then useing these:
gRPC Healthchecking in Rust
Golang
Go provides an implementation gprc_health_v1 of the gRPC Health-checking Protocol proto.
This is easily implemented:
package main
import (
pb "github.com/DazWilkin/.../protos"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
func main() {
...
serverOpts := []grpc.ServerOption{}
grpcServer := grpc.NewServer(serverOpts...)
// Register the pb service
pb.RegisterSomeServer(grpcServer, NewServer())
// Register the healthpb service
healthpb.RegisterHealthServer(grpcServer, health.NewServer())
listen, err := net.Listen("tcp", *grpcEndpoint)
if err != nil {
log.Fatal(err)
}
log.Printf("[main] Starting gRPC Listener [%s]\n", *grpcEndpoint)
log.Fatal(grpcServer.Serve(listen))
}
Because it’s gRPC, you need an implementation of the proto for the client, one is provided too grpc-health-probe: