使用 spring-cloud-azure-starter-servicebus 连接到 Azure 服务总线

问题描述 投票:0回答:1

我正在尝试遵循有关使用 spring-cloud-azure-starter-servicebus 连接到 azure 服务总线的 microsoft 文档。

参考:https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/using-service-bus-in-spring-applications

Pom 文件:

 <properties>
            <java.version>17</java.version>
            <spring-cloud-azure.version>5.9.1</spring-cloud-azure.version>
            <spring-cloud.version>2023.0.0</spring-cloud.version>
 </properties>

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Azure Service bus -->
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-servicebus</artifactId>
        </dependency>

        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-messaging-servicebus</artifactId>
            <version>7.15.0</version>
        </dependency>
</dependencies>

Java代码:

@Configuration(proxyBeanMethods = false)
@Slf4j
public class ProcessorClientConfiguration {
    @Bean
    ServiceBusRecordMessageListener processMessage() {
        return context -> {
            ServiceBusReceivedMessage message = context.getMessage();
            log.info("Processing message. Id: %s, Sequence #: %s. Contents: %s%n", message.getMessageId(),
                    message.getSequenceNumber(), message.getBody());
        };
    }

    @Bean
    ServiceBusErrorHandler processError() {
        return context -> {
            log.error("Error when receiving messages from namespace: '%s'. Entity: '%s'%n",
                    context.getFullyQualifiedNamespace(), context.getEntityPath());
        };
    }
}

application.properties 值:

spring.cloud.azure.servicebus.namespace=Endpoint=sb://sbns-...
spring.cloud.azure.servicebus.entity-name=data-preprod
spring.cloud.azure.servicebus.processor.subscription-name=data-preprod
spring.cloud.azure.servicebus.entity-type=topic

错误:

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.3.jar!/:6.1.3]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:647) ~[spring-beans-6.1.3.jar!/:6.1.3]
    ... 26 common frames omitted
Caused by: java.lang.IllegalArgumentException: Subscription cannot be null.
spring-boot spring-cloud azureservicebus azure-servicebus-topics azure-spring-cloud
1个回答
0
投票

无法实例化 [com.azure.messaging.servicebus.ServiceBusClientBuilder$ServiceBusReceiverClientBuilder]:工厂方法“serviceBusReceiverClientBuilder”引发异常并显示消息:订阅不能为空。

  • 根据我的观察,出现此错误是因为您在

    entity-type=topic
    中使用了
    entity-type=Queue
    而不是
    application.properties

  • 当我尝试使用Topic时,我遇到了同样的错误,但使用Queue type按预期工作。

我遵循相同的 MSDOC 使用 Spring Boot 应用程序将消息发送到服务总线队列。

遵循的步骤:

  • 创建了服务总线命名空间和队列。
  • 在服务总线中将以下角色分配给我的用户。
1. Azure Service Bus Data Owner
2. Azure Service Bus Data Receiver
3. Azure Service Bus Data Sender
  • 使用 Maven 创建了 Spring Boot 应用程序。

pom.xml:

<?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.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-starter-servicebus</artifactId>
          </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
          <dependency>
            <groupId>com.azure.spring</groupId>
            <artifactId>spring-cloud-azure-dependencies</artifactId>
            <version>5.9.1</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>

ServiceBusProcessorClientConfiguration.java:

@Configuration(proxyBeanMethods = false)
public class ServiceBusProcessorClientConfiguration {

    @Bean
    ServiceBusRecordMessageListener processMessage() {
        return context -> {
            ServiceBusReceivedMessage message = context.getMessage();
            System.out.printf("Processing message. Id: %s, Sequence #: %s. Contents: %s%n", message.getMessageId(),
                    message.getSequenceNumber(), message.getBody());
        };
    }

    @Bean
    ServiceBusErrorHandler processError() {
        return context -> {
            System.out.printf("Error when receiving messages from namespace: '%s'. Entity: '%s'%n",
                    context.getFullyQualifiedNamespace(), context.getEntityPath());
        };
    }
}

DemoController.java:

@SpringBootApplication
public class DemoApplication implements CommandLineRunner{

    private final ServiceBusSenderClient senderClient;

    public DemoApplication(ServiceBusSenderClient senderClient) {
        this.senderClient = senderClient;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        senderClient.sendMessage(new ServiceBusMessage("Hello, World!"));
        System.out.printf("Sent a message to the queue");
        senderClient.close();
        TimeUnit.SECONDS.sleep(10);
    }
}

应用程序属性:

spring.cloud.azure.servicebus.namespace=<ServiceBusNamespace>
spring.cloud.azure.servicebus.entity-name=<ServiceBusQueueName>
spring.cloud.azure.servicebus.entity-type=queue
spring.cloud.azure.servicebus.processor.auto-startup=false
  • 能够将消息发送到队列:

传送门:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.