Rabbitmq入门(一)

rabbitmq

目录

rabbitmq

mq的优势

mq的劣势

rabbitmq基于AMQP协议

官网给的六种模式实现

1、用到的工具类

2、第一种:"helloworld"实现


​​​​​​​

 

官网:Messaging that just works — RabbitMQ

mq的优势

  1. 应用解耦

  2. 异步提速 

  3. 削峰填谷

mq的劣势

  1. 降低系统的可用性

    系统引入的外部依赖越多,系统稳定性越差,一旦mq宕机,整个系统就会瘫痪

  2. 系统的复杂度提高

    保证消息的同步消息、避免重复消费、消息丢失等一系列问题

  3. 一致性问题

    如何保证多个服务写操作一致

rabbitmq基于AMQP协议

AMQP,即Advanced Message Queuing Protocol(高级消息队列协议),是一个网络协议,是应用层协议的一个开放标准,为面向消息的中间设计。

官网给的六种模式实现

1、用到的工具类

RabbitConstant.class和RabbitUtils.class

public class RabbitConstant {
    public static final String QUEUE_HELLOWORD = "helloworld";
    public static final String QUEUE_SMS = "queuesms";
    public static final String EXCHANGE_WEATHER = "weather";
    public static final String EXCHANGE_WEATHER_ROUTING = "weather_routing";
    public static final String EXCHANGE_WEATHER_TOPIC = "weather_topic";
    public static final String QUEUE_BAIDU = "baidu";
    public static final String QUEUE_SINA = "sina";
}
​
​
package com.liheng.rabbitmq.util;
​
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
​
/**
 * @Author liheng
 * @Date 2021/10/20 下午2:20
 * @Version 1.0
 */
public class RabbitUtils {
    private static ConnectionFactory connectionFactory = new ConnectionFactory();
​
    static {
        connectionFactory.setHost("xxx.xxx.xx.xx");//主机地址
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("liheng123");
        connectionFactory.setPassword("liheng123");
        connectionFactory.setVirtualHost("liheng123Virtual");
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            return connection;
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}

2、第一种:"helloworld"实现

Consumer.class 消费者代码

package com.liheng.rabbitmq.helloword;
​
import com.liheng.rabbitmq.util.RabbitConstant;
import com.liheng.rabbitmq.util.RabbitUtils;
import com.rabbitmq.client.*;
​
import java.io.IOException;
​
/**
 * @Author liheng
 * @Date 2021/10/20 上午11:53
 * @Version 1.0
 */
public class Consumer {
    public static void main(String[] args) {
        //获取tcp长链接
        try {
            Connection connection = RabbitUtils.getConnection();
            //创建通信 通道
            Channel channel = connection.createChannel();
          /**
             * @param queue the name of the queue
            * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
            * @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
            * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
            * @param arguments other properties (construction arguments) for the queue
            
            参数1:队列名称
            参数2:是否持久化 false不持久化 mq停掉就会丢失数据
            参数3:是否私有化,false表示所有消费这都可以消费,true表示只有第一次拥有它的消费这才可以使用
            参数4:是否自动删除,false表示连接停掉之后不自动删除这个队列
            参数5:其他额外参数
            */
            channel.queueDeclare(RabbitConstant.QUEUE_HELLOWORD,false,false,false,null);
​
            //从mq中去消费
            channel.basicConsume(RabbitConstant.QUEUE_HELLOWORD,false,new Reciver(channel));
​
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class Reciver extends DefaultConsumer{
​
    private Channel channel;
    public Reciver(Channel channel) {
        super(channel);
        this.channel = channel;
    }
​
    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        String message = new String(body);
        System.out.println("接受到的消息:"+ message);
        System.out.println("消息的tagId:"+ envelope.getDeliveryTag());
        //false 代表只前后当前的消息,true代表签收所有未签收的消息
        channel.basicAck(envelope.getDeliveryTag(),false);
    }
}

Producer.class 消费者

package com.liheng.rabbitmq.helloword;
​
import com.liheng.rabbitmq.util.RabbitConstant;
import com.liheng.rabbitmq.util.RabbitUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
​
import java.io.IOException;
import java.util.concurrent.TimeoutException;
​
/**
 * @Author liheng
 * @Date 2021/10/20 上午11:37
 * @Version 1.0
 */
public class Producer {
    public static void main(String[] args) {
        //获取tcp长链接
        try {
            Connection connection = RabbitUtils.getConnection();
            //创建通信 通道
            Channel channel = connection.createChannel();
            //RabbitConstant.QUEUE_HELLOWORD 如果没有当前队列 会自动生成队列
            /**
             * @param queue the name of the queue
            * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
            * @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
            * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
            * @param arguments other properties (construction arguments) for the queue
            
            参数1:队列名称
            参数2:是否持久化 false不持久化 mq停掉就会丢失数据
            参数3:是否私有化,false表示所有消费这都可以消费,true表示只有第一次拥有它的消费这才可以使用
            参数4:是否自动删除,false表示连接停掉之后不自动删除这个队列
            参数5:其他额外参数
            */
            channel.queueDeclare(RabbitConstant.QUEUE_HELLOWORD,false,false,false,null);
​
            String message = "李恒666";
          /**
           * @param exchange the exchange to publish the message to
           * @param routingKey the routing key
           * @param mandatory true if the 'mandatory' flag is to be set
           * @param props other properties for the message - routing headers etc
           * @param body the message body
           参数1:exchange名称 在这个简单模式下不需要指定
           参数2:队列名称
           参数3:其他的一些属性
           参数4:消息的字节数组
          */
            channel.basicPublish("",RabbitConstant.QUEUE_HELLOWORD,null,message.getBytes());
            channel.close();
            connection.close();
            System.out.println("数据发送成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半路笙歌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值