首页 > 其他分享 >消息中间件RabbitMQ_RabbitMQ高级特性6

消息中间件RabbitMQ_RabbitMQ高级特性6

时间:2024-04-15 17:11:07浏览次数:22  
标签:amqp 队列 消息中间件 springframework 特性 org import RabbitMQ 消息

一、RabbitMQ高级特性

  • 消息可靠性投递
  • Consumer ACK
  • 消费端限流
  • TTL
  • 死信队列
  • 延迟队列
  • 日志与监控
  • 消息可靠性分析与追踪
  • 管理

二、消息的可靠投递  

 1、模式

 在使用 RabbitMQ 的时候,作为消息发送方希望杜绝任何消息丢失或者投递失败场景。RabbitMQ 为我们提供了两种方式用来控制消息的投递可靠性模式。

  • confirm 确认模式
  • return 退回模式 

 rabbitmq 整个消息投递的路径为:producer--->rabbitmq broker--->exchange--->queue--->consumer

  • 消息从 producer 到 exchange 则会返回一个 confirmCallback回调函数 。
  • 消息从 exchange-->queue 投递失败则会返回一个 returnCallback 。

 我们将利用这两个 callback 控制消息的可靠性投递

 2、确认模式代码实现:

  spring配置文件中开启确认模式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"
                               publisher-confirms="true"
    />
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--消息的可靠性投递(生产端)-->
    <rabbit:queue id="test_queue_confirm" name="test_queue_confirm"></rabbit:queue>
    <rabbit:direct-exchange name="test_exchange_confirm">
        <rabbit:bindings>
            <rabbit:binding queue="test_queue_confirm" key="confirm"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

  定义ConfirmCallBack回调函数

package com.itheima;
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.amqp.rabbit.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml") public class ProducerTest { //1、注入 RabbitTemplate @Autowired private RabbitTemplate rabbitTemplate; /** * 确认模式: * 步骤: * 1、确认模式开启:ConnectionFactory中开启publisher-confirms="true" * 2、在rabbitTemplate定义一个ConfirmCallBack回调函数 */ @Test public void testConfirm(){ //2、定义回调 rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() { /** * * @param correlationData 相关配置信息 * @param ack exchange交换机是否成功收到了消息。true成功,false代表失败 * @param cause 失败原因 */ @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { System.out.println("confirm方法被执行了..."); if(ack){ //接收成功 System.out.println("接收成功消息" + cause); }else { //接收失败 System.out.println("接收失败消息" + cause); //做一些处理,让消息再次发送。 } } }); //3、发送消息 rabbitTemplate.convertAndSend("test_exchange_confirm","confirm","message confirm...");
      //交换机名称错误,发送失败
     rabbitTemplate.convertAndSend("test_exchange_confirm1","confirm","message confirm...");
} }

  执行testConfirm,控制台输出信息如下:

   

 2、退回模式代码编写

  开启回退模式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"
                               publisher-confirms="true"
                               publisher-returns="true"
    />
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--消息的可靠性投递(生产端)-->
    <rabbit:queue id="test_queue_confirm" name="test_queue_confirm"></rabbit:queue>
    <rabbit:direct-exchange name="test_exchange_confirm">
        <rabbit:bindings>
            <rabbit:binding queue="test_queue_confirm" key="confirm"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

  定义ReturnCallBack

