worker round ribbon fail公平模式


  channel.basicQos(10);//要根据机器内存设置  内存如上图所示,这里合理设置大小
比如5台集群, 每秒钟有1000个消息 那么每个信道设置为100,这样避免消耗掉服务器的内存


/** * TODO * * @author wangbiao * @Title TODO * @module TODO * @description rout 公平消息 的消费者会根据机器性能 从而能者多劳 * FailWorker1 FailWorker2 根据channel.basicQos(10)的设置大小决定每次分配的数目进行消费 一般消费最大内存为物理机器内存的0.4倍,生产环境根据内存进行配置
channel.basicQos(10);//服务每次要从队列里面获取消息的数量   不设置的就是轮询消费模式按均分配

 

生产者:
-

package com.wangbiao.work.fail;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * TODO
 *
 * @author wangbiao
 * @Title TODO
 * @module TODO
 * @description rout 公平消息 的消费者会根据机器性能  从而能者多劳
 * FailWorker1  FailWorker2  根据channel.basicQos(10)的设置大小决定每次分配的数目进行消费  一般消费最大内存为物理机器内存的0.4倍,生产环境根据内存进行配置
 *
 * @date 2021/3/22 21:54
 */
public class FailProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //所有的中间件的技术都是基于tcp/ip协议 ,只不过rabbitmq遵循的是amqp

        //1创建工程连接
        ConnectionFactory connectionFactory=new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setPassword("guest");
        connectionFactory.setUsername("guest");
        connectionFactory.setVirtualHost("/");
        Connection  connection=null;
        Channel channel=null;
        //2创建连接   基于channel处理的原因是长连接  高并发的场景下会创建多个通道性能很高    如果是基于连接就会产生3次挥手等行为 很消耗性能
        try {
            connection=connectionFactory.newConnection("生产者");
            //3通过连接获取通道
            channel=connection.createChannel();

            for (int i = 0; i <=100000 ; i++) {
                String msg="我要变强:"+i;
                channel.basicPublish("","queue1",null,msg.getBytes());//默认是rabbit-AMQP默认的direct直连路由
            }

            System.out.println("消息发送成功");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("消息发送异常");
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
            //7关闭连接
            if(channel!=null&&channel.isOpen()){
                channel.close();
            }
            //8关闭通道
            if(connection!=null&&connection.isOpen()){
                connection.close();
            }
        }


    }
}

 消费者一:

package com.wangbiao.work.fail;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeoutException;

import static java.lang.Thread.sleep;

/**
 * TODO
 *
 * @author wangbiao
 * @Title TODO
 * @module TODO
 * @description TODO
 * @date 2021/3/22 22:28
 */
public class FailWorker1 {
    public static Runnable runable=new Runnable() {
        @Override
        public void run() {
            //所有的中间件的技术都是基于tcp/ip协议 ,只不过rabbitmq遵循的是amqp

            //1创建工程连接
            ConnectionFactory connectionFactory=new ConnectionFactory();
            connectionFactory.setHost("127.0.0.1");
            connectionFactory.setPort(5672);
            connectionFactory.setPassword("guest");
            connectionFactory.setUsername("guest");
            connectionFactory.setVirtualHost("/");
            Connection connection=null;
            Channel channel=null;
            //获取队列名字
            final  String queueName=Thread.currentThread().getName();

            //2创建连接工程
            try {
                connection=connectionFactory.newConnection("消费者-worker1");
                //3通过连接获取通道
                channel=connection.createChannel();
                //4通过创建交换机,生命队列,绑定关系,路由key,发送消息,和接收消息
                channel.basicQos(10);//服务每次要从队列里面获取消息的数量   不设置的就是轮询消费模式
                //定义接收消息的回调
                Channel finalChannel = channel;
                channel.basicConsume(queueName, false,new DeliverCallback(){//公平应答为false
                    //为false就是要手动确认避免死信队列
                    public void handle(String consumerTag,Delivery message) throws IOException {
//                        try {
//                            sleep(1000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                        System.out.println(queueName+"收到的消息是:"+new String(message.getBody(),"utf-8"));
// 手动应答 finalChannel.basicAck(message.getEnvelope().getDeliveryTag(),false); //单执行 } },new CancelCallback(){ public void handle(String consumerTag) throws UnsupportedEncodingException { System.out.println(queueName+"接收失败了"); } }); System.in.read(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); }finally { //7关闭连接 if(channel!=null&&channel.isOpen()){ try { channel.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } } //8关闭通道 if(connection!=null&&connection.isOpen()){ try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } } }; public static void main(String[] args) throws IOException, TimeoutException { new Thread(runable,"queue1").start(); } }

消费二:

package com.wangbiao.work.fail;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeoutException;

import static java.lang.Thread.sleep;

/**
 * TODO
 *
 * @author wangbiao
 * @Title TODO
 * @module TODO
 * @description TODO
 * @date 2021/3/22 22:28
 */
public class FailWorker2 {
    public static Runnable runable=new Runnable() {
        @Override
        public void run() {
            //所有的中间件的技术都是基于tcp/ip协议 ,只不过rabbitmq遵循的是amqp

            //1创建工程连接
            ConnectionFactory connectionFactory=new ConnectionFactory();
            connectionFactory.setHost("127.0.0.1");
            connectionFactory.setPort(5672);
            connectionFactory.setPassword("guest");
            connectionFactory.setUsername("guest");
            connectionFactory.setVirtualHost("/");
            Connection connection=null;
            Channel channel=null;
            //获取队列名字
            final  String queueName=Thread.currentThread().getName();

            //2创建连接工程
            try {
                connection=connectionFactory.newConnection("消费者-worker1");
                //3通过连接获取通道
                channel=connection.createChannel();
                //4通过创建交换机,生命队列,绑定关系,路由key,发送消息,和接收消息
                channel.basicQos(10);//服务每次要从队列里面获取消息的数量  不设置的就是轮询消费模式
                //定义接收消息的回调
                Channel finalChannel = channel;
                channel.basicConsume(queueName, false,new DeliverCallback(){//公平应答为false
                    public void handle(String consumerTag,Delivery message) throws IOException {
//                        try {
//                            sleep(1000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
                        System.out.println(queueName+"收到的消息是:"+new String(message.getBody(),"utf-8"));
//手动应答 finalChannel.basicAck(message.getEnvelope().getDeliveryTag(),false);//单执行 } },new CancelCallback(){ public void handle(String consumerTag) throws UnsupportedEncodingException { System.out.println(queueName+"接收失败了"); } }); System.in.read(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); }finally { //7关闭连接 if(channel!=null&&channel.isOpen()){ try { channel.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } } //8关闭通道 if(connection!=null&&connection.isOpen()){ try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } } }; public static void main(String[] args) throws IOException, TimeoutException { new Thread(runable,"queue1").start(); } }

 

posted @ 2021-03-24 21:21  余生请多指教ANT  阅读(1)  评论(0编辑  收藏  举报