1. 程式人生 > >springboot-rabbitmq整合入門(一)

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