package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {

    //1、注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 回退模式:当消息发送给Exchange后,Exchange路由到Queue失败时 才会执行ReturnCallBack
     *  步骤:
     *      1、开启回退模式:publisher-returns="true"
     *      2、设置ReturnCallBack
     *      3、设置Exchange处理消息的模式:
     *          1. 如果消息没有路由到Queue,则丢弃消息(默认)
     *          2. 如果消息没有路由到Queue,返回给消息发送方ReturnCallBack
     */
    @Test
    public void testReturn(){
        //设置交换机处理失败消息的模式,不设置则丢弃消息,设置为true则交换机会将消息返回给消息生产者,消息生产者通过监听拿到消息
        rabbitTemplate.setMandatory(true);

        //2、设置ReturnCallBack
        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            /**
             *
             * @param message   发送的消息对象
             * @param replyCode 失败的错误码
             * @param replyText 失败的错误信息
             * @param exchange  交换机
             * @param routingKey    路由键
             */
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("return 执行了...");
                System.out.println("message:"+message);
                System.out.println("replyCode:"+replyCode);
                System.out.println("replyText:"+replyText);
                System.out.println("exchange:"+exchange);
                System.out.println("routingKey:"+routingKey);

          //处理   } }); //3、发送消息 rabbitTemplate.convertAndSend("test_exchange_confirm","confirm1","message confirm..."); } }

  执行testReturn方法,控制台打印结果:

   

  3、小结

  设置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中也提供了事务机制,但是性能较差,此处不做讲解。

   使用channel下列方法,完成事务控制:
    txSelect(), 用于将当前channel设置成transaction模式
    txCommit(),用于提交事务
    txRollback(),用于回滚事务

三、Consumer Ack

 1、确认方式

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

  • 自动确认:acknowledge="none",默认
  • 手动确认:acknowledge="manual"
  •  根据异常情况确认:acknowledge="auto",(这种方式使用麻烦,不作讲解)

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

 2、自动确认代码实现

  spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>

    <context:component-scan base-package="com.itheima.rabbitmq.listener"/>

    <!--定义监听器容器-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual">
        <rabbit:listener ref="ackListener" queue-names="test_queue_confirm"></rabbit:listener>
    </rabbit:listener-container>
</beans>

  编写监听器类

package com.itheima.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Consumer ACK机制:
 *  1、设置手动签收acknowledge="manual"
 *  2、让监听器类实现ChannelAwareMessageListener接口
 *  3、如果消息成功处理,则调用channel的basicAck()签收
 *  4、如果消息处理失败,则调用channel的basicNack()拒绝签收,broker重新发送给consumer
 */
@Component
public class AckListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        Thread.sleep(1000);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //1. 接收转换消息
            System.out.println(new String(message.getBody()));

            //2.处理业务逻辑
            System.out.println("处理业务逻辑...");
            //int i=3/0;  //出现错误
            //3.手动签收    multiple表示是否签收多条消息
            channel.basicAck(deliveryTag,true);
        } catch (Exception e) {
            //4.拒绝签收
            /*
                第三个参数:requeue表示重回队列。如果设置为true,则消息重新回到queue,broker会重新发送该消息给消费端
             */
            channel.basicNack(deliveryTag,true,true);
//            channel.basicReject(deliveryTag,true);//不推荐
        }

    }
}

  编写测试类

package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class ConsumerTest {

    @Test
    public void test1(){
        boolean flag = true;
        while (true){

        }
    }
}

  生产者发送消息,执行消费者test1方法接收成功消息

   

   AckListener中加入错误代码int i=3/0;,消费者重新接收消息,控制台会一直重复打印消息,RabbitMQ消息一直是未签收状态(消息重回队列,消费者又进行接收,失败后又重回队列)

   

   

 3、小结

   在rabbit:listener-container标签中设置acknowledge属性,设置ack方式none:自动确认,manual:手动确认

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

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

四、消息可靠性总结

  1. 持久化

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

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

 五、消费端限流

 

  每秒从MQ中拉取1000个请求,就是在消费消息的时候做了限流;

  A系统维护停机,维护好启动后很多消息一次性打到A系统就会挂掉,所以也要做限流

 1、代码编写

  编写spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>

    <context:component-scan base-package="com.itheima.rabbitmq.listener"/>

    <!--定义监听器容器-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" prefetch="1">
        <!--<rabbit:listener ref="ackListener" queue-names="test_queue_confirm"></rabbit:listener>-->
        <rabbit:listener ref="qosListener" queue-names="test_queue_confirm"></rabbit:listener>
    </rabbit:listener-container>
</beans>

  编写QosListener监听器

package com.itheima.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * Consumer 限流
 *      1、确保ack机制为手动确认。
 *      2、listener-container配置属性
 *              prefetch="1"   表示消费端每次从mq拉取一条消息来消费,直到手动确认消息完毕后,才会继续拉取下一条消息
 */
@Component
public class QosListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception{
        Thread.sleep(1000);
        //1.获取消息
        System.out.println(new String(message.getBody()));
        //2. 处理业务逻辑

