如何在 Google.Cloud.PubSub.V1 SubscriberServiceApiClientBuilder 中配置通道选项
How to configure channel options in Google.Cloud.PubSub.V1 SubscriberServiceApiClientBuilder
在 Google.Cloud.PubSub.V1 版本 1.x.x 中,我使用 PublisherServiceApiClient.Create 并传入“Channel”对象,该对象配置有目标、凭据和 ChannelOptions。 ChannelOptions 是 grpc.max_receive_message_length,然后是 grpc.max_send_message_length。
根据文档 (https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/api/Google.Cloud.PubSub.V1.SubscriberServiceApiClient.html#Google_Cloud_PubSub_V1_SubscriberServiceApiClient_Create) you have to use SubscriberServiceApiClientBuilder (if you are not going to use default values: https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/api/Google.Cloud.PubSub.V1.SubscriberServiceApiClientBuilder.html)。
SubscriberServiceApiClientBuilder 允许您设置 ChannelCredentials 和 Endpoint,但我看不到设置 ChannelOptions 的方法。如何在 SubscriberServiceApiClientBuilder 中设置 ChannelOptions?
用于创建 SubScriberServiceApiClient 的旧代码:
SubscriberServiceApiClient.Create(channel); // channel is of type Channel
用于创建 SubscriberServiceApiClient 的新代码:
new SubscriberServiceApiClientBuilder
{
ChannelCredentials = channelCredentials,
Endpoint = endPoint,
}.Build()
正如您所指出的,SubscriberServiceApiClient.Create()
已停止接受 grpccore::Channel
对象。但是您仍然可以通过直接创建底层 SubscriberServiceApiClientImpl
对象来创建客户端。
为了详细说明,SubscriberServiceApiClientImpl
接收一个 Subscriber.SubscriberClient
对象,该对象派生自您希望传入的 channel
变量(一个 grpccore::Channel
对象) . 您可以查看 old implementation 以了解 grpcClient
是如何创建的。
总而言之,您的代码可能如下所示:
// Assuming `settings` (a `SubscriberServiceApiSettings` object) is
// created (default to null)
// Assuming you’re created a `channel` object that is a `grpccore::Channel`
gax::GaxPreconditions.CheckNotNull(channel, nameof(channel));
grpccore::CallInvoker callInvoker = new grpccore::DefaultCallInvoker(channel);
gax::GaxPreconditions.CheckNotNull(callInvoker, nameof(callInvoker));
grpccore::Interceptors.Interceptor interceptor = settings?.Interceptor;
if (interceptor != null) {
callInvoker = grpccore::Interceptors.CallInvokerExtensions.Intercept(
callInvoker, interceptor);
}
Subscriber.SubscriberClient grpcClient = new Subscriber.SubscriberClient(callInvoker);
SubscriberServiceApiClient client = new SubscriberServiceApiClientImpl(
grpcClient, settings);
在 Google.Cloud.PubSub.V1 版本 1.x.x 中,我使用 PublisherServiceApiClient.Create 并传入“Channel”对象,该对象配置有目标、凭据和 ChannelOptions。 ChannelOptions 是 grpc.max_receive_message_length,然后是 grpc.max_send_message_length。
根据文档 (https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/api/Google.Cloud.PubSub.V1.SubscriberServiceApiClient.html#Google_Cloud_PubSub_V1_SubscriberServiceApiClient_Create) you have to use SubscriberServiceApiClientBuilder (if you are not going to use default values: https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.PubSub.V1/api/Google.Cloud.PubSub.V1.SubscriberServiceApiClientBuilder.html)。
SubscriberServiceApiClientBuilder 允许您设置 ChannelCredentials 和 Endpoint,但我看不到设置 ChannelOptions 的方法。如何在 SubscriberServiceApiClientBuilder 中设置 ChannelOptions?
用于创建 SubScriberServiceApiClient 的旧代码:
SubscriberServiceApiClient.Create(channel); // channel is of type Channel
用于创建 SubscriberServiceApiClient 的新代码:
new SubscriberServiceApiClientBuilder
{
ChannelCredentials = channelCredentials,
Endpoint = endPoint,
}.Build()
正如您所指出的,SubscriberServiceApiClient.Create()
已停止接受 grpccore::Channel
对象。但是您仍然可以通过直接创建底层 SubscriberServiceApiClientImpl
对象来创建客户端。
为了详细说明,SubscriberServiceApiClientImpl
接收一个 Subscriber.SubscriberClient
对象,该对象派生自您希望传入的 channel
变量(一个 grpccore::Channel
对象) . 您可以查看 old implementation 以了解 grpcClient
是如何创建的。
总而言之,您的代码可能如下所示:
// Assuming `settings` (a `SubscriberServiceApiSettings` object) is
// created (default to null)
// Assuming you’re created a `channel` object that is a `grpccore::Channel`
gax::GaxPreconditions.CheckNotNull(channel, nameof(channel));
grpccore::CallInvoker callInvoker = new grpccore::DefaultCallInvoker(channel);
gax::GaxPreconditions.CheckNotNull(callInvoker, nameof(callInvoker));
grpccore::Interceptors.Interceptor interceptor = settings?.Interceptor;
if (interceptor != null) {
callInvoker = grpccore::Interceptors.CallInvokerExtensions.Intercept(
callInvoker, interceptor);
}
Subscriber.SubscriberClient grpcClient = new Subscriber.SubscriberClient(callInvoker);
SubscriberServiceApiClient client = new SubscriberServiceApiClientImpl(
grpcClient, settings);