ActiveMQ
下载activemq 直接cmd进入bin目录 activemq start启动 默认端口 8161
进入管理页面之后
queues ->一对一的发送消息

这种是点对点模式的,服务端发送消息到队列里面,客户端接收队列里面的消息

这种模式是一个服务器对应多个客户端的模式,这种模式下默认的是多个客户端平均分配队列里面的消息,(一次性平均分配) 分配完成之后每个队列再进行消费
第二种模式是谁消耗得快谁就多处理一些,谁慢谁就少处理一些, 这个就需要一个ACK确认机制,消费一条确认一条,确认一条之后再消费一条
这个只需要把Ack打开
广播模型

- 有一个交换机
- 可以有多个消费者
- 每个消费者有自己的队列
- 每个队列要绑定自己的交换机
- 生产者发送的消息只能够发送到交换机,交换机来决定发送给那个队列,生产者无法决定
- 交换机把消息发送到绑定过的队列
- 队列的消费者都能够拿到消息,实现一个消息被多个消费者消费
路由模型
routing之订阅模型-> Direct直连



说了这么多主要还是要讲Springboot操作rabbitmq
配置文件
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 121.36.36.156
port: 5672
username: ems
password: 123
virtual-host: /ems
server:
port: 8080
依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
java代码
controller
@RestController
@Api(tags = "hello world demo")
public class Controller {
@Autowired
RabbitTemplate rabbitTemplate;
/**
* work模式
*
* @param message
* @return
*/
@ApiOperation(tags = "发送消息 work方式 -> 多个队列接收", value = "hello world")
@GetMapping("sendWork/{message}")
public String testWork(@PathVariable("message") String message) {
for (int i = 0; i < 100; i++) {
rabbitTemplate.convertAndSend("work", message);
}
return "success";
}
/**
* Fanout模式 广播
*
* @param message
* @return
*/
@ApiOperation(tags = "发送消息 Fanout方式 -> 多个队列接收", value = "hello world")
@GetMapping("sendFanout/{message}")
public String testFanout(@PathVariable("message") String message) {
for (int i = 0; i < 100; i++) {
rabbitTemplate.convertAndSend("logs", "", message);
}
return "success";
}
/**
* router 路由模式
*/
@ApiOperation(tags = "发送消息 Router方式 info 方式发送", value = "hello world")
@GetMapping("sendRouter/{message}")
public String testRouter(@PathVariable("message") String message) {
rabbitTemplate.convertAndSend("directs", "info", message);
return "success";
}
@ApiOperation(tags = "发送消息 Router方式 error 方式发送", value = "hello world")
@GetMapping("sendRouter2/{message}")
public String testRouter2(@PathVariable("message") String message) {
rabbitTemplate.convertAndSend("directs", "error", message);
return "success";
}
/**
* topic 动态路由 订阅模式
*/
@ApiOperation(tags = "发送消息 Topic user.save", value = "hello world")
@GetMapping("sendTopic/{message}")
public String testTopic(@PathVariable("message") String message) {
rabbitTemplate.convertAndSend("topics", "user.save", message);
return "success";
}
@ApiOperation(tags = "发送消息 Topic product.save", value = "hello world")
@GetMapping("sendTopic2/{message}")
public String testTopic2(@PathVariable("message") String message) {
rabbitTemplate.convertAndSend("topics", "product.save", message);
return "success";
}
}
接收端 consumer
@Component
public class Consumers {
/**
* Work 模式
* 在spring中默认的是公平消费
* 如果要做能者多劳的话要去做额外的配置
*
* @param message
*/
//一个消费者
@RabbitListener(queuesToDeclare = @Queue(name = "work"))
public void receiveWork(String message) {
System.out.println("收到了消息1-> " + message);
}
//一个消费者
@RabbitListener(queuesToDeclare = @Queue(name = "work"))
public void receiveWork2(String message) {
System.out.println("收到了消息2-> " + message);
}
/**
* Fanout 模式
*/
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //不指定名字 表示生成临时队列
exchange = @Exchange(value = "logs", type = "fanout")//绑定交换机
)
})
public void receiveFanout1(String message) {
System.out.println("message1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //不指定名字 表示生成临时队列
exchange = @Exchange(value = "logs", type = "fanout")//绑定交换机
)
})
public void receiveFanout2(String message) {
System.out.println("message2 = " + message);
}
/**
* Router 模式
*/
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //不指定名字 表示生成临时队列
exchange = @Exchange(value = "directs", type = "direct"),//绑定交换机名称和类型
key = {"info", "error", "warning"}
)
})
public void receiveRouter1(String message) {
System.out.println("message1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //不指定名字 表示生成临时队列
exchange = @Exchange(value = "directs", type = "direct"),//绑定交换机名称和类型
key = {"error"}
)
})
public void receiveRouter2(String message) {
System.out.println("message2 = " + message);
}
/**
* Topic 模式
*/
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //不指定名字 表示生成临时队列
exchange = @Exchange(value = "topics", type = "topic"),//绑定交换机名称和类型
key = {"user.save","user.*"}
)
})
public void receiveTopic1(String message) {
System.out.println("message1 = " + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //不指定名字 表示生成临时队列
exchange = @Exchange(value = "topics", type = "topic"),//绑定交换机名称和类型
key = {"order.#","product.#","user.*"}
)
})
public void receiveTopic2(String message) {
System.out.println("message2 = " + message);
}
}
MQ的应用场景




121

被折叠的 条评论
为什么被折叠?