        //3.签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
    }
}

  生产者发送10条消息,消费者执行ConsumerTest.test1测试方法,控制台每隔1秒接收到消息并且输出:

   

 2、小结

   在<rabbit:listener-container> 中配置 prefetch属性设置消费端一次拉取多少消息

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

六、TTL

  TTL 全称 Time To Live(存活时间/过期时间)。

  当消息到达存活时间后,还没有被消费,会被自动清除。

  RabbitMQ可以对消息设置过期时间,也可以对整个队列(Queue)设置过期时间。

  

 1、RabbitMQ管控台操作设置过期时间的效果

  添加一个队列:test_queue_ttl

      

  添加一个交换机:test_exchange_ttl

     

  交换机绑定队列

     

   发送消息

     

  10秒后,检查队列中消息数量

   

 2、代码实现

  1. 队列过期

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"
                               publisher-confirms="true"
                               publisher-returns="true"
    />
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--消息的可靠性投递(生产端)-->
    <rabbit:queue id="test_queue_confirm" name="test_queue_confirm"></rabbit:queue>
    <rabbit:direct-exchange name="test_exchange_confirm">
        <rabbit:bindings>
            <rabbit:binding queue="test_queue_confirm" key="confirm"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!--ttl-->
    <rabbit:queue name="test_queue_ttl" id="test_queue_ttl">
        <!--设置queue的参数-->
        <rabbit:queue-arguments>
            <!--x-message-ttl队列的过期时间-->
            <entry key="x-message-ttl" value="10000" value-type="java.lang.Integer"></entry>
        </rabbit:queue-arguments>
    </rabbit:queue>
    <rabbit:topic-exchange name="test_exchange_ttl">
        <rabbit:bindings>
            <rabbit:binding pattern="ttl.#" queue="test_queue_ttl"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>
