我正在使用使用ServiceBustrigger和查询Azure表存储的Azure函数。为了尽快处理多个消息,我们使用MaxConcurrentCalls设置来启用并行消息处理(例如,我们将MaxConcurrentCalls设置为200)。 我们正在使用托管身份来通过DefaultazureCredential访问服务总线和Azure表存储。

问题描述 投票:0回答:1

获得其他消息处理实例使用并消除对/msi/token的冗余调用?

我们使用Python作为编程语言。

这是初始化Azure Resources
    的代码
  1. # helper function to initialize global table service client def init_azure_resource_clients(config_settings: EligibilitySettings): """get table service client for Azure Table Storage and service bus client""" non_aio_credential = DefaultAzureCredential() # initialize global Service Bus client global _azure_servicebus_client _azure_servicebus_client = ServiceBusClient(fully_qualified_namespace=config_settings.serviceBusNamespaceFQ, credential=non_aio_credential) # initialize global Table Service Client global _azure_table_service_client # prefer connection string if available if config_settings.tableStorageConnectionString: _azure_table_service_client = TableServiceClient.from_connection_string(conn_str=config_settings.tableStorageConnectionString) else: _azure_table_service_client = TableServiceClient(endpoint=f"https://{config_settings.tableStorageAccount}.table.core.windows.net", credential=non_aio_credential)
  2. 这是一些示例代码,涉及:

import logging import azure.functions as func # global reference to the azure resources we need to access _azure_table_service_client = None _azure_servicebus_client = None app = func.FunctionApp() @app.function_name(name="ServiceBusQueueTrigger1") @app.service_bus_queue_trigger(arg_name="msg", queue_name="<QUEUE_NAME>", connection="<CONNECTION_SETTING>") def test_function(msg: func.ServiceBusMessage): logging.info('ServiceBus queue trigger processed message: %s', msg.get_body().decode('utf-8')) # initialize global azure resources init_azure_resource_clients(config_settings) # parse incoming message message_body = msg.get_body().decode('utf-8') message_json = json.loads(message_body) result = process_message(message_json)

要防止每个函数实例在获取
/msi/token

时呼叫

ManagedIdentityCredential
端点,请初始化一次,然后将其缓存在
DefaultAzureCredential
python azure function azureservicebus azure-managed-identity
1个回答
0
投票
_credential

/msi/token

地,确保分配
Storage Table Data Contributor
Storage Table Data Reader
角色。
转到此

link

,以指导使用Python中的Azure函数。

below示例代码正在使用python
在Azure函数中使用
DefaultAzureCredential()

DefaultAzureCredential()

输出:


    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.