springboot-rabbitmq整合入门(一)

 

一 前言

最近项目用到mq,于是学习了一波。直接上代码

二 工程结构图

三 代码

一对一 模式:

/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 15:48
 * @Modified By
 */
@Configuration
public class Config {
    @Bean
    public Queue Queue1() {
        return new Queue(QueueNames.ONETOMANY_NAME.getVal());
    }

    @Bean
    public Queue Queue2() {
        return new Queue(QueueNames.MANYTOMANY_NAME.getVal());
    }
}
//消息发送

@Component
public class Sender1 {
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送消息
     */
    public void send() {
        String context = "hello hahah";
        System.out.println("sender1 " + context);
        amqpTemplate.convertAndSend("queue1", context);
    }
}


//消息消费方

/**
 * @author qianyongchao
 * @description consumer1 主要是用来测一对一 consumer2 consumer3 用来测多对多
 * @date 17:02 2018/12/6
 * @param
 * @return
 */
@Component
@RabbitListener(queues = "queue1")
public class Consumer1 {
    /*
     * @author qianyongchao
     * @description hello内容就是 Sender1 sender()方法中的context内容
     * @date 17:30 2018/12/6
     * @param
     * @return void
     */
    @RabbitHandler
    public void comsumer(String  hello) {
        System.out.println("Consumer1 "+ hello);
    }
}




//测试

/*
     * @author qianyongchao
     * @description 一对一
     * @date 17:08 2018/12/6
     * @param
     * @return
     */
    @Test
    public void oneToOne() {
	    sender1.send();
    }

一对多:

/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 16:59
 * @Modified By
 */
@Component
public class Sender2 {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送消息
     */
    public void send() {
        String context = "hello "+ new Date();
        amqpTemplate.convertAndSend("queue2", context);
    }
}





/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 16:59
 * @Modified By
 */
@Component
public class Sender3 {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送消息
     */
    public void send() {
        String context = "hello "+ new Date();
        amqpTemplate.convertAndSend("queue2", context);
    }
}



/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 16:50
 * @Modified By
 */
@Component
@RabbitListener(queues = "queue2")
public class Consumer2 {
    @RabbitHandler
    public void oneToMany1(String hello) {
        System.out.println("Consumer2 " + hello);
    }
}




@Component
@RabbitListener(queues = "queue2")
public class Consumer3 {

    @RabbitHandler
    public void oneToMany2(String hello) {
        System.out.println("Consumer3 " + hello);
    }
}

测试:
    /*
     * @author qianyongchao
     * @description 一对多
     * @date 17:08 2018/12/6
     * @param
     * @return
     */
    /*@Test
    public void oneToMany() throws InterruptedException {
        for(int i = 0; i < 10; i++) {
            Thread.sleep(3000);
            sender2.send();
        }
    }*/
        Consumer2 hello Thu Dec 06 17:17:11 CST 2018
        Consumer3 hello Thu Dec 06 17:17:14 CST 2018
        Consumer2 hello Thu Dec 06 17:17:17 CST 2018
        Consumer3 hello Thu Dec 06 17:17:20 CST 2018
        Consumer2 hello Thu Dec 06 17:17:23 CST 2018
        Consumer3 hello Thu Dec 06 17:17:26 CST 2018
        Consumer2 hello Thu Dec 06 17:17:29 CST 2018
        Consumer3 hello Thu Dec 06 17:17:32 CST 2018
        Consumer2 hello Thu Dec 06 17:17:35 CST 2018
        Consumer3 hello Thu Dec 06 17:17:38 CST 2018
    /*
     * @author qianyongchao
     * @description 多对多
     * @date 17:08 2018/12/6
     * @param
     * @return
     */
    @Test
    public void manyToMany() throws InterruptedException {
        for(int i = 0; i < 10; i++) {
            sender2.send();
            sender3.send();
            Thread.sleep(3000);
        }
    }
    Consumer3 hello Thu Dec 06 17:21:45 CST 2018
    Consumer2 hello Thu Dec 06 17:21:45 CST 2018
    Consumer2 hello Thu Dec 06 17:21:48 CST 2018
    Consumer3 hello Thu Dec 06 17:21:48 CST 2018
    Consumer2 hello Thu Dec 06 17:21:51 CST 2018
    Consumer3 hello Thu Dec 06 17:21:51 CST 2018
    Consumer2 hello Thu Dec 06 17:21:54 CST 2018
    Consumer3 hello Thu Dec 06 17:21:54 CST 2018
    Consumer3 hello Thu Dec 06 17:21:57 CST 2018
    Consumer2 hello Thu Dec 06 17:21:57 CST 2018
    Consumer2 hello Thu Dec 06 17:22:00 CST 2018
    Consumer3 hello Thu Dec 06 17:22:00 CST 2018
    Consumer2 hello Thu Dec 06 17:22:03 CST 2018
    Consumer3 hello Thu Dec 06 17:22:03 CST 2018
    Consumer2 hello Thu Dec 06 17:22:06 CST 2018
    Consumer3 hello Thu Dec 06 17:22:06 CST 2018
    Consumer3 hello Thu Dec 06 17:22:09 CST 2018
    Consumer2 hello Thu Dec 06 17:22:09 CST 2018
    Consumer3 hello Thu Dec 06 17:22:12 CST 2018
    Consumer2 hello Thu Dec 06 17:22:12 CST 2018

个人想法:不管是一对多还是多对多,并不是按照一定的顺序执行的,也就是现在我还不清楚情况下,消费者 消费的先后顺序!

对象传输:


/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 17:56
 * @Modified By
 */