package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {

    //1、注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * TTL:过期时间
     *      1、队列统一过期
     *
     *      2、消息单独过期
     *              如果设置了消息的过期时间,也设置了队列的过期时间,以时间短的为准。
     *      队列过期后会将队列所有消息全部移除。
     *      消息过期后,只有消息在队列顶端,才会判断其是否过期,过期则移除
     */
    @Test
    public void testTtl(){
for (int i = 0; i < 10; i++) { rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe","message ttl..."); } } }

  执行testTtl,观察RabbitMQ管控台

   

   10秒后,队列中消息全部移除

   

   2. 消息过期

package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {

    //1、注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * TTL:过期时间
     *      1、队列统一过期
     *
     *      2、消息单独过期
     *              如果设置了消息的过期时间,也设置了队列的过期时间,以时间短的为准。
     *      队列过期后会将队列所有消息全部移除。
     *      消息过期后,只有消息在队列顶端,才会判断其是否过期,过期则移除
     */
    @Test
    public void testTtl(){//消息后处理对象,设置一些消息的参数信息
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {

            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //1. 设置message的信息
                message.getMessageProperties().setExpiration("5000");

                //2. 返回该消息
                return message;
            }
        };

        //消息单独过期
        rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe","message ttl...",messagePostProcessor);

     /*   for (int i = 0; i < 10; i++) {
            if(i==5){
                //发送过期消息,这些消息没有在队列顶端所以不能移除
                rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe","message ttl...",messagePostProcessor);
            }else{
                //发送不过期消息
                rabbitTemplate.convertAndSend("test_exchange_ttl","ttl.hehe","message ttl...");
            }
        }*/
    }
}

  执行testTtl,观察管控台消息,5秒后过期移除

 3、小结

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

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

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

七、死信队列

 死信队列,英文缩写:DLX 。Dead Letter Exchange(死信交换机),当消息成为Dead message后,可以被重新发送到另一个交换机,这个交换机就是DLX。 

  

 消息成为死信的三种情况:

  1. 队列消息长度到达限制;

  2. 消费者拒接消费消息,basicNack/basicReject,并且不把消息重新放入原目标队列,requeue=false;

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

 队列绑定死信交换机:

  给队列设置参数: x-dead-letter-exchange 和 x-dead-letter-routing-key

  

 1、代码实现

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"
                               publisher-confirms="true"
                               publisher-returns="true"
    />
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--死信队列:
                1. 声明正常的队列test_queue_dlx和交换机test_exchange_dlx
                2. 声明死信队列queue_dlx和死信交换机exchange_dlx
                3. 正常队列绑定死信交换机
                            设置两个参数:
                                x-dead-letter-exchange:死信交换机名称
                                x-dead-letter-routing-key:发送给死信交换机的routingkey
    -->
    <!--1. 声明正常的队列test_queue_dlx和交换机test_exchange_dlx-->
    <rabbit:queue name="test_queue_dlx" id="test_queue_dlx">
        <rabbit:queue-arguments>
            <!--3.1 设置x-dead-letter-exchange:死信交换机名称-->
            <entry key="x-dead-letter-exchange" value="exchange_dlx"/>
            <!--3.2 x-dead-letter-routing-key:发送给死信交换机的routingkey-->
            <entry key="x-dead-letter-routing-key" value="dlx.hehe"/>
            <!--4.1 设置队列的过期时间 ttl-->
            <entry key="x-message-ttl" value="10000" value-type="java.lang.Integer"/>
            <!--4.2 设置队列的长度限制max-length-->
            <entry key="x-max-length" value="10" value-type="java.lang.Integer"/>
        </rabbit:queue-arguments>
    </rabbit:queue>
    <rabbit:topic-exchange name="test_exchange_dlx">
        <rabbit:bindings>
            <rabbit:binding pattern="test.dlx.#" queue="test_queue_dlx"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--2. 声明死信队列queue_dlx和死信交换机exchange_dlx-->
    <rabbit:queue name="queue_dlx" id="queue_dlx"></rabbit:queue>
    <rabbit:topic-exchange name="exchange_dlx">
        <rabbit:bindings>
            <rabbit:binding pattern="dlx.#" queue="queue_dlx"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>


    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>

  1. 过期时间测试

package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {

    //1、注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送测试死信消息
     *      1.过期时间
     *      2.长度限制
     *      3.消息拒收
     */
    @Test
    public void testDlx(){
        //1. 测试过期时间,死信消息
        rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.hehe","message dlx...");
    }

}

  执行testDlx,观察RabbitMQ管控台变化:

   

   10秒后,消息过期未被消费,成为死信队列

   

   2. 长度限制测试

package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {

    //1、注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送测试死信消息
     *      1.过期时间
     *      2.长度限制
     *      3.消息拒收
     */
    @Test
    public void testDlx(){

        //2. 测试长度限制后,消息死信
        for (int i = 0; i < 20; i++) {
            rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.haha","message dlx...");
        }
    }
}

  执行testDlx,观察RabbitMQ管控台变化:

   10条消息进入正常队列,10条消息进入死信队列

    

    10秒后,进入正常队列的消息到了过期时间也进入死信队列

    

  3. 消费端拒收消息

package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {

    //1、注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送测试死信消息
     *      1.过期时间
     *      2.长度限制
     *      3.消息拒收
     */
    @Test
    public void testDlx(){

        //3. 测试消息拒收
        rabbitTemplate.convertAndSend("test_exchange_dlx","test.dlx.hehe","message dlx...");
    }
}

  消费端工程spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>

    <context:component-scan base-package="com.itheima.rabbitmq.listener"/>

    <!--定义监听器容器-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" prefetch="1">
        <!--定义监听器,监听正常的队列-->
        <rabbit:listener ref="dlxListener" queue-names="test_queue_dlx"></rabbit:listener>
    </rabbit:listener-container>
</beans>
package com.itheima.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * Consumer ACK机制:
 *  1、设置手动签收acknowledge="manual"
 *  2、让监听器类实现ChannelAwareMessageListener接口
 *  3、如果消息成功处理,则调用channel的basicAck()签收
 *  4、如果消息处理失败,则调用channel的basicNack()拒绝签收,broker重新发送给consumer
 */
@Component
public class DlxListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        Thread.sleep(1000);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //1. 接收转换消息
            System.out.println(new String(message.getBody()));

            //2.处理业务逻辑
            System.out.println("处理业务逻辑...");
            int i=3/0;  //出现错误
            //3.手动签收    multiple表示是否签收多条消息
            channel.basicAck(deliveryTag,true);
        } catch (Exception e) {
            //4.拒绝签收,不重回队列,requeue=false
            System.out.println("出现异常,拒绝接收");
            /*
                第三个参数:requeue表示重回队列。如果设置为true,则消息重新回到queue,broker会重新发送该消息给消费端
             */
            channel.basicNack(deliveryTag,true,false);
        }
    }
}
package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class ConsumerTest {

    @Test
    public void test1(){
        boolean flag = true;
        while (true){

        }
    }
}

  执行test1,然后执行ProducerTest.testDlx方法发送消息,观察RabbitMQ管控台:

   

 2、小结

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

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

  3. 消息成为死信的三种情况:
              1. 队列消息长度到达限制;
              2. 消费者拒接消费消息,并且不重回队列;
              3. 原队列存在消息过期设置,消息到达超时时间未被消费;

