本文介绍如何使用 Spring Cloud Azure 和 Spring Messaging Azure 服务总线。 Spring Framework 为与消息传送系统集成提供了广泛的支持。
Spring 消息传递 Azure 服务总线
关键概念
Azure 服务总线是一个完全托管的企业消息代理,其中包含消息队列和发布订阅主题。 Azure 服务总线项目的 Spring Messaging 将核心 Spring 概念应用于基于服务总线的消息传送解决方案的开发。 它提供了一个 模板 作为发送消息的高级抽象。 它还支持消息驱动的 POJO,其中包含 @ServiceBusListener 批注和 侦听器容器。 这些库可促进使用依赖项注入和声明性配置。 在所有这些情况下,可以在 Spring Framework 和 Spring AMQP 中的 RabbitMQ 支持中看到 JMS 支持相似之处。
依赖项设置
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-messaging-azure-servicebus</artifactId>
</dependency>
配置
该库为 ServiceBusTemplate 和 @ServiceBusListener提供以下配置选项:
| 财产 | 类型 | 说明 |
|---|---|---|
spring.cloud.azure.message-converter.isolated-object-mapper |
布尔型 | 隔离的 ObjectMapper bean 是否用于服务总线消息转换器。 默认启用。 |
spring.cloud.azure.servicebus.enabled |
布尔型 | 是否启用 Azure 服务总线。 |
spring.cloud.azure.servicebus.connection-string |
字符串 | 服务总线命名空间连接字符串值。 |
spring.cloud.azure.servicebus.custom-endpoint-address |
字符串 | 连接到服务总线时要使用的自定义终结点地址。 |
spring.cloud.azure.servicebus.namespace |
字符串 | 服务总线命名空间值,它是 FQDN 的前缀。 FQDN 应由 NamespaceName.DomainName 组成 |
spring.cloud.azure.servicebus.entity-type |
字符串 | Azure 服务总线的实体类型。 |
基本用法
自定义服务总线消息转换器
可通过两种方法配置服务总线消息转换器:
将以下属性配置为让默认服务总线消息转换器使用
ObjectMapperbean,可以是自定义ObjectMapperbean 或由 Spring Boot 管理的 bean:spring: cloud: azure: message-converter: isolated-object-mapper: false直接定义服务总线消息转换器 bean:
@Bean AzureMessageConverter<ServiceBusReceivedMessage, ServiceBusMessage> serviceBusMessageConverter() { JsonMapper jsonMapper = JsonMapper.builder().addModule(new JavaTimeModule()).build(); return new ServiceBusMessageConverter(jsonMapper); }
将消息发送到 Azure 服务总线
使用以下步骤发送消息:
使用以下方法之一填写凭据配置选项:
对于
DefaultAzureCredential凭据,请在 application.yml 文件中配置以下属性:spring: cloud: azure: servicebus: namespace: ${AZURE_SERVICE_BUS_NAMESPACE} entity-type: ${AZURE_SERVICE_BUS_ENTITY_TYPE}对于凭据作为连接字符串,请在 application.yml 文件中配置以下属性:
spring: cloud: azure: servicebus: connection-string: ${AZURE_SERVICE_BUS_CONNECTION_STRING} entity-type: ${AZURE_SERVICE_BUS_ENTITY_TYPE}对于作为托管标识的凭据,请在 application.yml 文件中配置以下属性:
spring: cloud: azure: credential: managed-identity-enabled: true client-id: ${AZURE_CLIENT_ID} profile: tenant-id: <tenant> servicebus: namespace: ${AZURE_SERVICE_BUS_NAMESPACE} entity-type: ${AZURE_SERVICE_BUS_ENTITY_TYPE}
注释
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> servicebus: namespace: ${AZURE_SERVICE_BUS_NAMESPACE} entity-type: ${AZURE_SERVICE_BUS_ENTITY_TYPE}
注释
tenant-id允许以下值:common、organizations、consumers或租户 ID。 有关这些值的详细信息,请参阅错误 AADSTS50020 - 来自标识提供者的用户帐户不存在于租户中部分中的使用了错误的终结点(个人和组织帐户)部分。 有关转换单租户应用的信息,请参阅 在 Microsoft Entra ID上将单租户应用转换为多租户。
ServiceBusTemplate已自动配置,你可以将其直接连接到自己的豆类,如以下示例所示:@Component public class MyBean { private final ServiceBusTemplate serviceBusTemplate; public MyBean(ServiceBusTemplate serviceBusTemplate) { this.serviceBusTemplate = serviceBusTemplate; } public void someMethod() { this.serviceBusTemplate.sendAsync('QUEUE_NAME', MessageBuilder.withPayload("Hello world").build()).subscribe(); } }
从 Azure 服务总线接收消息
使用以下步骤接收消息:
填写凭据配置选项。
添加
@EnableAzureMessaging批注,这会触发使用@ServiceBusListener批注的方法的发现,从而在封面下创建消息侦听器容器。@SpringBootApplication @EnableAzureMessaging public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }注释
为了避免重复,自版本
5.21.0起,Spring Cloud Azure 自动配置已启用批注@EnableAzureMessaging。当存在 ServiceBus 基础结构时,可以使用
@ServiceBusListener批注任何 bean 来创建侦听器终结点。 以下组件在QUEUE_NAME队列上创建侦听器终结点:@Component public class MyBean { @ServiceBusListener(destination = "QUEUE_NAME") public void processMessage(String content) { // ... } }
自定义服务总线客户端属性
开发人员可以使用 AzureServiceClientBuilderCustomizer 来自定义服务总线客户端属性。 以下示例自定义 sessionIdleTimeout中的 ServiceBusClientBuilder 属性:
@Bean
public AzureServiceClientBuilderCustomizer<ServiceBusClientBuilder.ServiceBusSessionProcessorClientBuilder> customizeBuilder() {
return builder -> builder.sessionIdleTimeout(Duration.ofSeconds(10));
}
示例
有关详细信息,请参阅 azure-spring-boot-samples GitHub 上的存储库。