栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringBoot整合RabbitMQ

SpringBoot整合RabbitMQ

maven
    
    
      org.springframework.boot
      spring-boot-starter-amqp
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
    
    
      org.springframework.amqp
      spring-rabbit-test
      test
    
生产者 Provider
package springboot;

import com.jia.RabbitMQMain;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = RabbitMQMain.class)
@RunWith(SpringRunner.class)
public class TestRabbitMQ {
   //注入rabbitTemplate
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //helloWord

    @Test
    public void helloWord(){
        rabbitTemplate.convertAndSend("hello","helloWord");
    }

    //work
    @Test
    public void work(){
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("work","work");
        }
    }

    //fanout
    @Test
    public void testFanout() throws InterruptedException {
        rabbitTemplate.convertAndSend("logs","","这是日志广播");
    }

    //route
    @Test
    public void testDirect(){
        rabbitTemplate.convertAndSend("directs","info","info 的日志信息");
    }

    //topic
    @Test
    public void testTopic(){
        rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息");
    }
}

消费者 helloWord
package com.jia.springboot.helloword;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Component("helloWordConsumer")
@RabbitListener(queuesToDeclare = @Queue(value = "hello", autoDelete = "false", durable = "true"))
public class Consumer {
    @RabbitHandler
    public void receive(String msg) {
        System.out.println(msg);
    }
}

work 工作队列
package com.jia.springboot.work;

import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component("workConsumer")
public class Consumer {

    @RabbitListener(queuesToDeclare = @Queue(value = "work"))
    public void recive1(String msg){
        System.out.println("consumer1:"+ msg);
    }

    @RabbitListener(queuesToDeclare = @Queue(value = "work"))
    public void recive2(String msg){
        System.out.println("consumer2:"+ msg);
    }
}

fanout 广播
package com.jia.springboot.fanout;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component("fanoutConsumer")
public class Consumer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,//创建临时队列
            exchange = @Exchange(name="logs",type = "fanout")
    ))
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue, //创建临时队列
            exchange = @Exchange(name="logs",type = "fanout")  //绑定交换机类型
    ))
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

route 路由
package com.jia.springboot.route;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component("routeConsumer")
public class Consumer {
    @RabbitListener(bindings ={
            @QueueBinding(
                    value = @Queue(),
                    key={"info","error"},
                    exchange = @Exchange(type = "direct",name="directs")
            )})
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings ={
            @QueueBinding(
                    value = @Queue(),
                    key={"error"},
                    exchange = @Exchange(type = "direct",name="directs")
            )})
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

topic 主题
package com.jia.springboot.topic;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component("topicConsumer")
public class Comsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    key = {"user.*"},
                    exchange = @Exchange(type = "topic",name = "topics",durable = "false")
            )
    })
    public void receive1(String message){
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    key = {"user.#"},
                    exchange = @Exchange(type = "topic",name = "topics",durable = "false")
            )
    })
    public void receive2(String message){
        System.out.println("message2 = " + message);
    }
}

注:RabbitMQ 文档:https://www.rabbitmq.com/getstarted.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/706325.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号