八、延迟队列

 延迟队列,即消息进入队列后不会立即被消费,只有到达指定时间后,才会被消费。

 1、需求:
   1. 下单后,30分钟未支付,取消订单,回滚库存。

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

  实现方式:
   1. 定时器

   2. 延迟队列

  

 2、很可惜,在RabbitMQ中并未提供延迟队列功能

  但是可以使用:TTL+死信队列 组合实现延迟队列的效果。

  

  3、代码实现

  生产端spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"
                               publisher-confirms="true"
                               publisher-returns="true"
    />
    <!--定义管理交换机、队列-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--延迟队列
            1.定义正常交换机order_exchange和队列order_queue
            2.定义死信交换机order_exchange_dlx和队列order_queue_dlx
            3.绑定,设置正常队列过期时间为30分钟
    -->
    <!--1.定义正常交换机order_exchange和队列order_queue-->
    <rabbit:queue name="order_queue" id="order_queue">
        <!--3.绑定,设置正常队列过期时间为30分钟-->
        <rabbit:queue-arguments>
            <entry key="x-dead-letter-exchange" value="order_exchange_dlx"/>
            <entry key="x-dead-letter-routing-key" value="dlx.order.cancel"/>
            <entry key="x-message-ttl" value="10000" value-type="java.lang.Integer"/>
        </rabbit:queue-arguments>
    </rabbit:queue>
    <rabbit:topic-exchange name="order_exchange">
        <rabbit:bindings>
            <rabbit:binding pattern="order.#" queue="order_queue"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--2.定义死信交换机order_exchange_dlx和队列order_queue_dlx-->
    <rabbit:queue name="order_queue_dlx" id="order_queue_dlx"></rabbit:queue>
    <rabbit:topic-exchange name="order_exchange_dlx">
        <rabbit:bindings>
            <rabbit:binding pattern="dlx.order.#" queue="order_queue_dlx"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--定义rabbitTemplate对象操作可以在代码中方便发送消息-->
    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>
package com.itheima;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class ProducerTest {

    //1、注入 RabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testDelay() throws InterruptedException {
        //1. 发送订单消息。将来是在订单系统中,下单成功后,发送消息
        rabbitTemplate.convertAndSend("order_exchange","order.msg","订单信息:id=1,time=2024/04/15 13:20");

        //2. 打印倒计时10秒
        for (int i = 10; i > 0; i--) {
            System.out.println(i+"...");
            Thread.sleep(1000);
        }
    }

}

  

package com.itheima.rabbitmq.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * Consumer ACK机制:
 *  1、设置手动签收acknowledge="manual"
 *  2、让监听器类实现ChannelAwareMessageListener接口
 *  3、如果消息成功处理,则调用channel的basicAck()签收
 *  4、如果消息处理失败,则调用channel的basicNack()拒绝签收,broker重新发送给consumer
 */
