In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
In our logging system, we want to subscribe not only to severity-based logs, but also to send-source-based logs. The Unix tool syslog is also based on severity-severity (info/warn/crit …) And equipment-facility (auth/cron/kern …) To route the log.
If this is the case, it will give us a great deal of flexibility. We can monitor not only logs from "cron" with a severity of "critical errors", but also all logs from "kern".
To achieve this, let's learn how to use another, more complex switch, topic exchange.
Topic exchange
Topic exchange is similar to direct exchange in that it routes messages to Queue that matches binding key and routing key, but the matching rules here are somewhat different. It stipulates:
Routing key is a period. Delimited string (we will be a period. Each separate string separated is called a word, such as "stock.usd.nyse", "nyse.vmw", "quick.orange.rabbit"
Binding key is a period like routing key. Delimited string
There can be two special characters * and # in binding key for fuzzy matching, where * is used to match one word and # is used to match multiple words (which can be zero)
In this example, all the messages we send are used to describe small animals. The routing key carried by the message is made up of three words, which are two. Split it up. The first word in the routing button describes the speed of the animal, the second word is the color of the animal, and the third is the kind of animal. So it looks like this: "Speed. Color. Category".
We created three bindings: the binding key of Q1 is ".orange." and the binding key of Q2 is "... rabbit" and "lazy.#".
These three binding key can be summarized as follows:
Q1 is interested in all orange animals.
Q2 is interested in all rabbits and all lazy animals.
The configuration in the above figure is an example:
RoutingKey= "quick.orange.rabbit" messages are routed to both Q1 and Q2
RoutingKey= "lazy.orange.fox" messages will be routed to Q1 and Q2
RoutingKey= "lazy.brown.fox" messages will be routed to Q2
RoutingKey= "lazy.pink.rabbit" messages are routed to Q2 (delivered only once to Q2, although this routingKey matches both bindingKey of Q2)
RoutingKey= "quick.brown.fox", routingKey= "orange", and routingKey= "quick.orange.male.rabbit" messages will be discarded because they do not match any bindingKey
If we break the agreement and send a message with a routing key of one word or four words ("orange" or "quick.orange.male.rabbit"), the message will not be delivered to any queue and will be lost.
To understand the springcloud architecture, please add: three, six, two, four, seven, two, 59
However, even if "lazy.orange.male.rabbit" has four words, it still matches the last binding and is delivered to the second queue.
Topic exchange
Topic exchange is powerful and can exhibit similar behavior to other exchange.
When a queue's binding key is "#" (pound sign), it receives all messages regardless of routing key, just like fanout exchange.
When the special characters * (asterisk) and # (pound sign) do not appear in the binding key, topic exchange will behave like direct exchange.
Code integration
Producer
Public class Tut5Sender {@ Autowird private AmqpTemplate template; @ Autowird private TopicExchange topic; private int index; private int count; private final String [] keys = {"quick.orange.rabbit", "lazy.orange.elephant", "quick.orange.fox", "lazy.brown.fox", "lazy.pink.rabbit", "quick.brown.fox"} @ Scheduled (fixedDelay = 1000, initialDelay = 1000) public void send () {StringBuilder builder = new StringBuilder ("Hello to"); if (+ + this.index = = keys.length) {this.index = 0;} String key = keys [this.index]; builder.append (key) .append ('); builder.append (Integer.toString (+ + this.count)) String message = builder.toString (); template.convertAndSend (topic.getName (), key, message); System.out.println ("[x] Sent'" + message + "");}}
Consumer
Public class Tut5Receiver {@ RabbitListener (queues = "# {autoDeleteQueue1}") public void receiver1 (String in) throws InterruptedException {receive (in, 1);} @ RabbitListener (queues = "# {autoDeleteQueue2}") public void receiver2 (String in) throws InterruptedException {receive (in, 2);} private void receive (String in, int instance) throws InterruptedException {StopWatch watch = new StopWatch (); watch.start () System.out.println ("instance" + instance + "[x] Received'" + in + "'"); doWork (in); watch.stop (); System.out.println ("instance" + instance + "[x] Done in" + watch.getTotalTimeSeconds () + "s") } private void doWork (String in) throws InterruptedException {for (char c: in.toCharArray ()) {if (c = ='.') {Thread.sleep (1000);}
Configuration class
@ Profile ({"tut5", "topics"}) @ Configurationpublic class Tut5Config {@ Bean public TopicExchange topic () {return new TopicExchange ("tut.topic");} @ Profile ("receiver") private static class ReceiverConfig {@ Bean public Queue autoDeleteQueue1 () {return new AnonymousQueue ();} @ Bean public Queue autoDeleteQueue2 () {return new AnonymousQueue () } @ Bean public Binding binding1a (TopicExchange topic, Queue autoDeleteQueue1) {return BindingBuilder.bind (autoDeleteQueue1) .to (topic) .with ("* .orange. *") } @ Bean public Binding binding2a (TopicExchange topic, Queue autoDeleteQueue2) {return BindingBuilder.bind (autoDeleteQueue2) .to (topic) .with ("*. * .clients") } @ Bean public Binding binding2b (TopicExchange topic, Queue autoDeleteQueue2) {return BindingBuilder.bind (autoDeleteQueue2) .to (topic) .with ("lazy.#");} @ Bean public Tut5Receiver receiver () {return new Tut5Receiver () @ Profile ("sender") @ Bean public Tut5Sender sender () {return new Tut5Sender ();}}
Running
Maven compilation
Mvn clean package-Dmaven.test.skip=true
Running
Java-jar target/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar-spring.profiles.active=tut5,receiver-tutorial.client.duration=60000
Java-jar target/rabbitmq-tutorial-0.0.1-SNAPSHOT.jar-spring.profiles.active=tut5,sender-tutorial.client.duration=60000
Output
/ / Sender
Ready... Running for 60000ms
[X] Sent 'Hello to lazy.orange.elephant 1'
[X] Sent 'Hello to quick.orange.fox 2'
[X] Sent 'Hello to lazy.brown.fox 3'
[X] Sent 'Hello to lazy.pink.rabbit 4'
[X] Sent 'Hello to quick.brown.fox 5'
[X] Sent 'Hello to quick.orange.rabbit 6'
[X] Sent 'Hello to lazy.orange.elephant 7'
[X] Sent 'Hello to quick.orange.fox 8'
[X] Sent 'Hello to lazy.brown.fox 9'
[X] Sent 'Hello to lazy.pink.rabbit 10'
/ / Receiver
Ready... Running for 60000ms
Instance 1 [x] Received 'Hello to lazy.orange.elephant 1'
Instance 2 [x] Received 'Hello to lazy.orange.elephant 1'
Instance 2 [x] Done in 2.004s
Instance 1 [x] Done in 2.004s
Instance 2 [x] Received 'Hello to lazy.brown.fox 3'
Instance 1 [x] Received 'Hello to quick.orange.fox 2'
Instance 1 [x] Done in 2.006s
Instance 2 [x] Done in 2.006s
Instance 2 [x] Received 'Hello to lazy.pink.rabbit 4'
Instance 1 [x] Received 'Hello to quick.orange.rabbit 6'
Instance 2 [x] Done in 2.006s
Instance 2 [x] Received 'Hello to quick.orange.rabbit 6'
Instance 1 [x] Done in 2.007s
Instance 1 [x] Received 'Hello to lazy.orange.elephant 7'
Instance 2 [x] Done in 2.006s
Instance 2 [x] Received 'Hello to lazy.orange.elephant 7'
Instance 1 [x] Done in 2.003s
Instance 1 [x] Received 'Hello to quick.orange.fox 8'
Instance 2 [x] Done in 2.005s
Instance 2 [x] Received 'Hello to lazy.brown.fox 9'
Instance 1 [x] Done in 2.005s
Instance 2 [x] Done in 2.004s
Instance 2 [x] Received 'Hello to lazy.pink.rabbit 10'
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.