@Component
public class ObjectSender1 {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sender(User user) {

        System.out.println("object_sender:"+ user.toString());
        amqpTemplate.convertAndSend("queue2", user.toString());
    }

}




/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 18:01
 * @Modified By
 */

@Component
@RabbitListener(queues = "queue2")
public class ObjectConsumer1 {

    @RabbitHandler
    public void consumer1(User user) {
        System.out.println("object_consumer1 " + user.toString());

    }
}

测试:

    /*
     * @author qianyongchao
     * @description
     * @date 18:20 2018/12/6
     * @param 对象demo
     * @return void
     */    
    @Test
    public void objectTest() {
        User user = new User("qianyongchao", "123456");
        objectSender1.sender(user);
    }
        object_sender:User(userName=qianyongchao, password=123456)
        Consumer2 User(userName=qianyongchao, password=123456)

topic模式:

/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 18:22
 * @Modified By
 */
@Configuration
public class TopicConfig {
    final static String MESSAGE1 = "topic.message1";
    final static String MESSAGE2 = "topic.message2";


    @Bean
    public Queue queueMessage1() {
        return new Queue(TopicConfig.MESSAGE1);
    }

    @Bean
    public Queue queueMessage2() {
        return new Queue(TopicConfig.MESSAGE2);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }

    @Bean
    Binding bindingExchangeMessage1(Queue queueMessage1, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage1).to(exchange).with("topic.message1");
    }

    @Bean
    Binding bindingExchangeMessage2(Queue queueMessage2, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage2).to(exchange).with("topic.*");
    }



}
/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 18:28
 * @Modified By
 */
@Component
public class TopicSender1 {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void send1() {
        String context = " message 1";
        System.out.println("topic sender1 "+context);
        amqpTemplate.convertAndSend("exchange", "topic.message1", context);
    }

    public void send2() {
        String context = " message 2";
        System.out.println("topic sender2 "+context);
        amqpTemplate.convertAndSend("exchange", "topic.message2", context);
    }
}






/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 18:28
 * @Modified By
 */
@Component
@RabbitListener(queues = "topic.message1")
public class TopicConsumer1 {

    @RabbitHandler
    public void consumer(String message) {
        System.out.println("topic consumer1 "+ message);
    }
}




/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/6 18:28
 * @Modified By
 */
@Component
@RabbitListener(queues = "topic.message2")
public class TopicConsumer2 {

    @RabbitHandler
    public void consumer(String message) {
        System.out.println("topic consumer2 "+ message);
    }
}

测试:

    @Test
    public void topicTest() {
        topicSender1.send1();
    }
    topic sender1  message 1
    topic consumer2  message 1
    topic consumer1  message 1

fanout:


/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/7 9:38
 * @Modified By
 */
@Configuration
public class FanoutConfig {

    //生成三个队列
    @Bean
    public Queue fanoutQueue1() {
        return new Queue("fanout-queue-1");
    }

    @Bean
    public Queue fanoutQueue2() {
        return new Queue("fanout-queue-2");
    }

    @Bean
    public Queue fanoutQueue3() {
        return new Queue("fanout-queue-3");
    }

    //生成一个交换机 fanoutExchange
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    //交换机绑定fanoutQueue1
    @Bean
    Binding fanoutExchangeQueue1( Queue fanoutQueue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    //交换机绑定fanoutQueue2
    @Bean
    Binding fanoutExchangeQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }

    //交换机绑定fanoutQueue3
    @Bean
    Binding fanoutExchangeQueue3(Queue fanoutQueue3, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue3).to(fanoutExchange);
    }

}
/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/7 9:48
 * @Modified By
 */
@Component
public class FanoutSender {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void fanoutSender() {
        String context = "fanout sender " + new Date();
        System.out.println(context);
        amqpTemplate.convertAndSend("fanoutExchange", "", context);
    }

}



/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/7 9:56
 * @Modified By
 */
@Component
@RabbitListener(queues = "fanout-queue-1")
public class FanoutConsumer1 {

    @RabbitHandler
    public void test(String message) {
        System.out.println("fanout consumer1 "+message);
    }
}



/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/7 9:57
 * @Modified By
 */
@Component
@RabbitListener(queues = "fanout-queue-2")
public class FanoutConsumer2 {

    @RabbitHandler
    public void test(String context) {
        System.out.println("fanout consumer2 "+context);
    }
}



/**
 * @Author qianyongchao
 * @Description
 * @Date 2018/12/7 9:57
 * @Modified By
 */
@Component
@RabbitListener(queues = "fanout-queue-3")
public class FanoutConsumer3 {

    @RabbitHandler
    public void test(String context) {
        System.out.println("fanout consumer3 "+context);
    }
}

测试:

    @Test
    public void fanoutTest() {
        fanoutSender.fanoutSender();
    }
        fanout sender Fri Dec 07 10:30:32 CST 2018
        fanout consumer1 fanout sender Fri Dec 07 10:30:32 CST 2018
        fanout consumer3 fanout sender Fri Dec 07 10:30:32 CST 2018
        fanout consumer2 fanout sender Fri Dec 07 10:30:32 CST 2018

由于本人也是刚学,虽然入门代码跑起来了,但是一些细节和底层原理不是很清楚,在这里和大家一起共勉!

参考:https://www.cnblogs.com/ityouknow/p/6120544.html

本人代码已提交到github:https://github.com/Tablish/sprintboot_redis_demo/tree/master/springboot_rabbitmq

猜你喜欢

转载自blog.csdn.net/u014425865/article/details/84888662