@Component
public class OrderListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        Thread.sleep(1000);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();

        try {
            //1. 接收转换消息
            System.out.println(new String(message.getBody()));

            //2.处理业务逻辑
            System.out.println("处理业务逻辑...");
            System.out.println("根据订单id查询其状态...");
            System.out.println("判断状态是否为支付成功");
            System.out.println("取消订单,回滚库存...");

            //3.手动签收    multiple表示是否签收多条消息
            channel.basicAck(deliveryTag,true);
        } catch (Exception e) {
            //4.拒绝签收,不重回队列,requeue=false
            System.out.println("出现异常,拒绝接收");
            channel.basicNack(deliveryTag,true,false);
        }
    }
}

  生产端spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/rabbit
       http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
    <!--加载配置文件-->
    <context:property-placeholder location="classpath:rabbitmq.properties"/>

    <!-- 定义rabbitmq connectionFactory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
                               port="${rabbitmq.port}"
                               username="${rabbitmq.username}"
                               password="${rabbitmq.password}"
                               virtual-host="${rabbitmq.virtual-host}"/>

    <context:component-scan base-package="com.itheima.rabbitmq.listener"/>

    <!--定义监听器容器-->
    <rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" prefetch="1">
        <!--延迟队列效果实现,一定要监听的是死信队列!!!-->
        <rabbit:listener ref="orderListener" queue-names="order_queue_dlx"></rabbit:listener>
    </rabbit:listener-container>


</beans>

  消费者先启动消费消息,然后执行生产者发送消息,观察控制台:生产端倒计时输出数字时,消费端无接收消息,直到倒计时结束消费端输出接收消息

     

 3、小结

  1. 延迟队列 指消息进入队列后,可以被延迟一定时间,再进行消费。

  2. RabbitMQ没有提供延迟队列功能,但是可以使用 : TTL(过期时间) + DLX(死信交换机) 来实现延迟队列效果。

九、日志与监控

 1、RabbitMQ日志  

  RabbitMQ默认日志存放路径: /var/log/rabbitmq/[email protected]  

  日志包含了RabbitMQ的版本号、Erlang的版本号、RabbitMQ服务节点名称、cookie的hash值、RabbitMQ配置文件地址、内存限制、磁盘限制、默认账户guest的创建以及权限配置等等。

 2、web管控台监控

  

 3、rabbitmqctl管理和监控

查看队列
# rabbitmqctl list_queues

查看exchanges
# rabbitmqctl list_exchanges

查看用户
# rabbitmqctl list_users

查看连接
# rabbitmqctl list_connections

查看消费者信息
# rabbitmqctl list_consumers

查看环境变量
# rabbitmqctl environment

查看未被确认的队列
# rabbitmqctl list_queues  name messages_unacknowledged

查看单个队列的内存使用
# rabbitmqctl list_queues name memory

查看准备就绪的队列
# rabbitmqctl list_queues name messages_ready

十、消息追踪

 在使用任何消息中间件的过程中,难免会出现某条消息异常丢失的情况。对于RabbitMQ而言,可能是因为生产者或消费者与RabbitMQ断开了连接,而它们与RabbitMQ又采用了不同的确认机制;也有可能是因为交换器与队列之间不同的转发策略;甚至是交换器并没有与任何队列进行绑定,生产者又不感知或者没有采取相应的措施;另外RabbitMQ本身的集群策略也可能导致消息的丢失。这个时候就需要有一个较好的机制跟踪记录消息的投递过程,以此协助开发和运维人员进行问题的定位。

 在RabbitMQ中可以使用Firehose和rabbitmq_tracing插件功能来实现消息追踪。

 1、消息追踪-Firehose

  firehose的机制是将生产者投递给rabbitmq的消息,rabbitmq投递给消费者的消息按照指定的格式发送到默认的exchange上。这个默认的exchange的名称为amq.rabbitmq.trace,它是一个topic类型的exchange。发送到这个exchange上的消息的routing key为 publish.exchangename 和 deliver.queuename。其中exchangename和queuename为实际exchange和queue的名称,分别对应生产者投递到exchange的消息,和消费者从queue上获取的消息。

  注意:打开 trace 会影响消息写入功能,适当打开后请关闭。
    rabbitmqctl trace_on:开启Firehose命令

    rabbitmqctl trace_off:关闭Firehose命令

    

  2、消息追踪-rabbitmq_tracing

  rabbitmq_tracing和Firehose在实现上如出一辙,只不过rabbitmq_tracing的方式比Firehose多了一层GUI的包装,更容易使用和管理。

  启用插件:rabbitmq-plugins enable rabbitmq_tracing

   

   

   

 

