使用 Spring 中的 JMS 访问Azure 服务总线

本教程演示如何使用 Spring Boot Starter Azure 服务总线 JMS 向服务总线queuestopics接收消息发送消息。

Azure 提供了一个异步消息平台,称为 Azure 服务总线(“服务总线”),该平台基于高级消息队列协议 1.0(“AMQP 1.0”)标准。 可以在受支持的 Azure 平台范围内使用服务总线。

适用于 Azure 服务总线 JMS 的 Spring Boot Starter 提供 Spring JMS 与 服务总线 的集成。

以下视频介绍如何使用 JMS 2.0 将 Spring JMS 应用程序与 Azure 服务总线集成。


在本教程中,我们包括两种身份验证方法: Microsoft Entra 身份验证共享访问签名(SAS)身份验证。 无密码选项卡显示 Microsoft Entra 身份验证,连接字符串选项卡显示 SAS 身份验证。

Microsoft Entra 身份验证是一种使用 Microsoft Entra ID 中定义的标识连接到 Azure 服务总线 JMS 的机制。 通过 Microsoft Entra 身份验证,可以在一个中心位置集中管理数据库用户标识和其他 Microsoft 服务,从而简化权限管理。

SAS 身份验证使用Azure 服务总线命名空间的连接字符串来委托访问 服务总线 JMS。 如果选择使用共享访问签名作为凭据,则需要自行管理连接字符串。

先决条件

重要

完成本教程中的步骤需要 Spring Boot 2.5 或更高版本。

从Azure 服务总线发送和接收消息

对于Azure 服务总线的队列或主题,可以使用 Spring Cloud Azure 服务总线 JMS 发送和接收消息。

若要安装 Spring Cloud Azure 服务总线 JMS Starter 模块,请将以下依赖项添加到pom.xml文件:

  • Spring Cloud Azure 材料清单(BOM):

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.azure.spring</groupId>
          <artifactId>spring-cloud-azure-dependencies</artifactId>
          <version>5.11.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    

    注意

    如果使用 Spring Boot 2.x,请确保将 spring-cloud-azure-dependencies 版本设置为 4.17.0。 此材料清单(BOM)应在pom.xml文件的部分中进行配置<dependencyManagement>。 这可确保所有 Spring Cloud Azure 依赖项都使用相同的版本。 有关用于此 BOM 的版本的详细信息,请参阅 我应使用哪个版本的 Spring Cloud Azure。

  • Spring Cloud Azure 服务总线 JMS Starter 项目:

    <dependency>
      <groupId>com.azure.spring</groupId>
      <artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
    </dependency>
    

编写应用程序代码

使用以下步骤将应用程序配置为使用服务总线队列或主题发送和接收消息。

  1. 通过将以下属性添加到 application.properties 文件来配置服务总线凭据。

    注意

    Azure 服务总线 JMS 支持使用 Microsoft Entra ID 授权对服务总线资源的请求。 使用 Microsoft Entra ID,可以使用 Azure 基于角色的访问控制 (Azure RBAC) 向安全主体(可能是用户或应用程序服务主体)授予权限。

    重要

    在开始之前,请确保已将Azure 服务总线数据所有者角色分配给当前使用的 Microsoft Entra 帐户。 有关详细信息,请参阅使用 Azure 门户分配 Azure 角色

    spring.jms.servicebus.namespace=<ServiceBusNamespace>
    spring.jms.servicebus.pricing-tier=<ServiceBusPricingTier>
    spring.jms.servicebus.passwordless-enabled=true
    spring.jms.listener.receive-timeout=60000
    

    下表描述了配置中的字段:

    字段 说明
    spring.jms.servicebus.namespace 指定从Azure 门户获取的服务总线服务实例中获取的命名空间。
    spring.jms.servicebus.pricing-tier 指定服务总线的定价层。 支持的值是 高级 值和 标准值。 高级版层使用 Java 消息服务 (JMS) 2.0,而标准层则使用 JMS 1.1 与Azure 服务总线交互。
    spring.jms.servicebus.passwordless-enabled 指定是否使用无密码。
    spring.jms.listener.receive-timeout 默认情况下,接收超时值为 1000。 建议将其设置为 60000
  2. 添加 @EnableJms 以启用对 JMS 侦听器批注终结点的支持。 用于 JmsTemplate 发送消息和 @JmsListener 接收消息,如以下示例所示:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.jms.annotation.EnableJms;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.jms.core.JmsTemplate;
    
    @SpringBootApplication
    @EnableJms
    public class ServiceBusJMSQueueApplication implements CommandLineRunner {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(ServiceBusJMSQueueApplication.class);
        private static final String QUEUE_NAME = "<QueueName>";
    
        @Autowired
        private JmsTemplate jmsTemplate;
    
        public static void main(String[] args) {
            SpringApplication.run(ServiceBusJMSQueueApplication.class, args);
        }
    
        @Override
        public void run(String... args) {
            LOGGER.info("Sending message");
            jmsTemplate.convertAndSend(QUEUE_NAME, "Hello World");
        }
    
        @JmsListener(destination = QUEUE_NAME, containerFactory = "jmsListenerContainerFactory")
        public void receiveMessage(String message) {
            LOGGER.info("Message received: {}", message);
        }
    
    }
    

    <QueueName> 替换为在服务总线命名空间中配置的你自己的队列名称。

    提示

    在本教程中,配置或代码中没有身份验证操作。 但是,连接到 Azure 服务需要身份验证。 要完成身份验证,需要使用 Azure 标识。 Spring Cloud Azure 使用 DefaultAzureCredentialAzure 标识库提供的帮助获取凭据,而无需进行任何代码更改。

    DefaultAzureCredential 支持多种身份验证方法,并确定应在运行时使用哪种方法。 此方法使应用能够在不同环境(如本地环境和生产环境)中使用不同的身份验证方法,而无需实现特定于环境的代码。 有关详细信息,请参阅 DefaultAzureCredential

    若要在本地开发环境中完成身份验证,可以使用 Azure CLI、Visual Studio Code、PowerShell 或其他方法。 有关详细信息,请参阅 Java 开发环境中的 Azure 身份验证。 若要在 Azure 托管环境中完成身份验证,建议使用用户分配的托管标识。 有关详细信息,请参阅什么是 Azure 资源的托管标识?

  3. 启动应用程序。 应会看到 Sending messageHello World 发布到应用程序日志,如以下示例输出中所示:

    Sending message
    Message received: Hello World
    

部署到 Azure Spring Apps

现在,你已在本地运行 Spring Boot 应用程序,现在可以将其移动到生产环境。 使用 Azure Spring Apps 可以轻松地将 Spring Boot 应用程序部署到 Azure,而无需进行任何代码更改。 该服务管理 Spring 应用程序的基础结构,让开发人员可以专注于代码。 Azure Spring Apps 可以通过以下方法提供生命周期管理:综合性监视和诊断、配置管理、服务发现、CI/CD 集成、蓝绿部署等。 若要将应用程序部署到 Azure Spring Apps,请参阅将 第一个应用程序部署到 Azure Spring Apps

后续步骤