跳到主要内容

快速入门

DeepSeek V3 中英对照 Quick Tour

目录

我们将通过展示一个以 Reactive 方式生产和消费的 Spring Boot 示例应用程序,快速浏览 Spring 对 Apache Pulsar 的 Reactive 支持。这是一个完整的应用程序,只要您在默认位置(localhost:6650)运行了一个 Pulsar 集群,就不需要任何额外的配置。

1. 依赖

Spring Boot 应用程序只需要 spring-boot-starter-pulsar-reactive 依赖项。以下列表分别展示了如何为 Maven 和 Gradle 定义依赖项:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-pulsar-reactive</artifactId>
<version>3.4.3</version>
</dependency>
</dependencies>
xml

2. 应用程序代码

以下是应用程序的源代码:

@SpringBootApplication
public class ReactiveSpringPulsarHelloWorld {

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

@Bean
ApplicationRunner runner(ReactivePulsarTemplate<String> pulsarTemplate) {
return (args) -> pulsarTemplate.send("hello-pulsar-topic", "Hello Reactive Pulsar World!").subscribe();
}

@ReactivePulsarListener(subscriptionName = "hello-pulsar-sub", topics = "hello-pulsar-topic")
Mono<Void> listen(String message) {
System.out.println("Reactive listener received: " + message);
return Mono.empty();
}
}
java

就是这样,仅仅通过几行代码,我们就拥有了一个可工作的 Spring Boot 应用程序,它以 Reactive 的方式生产和消费来自 Pulsar topic 的消息。

启动后,应用程序使用 ReactivePulsarTemplatehello-pulsar-topic 发送消息。然后,它使用 @ReactivePulsarListenerhello-pulsar-topic 消费消息。

备注

简洁性的关键因素之一是 Spring Boot 启动器,它自动配置并为应用程序提供所需的组件。