rabbitmq entry program

What is rabbitmq

rabbitmq is a kind of message queue, which is used to transfer information between services and decouple services. It is an open source implementation of AMQP (Advanced Message Queue) developed by erlang.

Principle structure

Insert picture description here

Contains elements

Producer, consumer, news

Producer: the creator of the message, sent to rabbitmq;
consumer: connect to rabbitmq, subscribe to the queue, consume the message, continuous subscription (basicConsumer) and single subscription (basicGet).
Message: contains payload and tag, payload refers to For the data to be transmitted, the tag describes the payload, and rabbitmq uses it to determine who gets the message. Consumers can only get the payload and do not know who the producer is.

Channel

Channel, concept: The channel is the communication channel between the producer and the consumer and the rabbit. The producer publishes or the consumer subscribes to a queue communicates through the channel. A channel is a virtual connection established on a TCP connection. What does it mean? That is to say, rabbitmq establishes hundreds of channels on a TCP to achieve multiple thread processing. This TCP is shared by multiple threads, and each thread corresponds to a channel. The channel has a unique ID in the rabbit, which ensures the privacy of the channel. , Corresponding to the use of the only thread.

Switch, queue, binding, routing key

The queue is bound to the exchange through a routing key (routing key, a certain definite rule), the producer publishes the message to the exchange, and the exchange routes the message to a specific queue according to the bound routing key, and then subscribed to this queue Consumers receive.

Starter program

rely

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.0.0</version>
        </dependency>

Producer

public class Pro {
    //交换器名称
    public final static String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        Connection connection = connectionFactory.newConnection("127.0.0.1");

        //信道
        Channel channel = connection.createChannel();
        //创建交换器,BuiltinExchangeType.DIRECT为交换器类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);


        /*日志消息级别,作为路由键使用*/
        String[] serverities = {"error", "info", "warning"};
        for (int i = 0; i < 3; i++) {
            //路由键
            String severity = serverities[i % 3];
            String msg = "Hellol,RabbitMq" + (i + 1);
            //发送消息
            channel.basicPublish(EXCHANGE_NAME, severity, null, msg.getBytes());

            System.out.println("发送的消息是 = [" + msg + "]");
        }


        channel.close();
        connection.close();

    }

}

consumer

public class NolCum {

    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        Connection connection = connectionFactory.newConnection("127.0.0.1");

        //信道
        Channel channel = connection.createChannel();
        //交换器
        channel.exchangeDeclare(Pro.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

        //创建队列
        String newQueue = "newQueue";
        channel.queueDeclare(newQueue, false, false, false, null);


        //根据路由键将队列和交换器进行绑定
        String routekey = "info";/*表示只关注error级别的日志消息*/
        channel.queueBind(newQueue, Pro.EXCHANGE_NAME, routekey);


        //开始接受消息
        final 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, "UTF-8");
                System.out.println("Received[" + envelope.getRoutingKey()
                        + "]" + message);
            }
        };

        //消费者开始消费队列上的消息
        channel.basicConsume(newQueue, true, consumer);

    }
}

Guess you like

Origin blog.csdn.net/qq_28822933/article/details/85238294