Core concepts, architecture and lifecycle | gRPC
Supported channels:
-
HTTP/2
-
HTTP: gRPC-Web
-
Unix domain sockets
Using gRPC for (local) inter-process communication | F. Werner’s Research Page
-
Named pipes: I could use a Windows named pipe transport. · Issue #13447 · grpc/grpc
-
Shared memory: Is there a way to use shared memory instead of socket on a local machine for rpc? · Issue #19959 · grpc/grpc
Awesome gRPC: A curated list of useful resources for gRPC
gRPC guarantees message ordering within an individual RPC call.
How does grpc guarantee the request (and response) order?
-
As far as I understand, we give no guarantees about the order in which messages will be delivered for separate calls, even on the same channel. Even if it did, the calls could be handled by different threads on the server, which could arbitrarily interleave and execute their handling code in any order.
-
If you need ordering you could use a grpc stream, which guarantees in order delivery of each message sent on the stream.
gRPC is stateless. gRPC services are transient.
gRPC 只支持单向调用,不过 bidirectional streaming 可以作为双向调用的一种间接实现,例如:
service FullDuplex {
rpc WaitRequests(stream ClientResponse) returns (stream ServerRequest);
}虽然用这种方法实现反向调用有些 hacky,但这样做也有一个好处:方便追踪调用链。
gRPC-Web 目前尚不支持 bidirectional streaming。
protocol buffers - Protobuf RPC callbacks - Stack Overflow
-
Metadata
authorization
Rust: tonic/examples/src/authentication/server.rs at master - hyperium/tonic
#[derive(Clone)] pub struct AuthInterceptor { pub auth: Option<Arc<tonic::metadata::MetadataValue<tonic::metadata::Ascii>>>, } impl AuthInterceptor { pub fn new(auth: Option<&str>) -> Self { Self { auth: auth.map(|auth| auth.parse().unwrap()).map(Arc::new), } } } impl tonic::service::Interceptor for AuthInterceptor { fn call(&mut self, mut req: tonic::Request<()>) -> tonic::Result<tonic::Request<()>> { if let Some(auth) = &self.auth { req.metadata_mut() .insert("authorization", auth.as_ref().clone()); } Ok(req) } } client: MyServiceClient< tonic::service::interceptor::InterceptedService< tonic::transport::channel::Channel, AuthInterceptor, >, >,
- C++
- Rust (unofficial)
- Go
- .NET
- JVM
- Java
- Kotlin
- Python
- Ruby
- Web
- JavaScript (gRPC-Web)
- Node.js
- PHP
- Objective-C
- Dart
-
gRPC for .NET (Grpc.Net.Client)
Use gRPC client with .NET Standard 2.0 | Microsoft Learn
-
.NET Framework: HTTP/2 support is only available on Windows 11+ and must be over TLS.
Running a server requires ASP.NET Core Runtime:
-
-
gRPC C# (Grpc.Core) (discontinued)
尽管 Grpc.Core.Xamarin 支持 Android 和 iOS,但它只支持 Xamarin,不支持 MAUI,并且 Android 每个 ABI 下的 lib 体积都在 100 MiB 左右,远超桌面端的 10 MiB。由于包体积过大,从 v2.45 开始的包无法再上传到 NuGet 上,只能手动下载:
可以通过以下方式手动支持 .NET Android:
<PropertyGroup> <RuntimeIdentifiers Condition="$(TargetFramework.Contains('-android'))">android-arm64;android-arm;android-x86</RuntimeIdentifiers> </PropertyGroup> <ItemGroup Condition="$(TargetFramework.Contains('-android'))"> <AndroidNativeLibrary Include="$(NuGetPackageRoot)grpc.core.xamarin\2.44.0\native\android\arm64-v8a\libgrpc_csharp_ext.so"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Abi>arm64-v8a</Abi> </AndroidNativeLibrary> </ItemGroup> <ItemGroup Condition="$(TargetFramework.Contains('-android'))"> <AndroidNativeLibrary Include="$(NuGetPackageRoot)grpc.core.xamarin\2.44.0\native\android\armeabi-v7a\libgrpc_csharp_ext.so"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Abi>armeabi-v7a</Abi> </AndroidNativeLibrary> </ItemGroup> <ItemGroup Condition="$(TargetFramework.Contains('-android'))"> <AndroidNativeLibrary Include="$(NuGetPackageRoot)grpc.core.xamarin\2.44.0\native\android\x86\libgrpc_csharp_ext.so"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Abi>x86</Abi> </AndroidNativeLibrary> </ItemGroup>
-
Google.Protobuf is also required.
Known issues:
-
tonic: A native gRPC client & server implementation with async/await support.
-
tonic-web: grpc-web protocol translation for tonic services.
-
Unix domain sockets: examples/src/uds
Unix Domain Socket on Windows for gRPC connection - help - The Rust Programming Language Forum
-
Named pipe: How to implement an async read/write incoming stream with tokio named pipe? - Issue #1518 - hyperium/tonic
- async-stream: catalinsh/tonic-named-pipe-example: Basic Tonic (gRPC) example using Windows Named Pipes for communication
Helper struct/wrapper for a stream of named pipe connections - Issue #6591 - tokio-rs/tokio
-
concurrent use of client - Issue #285 - hyperium/tonic
client.clone() -
This means that you can get all the benefits of tonic while using regular Rust types and without needing to use proto files or build scripts. Of course, this comes at the sacrifice of interoperability.
-
-
grpc-rs: The gRPC library for Rust built on C Core library and futures
Notes:
-
Client::connect()is async and fallible.Workaround:
Client::new(tonic::transport::Endpoint::from_static("http://[::1]:50051").connect_lazy())
-
Interceptor: tonic/examples/src/interceptor/client.rs at master - hyperium/tonic
Build:
-
One crate
[package] name = "plugin" version = "0.1.0" edition = "2021" [[bin]] name = "plugin-server" path = "src/server.rs" [[bin]] name = "plugin-client" path = "src/client.rs" [dependencies] prost = "0.12" tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } tonic = "0.11" ...server_deps ...client_deps [build-dependencies] tonic-build = "0.11"
-
Three crates
plugin:[package] name = "plugin" version = "0.1.0" edition = "2021" [dependencies] prost = "0.12" tonic = "0.11" [build-dependencies] tonic-build = "0.11"
plugin-server:[package] name = "plugin-server" version = "0.1.0" edition = "2021" [dependencies] plugin = { path = "../plugin" } tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } tonic = "0.11" ...server_deps
plugin-client:[package] name = "plugin-client" version = "0.1.0" edition = "2021" [dependencies] plugin = { path = "../plugin" } tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } tonic = "0.11" ...client_deps
GUI:
- Postman
- gRPC UI: An interactive web UI for gRPC, along the lines of postman
- gRPCox: Like Postman, but for gRPC
- BllomRPC (discontinued)
CLI: