上篇文章介绍了rabbitmq的原理实现,如果不知道rabbitmq的原理实现的童鞋推荐先看下上篇文章在继续这章的实践。
windows本地安装rabbitmq介绍
源码下载地址 https://gitee.com/v_soul/boot-rabbitmq-produce,https://gitee.com/v_soul/boot-rabbitmq-consumer
springboot集成rabbitmq实战 ------ direct模式
首先创建两个springboot项目
一 生产者项目结构
消费者项目结构
1 pom文件添加spring-boot-starter-amqp支持
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2 配置文件配置rabbitmq
spring.application.name=spirng-boot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
这里的用户名密码默认guest,spring.application.name如果生产者和消费者不是同一个项目不能相同(坑1)
3 配置队列
package com.rabbit.produce.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
@Bean
public Queue queue() {
return new Queue("hello");
}
}
4 创建发送者
package com.rabbit.produce.controller;
import com.rabbit.produce.entrty.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.util.Date;
@Controller
public class HelloSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String context = "