This is the ninth day of my participation in the First Challenge 2022. For details: First Challenge 2022.

2.6 Routing model

  • P: the producer sends a message to the Exchange. When sending a message, a routing key is specified.
  • X: Exchange, which receives the message from the producer and sends it to a queue that matches the routing key exactly
  • C1: consumer, whose queue specifies the message whose routing key is error
  • C2: consumer, whose queue specifies the message whose routing key is info, Error, and Warning

In fanout mode, a message is consumed by all subscribed queues. However, in some scenarios, we want different messages to be consumed by different queues. This is where Exchange of type Direct is used.

Under the Direct model:

  • The binding of the queue to the switch cannot be arbitrary, but must be specifiedRoutingKey(Route Key)
  • The sender of a message must also specify the message when sending a message to ExchangeRoutingKey.
  • Exchange no longer delivers messages to each bound queue, but to each bound queueRouting KeyTo make a judgment, only queueRoutingkeyWith the message ofRouting keyThe message is received only when it is exactly the same

2.6.1 producers

_ = ch.ExchangeDeclare("logs_direct"."direct".true.false.false.false.nil, )
body := "Hello World "
_ = ch.Publish("logs_direct"."".false.false,
	amqp.Publishing{
		ContentType: "text/plain",
		Body:        []byte(body),
	})
Copy the code

2.6.2 consumers

  • We only accept the warn
_ = ch.ExchangeDeclare("logs_direct"."direct".true.false.false.false.nil, )
q, _ := ch.QueueDeclare("hello".false.false.true.false.nil, )
_ = ch.QueueBind(q.Name, "warn"."logs_direct".false.nil, )
msgs, _ := ch.Consume(q.Name, "".true.false.false.false.nil.)Copy the code
  • We only accept the info
_ = ch.ExchangeDeclare("logs_direct"."direct".true.false.false.false.nil, )
q, _ := ch.QueueDeclare("hello".false.false.true.false.nil, )
_ = ch.QueueBind(q.Name, "info"."logs_direct".false.nil, )
msgs, _ := ch.Consume(q.Name, "".true.false.false.false.nil.)Copy the code

2.7 switchable viewer model

Topic exchanges, compared to Direct, can route messages to different queues based on a RoutingKey. Topic Exchange allows queues to bind Routing keys using wildcards.

A Routingkey typically consists of one or more words, separated by a “. Split, for example, item.insert

  • The wildcard * matches exactly 1 word # matches one or more words
  • * can only match fan. One

2.7.1 producers

_ = ch.ExchangeDeclare("logs_topic"."topic".true.false.false.false.nil, )
body := "Hello World "
_ = ch.Publish("logs_topic"."".false.false,
	amqp.Publishing{
		ContentType: "text/plain",
		Body:        []byte(body),
	})
Copy the code

2.7.2 consumers

  • Only accept *. One
_ = ch.ExchangeDeclare("logs_topic"."topic".true.false.false.false.nil, )
q, _ := ch.QueueDeclare("hello".false.false.true.false.nil, )
_ = ch.QueueBind(q.Name, "*.one"."logs_topic".false.nil, )
msgs, _ := ch.Consume(q.Name, "".true.false.false.false.nil.)Copy the code
  • Only accept *. Fan
_ = ch.ExchangeDeclare("logs_topic"."topic".true.false.false.false.nil, )
q, _ := ch.QueueDeclare("hello".false.false.true.false.nil, )
_ = ch.QueueBind(q.Name, "*.fan"."logs_topic".false.nil, )
msgs, _ := ch.Consume(q.Name, "".true.false.false.false.nil.)Copy the code

2.8 the RPC model

Add in the future