![](/img/trans.png)
[英]Azure function using Powershell to consume the data from an Event hub and POST the same to a REST API
[英]Outbound TCP Connection issue cause be sending data to event hub and data lake from azure function
我正在使用 Azure function 和 http POST 触发器,一旦客户端调用它并发布 json 数据,我会将其发送到事件中心并保存到数据湖。 一旦遇到高流量,20k/小时,azure 函数将生成高出站 TCP 连接,这将超过计划的限制(1920)。
这是将数据发送到事件中心的代码:
EventHubClient ehc = EventHubClient.CreateFromConnectionString(cn);
try
{
log.LogInformation($"{CogniPointListener.LogPrefix}Sending {batch.Count} Events: {DateTime.UtcNow}");
await ehc.SendAsync(batch);
await ehc.CloseAsync();
}
catch (Exception exception)
{
log.LogError($"{CogniPointListener.LogPrefix}SendingMessages: {DateTime.UtcNow} > Exception: {exception.Message}");
throw;
}
这是向数据湖发送数据:
var creds = new ClientCredential(clientId, clientSecret);
var clientCreds = ApplicationTokenProvider.LoginSilentAsync(tenantId, creds).GetAwaiter().GetResult();
// Create ADLS client object
AdlsClient client = AdlsClient.CreateClient(adlsAccountFQDN, clientCreds);
try
{
using (var stream = client.CreateFile(fileName, IfExists.Overwrite))
{
byte[] textByteArray = Encoding.UTF8.GetBytes(str);
stream.Write(textByteArray, 0, textByteArray.Length);
}
// Debug
log.LogInformation($"{CogniPointListener.LogPrefix}SaveDataLake saved ");
}
catch (System.Exception caught)
{
string err = $"{caught.Message}Environment.NewLine{caught.StackTrace}Environment.NewLine";
log.LogError(err, $"{CogniPointListener.LogPrefix}SaveDataLake");
throw;
}
谢谢,
我刚刚提出了一个问题 Azure SDK https://github.com/Azure/azure-sdk-for.net/issues/26884报告使用ApplicationTokenProvider.LoginSilentAsync
时套接字耗尽的问题。
Microsoft.IdentityModel.Clients.ActiveDirectory
的当前版本2.4.1
使用Microsoft.Rest.ClientRuntime.Azure.Authentication
的旧版本4.3.0
,它会在每次调用时创建一个新的HttpClientHandler
。
在每个上创建HttpClientHandler
是不好的。 处理HttpClientHandler
后,底层套接字连接在很长一段时间内仍处于活动状态(根据我的经验,超过 30 秒)。
有一个叫做HttpClientFactory
的东西可以确保不经常创建HttpClientHandler
。 这是 Microsoft 的指南,解释了如何正确使用HttpClient
和HttpClientHandler
- Use IHttpClientFactory to implement resilient HTTP requests 。 我希望他们审查他们的 SDK 以确保他们遵循自己的指导方针。
Microsoft.IdentityModel.Clients.ActiveDirectory
自版本 5.0.1 预览版以来支持传递自定义HttpClientFactory
。
IHttpClientFactory myHttpClientFactory = new MyHttpClientFactory();
AuthenticationContext authenticationContext = new AuthenticationContext(
authority: "https://login.microsoftonline.com/common",
validateAuthority: true,
tokenCache: <some token cache>,
httpClientFactory: myHttpClientFactory);
因此,应该可以复制ApplicationTokenProvider.LoginSilentAsync
在您的代码库中执行的操作,以创建传递您自己的HttpClientFactory
实例的AuthenticationContext
。
您可能需要做的事情:
Microsoft.IdentityModel.Clients.ActiveDirectory
添加到项目中HttpClientFactory
。 更多信息可以在另一个StackOverflow 答案中找到ApplicationTokenProvider.LoginSilentAsync(tenantId, creds)
(此代码是httpClientFactory
的内联版本, LoginSilentAsync
传递给AuthenticationContext
var settings = ActiveDirectoryServiceSettings.Azure;
var audience = settings.TokenAudience.OriginalString;
var context = new AuthenticationContext(settings.AuthenticationEndpoint + domain,
settings.ValidateAuthority,
TokenCache.DefaultShared,
httpClientFactory);
var authenticationProvider = new MemoryApplicationAuthenticationProvider(clientCredential);
var authResult = await authenticationProvider.AuthenticateAsync(clientCredential.ClientId, audience, context).ConfigureAwait(false);
var credentials = new TokenCredentials(
new ApplicationTokenProvider(context, audience, clientCredential.ClientId, authenticationProvider, authResult),
authResult.TenantId,
authResult.UserInfo == null ? null : authResult.UserInfo.DisplayableId);
我真的没有复制解决方法中的逻辑,但我认为在Microsoft.Rest.ClientRuntime.Azure.Authentication
中正确修复之前没有其他选择
祝你好运!
TCP连接受到特定数量的限制,具体取决于您所使用的计划(任何级别B / S / P上的消耗量或静态计划)。 对于高工作量,我更喜欢
答 :使用具有单独功能的队列,并通过功能批处理大小和其他设置来限制并发性
要么
B :使用SemaphoreSlim来控制传出流量的并发性。 ( https://docs.microsoft.com/de-de/dotnet/api/system.threading.semaphoreslim?redirectedfrom=MSDN&view=netframework-4.7.2 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.