具体rabbitMQ的使用以及下载安装,详见上篇帖子
这里想把生产者和消费者各自建立一个服务
步骤
①pom中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
②配置文件新增链接rabbitmq的配置
生产者:
server:
port: 6001
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
application:
name: producer
消费者:
server:
port: 6000
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
application:
name: consumer
③配置config文件
生产者和消费者各自配置config文件(这两个文件内容是相同的),或者写一个公共common服务,把配置文件放进去:
PS:不能写公共common.否则consumer启动会报错,缺少那个监听队列的注入。别问我为啥这么
清楚,问就是踩过。
配置了四种类型的交换机,并配置了队列
package com.hyj.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqConfig {
//配置direct交换机
@Bean
public DirectExchange directExchange() {
return new DirectExchange("directExchange");
}
//配置direct队列
@Bean
public Queue directQueue() {
return new Queue("directQueue");
}
//将direct队列绑定到交换机上
@Bean
public Binding directBinding(Queue directQueue, DirectExchange directExchange) {
// 参数 1 为需要绑定的队列
// 参数 2 为需要绑定的交换机
// 参数 3绑定时的RoutingKey
return BindingBuilder.bind(directQueue).to(directExchange).with("directRouting");
}
//配置Fanout交换机
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
//配置Topic交换机
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topicExchange");
}
}
绑定多个队列用法
package com.gwmfc.consumer.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqConfig {
@Autowired
private AppConfig appConfig;
@Bean
public DirectExchange directExchange() {
return new DirectExchange(appConfig.exchangeName, true, false);
}
@Bean
public Queue directQueue01() {
return new Queue(appConfig.callbackQueueName);
}
@Bean
public Queue directQueue02() {
return new Queue(appConfig.thumbQueueName);
}
@Bean
public Queue directQueue03() {
return new Queue(appConfig.fastDfsQueueName);
}
//callback队列绑定到交换机,001
@Bean
public Binding directBinding01() {
return BindingBuilder.bind(directQueue01()).to(directExchange()).with(appConfig.callbackKey);
}
//thumbPic队列绑定到交换机,002
@Bean
public Binding directBinding02() {
return BindingBuilder.bind(directQueue02()).to(directExchange()).with(appConfig.thumbKey);
}
//fastDfs队列绑定到交换机,003
@Bean
public Binding directBinding03() {
return BindingBuilder.bind(directQueue03()).to(directExchange()).with(appConfig.fastDfsKey);
}
}
④调用
生产者:
SpringBoot提供了AmqpTemplate来操作RabbitMQ
@RestController
@Slf4j
public class TestController {
@Autowired
private AmqpTemplate amqpTemplate;
@GetMapping(value = "/setMsg")
public void SetMsg(@RequestParam("msg") String msg) {
amqpTemplate.convertAndSend("directExchange", "directRouting", msg);
log.info("生产消息:{}", msg);
}
}
消费者:
@Service
@Slf4j
public class MsgService {
@RabbitListener(queues = "directQueue")
public void GetMsg(String msg){
log.info("消费消息:{}",msg);
}
}
⑤结果
完成。
@RabbitListener注解:
当RabbitListener注解在方法上时,对应的方式就是Rabbit消息的监听器。
当RabbitListener注解在类上时,和RabbitHandle注解配合使用,可以实现不同类型的消息的分
发,类中被RabbitHandle注解的方法就是Rabbit消息的监听器。