标签:amqp,队列,消息中间件,springframework,特性,org,import,RabbitMQ,消息
From: https://www.cnblogs.com/ajing2018/p/18136490

相关文章

  • openGauss AI特性
    AI特性人工智能技术最早可以追溯到上世纪50年代,甚至比数据库系统的发展历史还要悠久。但是,由于各种各样客观因素的制约,在很长的一段时间内,人工智能技术并没有得到大规模的应用,甚至还经历了几次明显的低谷期。到了近些年,随着信息技术的进一步发展,从前限制人工智能发展的因素已经逐......
  • docker安装rabbitmq
    //查找镜像dockersearchrabbitmq//默认拉取官方最新版本dockerpullrabbitmq//创建容器,也可直接执行该命令,没有镜像会去先拉取镜像dockerrun-d--namemyrabbitmq-p5672:5672-p15672:15672rabbitmq//进入镜像dockerexec-it容器id/bin/bash//安装UI插件rabb......
  • 哪(个)些特性功能不用于数据的查询优化?() 并行查询 索引 视图 分区
    哪(个)些特性功能不用于数据的查询优化?()并行查询索引视图分区视图并不在数据库中以存储的数据值集形式存在,而仅仅是一个给用户展示的逻辑虚表,其在数据库中底层还是以完整的数据存储。数据的查询优化策略:避免全表扫描,采用分区的形式,找到指定区域来避免全表查询。建......
  • 中国联通点对点高质量传输服务的关键特性
    中国联通点对点高质量传输服务,如IEPL(InternationalEthernetPrivateLine)和IPLC(InternationalPrivateLeasedCircuit),是专为高端企业客户设计的运营商级数据传输解决方案,旨在满足在不同区域(包括国内跨城市或跨国)进行机房或办公室业务部署时对于线路安全、数据保密性和高效传输的......
  • php特性
    这里是根据ctfshowphp特性做的题积累的知识1.preg_match()函数可以利用数组绕过,因为preg_match只能处理字符串,所以当传入的subject是数组时会返回false2.intval()函数!!!如果是字符串,它返回的内容取决于字符串最左侧的字符。如intval(‘11a0’)=11。所有输入的内容加上一个字母......
  • php8 新特性 match
     https://www.php.net/manual/en/control-structures.match.php   $shape=['type'=>'circle','radius'=>'10'];$res=match($shape){['type'=>'circle','rad......
  • PHP特性 web107-
    Web107parse_str函数将前字符串解析到后边,实现变量的覆盖if(isset($_POST['v1'])){    $v1 = $_POST['v1'];    $v3 = $_GET['v3'];       parse_str($v1,$v2);       if($v2['flag']==md5($v3)){           echo $flag;    ......
  • RabbitMQ 入门:我的思维导图总结
    刚看完尚硅谷的RabbitMQ入门教程,感觉自己对知识点的理解还有些混乱,看完相关面试题后整理了一份简单的思维导图,仅供参考,如有错误,欢迎指正!视频教程:尚硅谷RabbitMQ教程丨快速掌握MQ消息中间件rabbitmq_哔哩哔哩_bilibili1.概览2.具体细节2.1基本概念常见面试题:什......
  • 暖风机/寻线仪防干扰/防静电LCD液晶段码屏驱动芯片VK1C21A/B/C/D/DA/E/EA具备显示效果
    概述:VK1C21A/B是一个点阵式存储映射的LCD驱动器,可支持最大128点(32SEGx4COM)的LCD屏,也支持2COM和3COM的LCD屏。单片机可通过3/4个通信脚配置显示参数和发送显示数据,也可通过指令进入省电模式。具备高抗干扰,显示效果好,静电耐压高等优良特性,可替代市面上大部分LCD驱动芯片。 特点:......
  • FastAPI: 测试lifespan特性(转)
    addbyzhj:实践出真知,文章写得真不错,自己测试这些条件原文:FastAPI:experimentlifespanfeatureInitInFastAPI,oneofwayscreatingasharedresourceandlivingaslongasapplicationisupisusing lifespan featureinFastAPI.Thislifespanfeaturecandoc......