RabbtiMQ的安装与在Springboot中的使用!!!

一、安装Erlang与Rabbitmq

安装教程本教程是在centos8下试验的,其实linux系统的都差不多RabbitMQ官方:Messaging that just works --- RabbitMQRabbitMQ是开源AMQP实现,服务器端用Erlang语言编写,Python、Ruby、 NET、Java、JMS、c、PHP、action screcrive AMQP (高级消息队列协议)和高级消息队列协议是APP应用层协议的开放标准,是为面向消息的中间件而设计的。消息中间件主要用于组件之间的解耦,其中消息的发送方不需要知道消息使用者的存在,反之_rabbitmq安装https://blog.csdn.net/weixin_44545251/article/details/128216395

二、在springboot中使用rabbitmq

1.导入依赖

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.编写配置文件

复制代码
spring:
 rabbitmq:
   host: localhost
   port: 5672
   username: guest
   password: guest

3.配置一个队列

java 复制代码
package com.example.Rabbtimq.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueueAndExchangeConfig {
    @Bean("myFirstQueue")
    public Queue getFirstQueue(){
        return new Queue("my-first-queue");
    }
}

4.编写一个生产者类

java 复制代码
package com.example.Rabbtimq.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("producer")
public class RabbitMqController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("producerSendFirstQueue")
    public String sendMsg(String msg){
        rabbitTemplate.convertAndSend("my-first-queue",msg);
        return msg;
    }

}

5.编写一个消费者监听队列中的消息

java 复制代码
package com.example.Rabbtimq.controller;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Component
public class ConsumerHandler {
    @RabbitListener(queues = "my-first-queue")
    public void getFirstQueue(String msg){
        System.out.println("消费者1:"+msg);
    }
}

6.使用接口工具调生产接口

7.查看图形化界面

相关推荐
程序员cxuan2 分钟前
Codex 一直 Reconnecting?我最后发现,常见就两个坑
人工智能·后端·程序员
qq_458148206 分钟前
科大讯飞实时语音识别(rtasr)真实项目踩坑经验总结与手把手教学真实可运行Demo
java·开发语言·websocket·语音识别
三品吉他手会点灯6 分钟前
C语言学习笔记 - 46.运算符和表达式 - 运算符4 - 对初学运算符的一些建议
c语言·开发语言·笔记·学习
创业之路&下一个五年9 分钟前
mvvm中v和vm关系,vm中v和m的关系?
java·开发语言·javascript
SilentSamsara10 分钟前
缓存策略实战:Redis + Python 多级缓存设计与失效策略
开发语言·redis·python·缓存·性能优化
zlinear数据采集卡14 分钟前
输出短路保护电路深度解析:从电源的“最后一道防线”到ZLinear采集卡的硬核守护实战
开发语言·嵌入式硬件·持续集成
程序员海军16 分钟前
沪漂五周年了:我越来越迷茫了
前端·人工智能·后端
剑锋所指,所向披靡!17 分钟前
C++多线程实现
开发语言·c++·chrome
十五年专注C++开发24 分钟前
Qt之QScopedPointer、QScopeGuard、QScopedValueRollback使用及源码解读
开发语言·c++·qt·qscopedpointer·qscopeguard
fox_lht27 分钟前
13.3.测试的组织方式
开发语言·后端·rust