GRPC ——官网

官网

Guides

Task-oriented walkthroughs of common use cases

The documentation covers the following techniques:


Authentication

An overview of gRPC authentication, including built-in auth mechanisms, and how to plug in your own authentication systems.

Benchmarking

gRPC is designed to support high-performance open-source RPCs in many languages. This page describes performance benchmarking tools, scenarios considered by tests, and the testing infrastructure.

Error handling

How gRPC deals with errors, and gRPC error codes.

 

Error status codes

Errors are raised by gRPC under various circumstances, from network failures to unauthenticated connections, each of which is associated with a particular status code. The following error status codes are supported in all gRPC languages.

General errors

CaseStatus code
Client application cancelled the request GRPC_STATUS_CANCELLED
Deadline expired before server returned status GRPC_STATUS_DEADLINE_EXCEEDED
Method not found on server GRPC_STATUS_UNIMPLEMENTED
Server shutting down GRPC_STATUS_UNAVAILABLE
Server threw an exception (or did something other than returning a status code to terminate the RPC) GRPC_STATUS_UNKNOWN

Network failures

CaseStatus code
No data transmitted before deadline expires. Also applies to cases where some data is transmitted and no other failures are detected before the deadline expires GRPC_STATUS_DEADLINE_EXCEEDED
Some data transmitted (for example, the request metadata has been written to the TCP connection) before the connection breaks GRPC_STATUS_UNAVAILABLE

Protocol errors

CaseStatus code
Could not decompress but compression algorithm supported GRPC_STATUS_INTERNAL
Compression mechanism used by client not supported by the server GRPC_STATUS_UNIMPLEMENTED
Flow-control resource limits reached GRPC_STATUS_RESOURCE_EXHAUSTED
Flow-control protocol violation GRPC_STATUS_INTERNAL
Error parsing returned status GRPC_STATUS_UNKNOWN
Unauthenticated: credentials failed to get metadata GRPC_STATUS_UNAUTHENTICATED
Invalid host set in authority metadata GRPC_STATUS_UNAUTHENTICATED
Error parsing response protocol buffer GRPC_STATUS_INTERNAL
Error parsing request protocol buffer GRPC_STATUS_INTERNAL

C#

Base case - no encryption or authentication
var channel = new Channel("localhost:50051", ChannelCredentials.Insecure);
var client = new Greeter.GreeterClient(channel);
...
With server authentication SSL/TLS
var channelCredentials = new SslCredentials(File.ReadAllText("roots.pem"));  // Load a custom roots file.
var channel = new Channel("myservice.example.com", channelCredentials);
var client = new Greeter.GreeterClient(channel);
Authenticate with Google
using Grpc.Auth;  // from Grpc.Auth NuGet package
...
// Loads Google Application Default Credentials with publicly trusted roots.
var channelCredentials = await GoogleGrpcCredentials.GetApplicationDefaultAsync();

var channel = new Channel("greeter.googleapis.com", channelCredentials);
var client = new Greeter.GreeterClient(channel);
...
Authenticate a single RPC call
var channel = new Channel("greeter.googleapis.com", new SslCredentials());  // Use publicly trusted roots.
var client = new Greeter.GreeterClient(channel);
...
var googleCredential = await GoogleCredential.GetApplicationDefaultAsync();
var result = client.SayHello(request, new CallOptions(credentials: googleCredential.ToCallCredentials()));
...

posted @ 2021-05-14 14:40  PanPan003  阅读(443)  评论(0编辑  收藏  举报