spring 使用 rabbit mq

rabbit mq 安装

  • brew 安装
// 安装 安装目录为: /usr/local/Cellar/rabbitmq
brew install rabbitmq

// 启动
brew services start rabbitmq

// 启动插件
/usr/local/Cellar/rabbitmq/3.7.15/sbin/rabbitmq-plugins enable rabbitmq_management

// 开启rabbit mq
rabbitmq-server
  • rabbit mq后台
    rabbit mq后台 用户名:guest 密码:guest
image.png

rabbit mq 账户配置

// 添加账号
/usr/local/Cellar/rabbitmq/3.7.15/sbin/rabbitmqctl add_user admin admin

// 添加访问权限
/usr/local/Cellar/rabbitmq/3.7.15/sbin/rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"

// 设置超级权限
/usr/local/Cellar/rabbitmq/3.7.15/sbin/rabbitmqctl set_user_tags admin administrator

spring使用rabbit mq

  • pom依赖
        
            com.rabbitmq
            amqp-client
            5.7.3
        
  • connection获取
package com.shisir;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RabbitMqUtils {
    static public Connection getRabbitMqConnection(String host, Integer port, String userName, String password)
            throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(password);

        return connectionFactory.newConnection();
    }
}
  • 生产者
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.shisir.RabbitMqUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

public class RabbitMqProducerDemo {
    private final static String QUENE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 创建连接
        Connection connection = RabbitMqUtils.getRabbitMqConnection("127.0.0.1", 5672, "admin", "admin");

        // 创建消息通道
        Channel channel = connection.createChannel();

        // 生成一个消息队列
        channel.queueDeclare(QUENE_NAME, true, false, false, null);

        System.out.println("[*] Waiting for message. To exist press CTRL+C");

        for (int i = 0; i < 20; i++) {
            String message = "Hello Rabbit mq : " + i;

            channel.basicPublish("", QUENE_NAME, null, message.getBytes());

            System.out.println("send '" + message + "'");
        }

        channel.close();

        connection.close();
    }
}
  • 消费者
import com.rabbitmq.client.*;
import com.shisir.RabbitMqUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

public class RabbitMqConsumerDemo {
    private final static String QUENE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = RabbitMqUtils.getRabbitMqConnection("127.0.0.1", 5672, "admin", "admin");

        Channel channel = connection.createChannel();

        channel.queueDeclare(QUENE_NAME, true, false, false, null);

        AtomicInteger count = new AtomicInteger(0);
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body);

                try {
                    System.out.println("receive '" + message + "'");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("Done");

                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            }
        };

        channel.basicConsume(QUENE_NAME, false, consumer);

        Thread.sleep(1000 * 60);
    }
}

总结

参考

https://www.cnblogs.com/yihuihui/p/9095130.html

你可能感兴趣的:(spring 使用 rabbit mq)