This article contains 516 words

Introduction to the

A message queue is a container that holds messages during their transmission.

Application scenarios

  • The decoupling

My service calls other services through the API, and suddenly the other services are no longer needed, and I have to remove the services that I don’t need. Or one day someone asked me to add a service address, not only development trouble, but also affect the performance, so to make the API of this service into a message sent, someone to get a message on their own to connect to my message queue can.

  • Peak clipping

When the service has a large amount of traffic, it is easy to put Redis, MySQL hit down. So you need to make the flow like a pipe, each time the same amount of water, so that the service consumption capacity in general will not die.

  • asynchronous

If it is an order service, you have to wait for payment for the next order, and then you have to wait for a series of operations for the integral calculation after payment, so the access is too slow due to the long link. However, we found that the above process can be done at the same time, and the integral calculation can be done at the same time after you successfully pay. Then start to order into the message queue, payment and points at the same time to execute consumption, improve efficiency.

concept

  • producers

The component that sends the message

  • consumers

Components that consume messages

  • The queue

The component that holds the message, like a real-life container

kafka

component

  • Topic C. Messages are grouped by Topic
  • Partition, partition
  • -Serena: Well, I’m not being a producer.
  • -Penny: You’re a consumer.
  • Broker: Each kafka instance (server)
  • Offset: message offset

Fast installation

Kafka can be downloaded via the website: https://kafka.apache.org/downloads to find the installation package: kafka_2. 11-2.2.2. TGZ. Go to the bin directory and run the./kafka-server-start.sh.. / config/server. The properties to start the service

Test the

  • Create the toppic

./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic wqy

  • Kafka producer client command

./kafka-console-producer.sh --broker-list localhost:9092 --topic wqy

  • Kafka consumer client command

./kafka-console-consumer.shΒ Β --bootstrap-server localhost:9092 --from-beginning --topic wqy

  • Delete the topic:

./kafka-topics.sh --zookeeper localhost:2181 --delete --topic wqy