涉及到的依赖
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
生产者product模块
1.常量类
@Configuration
public class RabbitmqConstant {
public static final String EXCHANGE_NAME = "young.exchange.info";
public static final String QUEUE_INFO_ADD = "queue.info.add";
public static final String QUEUE_INFO_DELETE = "queue.info.delete";
public static final String QUEUE_INFO_UPDATE = "queue.info.update";
public static final String QUEUE_INFO_SELECT = "queue.info.select";
public static final String KEY_INFO_ADD = "key.info.add";
public static final String KEY_INFO_DELETE = "key.info.delete";
public static final String KEY_INFO_UPDATE = "key.info.update";
public static final String KEY_INFO_SELECT = "key.info.select";
}
2.初始化rabbitmqTemplate,交换机,队列,路由key
@Configuration
@Slf4j
public class RabbitmqConfig {
@Resource
private AmqpAdmin amqpAdmin;
/**
* @Description: 初始化exchange, queue和routingKey
* @Author: young
* @Date: 2022/8/25 14:53
*/
@Bean
public void initBinding() {
//用直接交换机(和topic交换机一样,需要queue和routingKey;Fanout交换机不需要routingKey)
amqpAdmin.declareExchange(new DirectExchange(RabbitmqConstant.EXCHANGE_NAME, true, false));
//add
amqpAdmin.declareQueue(new Queue(RabbitmqConstant.QUEUE_INFO_ADD, true));
amqpAdmin.declareBinding(new Binding(RabbitmqConstant.QUEUE_INFO_ADD, Binding.DestinationType.QUEUE, RabbitmqConstant.EXCHANGE_NAME, RabbitmqConstant.KEY_INFO_ADD, null));
//delete
amqpAdmin.declareQueue(new Queue(RabbitmqConstant.QUEUE_INFO_DELETE, true));
amqpAdmin.declareBinding(new Binding(RabbitmqConstant.QUEUE_INFO_DELETE, Binding.DestinationType.QUEUE, RabbitmqConstant.EXCHANGE_NAME, RabbitmqConstant.KEY_INFO_DELETE, null));
//update
amqpAdmin.declareQueue(new Queue(RabbitmqConstant.QUEUE_INFO_UPDATE, true));
amqpAdmin.declareBinding(new Binding(RabbitmqConstant.QUEUE_INFO_UPDATE, Binding.DestinationType.QUEUE, RabbitmqConstant.EXCHANGE_NAME, RabbitmqConstant.KEY_INFO_UPDATE, null));
//select
amqpAdmin.declareQueue(new Queue(RabbitmqConstant.QUEUE_INFO_SELECT, true));
amqpAdmin.declareBinding(new Binding(RabbitmqConstant.QUEUE_INFO_SELECT, Binding.DestinationType.QUEUE, RabbitmqConstant.EXCHANGE_NAME, RabbitmqConstant.KEY_INFO_SELECT, null));
}
@Bean
public RabbitTemplate amqpTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//支持send消息的时候传输javaBean
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
}
3.定时发送消息
@EnableScheduling
@EnableAsync
@Component
@Slf4j
public class SendMessage {
@Resource
private RabbitTemplate amqpTemplate;
@Async
@Scheduled(cron = "0/10 * * * * *")
public void sendAdd() {
String message = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
log.info("routingKey={},message={}", RabbitmqConstant.KEY_INFO_ADD, message);
amqpTemplate.convertAndSend(RabbitmqConstant.EXCHANGE_NAME, RabbitmqConstant.KEY_INFO_ADD, message);
}
@Async
@Scheduled(cron = "0/10 * * * * *")
public void sendUpdate() {
String message = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
log.info("routingKey={},message={}", RabbitmqConstant.KEY_INFO_UPDATE, message);
amqpTemplate.convertAndSend(RabbitmqConstant.EXCHANGE_NAME, RabbitmqConstant.KEY_INFO_UPDATE, message);
}
}
4.application.yml
spring:
application:
name: rabbitmq-product
rabbitmq:
host: yourHost
port: 5672
virtual-host: /
username: guest
password: guest
server:
port: 8082
5.客户端查看
消费者consumer模块
1.监听队列
@Component
@Slf4j
public class SpringRabbitListener {
@Resource
private Environment environment;
@Value("${spring.application.name}")
private String serverName;
@RabbitListener(queues = RabbitmqConstant.QUEUE_INFO_ADD)
public void listenerTopicAdd(String message) {
log.info("{} 监听到 add 消息:" + message, environment.getProperty("spring.application.name"));
}
@RabbitListener(queues = RabbitmqConstant.QUEUE_INFO_UPDATE)
public void listenerTopicUpdate(String message) {
log.info("{} 监听到 update 消息:" + message, serverName);
}
@RabbitListener(queues = RabbitmqConstant.QUEUE_INFO_DELETE)
public void listenerTopicDelete(String message) {
log.info("{} 监听到 delete 消息:" + message, serverName);
}
@RabbitListener(queues = RabbitmqConstant.QUEUE_INFO_SELECT)
public void listenerTopicSelect(String message) {
log.info("{} 监听到 select 消息:" + message, serverName);
}
}
2.application.yml
spring:
application:
name: rabbitmq-consumer1
rabbitmq:
host: yourHost
port: 5672
virtual-host: /
username: guest
password: guest
server:
port: 8083
3.消费日志