本文介绍如何使用 Spring Cloud Azure 和 Spring Messaging Azure 存储队列。 Spring Framework 为与消息传送系统集成提供了广泛的支持。
Spring Messaging Azure 存储队列
关键概念
Azure 队列存储是用于存储大量消息的服务。 使用 HTTP 或 HTTPS 通过经过身份验证的调用从世界任何地方访问消息。 队列消息的大小最多可为 64 KB。 队列可以包含数百万条消息,最多只能包含存储帐户的总容量限制。 队列通常用于创建以异步方式处理的积压工作。 Azure 队列存储项目的 Spring Messaging 将核心 Spring 概念应用于基于服务总线的消息传送解决方案的开发。 它提供了一个 模板, 作为用于发送和接收消息的高级抽象。 这些库可促进使用依赖项注入和声明性配置。
依赖项设置
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-messaging-azure-storage-queue</artifactId>
</dependency>
配置
该库为 StorageQueueTemplate提供以下配置选项:
| 财产 | 类型 | 说明 |
|---|---|---|
| spring.cloud.azure.message-converter.isolated-object-mapper | 布尔型 | 隔离的 ObjectMapper bean 是否用于存储队列消息转换器。 默认启用。 |
| spring.cloud.azure.storage.queue.enabled | 布尔型 | 是否启用了 Azure 存储队列。 |
| spring.cloud.azure.storage.queue.connection-string | 字符串 | 存储队列命名空间连接字符串值。 |
| spring.cloud.azure.storage.queue.accountName | 字符串 | 存储帐户名称。 |
| spring.cloud.azure.storage.queue.accountKey | 字符串 | 存储帐户密钥。 |
基本用法
自定义存储队列消息转换器
可通过两种方法配置存储队列消息转换器:
将以下属性配置为让默认存储队列消息转换器使用
ObjectMapperbean,可以是自定义ObjectMapperbean 或由 Spring Boot 管理的 bean:spring: cloud: azure: message-converter: isolated-object-mapper: false直接定义存储队列消息转换器 bean:
@Bean AzureMessageConverter<QueueMessageItem, QueueMessageItem> storageQueueMessageConverter() { JsonMapper jsonMapper = JsonMapper.builder().addModule(new JavaTimeModule()).build(); return new ServiceBusMessageConverter(jsonMapper); }
向 Azure 存储队列发送和接收消息
使用以下步骤发送和接收消息:
使用以下方法之一填写凭据配置选项:
对于
DefaultAzureCredential凭据,请在 application.yml 文件中配置以下属性:spring: cloud: azure: storage: queue: account-name: ${AZURE_STORAGE_QUEUE_ACCOUNT_NAME}对于凭据作为连接字符串,请在 application.yml 文件中配置以下属性:
spring: cloud: azure: storage: queue: connection-string: ${AZURE_STORAGE_QUEUE_CONNECTION_STRING}对于作为托管标识的凭据,请在 application.yml 文件中配置以下属性:
spring: cloud: azure: credential: managed-identity-enabled: true client-id: ${AZURE_CLIENT_ID} profile: tenant-id: <tenant> storage: queue: account-name: ${AZURE_STORAGE_QUEUE_ACCOUNT_NAME}
注释
tenant-id允许以下值:common、organizations、consumers或租户 ID。 有关这些值的详细信息,请参阅错误 AADSTS50020 - 来自标识提供者的用户帐户不存在于租户中部分中的使用了错误的终结点(个人和组织帐户)部分。 有关转换单租户应用的信息,请参阅 在 Microsoft Entra ID上将单租户应用转换为多租户。
对于作为服务主体的凭据,请在 application.yml 文件中配置以下属性:
spring: cloud: azure: credential: client-id: ${AZURE_CLIENT_ID} client-secret: ${AZURE_CLIENT_SECRET} profile: tenant-id: <tenant> storage: queue: account-name: ${AZURE_STORAGE_QUEUE_ACCOUNT_NAME}
注释
tenant-id允许以下值:common、organizations、consumers或租户 ID。 有关这些值的详细信息,请参阅错误 AADSTS50020 - 来自标识提供者的用户帐户不存在于租户中部分中的使用了错误的终结点(个人和组织帐户)部分。 有关转换单租户应用的信息,请参阅 在 Microsoft Entra ID上将单租户应用转换为多租户。
StorageQueueTemplate已自动配置。 你可以将其直接连接到自己的豆类以发送或接收消息,如以下示例所示:@Component public class MyBean { private final StorageQueueTemplate storageQueueTemplate; public MyBean(StorageQueueTemplate storageQueueTemplate) { this.storageQueueTemplate = storageQueueTemplate; } public void someMethod() { this.serviceBusTemplate.sendAsync('STORAGE_QUEUE_NAME', MessageBuilder.withPayload("Hello world").build()).subscribe(); } public void processMessage() { Message<?> message = storageQueueTemplate.receiveAsync('STORAGE_QUEUE_NAME', Duration.ofSeconds(30)).block(); // ... } }
示例
有关详细信息,请参阅 GitHub 上的 azure-spring-boot-samples 存储库