• Docker下安装RabbitMQ及使用


    Docker安装RabbitMQ

    1.搜索

    docker search rabbitmq

    2.安装

    docker pull rabbitmq

    3.启动

    docker run -d --hostname my-rabbit --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq

    4.安装管理插件(两种写法)

    进入容器内部docker exec -it rabbit/bin/bash

    docker exec -it 1114cd9fcb59/bin/bash

    5.安装插件

    rabbitmq-plugins enable rabbitmq_management

    6.查看插件情况

    rabbitmq-plugins list

    7.访问

    http://169.254.140.100:15672/

    用户名和密码用guest

    8.使用步骤,maven项目

    1. 依赖

    com.rabbitmq

    amqp-client

    4.2.0

    org.slf4j

    slf4j-simple

    1.7.25

    2.官网

    https://www.rabbitmq.com/

    3.编写生产者发送消息

    //创建队列,发送消息

    public static void sendMessage() throws Exception {

    //获取连接

    Connection connection = MessageUitl.getConnection();

    //创建通道

    Channel channel = connection.createChannel();

    //声明创建队列

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    //消息内容

    String message = “Hello World!”;

    channel.basicPublish(“”, QUEUE_NAME, null, message.getBytes());

    System.out.println(“发送消息:” + message);

    //关闭连接和通道

    channel.close();

    connection.close();

    }

    到RabbitMQ上查看

    4.编写消费者

    //消费者消费消息

    public static void receiver() throws Exception {

    //获取连接和通道

    Connection connection = MessageUitl.getConnection();

    Channel channel = connection.createChannel();

    //声明通道

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);

    Consumer consumer = new DefaultConsumer(channel) {

    @Override

    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

    String msg = new String(body, “UTF-8”);

    System.out.println(“接收到的消息:” + msg);

    }

    };

    channel.basicConsume(QUEUE_NAME, consumer);

    }

    测试

    再去运行生产者代码

    发送三次,记得每次加1

    下次整合SpringBoot

  • 相关阅读:
    XAMPP、Apache搭建本地PHP服务器(全网最保姆级)
    Nginx(七) root和alias的区别及详细测试
    算法通关村-----透彻理解动态规划
    NLP项目:维基百科文章爬虫和分类【02】 - 语料库转换管道
    qt使用http get和post
    telegrom bot 发送消息
    《Fundamantals of Software Architecture》 Q&A
    浮点数 C语言 IEEE754
    大理环洱海自动驾驶项目一期投入2.89亿元,拟购73台自动驾驶车
    算法刷题日志10.20
  • 原文地址:https://blog.csdn.net/m0_67402731/article/details/126327353