简体   繁体   English

Azure 服务总线的 ServiceBusClient - 重用 tcp 连接,如 HttpClientFactory

[英]ServiceBusClient for Azure Service Bus - reuse tcp connections like with HttpClientFactory

I have many CRUD APIs that are communication with each other over HTTP calls (.NET 5)我有许多通过 HTTP 调用(.NET 5)相互通信的 CRUD API
To avoid too many open tcp connections, I use HttpClientFactory via DI in all those services.为了避免打开过多的 tcp 连接,我在所有这些服务中通过 DI 使用HttpClientFactory This works well and I do not experience too many connections that are opened via HTTP.这很好用,我没有遇到通过 HTTP 打开的太多连接。

But my Azure App Services are still complaining about too many SNAT-connections:但是我的 Azure 应用服务仍然抱怨 SNAT 连接太多:

在此处输入图像描述

I guess that the reason is the Azure Service Bus.我猜原因是 Azure 服务总线。 Each call of my APIs will write events to the bus.我的 API 的每次调用都会将事件写入总线。
To do this I create a new instance on each call:为此,我在每次调用时创建一个新实例:

            await using (ServiceBusClient client = new ServiceBusClient(_serviceBusConnectionString))
                {
                    var messageObject = new { message.Name, message.Body };
                    var messageJson = JsonConvert.SerializeObject(messageObject);
                    ServiceBusSender sender = client.CreateSender(_topicName);
                    await sender.SendMessageAsync(new ServiceBusMessage(messageJson));
                }

Many developers are using HttpClient like this (what is a bad idea, read why in the article above).许多开发人员都在使用这样的 HttpClient(这是个坏主意,请阅读上面的文章中的原因)。 The solution for HttpClient is the AddHttpClient method, provided by .NET for this purpose. HttpClient 的解决方案是 AddHttpClient 方法,由 .NET 为此提供。

But what about Azure Service Bus?但是 Azure 服务总线呢? There is nothing like an AzureServiceBusFactory or so and adding the AzureServiceBus as a Singleton would not be a good idea, because the configuration should be different on each call.没有什么像 AzureServiceBusFactory 这样的东西,将 AzureServiceBus 添加为 Singleton 不是一个好主意,因为每次调用的配置都应该不同。

How can I make sure that the connection pool is also reused for Azure Service Bus connections?如何确保连接池也可用于 Azure 服务总线连接? Is there any best practice I missed?有没有我错过的最佳实践? Or do you think the problem with the connections has another cause?还是您认为连接问题有其他原因?

Edit:编辑:

The accepted answer is right.公认的答案是正确的。 This works fine and my errors disappeared.这工作正常,我的错误消失了。 Just use the following to add the service bus client:只需使用以下内容添加服务总线客户端:

            services.AddAzureClients(cfg =>
            {
                cfg.AddServiceBusClient("your-connection-string");
            });

After this, you can easily get the client via DI in all your services.在此之后,您可以在所有服务中通过 DI 轻松获取客户端。

But what about Azure Service Bus?但是 Azure 服务总线呢? There is nothing like an AzureServiceBusFactory or so and adding the AzureServiceBus as a Singleton would not be a good idea, because the configuration should be different on each call.没有什么像 AzureServiceBusFactory 这样的东西,将 AzureServiceBus 添加为 Singleton 不是一个好主意,因为每次调用的配置都应该不同。

It is actually the other way round.它实际上是相反的。 Service Bus team recommends that connection to your Service Bus should be registered as Singleton only using ServiceBusClientBuilderExtensions and should not be closed or disposed after every operation.服务总线团队建议仅使用ServiceBusClientBuilderExtensions将与服务总线的连接注册为 Singleton,并且不应在每次操作后关闭或释放。

From this link :从这个link

The Service Bus objects that interact with the service, such as ServiceBusClient, ServiceBusSender, ServiceBusReceiver, and ServiceBusProcessor, should be registered for dependency injection as singletons (or instantiated once and shared).与服务交互的服务总线对象,例如 ServiceBusClient、ServiceBusSender、ServiceBusReceiver 和 ServiceBusProcessor,应注册为单例(或实例化一次并共享)以进行依赖注入。 ServiceBusClient can be registered for dependency injection with the ServiceBusClientBuilderExtensions.可以使用 ServiceBusClientBuilderExtensions 注册 ServiceBusClient 以进行依赖注入。

We recommend that you don't close or dispose these objects after sending or receiving each message.我们建议您不要在发送或接收每条消息后关闭或处置这些对象。 Closing or disposing the entity-specific objects (ServiceBusSender/Receiver/Processor) results in tearing down the link to the Service Bus service.关闭或处置特定于实体的对象(ServiceBusSender/Receiver/Processor)会导致与服务总线服务的链接断开。 Disposing the ServiceBusClient results in tearing down the connection to the Service Bus service.释放 ServiceBusClient 会导致断开与 Service Bus 服务的连接。

Please see this link for complete recommendations from Service Bus team: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements?tabs=net-standard-sdk-2 .请参阅此链接以获取服务总线团队的完整建议: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-performance-improvements?tabs=net-standard-sdk- 2 .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM