Master Apache Pulsar with Docker and Spring Boot: A Hands‑On Guide
This article provides a step‑by‑step tutorial on installing Apache Pulsar with Docker, managing it via Pulsar Manager, and integrating it into a Spring Boot application using the Pulsar Java Spring Boot starter, complete with code examples and UI screenshots.
Pulsar Overview
Pulsar is a server‑to‑server messaging middleware with multi‑tenant, high‑performance features. Originally developed by Yahoo and now an Apache project, it follows a publish‑subscribe model where producers publish to topics and consumers subscribe to process messages.
Key features include:
Native clustering support per instance.
Very low publish and end‑to‑end latency.
Scalable to over one million topics.
Simple client APIs for Java, Go, Python, C++.
Multiple subscription modes (exclusive, shared, failover).
Persistent storage via Apache BookKeeper.
Pulsar Installation
Docker provides the simplest installation method.
Pull the Pulsar Docker image:
<code>docker pull apachepulsar/pulsar:2.7.1</code>Run the container exposing port 8080 for HTTP and 6650 for the Pulsar protocol:
<code>docker run --name pulsar \
-p 6650:6650 \
-p 8080:8080 \
--mount source=pulsardata,target=/pulsar/data \
--mount source=pulsarconf,target=/pulsar/conf \
-d apachepulsar/pulsar:2.7.1 \
bin/pulsar standalone</code>Pulsar Visualization
The official Pulsar Manager provides a web UI for managing clusters.
Pull the manager image:
<code>docker pull apachepulsar/pulsar-manager:v0.2.0</code>Run the manager container (port 9527 for UI, 7750 for internal API):
<code>docker run -it --name pulsar-manager \
-p 9527:9527 -p 7750:7750 \
-e SPRING_CONFIGURATION_FILE=/pulsar-manager/pulsar-manager/application.properties \
-d apachepulsar/pulsar-manager:v0.2.0</code>Create a super‑user account via CSRF token request and PUT call (example shown).
<code>CSRF_TOKEN=$(curl http://localhost:7750/pulsar-manager/csrf-token)
curl -H "X-XSRF-TOKEN: $CSRF_TOKEN" -H "Cookie: XSRF-TOKEN=$CSRF_TOKEN;" -H 'Content-Type: application/json' -X PUT http://localhost:7750/pulsar-manager/users/superuser -d '{"name":"admin","password":"apachepulsar","description":"test","email":"[email protected]"}'
</code>After logging in, configure the Service URL (e.g., http://192.168.5.78:8080) and you can view tenants, topics, and topic details.
Using Pulsar with Spring Boot
Integration is straightforward with the official Java SDK or the third‑party Spring Boot starter. The example uses the starter.
Add the dependency to
pom.xml:
<code><!--SpringBoot integration Pulsar-->
<dependency>
<groupId>io.github.majusko</groupId>
<artifactId>pulsar-java-spring-boot-starter</artifactId>
<version>1.0.4</version>
</dependency>
</code>Configure the service URL in
application.yml:
<code>pulsar:
service-url: pulsar://192.168.5.78:6650
</code>Define a configuration class that creates two producers for a custom
MessageDtoand a String topic.
<code>@Configuration
public class PulsarConfig {
@Bean
public ProducerFactory producerFactory() {
return new ProducerFactory()
.addProducer("bootTopic", MessageDto.class)
.addProducer("stringTopic", String.class);
}
}
</code>Implement a producer component that sends
MessageDtoobjects.
<code>@Component
public class PulsarProducer {
@Autowired
private PulsarTemplate<MessageDto> template;
public void send(MessageDto message) {
try {
template.send("bootTopic", message);
} catch (PulsarClientException e) {
e.printStackTrace();
}
}
}
</code>Implement a consumer annotated with
@PulsarConsumerto receive messages.
<code>@Slf4j
@Component
public class PulsarRealConsumer {
@PulsarConsumer(topic="bootTopic", clazz=MessageDto.class)
public void consume(MessageDto message) {
log.info("PulsarRealConsumer consume id:{},content:{}", message.getId(), message.getContent());
}
}
</code>Expose a REST controller to trigger message sending.
<code>@Api(tags = "PulsarController", description = "Pulsar functionality test")
@Controller
@RequestMapping("/pulsar")
public class PulsarController {
@Autowired
private PulsarProducer pulsarProducer;
@ApiOperation("Send message")
@RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
@ResponseBody
public CommonResult sendMessage(@RequestBody MessageDto message) {
pulsarProducer.send(message);
return CommonResult.success(null);
}
}
</code>Calling the endpoint via Swagger shows a log entry confirming successful consumption:
<code>2021-05-21 16:25:07.756 INFO ... PulsarRealConsumer consume id:1,content:SpringBoot Message!</code>Conclusion
Pulsar offers better Docker support and more complete documentation than Kafka, though its visual tool (Pulsar Manager) is relatively simple. Major internet companies use Pulsar, indicating strong performance and stability.
References: Pulsar official documentation, Spring Boot starter repository, and the source code at https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-pulsar.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.