您的位置:首页 > 编程语言 > Java开发

SpringBoot整合RabbitMq

2020-03-08 13:36 579 查看

SpringBoot整合RabbitMq

RabbitMQ是基于 erlang 语言开发的, 为了使用 rabbitMQ 需要安装 erlang环境。

安装erlang 环境

  1. 下载地址 https://www.erlang.org/downloads
    选择自己适合的操作系统下载
  2. 配置环境变量
  3. 测试是否安装成功
    win+r打开cmd,输入erl,出现如图所示内容表示安装成功

安装RabbitMQ

1.下载地址https://www.rabbitmq.com/download.html
选择自己的盘符默认下一步安装就行
2.配置插件
win+r打开cmd输入以下命令
“C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.5\sbin\rabbitmq-plugins.bat” enable rabbitmq_management

出现如图所示表示插件配置成功。
3.重启 RabbitMQ
以管理员身份运行以下命令:
net stop RabbitMQ && net start RabbitMQ

4.访问管理界面
http://127.0.0.1:15672
输入账号: guest,密码: guest
登陆成功页面如下

至此环境准备好,下面进行springboot和rabbitmq的整合

整合SprinBoot和RabbitMQ

1.创建springboot项目,pom.xml引入下面依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>

2.创建application.properties配置文件

#配置rabbitMQ的支持
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3.创建RabbitmqConfig配置类

/**
*创建fanout模式队列
*/
@Bean
public Queue queueA(){
return new Queue("queueA");
}

@Bean
public Queue queueB(){
return new Queue("queueB");
}

/**
*创建topic模式队列
*/
@Bean
public Queue queueMessage() {
return new Queue("topic.message");
}

@Bean
public Queue queueMessages() {
return new Queue("topic.messages");
}

/**
*创建交换器
*/
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanoutExchange");
}

@Bean
public TopicExchange topicExchange(){
return new TopicExchange("topicExchange");
}

/**
* 交换器和队列绑定 fanout模式
* */
@Bean
public Binding bindingExchangeQueueFanoutA(Queue queueA,FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueA).to(fanoutExchange);
}
@Bean
public Binding bindingExchangeQueueFanoutB(Queue queueB,FanoutExchange fanoutExchange){
return BindingBuilder.bind(queueB).to(fanoutExchange);
}

/**
* 交换器和队列绑定 topic模式
* */
@Bean
public Binding bindingExchangeMessage(Queue queueMessage,TopicExchange topicExchange){
return BindingBuilder.bind(queueMessage).to(topicExchange).with("topic.message");
}

@Bean
Binding bindingExchangeMessages(Queue queueMessages, TopicExchange topicExchange) {
return BindingBuilder.bind(queueMessages).to(topicExchange).with("topic.#");
}

4.创建生产者

@Component
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate;

/**
* fanout模式生产消息
*/
public void fanoutProduction() {
for (int i = 0; i < 10; i++) {
String message = "消息" + i;
rabbitTemplate.convertAndSend("fanoutExchange","", message);
}
}

/**
* topic模式生产消息
*/
public void topicProduction() {
String message = "topic.message";
rabbitTemplate.convertAndSend("topicExchange", "topic.message", message);

String messages = "topic.messages";
rabbitTemplate.convertAndSend("topicExchange", "topic.messages", messages);
}
}

5.创建消费者
fanout的模式这里只实现了单个生产者对多个消费者的情况
两个消费者如下:

@Component
@RabbitListener(queues = "queueA")
public class FanoutConsumeA {

@RabbitHandler
public void  consume(String msg){
System.out.println("queueA:" + msg) ;
}
}
@Component
@RabbitListener(queues = "queueB")
public class FanoutConsumeB {
@RabbitHandler
public void  consume(String msg){
System.out.println("queueB:" + msg);
}
}

topic模式消费者如下

@Component
@RabbitListener(queues = "topic.message")
public class TopicConsumeMessage {

@RabbitHandler
public void consume(String msg){
System.out.println("topic.message:" + msg) ;
}

}
@Component
@RabbitListener(queues = "topic.messages")
public class TopicConsumeMessages {

@RabbitHandler
public void consume(String msg){
System.out.println("topic.messages:" + msg) ;
}
}

6.创建启动类访问

@RestController
@RequestMapping("/rabbit")
public class RabbitController {
@Autowired
private Producer producer;

@GetMapping("/fanout")
public void fanout(){
producer.fanoutProduction();
}

@GetMapping("/topic")
public void topic(){
producer.topicProduction();
}
}

启动项目
访问 http://127.0.0.1:8080/rabbit/fanout

访问 http://127.0.0.1:8080/rabbit/topic

完整代码
https://gitee.com/zhching/springboot-rabbitmq

  • 点赞
  • 收藏
  • 分享
  • 文章举报
zhching 发布了2 篇原创文章 · 获赞 0 · 访问量 112 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: