本教程演示如何使用 Spring Boot Starter for Azure 服务总线 JMS 向服务总线
queues
topics
和 接收消息。
Azure 提供了一个异步消息平台,称为
Azure 服务总线
(“服务总线”),该平台基于
高级消息队列协议 1.0
(“AMQP 1.0”)标准。 可以在受支持的 Azure 平台范围内使用服务总线。
适用于 Azure 服务总线 JMS 的 Spring Boot Starter 提供 Spring JMS 与服务总线的集成。
以下视频介绍如何使用 JMS 2.0 将 Spring JMS 应用程序与Azure 服务总线集成。
Azure 订阅 -
免费创建订阅
。
Java 开发工具包 (JDK)
版本 8 或更高版本。
Apache Maven
版本 3.2 或更高版本。
用来测试功能的
cURL
或类似的 HTTP 实用工具。
Azure 服务总线的队列或主题。 如果没有,请参阅
使用 Azure 门户 创建服务总线命名空间和队列
或使用
Azure 门户创建服务总线主题和主题订阅
。
Spring Boot 应用程序。 如果没有,请使用
Spring Initializr
创建一个 Maven 项目。 请务必选择
“Maven 项目
”,并在
“依赖项”
下添加
Spring Web
依赖项,然后选择“Java 版本 8 或更高版本”。
需要 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>4.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
如果使用 Spring Boot 3.x,请确保将 spring-cloud-azure-dependencies
版本设置为 5.0.0
。
有关版本 spring-cloud-azure-dependencies
的详细信息,请参阅 我应使用哪个版本的 Spring Cloud Azure。
Spring Cloud Azure 服务总线 JMS Starter 项目:
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-starter-servicebus-jms</artifactId>
</dependency>
编写应用程序代码
使用以下步骤将应用程序配置为使用服务总线队列或主题来发送和接收消息。
通过将以下属性添加到 application.properties 文件来配置服务总线凭据。
使用服务总线队列
使用服务总线主题
spring.jms.servicebus.connection-string=<ServiceBusNamespaceConnectionString>
spring.jms.servicebus.pricing-tier=<ServiceBusPricingTier>
下表描述了配置中的字段:
spring.jms.servicebus.connection-string=<ServiceBusNamespaceConnectionString>
spring.jms.servicebus.topic-client-id=<ServiceBusSubscriptionID>
spring.jms.servicebus.pricing-tier=<ServiceBusPricingTier>
下表描述了配置中的字段:
spring.jms.servicebus.pricing-tier
指定服务总线的定价层。 支持的值为:“高级”、“标准”和“基本”。 高级层使用 Java Message Service (JMS) 2.0,而标准层和基本层使用 JMS 1.0 与 Azure 服务总线交互。
spring.jms.servicebus.topic-client-id
指定 JMS 客户端 ID,它是 Azure 门户中的服务总线订阅 ID。
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 Word");
@JmsListener(destination = QUEUE_NAME, containerFactory = "jmsListenerContainerFactory")
public void receiveMessage(String message) {
LOGGER.info("Message received: {}", message);
将 <QueueName>
替换为在服务总线命名空间中配置的你自己的队列名称。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
@SpringBootApplication
@EnableJms
public class ServiceBusJMSTopicApplication implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceBusJMSTopicApplication.class);
private static final String TOPIC_NAME = "<TopicName>";
private static final String SUBSCRIPTION_NAME = "<SubscriptionName>";
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
SpringApplication.run(ServiceBusJMSTopicApplication.class, args);
@Override
public void run(String... args) {
LOGGER.info("Sending message");
jmsTemplate.convertAndSend(TOPIC_NAME, "Hello Word");
@JmsListener(destination = TOPIC_NAME, containerFactory = "topicJmsListenerContainerFactory",
subscription = SUBSCRIPTION_NAME)
public void receiveMessage(String message) {
LOGGER.info("Message received: {}", message);
将 <TopicName>
占位符替换为在服务总线命名空间中配置的你自己的主题名称。 将 <SubscriptionName>
占位符替换为服务总线主题中的你自己的订阅名称。
在本教程中,配置或代码中没有身份验证操作。 但是,连接到 Azure 服务需要身份验证。 要完成身份验证,需要使用 Azure 标识。 Spring Cloud Azure 使用 DefaultAzureCredential
Azure 标识库提供的 ,可帮助你获取凭据,而无需更改任何代码。
DefaultAzureCredential
支持多种身份验证方法,并确定应在运行时使用哪种方法。 此方法使应用能够在不同环境中使用不同的身份验证方法, (例如本地和生产环境) ,而无需实现特定于环境的代码。 有关详细信息,请参阅对 Azure 托管的 Java 应用程序进行身份验证的默认 Azure 凭据部分。
若要在本地开发环境中完成身份验证,可以使用 Azure CLI、Visual Studio Code、PowerShell 或其他方法。 有关详细信息,请参阅 Java 开发环境中的 Azure 身份验证。 若要在 Azure 托管环境中完成身份验证,建议使用托管标识。 有关详细信息,请参阅什么是 Azure 资源的托管标识?
启动应用程序。 应会看到 Sending message
并 Hello Word
发布到应用程序日志,如以下示例输出所示:
Sending message
Message received: Hello Word
部署到 Azure Spring Apps
在本地运行 Spring Boot 应用程序后,可以将其移动到生产环境。 借助 Azure Spring Apps ,可以轻松地将 Spring Boot 应用程序部署到 Azure,而无需进行任何代码更改。 该服务管理 Spring 应用程序的基础结构,让开发人员可以专注于代码。 Azure Spring Apps 可以通过以下方法提供生命周期管理:综合性监视和诊断、配置管理、服务发现、CI/CD 集成、蓝绿部署等。 若要将应用程序部署到 Azure Spring Apps,请参阅 将第一个应用程序部署到 Azure Spring Apps。
适用于 Spring 开发人员的 AzureSpring Cloud Azure 服务总线 JMS 示例