【问题标题】:How to configure channel options in Google.Cloud.PubSub.V1 SubscriberServiceApiClientBuilder如何在 Google.Cloud.PubSub.V1 SubscriberServiceApiClientBuilder 中配置频道选项
【发布时间】:2021-05-14 10:06:53
【问题描述】:

在 Google.Cloud.PubSub.V1 版本 1.x.x 中,我使用的是 PublisherServiceApiClient.Create 并传入配置有目标、凭据和 ChannelOptions 的“Channel”对象。 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),您必须使用 SubscriberServiceApiClientBuilder(如果您不打算使用默认值: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()

【问题讨论】:

    标签: c# google-cloud-pubsub


    【解决方案1】:

    正如您所指出的,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);
    

    【讨论】:

      猜你喜欢
      • 2017-05-10
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 2016-10-08
      • 2016-06-07
      • 1970-01-01
      相关资源
      最近更新 更多