You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用发送消息至Azure Service Bus Topic报错排查及配置咨询

问题:Spring Boot对接Azure Service Bus Topic启动报错及自定义消息属性发送需求

一、启动报错问题

现象与现有配置

已引入spring-cloud-azure-starter-servicebus依赖,应用配置如下:

spring.cloud.azure.servicebus.namespace=<<some name>> 
spring.cloud.azure.servicebus.entity-name=<<some topic>> 

spring.cloud.azure.servicebus.processor.subscription-name=<<somesub>> 

spring.cloud.azure.servicebus.entity-type=topic

启动时抛出核心错误:

Caused by: java.lang.IllegalArgumentException: Subscription cannot be null.

完整错误栈:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceBusReceiverClientBuilder' defined in class path resource [com/azure/spring/cloud/autoconfigure/implementation/servicebus/AzureServiceBusConsumerClientConfiguration$NoneSessionConsumerClientConfiguration.class]: Failed to instantiate [com.azure.messaging.servicebus.ServiceBusClientBuilder$ServiceBusReceiverClientBuilder]: Factory method 'serviceBusReceiverClientBuilder' threw exception with message: Subscription cannot be null.
    
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.azure.messaging.servicebus.ServiceBusClientBuilder$ServiceBusReceiverClientBuilder]: Factory method 'serviceBusReceiverClientBuilder' threw exception with message: Subscription cannot be null.
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:177) ~[spring-beans-6.1.6.jar:6.1.6]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:644) ~[spring-beans-6.1.6.jar:6.1.6]
    ... 18 common frames omitted
Caused by: java.lang.IllegalArgumentException: Subscription cannot be null.
    at org.springframework.util.Assert.notNull(Assert.java:172) ~[spring-core-6.1.6.jar:6.1.6]
    at com.azure.spring.cloud.service.implementation.servicebus.factory.ServiceBusReceiverClientBuilderFactory.configureService(ServiceBusReceiverClientBuilderFactory.java:54) ~[spring-cloud-azure-service-5.11.0.jar:5.11.0]
    at com.azure.spring.cloud.service.implementation.servicebus.factory.ServiceBusReceiverClientBuilderFactory.configureService(ServiceBusReceiverClientBuilderFactory.java:15) ~[spring-cloud-azure-service-5.11.0.jar:5.11.0]
    at com.azure.spring.cloud.core.implementation.factory.AbstractAzureServiceClientBuilderFactory.build(AbstractAzureServiceClientBuilderFactory.java:128) ~[spring-cloud-azure-core-5.11.0.jar:5.11.0]
    at com.azure.spring.cloud.autoconfigure.implementation.servicebus.AzureServiceBusConsumerClientConfiguration$NoneSessionConsumerClientConfiguration.serviceBusReceiverClientBuilder(AzureServiceBusConsumerClientConfiguration.java:70) ~[spring-cloud-azure-autoconfigure-5.11.0.jar:5.11.0]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:140) ~[spring-beans-6.1.6.jar:6.1.6]
    ... 19 common frames omitted

项目POM文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>azureservicebus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>azureservicebus</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud-azure.version>5.11.0</spring-cloud-azure.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-servicebus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.azure.spring</groupId>
                <artifactId>spring-cloud-azure-dependencies</artifactId>
                <version>${spring-cloud-azure.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

需求为发送端与接收端分离部署,接收端由Logic App实现,询问当前配置缺失项。

二、自定义消息属性发送需求

需实现发送带有如下自定义属性的消息:
自定义消息属性示例图


解答

一、启动报错解决

原因分析

spring-cloud-azure-starter-servicebus默认会自动配置消费端(Receiver/Processor)的Bean,但你的需求仅作为发送端,不需要消费端逻辑。错误源于自动配置尝试初始化消费端,而配置的订阅名称未被正确识别,或不必要的消费端初始化被触发。

解决方案

  1. 禁用消费端自动配置
    在启动类添加注解,排除消费端自动配置类,避免创建不必要的Receiver Bean:

    @SpringBootApplication(exclude = AzureServiceBusConsumerClientConfiguration.class)
    public class ServiceBusQueueApplication {
        public static void main(String[] args) {
            SpringApplication.run(ServiceBusQueueApplication.class, args);
        }
    }
    
  2. 简化发送端配置
    移除消费端相关配置,仅保留发送Topic所需属性:

    spring.cloud.azure.servicebus.namespace=<<你的命名空间>>
    spring.cloud.azure.servicebus.entity-name=<<你的Topic名称>>
    spring.cloud.azure.servicebus.entity-type=topic
    # 认证配置(二选一):
    # 连接字符串认证
    # spring.cloud.azure.servicebus.connection-string=<<你的连接字符串>>
    # Azure AD认证(需额外配置tenant-id、client-id等)
    

二、发送带自定义属性的消息

使用ServiceBusTemplate(Spring风格)或ServiceBusSenderClient直接发送,通过ServiceBusMessage的applicationProperties设置自定义属性:

示例代码(ServiceBusTemplate)

import com.azure.messaging.servicebus.ServiceBusMessage;
import org.springframework.cloud.azure.servicebus.core.ServiceBusTemplate;
import org.springframework.stereotype.Component;

@Component
public class TopicMessageSender {

    private final ServiceBusTemplate serviceBusTemplate;

    public TopicMessageSender(ServiceBusTemplate serviceBusTemplate) {
        this.serviceBusTemplate = serviceBusTemplate;
    }

    public void sendMessageWithCustomProps(String content) {
        ServiceBusMessage message = new ServiceBusMessage(content);
        // 添加自定义属性
        message.getApplicationProperties().put("EventType", "OrderCreated");
        message.getApplicationProperties().put("OrderId", "ORD-12345");
        message.getApplicationProperties().put("Priority", "High");

        serviceBusTemplate.send(message);
    }
}

说明

  • applicationProperties是一个Map<String, Object>,可添加任意键值对作为自定义属性,对应Azure Service Bus消息的自定义属性字段。
  • 接收端(如Logic App)可通过表达式获取这些属性,例如@{triggerBody()?['ApplicationProperties']?['OrderId']}。

内容的提问来源于stack exchange,提问作者Kris Swat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 05:50:53