在 Spring 中使用 JMS 存取 Azure 服務匯流排

本教學課程示範如何使用適用於 Azure 服務匯流排 JMS 的 Spring Boot Starter,將訊息傳送至和接收來自 服務匯流排 queues 和 的topics訊息。

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 識別碼中定義的身分識別連線到 Azure 服務匯流排 JMS 的機制。 透過 Microsoft Entra 驗證,您可以在集中的位置管理資料庫使用者的身分識別和其他 Microsoft 服務,從而簡化權限管理。

SAS 驗證會使用您 Azure 服務匯流排 命名空間的 連接字串,來委派存取 服務匯流排 JMS。 如果您選擇使用共用存取簽章作為認證,您必須自行管理 連接字串。

必要條件

重要

需要 Spring Boot 2.5 版或更高版本,才能完成本教學課程中的步驟。

從 Azure 服務匯流排 傳送和接收訊息

使用 Azure 服務匯流排 的佇列或主題,您可以使用 Spring Cloud Azure 服務匯流排 JMS 來傳送和接收訊息。

若要安裝 Spring Cloud Azure 服務匯流排 JMS 入門模組,請將下列相依性新增至您的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) 應該在<dependencyManagement>pom.xml檔案的 區段中設定。 這可確保所有 Spring Cloud Azure 相依性都使用相同的版本。 如需此 BOM 所用版本的詳細資訊,請參閱 應該使用哪個版本的 Spring Cloud Azure。

  • Spring Cloud Azure 服務匯流排 JMS 入門成品:

    <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 Identity。 Spring Cloud Azure 使用 DefaultAzureCredential,Azure 身分識別連結庫會提供它來協助您取得認證,而不需要變更任何程序代碼。

    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 應用程式提供生命週期管理,使用全方位的監視和診斷、組態管理、服務探索、持續整合與持續傳遞的整合、藍綠部署等等。 若要將應用程式部署至 Azure Spring Apps,請參閱 將第一個應用程式部署至 Azure Spring Apps

下一步