• RabbitMQ


    消息可靠性投递

    confirm 确认模式

    return 返回模式

    rabbitmq整个消息投递的路径为

    producer--->rabbitmq broker--->exchange--->queue--->consumer

    消息从 producer 到 exchange则会返一个 confirmCallback·

    消息从 exchange 到 queue投递失败则会返回个 returnCallback。

    ➢    设置ConnectionFactory的publisher-confirms="true" 开启确认模式。
    ➢    使用rabbitTemplate.setConfirmCallback设置回调函数。当消息发送到exchange后回调confirm方法。在方法中判断ack,如果为true,则发送成功,如果为false,则发送失败,需要处理。
    ➢    设置ConnectionFactory的publisher-returns="true" 开启退回模式。
    ➢    使用rabbitTemplate.setReturnCallback设置退回函数,当消息从exchange路由到queue失败后,如果设置了rabbitTemplate.setMandatory(true)参数,则会将消息退回给producer。并执行回调函数returnedMessage。
    ➢    在RabbitMQ中也提供了事务机制,但是性能较差

    Consumer Ack

    ack指Acknowledge,确认。 表示消费端收到消息后的确认方式。

    有三种确认方式:

    •    自动确认:acknowledge="none"
    •    手动确认:acknowledge="manual"
    •    根据异常情况确认:acknowledge="auto"

    其中自动确认是指,当消息一旦被Consumer接收到,则自动确认收到,并将相应 message 从 RabbitMQ 的消息缓存中移除。但是在实际业务处理中,很可能消息接收到,业务处理出现异常,那么该消息就会丢失。如果设置了手动确认方式,则需要在业务处理成功后,调用channel.basicAck(),手动签收,如果出现异常,则调用channel.basicNack()方法,让其自动重新发送消息。

            如果消费端没有出现异常,则调用channel.basicAck(deliveryTag,false);确认签收消息

            如果出现异常,则在catch中调用 basicNack或 basicReject,拒绝消息,让MQ重新发送

    消息可靠性总结

    1、持久化

            •    exchange要持久化
            •    queue要持久化
            •    message要持久化

    2、生产方确认Confirm
    3、消费方确认Ack
    4.、Broker高可用

    消费端限流

    中配置 prefetch属性设置消费端一次拉取多少消息

    消费端的确认模式一定为手动确认。acknowledge="manual"

     

    TTL(Time To Live)

    1、设置队列过期时间使用参数:x- message-ttl,单位毫秒,会对整个队列消息统一过期

    2、设置消息过期时使用参数: expiration。单位毫秒,当该消息在队列头部时(消费时),才会单独判断这一消息是否过期

    3、如果两者都进行了设置,以时间短的为准。

    死信队列

    死信交换机和死信队列和普通的没有区别

    需要设置给队列参数:

            x-dead-letter- exchange

            x-dead-letter- routing-key

    当消息成为死信后,如果该队列绑定了死信交换机,则消息会被死信交换机重新路由到死信队

    消息成为死信的三种情况

            1、队列消息长度到达限制

            2、消费者拒接消费消息,并且不重回队列

            3、原队列存在消息过期设置,消息到达超时时间末被消费

    延迟队列

    RabbitMQ中没用提供延迟队列功能

    但是可以使用  TTL+死信队列  来实现该功能

    应用场景:

            下单后,30分钟未支付,取消订单,回滚库存

            新用户注册成功7天后,发送短信问候

    消息可靠性保障--消息补偿

      消息幂等性保障--乐观锁机制

    幂等性指一次和多次请求某一个资源,对于资源本身应该具有同样的结果。

    也就是说,任意多次执行对资源本身所产生的影响均与一次执行的影响相同。

     

    spring配置rabbitMQ

    rabbitmq.properties

    1. rabbitmq.host=192.168.xxx.xxx
    2. rabbitmq.port=5672
    3. rabbitmq.username=guest
    4. rabbitmq.password=guest
    5. rabbitmq.virtual-host=/

    produce.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context
    8. https://www.springframework.org/schema/context/spring-context.xsd
    9. http://www.springframework.org/schema/rabbit
    10. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    11. <context:property-placeholder location="classpath:rabbitmq.properties"/>
    12. <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
    13. port="${rabbitmq.port}"
    14. username="${rabbitmq.username}"
    15. password="${rabbitmq.password}"
    16. virtual-host="${rabbitmq.virtual-host}"
    17. publisher-confirms="true"
    18. publisher-returns="true"
    19. />
    20. <rabbit:admin connection-factory="connectionFactory"/>
    21. <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
    22. <rabbit:queue id="test_queue_confirm" name="test_queue_confirm">rabbit:queue>
    23. <rabbit:direct-exchange name="test_exchange_confirm">
    24. <rabbit:bindings>
    25. <rabbit:binding queue="test_queue_confirm" key="confirm">rabbit:binding>
    26. rabbit:bindings>
    27. rabbit:direct-exchange>
    28. <rabbit:queue name="test_queue_ttl" id="test_queue_ttl">
    29. <rabbit:queue-arguments>
    30. <entry key="x-message-ttl" value="100000" value-type="java.lang.Integer"/>
    31. rabbit:queue-arguments>
    32. rabbit:queue>
    33. <rabbit:topic-exchange name="test_exchange_ttl" >
    34. <rabbit:bindings>
    35. <rabbit:binding pattern="ttl.#" queue="test_queue_ttl">rabbit:binding>
    36. rabbit:bindings>
    37. rabbit:topic-exchange>
    38. <rabbit:queue name="test_queue_dlx" id="test_queue_dlx">
    39. <rabbit:queue-arguments>
    40. <entry key="x-dead-letter-exchange" value="exchange_dlx" />
    41. <entry key="x-dead-letter-routing-key" value="dlx.hehe" />
    42. <entry key="x-message-ttl" value="10000" value-type="java.lang.Integer" />
    43. <entry key="x-max-length" value="10" value-type="java.lang.Integer" />
    44. rabbit:queue-arguments>
    45. rabbit:queue>
    46. <rabbit:topic-exchange name="test_exchange_dlx">
    47. <rabbit:bindings>
    48. <rabbit:binding pattern="test.dlx.#" queue="test_queue_dlx">rabbit:binding>
    49. rabbit:bindings>
    50. rabbit:topic-exchange>
    51. <rabbit:queue name="queue_dlx" id="queue_dlx">rabbit:queue>
    52. <rabbit:topic-exchange name="exchange_dlx">
    53. <rabbit:bindings>
    54. <rabbit:binding pattern="dlx.#" queue="queue_dlx">rabbit:binding>
    55. rabbit:bindings>
    56. rabbit:topic-exchange>
    57. <rabbit:queue id="order_queue" name="order_queue">
    58. <rabbit:queue-arguments>
    59. <entry key="x-dead-letter-exchange" value="order_exchange_dlx" />
    60. <entry key="x-dead-letter-routing-key" value="dlx.order.cancel" />
    61. <entry key="x-message-ttl" value="10000" value-type="java.lang.Integer" />
    62. rabbit:queue-arguments>
    63. rabbit:queue>
    64. <rabbit:topic-exchange name="order_exchange">
    65. <rabbit:bindings>
    66. <rabbit:binding pattern="order.#" queue="order_queue">rabbit:binding>
    67. rabbit:bindings>
    68. rabbit:topic-exchange>
    69. <rabbit:queue id="order_queue_dlx" name="order_queue_dlx">rabbit:queue>
    70. <rabbit:topic-exchange name="order_exchange_dlx">
    71. <rabbit:bindings>
    72. <rabbit:binding pattern="dlx.order.#" queue="order_queue_dlx">rabbit:binding>
    73. rabbit:bindings>
    74. rabbit:topic-exchange>
    75. beans>

    consumer.xml

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context
    8. https://www.springframework.org/schema/context/spring-context.xsd
    9. http://www.springframework.org/schema/rabbit
    10. http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    11. <context:property-placeholder location="classpath:rabbitmq.properties"/>
    12. <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
    13. port="${rabbitmq.port}"
    14. username="${rabbitmq.username}"
    15. password="${rabbitmq.password}"
    16. virtual-host="${rabbitmq.virtual-host}"/>
    17. <context:component-scan base-package="com.itheima.listener" />
    18. <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" prefetch="1" >
    19. <rabbit:listener ref="orderListener" queue-names="order_queue_dlx">rabbit:listener>
    20. rabbit:listener-container>
    21. beans>

  • 相关阅读:
    和xshell和crt说再见,认识了一款51k star多端跨平台终端神器,强大酷炫
    Python——字典数据存入excel
    如何在Java中实现LRU缓存
    深入理解netty(二)Channel
    【云原生|Docker系列9】Docker仓库管理使用详解
    ORACLE数据库前导0的问题
    CVF_统一多模态之文字生成
    KEIL5工程改名3步骤
    MySQL学习(二)——MySQL内置函数
    如何使用baostock代码下载股票数据?
  • 原文地址:https://blog.csdn.net/weixin_45729272/article/details/126267284