SpringBoot e-commerce project mall (40K + STAR) address: github.com/macrozheng/…

Abstract

Pulsar, a popular messaging middleware, has recently found that many of the articles on the web are about performance and comparison Kafka, but few of them are in practice! Therefore, I practiced on the official documents and wrote this article, which is estimated to be the first actual Pulsar article in China. I hope it will be helpful to you all!

Introduction of Pulsar

Pulsar is a server-side messaging middleware with multi-tenancy and high performance advantages. Pulsar was originally developed by Yahoo and is currently managed by the Apache Software Foundation. Pulsar adopts a public-subscribe design model, in which Producer publishes messages to topics, consumers subscribe to topics, and process messages in topics.

Pulsar has the following features:

  • A single instance of Pulsar natively supports clustering.
  • Extremely low release latency and end-to-end latency.
  • Seamlessly scales to over a million topics.
  • Easy to use client API, support Java, Go, Python and C++.
  • Supports multiple Topic subscription modes (exclusive subscription, shared subscription, failover subscription).
  • Message delivery is guaranteed through the persistent message store mechanism provided by Apache BookKeeper.

Pulsar installation

Using Docker to install Pulsar is the easiest, this time we will use Docker to install Pulsar.

  • First download Pulsar Docker image;
Docker pull apachepulsar/pulsar: 2.7.1Copy the code
  • After downloading, run Pulsar container for HTTP protocol access8080Pulsar protocol (Java, Python, and other clients) for access6650Port.
docker run --name pulsar \
-p 6650:6650 \
-p 8080:8080 \
--mount source=pulsardata,target=/pulsar/data \
--mount sourcePulsar = pulsarconf, target = / / conf \ d apachepulsar/pulsar: pulsar 2.7.1 \ bin/standaloneCopy the code

Pulsar visualization

Pulsar Manager is an official visualization tool that can manage multiple pulsArs visually. Although it does not have many functions, it is basically sufficient and supports Docker deployment.

  • downloadpulsar-managerDocker image;
Docker pull apachepulsar/pulsar - manager: v0.2.0Copy the code
  • Run after downloadingpulsar-managerThe container from9527The port can access Web pages;
docker run -it --name pulsar-manager\ -p 9527:9527 -p 7750:7750 \ -e Pulsar SPRING_CONFIGURATION_FILE = / manager/pulsar - manager/application. The properties \ - d apachepulsar/pulsar - manager: v0.2.0Copy the code
  • After the operation is successful, we cannot access it at first, so we need to create an administrator accountadmin:apachepulsar:
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]"}'
Copy the code
  • After the creation, log in to the system from the login page at http://192.168.5.78:9527

  • After successful login, we need to configure an environment first, that is, to configure the Pulsar service to be managedService URLTo:http://192.168.5.78:8080

  • You can view the Tenant list;

  • You can view the list of topics and manage topics;

  • You can also view details about the Topic.

Pulsar is used with SpringBoot

Pulsar is also very easy to use with SpringBoot. We can use Pulsar’s official Java SDK or a third-party SpringBoot Starter. Using Starter, it’s very simple!

  • First of all inpom.xmlTo add Pulsar dependency;
<! Pulsar - SpringBoot integration -- >
<dependency>
    <groupId>io.github.majusko</groupId>
    <artifactId>pulsar-java-spring-boot-starter</artifactId>
    <version>1.0.4</version>
</dependency>
Copy the code
  • Then, inapplication.ymlTo add PulsarService URLConfiguration;
pulsar:
  service-url: Pulsar: / / 192.168.5.78:6650
Copy the code
  • Add the Java configuration of Pulsar, declare two topics, and determine the type of messages to be sent;
/** * Created by macro on 2021/5/21. */
@Configuration
public class PulsarConfig {
    @Bean
    public ProducerFactory producerFactory(a) {
        return new ProducerFactory()
                .addProducer("bootTopic", MessageDto.class)
                .addProducer("stringTopic", String.class); }}Copy the code
  • Create a Pulsar producer to send a message to a Topic.
/** * Created by macro on 2021/5/19. */
@Component
public class PulsarProducer {
    @Autowired
    private PulsarTemplate<MessageDto> template;

    public void send(MessageDto message){
        try {
            template.send("bootTopic",message);
        } catch(PulsarClientException e) { e.printStackTrace(); }}}Copy the code
  • Create a Pulsar consumer to retrieve and consume messages from a Topic, which can also be directly retrieved from the message object.
/** * Pulsar message consumer * Created by macro on 2021/5/19. */
@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()); }}Copy the code
  • Add test interface, call producer send message;
/** * Created by macro on 2021/5/19. */
@API (tags = "PulsarController", description = "Pulsar functional 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); }}Copy the code
  • Call interface in Swagger for testing;

  • After a successful call, the console will enter the following information to indicate that the message has been successfully received and consumed.
16:25:07 2021-05-21. 11472-756 the INFO [al - the listener - 3-1] c.m.m.tiny.com ponent. PulsarRealConsumer: PulsarRealConsumer consume id:1,content:SpringBoot Message!Copy the code

conclusion

Kafka graphic tool Eagle, must recommend to you! The basic use of Kafka is introduced. Here is a comparison with Pulsar. Pulsar is definitely better for Docker support and the official documentation is more complete. Compared to Pulsar Manager and Kafka Eagle, Pulsar’s graphics tools feel a bit shabby. Considering that Yahoo, Tencent, 360 and other Internet giants are using Pulsar, Pulsar’s performance and stability should be very good!

The resources

The official Pulsar document is very complete, the style is also good, basically follow the document to get started.

  • Pulsar, the official document: pulsar.apache.org/docs/en/sta…
  • Official documentation of SpringBoot Starter: github.com/majusko/pul…

Project source code address

Github.com/macrozheng/…

In this paper, making github.com/macrozheng/… Already included, welcome everyone Star!