55.RabbitMQ核心编程模型以及消息应用场景详解

安装rabbitmq

#centos9安装rabbitmq
rpm -ivh erlang-25.3.2-1.el9.x86_64.rpm 
rpm -ivh rabbitmq-server-3.11.24-1.el8.noarch.rpm
yum install socat -y

#启动服务
service rabbitmq-server start

#启动网页管理界面
rabbitmq-plugins enable rabbitmq_management

#添加用户,设置角色
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator

RabbitMQ基础编程模型

maven依赖

    com.rabbitmq
    amqp-client
    5.9.0

首先创建连接,获取Channel

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.115.152");
factory.setPort(5672);
factory.setUsername("admin");
factory.setPassword("admin");
factory.setVirtualHost("/memory");
try {
    connection = factory.newConnection();
    channel = connection.createChannel();
} catch (IOException | TimeoutException e) {
    throw new RuntimeException(e);
}
声明Exchange-可选

String exchangeName = "ex.shop.orders";
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true, true, null);
声明queue
Map<String,Object> params = new HashMap<>();
params.put("x-queue-type","quorum");
String queueName = "queue.shop.stock";
channel.queueDeclare(queueName, true, false, false, params);
声明Exchange与Queue的绑定关系-可选
channel.queueBind(queueName, exchangeName, "");
Producer根据应用场景发送消息到queue
channel.basicPublish("", queueName, MessageProperties.TEXT_PLAIN, "message".getBytes()) ;
被动消费模式
channel.basicConsume(String queue, boolean autoAck, Consumer callback);
主动消费模式
GetResponse response = channel.basicGet(QUEUE_NAME, boolean autoAck);
完成以后关闭连接,释放资源
channel.close(); 
conection.clouse();
//Consumer完整案例
public static void main(String[] args) {
    Connection connection = null;
    Channel channel = null;
    //1.首先创建连接,获取Channel
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("192.168.115.152");
    factory.setPort(5672);
    factory.setUsername("admin");
    factory.setPassword("admin");
    factory.setVirtualHost("/memory");
    try {
        connection = factory.newConnection();
        channel = connection.createChannel();
    } catch (IOException | TimeoutException e) {
        throw new RuntimeException(e);
    }
    //声明queue
    Map<String,Object> params = new HashMap<>();
    params.put("x-queue-type","quorum");
    String queueName = "queue.shop.stock";
    //Consumer的队列定义需要与Publisher一致,或者先启动的程序定义一次。
    try {
        channel.queueDeclare(queueName, false, false, false, null);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    //定义回调函数处理业务
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
        //System.out.println("调用业务代码实验" + R.ok(null));
        System.out.println(" [x] Received '" + message + "'");
    };
    try {
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {});
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

RabbitMQ常用的消息场景

  • 队列直连

    • 最直接的方式,P端发送一个消息到一个指定的queue,中间不需要任何exchange规则。C端按queue方式进行消费。
  • workers

    • Producer消息发送给queue,多个Consumer同时往队列上消费消息。
  • fanout

    • producer只负责发送消息,至于消息进入哪个queue,由exchange来分配。
  • direct

    • 增加一个路由配置,指定exchange基于 routingKey 的消息分发到不同的queue上。
  • topic

    • 对routingKey进行了模糊匹配

SpringBoot集成RabbitMQ

引入依赖

	org.springframework.boot
	spring-boot-starter-amqp

配置关键参数

spring.rabbitmq.*

声明Exchange,Queue和Binding
使用RabbitmqTemplate对象发送消
使用@RabbitListener注解声明消费者

你可能感兴趣的:(726打卡,rabbitmq,ruby,分布式)