什么是消费端的限流?
消费端限流RabbitMQ提供的解决方案?
注意: prefetchSize和global这两项,RabbitMQ没有实现,暂且不研究;
prefetch_count在no_ask=false的情况下生效,即在自动应答的情况下,这两个值是不生效的;
自定义消费者:
package com.hyf.rabbitmqapi.limit;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:32
*/
public class MyConsumer extends DefaultConsumer {
private Channel channel;
public MyConsumer(Channel channel) {
super(channel);
this.channel = channel;
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.err.println("-----------consume message----------");
System.err.println("consumerTag: " + consumerTag);
System.err.println("envelope: " + envelope);
System.err.println("properties: " + properties);
System.err.println("body: " + new String(body));
// 表示这条消息已处理完,推送下一条信息来。 false : 代表手动签收
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
生产端:
package com.hyf.rabbitmqapi.limit;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:45
*/
public class Producer {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exChangeName = "test_qos_exchange";
String routingKey = "qos.save";
String msg = "消费端限流处理。。。。";
for (int i=0;i<5;i++){
channel.basicPublish(exChangeName,routingKey,true,null,msg.getBytes());
}
}
}
消费端:
package com.hyf.rabbitmqapi.limit;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:49
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exChangeName = "test_qos_exchange";
String exChangeType = "topic";
String queueName = "test_qos_queue";
String routingKey = "qos.#";
channel.exchangeDeclare(exChangeName,exChangeType,true,false,null);
channel.queueDeclare(queueName,true,false,false,null);
channel.queueBind(queueName,exChangeName,routingKey);
// 限流方式 第一件事就是 autoAck设置为 false
channel.basicQos(0,1,false);
/*
* 参数 b:false 代表手动接收
* */
channel.basicConsume(queueName,false,new MyConsumer(channel));
}
}
消费端手工ACK与NACK
消费端的重回队列
自定义消费者
package com.hyf.rabbitmqapi.ack;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:32
*/
public class MyConsumer extends DefaultConsumer {
private Channel channel;
public MyConsumer(Channel channel) {
super(channel);
this.channel = channel;
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.err.println("-----------consume message----------");
System.err.println("body: " + new String(body));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if((Integer) properties.getHeaders().get("num")==0){
// 参数3 requeue: true 重回队列 / false 不 重回队列
channel.basicNack(envelope.getDeliveryTag(),false,true);
}else{
channel.basicAck(envelope.getDeliveryTag(), false);
}
}
}
生产端:
package com.hyf.rabbitmqapi.ack;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.omg.CORBA.OBJ_ADAPTER;
import java.util.HashMap;
import java.util.Map;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:45
*/
public class Producer {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exChangeName = "test_ack_exchange";
String routingKey = "ack.save";
for (int i = 0; i < 5; i++) {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("num",i);
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.deliveryMode(2)
.contentEncoding("UTF-8")
.headers(headers)
.build();
String msg = "Hello RabbitMQ ACK Message " + i;
channel.basicPublish(exChangeName, routingKey, true, properties, msg.getBytes());
}
}
}
消费端:
package com.hyf.rabbitmqapi.ack;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:49
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exChangeName = "test_ack_exchange";
String exChangeType = "topic";
String queueName = "test_ack_queue";
String routingKey = "ack.#";
channel.exchangeDeclare(exChangeName,exChangeType,true,false,null);
channel.queueDeclare(queueName,true,false,false,null);
channel.queueBind(queueName,exChangeName,routingKey);
channel.basicConsume(queueName,false,new MyConsumer(channel));
}
}
测试结果:
我在自定义消费者中用 0 代替消息未成功处理,其他的处理成功了,就会把它放到尾部,重新处理。
TTL是Time To Live的缩写,也就是生存时间
RabbitMQ支持消息的过期时间,在消息发送时可以进行指定
RabbitMQ支持队列的过期时间,从消息入队列开始计算,只要超过了队列的超时时间配置,那么消息自动的清除
注意:主要针对消息设置,跟交换机、队列、消费者设置毫无关系
自定义消费者:
package com.hyf.rabbitmqapi.ttl;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:32
*/
public class MyConsumer extends DefaultConsumer {
public MyConsumer(Channel channel) {
super(channel);
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.err.println("-----------consume message----------");
System.err.println("body: " + new String(body));
System.out.println("headers get my1 value: "+properties.getHeaders().get("my1"));
}
}
消费端:
package com.hyf.rabbitmqapi.ttl;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:49
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exChangeName = "test_ttl_exchange";
String exChangeType = "topic";
String queueName = "test_ttl_queue";
String routingKey = "ttl.#";
channel.exchangeDeclare(exChangeName,exChangeType,true,false,null);
channel.queueDeclare(queueName,true,false,false,null);
channel.queueBind(queueName,exChangeName,routingKey);
channel.basicConsume(queueName,false,new MyConsumer(channel));
}
}
生产端:
package com.hyf.rabbitmqapi.ttl;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.util.HashMap;
import java.util.Map;
/**
* @author xhy
* @site www.4399.com
* @company xxx公司
* @create 2020-03-02 23:45
*/
public class Producer {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exChangeName = "test_ttl_exchange";
String routingKey = "ttl.save";
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("my1","1111");
headers.put("my2","2222");
for (int i = 0; i < 5; i++) {
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.deliveryMode(2)
.contentEncoding("UTF-8")
.expiration("10000")
.headers(headers)
.build();
String msg = "Hello RabbitMQ ttl Message " + i;
channel.basicPublish(exChangeName, routingKey, true, properties, msg.getBytes());
}
//5 记得要关闭相关的连接
channel.close();
connection.close();
}
}
死信队列:DLX,Dead-Letter-Exchange
消息变成死信有以下几种情况
死信队列的特点
死信队列设置
自定义消费者:
package com.hyf.rabbitmqapi.dlx;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
public class MyConsumer extends DefaultConsumer {
public MyConsumer(Channel channel) {
super(channel);
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.err.println("-----------consume message----------");
System.err.println("consumerTag: " + consumerTag);
System.err.println("envelope: " + envelope);
System.err.println("properties: " + properties);
System.err.println("body: " + new String(body));
}
}
生产端:
package com.hyf.rabbitmqapi.dlx;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
/**
* @author 小李飞刀
* @site www.javaxl.com
* @company
* @create 2019-11-20 11:38
*/
public class Producer {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exchange = "test_dlx_exchange";
String routingKey = "dlx.save";
String msg = "Hello RabbitMQ DLX Message";
for(int i =0; i<1; i ++){
AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
.deliveryMode(2)
.contentEncoding("UTF-8")
.expiration("10000")
.build();
channel.basicPublish(exchange, routingKey, true, properties, msg.getBytes());
}
}
}
消费端:
package com.hyf.rabbitmqapi.dlx;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.util.HashMap;
import java.util.Map;
public class Consumer {
public static void main(String[] args) throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("192.168.59.128");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
// 这就是一个普通的交换机 和 队列 以及路由
String exchangeName = "test_dlx_exchange";
String routingKey = "dlx.#";
String queueName = "test_dlx_queue";
channel.exchangeDeclare(exchangeName, "topic", true, false, null);
Map<String, Object> agruments = new HashMap<String, Object>();
agruments.put("x-dead-letter-exchange", "dlx.exchange");
//这个agruments属性,要设置到声明队列上
channel.queueDeclare(queueName, true, false, false, agruments);
channel.queueBind(queueName, exchangeName, routingKey);
//要进行死信队列的声明:
channel.exchangeDeclare("dlx.exchange", "topic", true, false, null);
channel.queueDeclare("dlx.queue", true, false, false, null);
channel.queueBind("dlx.queue", "dlx.exchange", "#");
channel.basicConsume(queueName, true, new MyConsumer(channel));
}
}