项目搭建
1.添加依赖
org.springframework.boot
spring-boot-starter-amqp
2.配置aplication.yml
## rabbitmq的最简单配置
spring:
rabbitmq:
host: 192.168.40.128 ## active tcp://
port: 5672 ## 通信端口 插件的图形化界面端口15672
username: user
password: 123456
一对一消息模型
1.配置Bean,新建队列
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConf {
// 新建队列
@Bean
public Queue getTestQueue() {
return new Queue("test");
}
}
启动项目后会在RabbitMQ中新建一个名为test的队列,如下图
也可以在图形化界面中添加队列,如下图
2.发送消息
import java.util.HashMap;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
@RestController
public class TestController {
@Autowired
private RabbitTemplate template;
@GetMapping("/send")
public String sendMQ() {
HashMap map = new HashMap();
map.put("name", "郭南林");
map.put("sex", "男");
//这里的routingKey填的是队列名test,发送的消息为map的JSON串
template.convertAndSend("test",JSON.toJSONString(map));
return "send ok";
}
}
访问http://localhost:8080/send则发送一条消息
3.消费消息
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues="test")
public class TestListener {
@RabbitHandler
public void handler(String object) {
System.out.println(object);
}
}
消息发送了之后,这边就可以消费消息,将消息打印至控制台。
一对多模型
RabbitMQ里面通过路由绑定(交换机绑定队列)来实现,常用的交换机有三种:
a.direct 直连交换机
b.fanout 广播交换机
c.topic 通配符匹配交换机
1.direct交换机
1.1新建交换机和队列,并绑定
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class DirectConf {
// 新建交换机
@Bean
public DirectExchange getDirectExchange() {
return new DirectExchange("dir.exc");
}
// 新建队列
@Bean
public Queue getDirQueue() {
return new Queue("dir.queue");
}
// 绑定
@Bean
public Binding getBinding() {
return BindingBuilder.bind(getDirQueue()).to(getDirectExchange()).with("dir.key");
}
}
1.2发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DirectController {
@Autowired
private RabbitTemplate template;
@GetMapping("/send/dir/{name}")
public String sendFaout(@PathVariable("name")String name) {
//这里exchange(交换机)为dir.exc,routingKey为绑定时.with中的值dir.key,消息为name
template.convertAndSend("dir.exc", "dir.key", name);
return "send ok";
}
}
1.3消费消息
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues="dir.queue")
public class DirectListener {
@RabbitHandler
public void handler(String object) {
System.out.println("我是来自直接路由的消息");
System.out.println(object);
}
}
2.fanout交换机
2.1新建交换机和队列,并绑定
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutConf {
// 新建交换机
@Bean
public FanoutExchange getFanoutExchange() {
return new FanoutExchange("program.exc");
}
// 新建队列
@Bean
public Queue getQueue1() {
return new Queue("java.program");
}
@Bean
public Queue getQueue2() {
return new Queue("php.program");
}
@Bean
public Queue getQueue3() {
return new Queue("c.program");
}
@Bean //把交换机和队列绑定在一起
public Binding getBinding1() {
return BindingBuilder.bind(getQueue1()).to(getFanoutExchange());
}
@Bean //把交换机和队列绑定在一起
public Binding getBinding2() {
return BindingBuilder.bind(getQueue2()).to(getFanoutExchange());
}
@Bean //把交换机和队列绑定在一起
public Binding getBinding3() {
return BindingBuilder.bind(getQueue3()).to(getFanoutExchange());
}
}
2.2发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FanoutController {
@Autowired
private RabbitTemplate template;
@GetMapping("/send/program/{name}")
public String sendFaout(@PathVariable("name")String name) {
//这里交换机为广播交换机program.exc,绑定了program.exc的所有队列都会收到消息
template.convertAndSend("program.exc", null, name);
return "send ok";
}
}
2.3消费消息
这里添加三个消费者,java.program、php.program、c.program。
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues="java.program")
public class Fanout1Listener {
@RabbitHandler
public void handler(String object) {
System.out.println("我是java:"+object);
}
}
3.topic交换机
3.1新建交换机和队列,并绑定
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 TopicConf {
// 新建交换机
@Bean
public TopicExchange getTopicExchange() {
return new TopicExchange("top.exc");
}
//新建队列
@Bean
public Queue getTopicQueue1() {
return new Queue("topic.1");
}
@Bean
public Queue getTopicQueue2() {
return new Queue("topic.2");
}
//绑定交换机和队列
@Bean
public Binding getTopicBinding1() {
//#匹配0个或多个单词,*匹配一个单词
return BindingBuilder.bind(getTopicQueue1()).to(getTopicExchange()).with("#.key");
}
@Bean
public Binding getTopicBinding2() {
return BindingBuilder.bind(getTopicQueue2()).to(getTopicExchange()).with("key.*");
}
}
3.2发送消息
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TopicController {
@Autowired
private RabbitTemplate template;
@GetMapping("/send/topic/{name}")
public String sendFaout(String routingKey,@PathVariable("name")String name) {
template.convertAndSend("top.exc", routingKey, name);
return "send ok";
}
}
3.3消费消息
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues="topic.1")
public class Topic1Listener {
@RabbitHandler
public void handler(String object) {
System.out.println("我是来topic.1的消息");
System.out.println(object);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues="topic.2")
public class Topic2Listener {
@RabbitHandler
public void handler(String object) {
System.out.println("我是来topic.2的消息");
System.out.println(object);
}
}
demo码云地址:https://gitee.com/gnliscream/rabbitmq-demo.git