当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot整合RabbitMQ实例(Topic模式)

Spring Boot整合RabbitMQ实例(Topic模式)

2019年07月22日  | 移动技术网IT编程  | 我要评论

1.topic交换器介绍

topic exchange 转发消息主要是根据通配符。 在这种交换机下,队列和交换机的绑定会定义一种路由模式,那么,通配符就要在这种路由模式和路由键之间匹配后交换机才能转发消息。

在这种交换机模式下:

    路由键必须是一串字符,用句号(.) 隔开,比如说 agreements.us,或者 agreements.eu.stockholm 等。

    路由模式必须包含一个 星号(*),主要用于匹配路由键指定位置的一个单词,比如说,一个路由模式是这样子:agreements..b.*,那么就只能匹配路由键是这样子的:第一个单词是 agreements,第四个单词是 b。 井号(#)就表示相当于一个或者多个单词,例如一个匹配模式是agreements.eu.berlin.#,那么,以agreements.eu.berlin开头的路由键都是可以的。
具体代码发送的时候还是一样,第一个参数表示交换机,第二个参数表示routing key,第三个参数即消息。如下:

rabbittemplate.convertandsend("testtopicexchange","key1.a.c.key2", " this is  rabbitmq!");

topic 和 direct 类似, 只是匹配上支持了"模式", 在"点分"的 routing_key 形式中, 可以使用两个通配符:

*表示一个词.

#表示零个或多个词.

如上图所示:此类交换器使得来自不同的源头的消息可以到达一个对列,其实说的更明白一点就是模糊匹配的意思,例如:上图中红色对列的routekey为usa.#,#代表匹配任意字符,但是要想消息能到达此对列,usa.必须匹配后面的#好可以随意。图中usa.news,usa.weather都能找到红色队列,符号“#”匹配一个或多个词,符号“”匹配不多不少一个词。因此“usa.#”能够匹配到“usa.news.xxx”,但是“usa.” 只会匹配到“usa.xxx”。
注:交换器说到底是一个名称与队列绑定的列表。当消息发布到交换器时,实际上是由你所连接的信道,将消息路由键同交换器上绑定的列表进行比较,最后路由消息

2.示例代码

1).rabbitmq的topic的bean配置

rabbittopic.java类:

package com.example.rabbitmqtopic;
import org.springframework.amqp.core.binding;
import org.springframework.amqp.core.bindingbuilder;
import org.springframework.amqp.core.queue;
import org.springframework.amqp.core.topicexchange;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
@configuration
public class rabbittopic {
 final static string message = "topic.message";
 final static string messages = "topic.messages";
 //创建队列
 @bean
 public queue queuemessage() {
  return new queue(rabbittopic.message);
 }
 //创建队列
 @bean
 public queue queuemessages() {
  return new queue(rabbittopic.messages);
 }
 //创建交换器
 @bean
 topicexchange exchange() {
  return new topicexchange("topicexchange");
 }
  //对列绑定并关联到routingkey
 @bean
 binding bindingexchangemessage(queue queuemessage, topicexchange exchange) {
  return bindingbuilder.bind(queuemessage).to(exchange).with("topic.message");
 }
 //对列绑定并关联到routingkey
 @bean
 binding bindingexchangemessages(queue queuemessages, topicexchange exchange) {
  return bindingbuilder.bind(queuemessages).to(exchange).with("topic.#");//*表示一个词,#表示零个或多个词
  }
}

2).消息生产者生产消息

topicsender.java类:

package com.example.rabbitmqtopic.rabbitmq;
import org.springframework.amqp.core.amqptemplate;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;
@component
public class topicsender {
 @autowired
 private amqptemplate rabbittemplate;
 public void send() {
  string context = "hi, i am message all";
  system.out.println("sender : " + context);
  this.rabbittemplate.convertandsend("topicexchange", "topic.1", context);
 }
 public void send1() {
  string context = "hi, i am message 1";
  system.out.println("sender : " + context);
  this.rabbittemplate.convertandsend("topicexchange", "topic.message", context);
 }
 public void send2() {
  string context = "hi, i am messages 2";
  system.out.println("sender : " + context);
  this.rabbittemplate.convertandsend("topicexchange", "topic.messages", context);
  }
}

3).消息消费者

topicreceiver.java类:

package com.example.rabbitmqtopic.rabbitmq;
import org.springframework.amqp.rabbit.annotation.rabbithandler;
import org.springframework.amqp.rabbit.annotation.rabbitlistener;
import org.springframework.stereotype.component;
@component
@rabbitlistener(queues = "topic.message")
public class topicreceiver {
 @rabbithandler
 public void process(string message) {
  system.out.println("topic receiver1 : " + message);
 }
}

topicreceiver2.java类:

package com.example.rabbitmqtopic.rabbitmq;
import org.springframework.amqp.rabbit.annotation.rabbithandler;
import org.springframework.amqp.rabbit.annotation.rabbitlistener;
import org.springframework.stereotype.component;
@component
@rabbitlistener(queues = "topic.messages")
public class topicreceiver2 {
 @rabbithandler
 public void process(string message) {
  system.out.println("topic receiver2 : " + message);
 }
}

4).测试

rabbitmqtopictest.java类:

package com.example.rabbitmqtopic.rabbitmq;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.test.context.junit4.springrunner;
@runwith(springrunner.class)
@springboottest
public class rabbitmqtopictest {
 @autowired
 private topicsender sender;
 @test
 public void topic() throws exception {
  sender.send();
 }
 @test
 public void topic1() throws exception {
  sender.send1();
 }
 @test
 public void topic2() throws exception {
  sender.send2();
 }
}

以上所述是小编给大家介绍的spring boot整合rabbitmq实例(topic模式),希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网