RabbitMQ--五种模式之RabbitMQ与SpringBoot进行整合

五种模式之RabbitMQ与SpringBoot进行整合

application.properties

spring.application.name=rabbitmq-springboot
spring.rabbitmq.host=10.15.0.9
spring.rabbitmq.port=5672
spring.rabbitmq.username=ems
spring.rabbitmq.password=123
spring.rabbitmq.virtual-host=/ems

#RabbitTemplate 用来简化操作

五种模式的测试类

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

    //hello world
    @Test
    public void testHelloWorld(){
    
    
        rabbitTemplate.convertAndSend("hello","hello world模型");
    }

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

    //fanout 广播
    @Test
    public void testFanout(){
    
    
        rabbitTemplate.convertAndSend("logs","","Fanout模型发送的消息");
    }

    //route路由模式---direct
    @Test
    public void testRoute(){
    
    
        rabbitTemplate.convertAndSend("directs","info","发送info的key的路由信息");
    }

    //topic 动态路由 订阅模式
    @Test
    public void testTopic(){
    
    
        rabbitTemplate.convertAndSend("topics","user.save","user.save路由信息");
    }

}

HelloConsumer

@Component
//默认是---持久化 非独占 不是自动删除的
@RabbitListener(queuesToDeclare = @Queue(value= "hello",durable = "true",autoDelete = "true"))
public class HelloConsumer {
    
    

    @RabbitHandler
    public void receive1(String message) {
    
    
        System.out.println("message = " + message);
    }
}

WorkConsumer

//默认在Spring AMQP实现中Work这种方式就是公平调度,如果需要实现能者多劳需要额外配置
@Component
public class WorkConsumer {
    
    
    //一个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String message) {
    
    
        System.out.println("message1=" + message);
    }

    //2个消费者
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String message) {
    
    
        System.out.println("message2=" + message);
    }
}

RouteConsumer

@Component
public class RouteConsumer {
    
    
    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "directs", type = "direct"),//指定交换机名称和类型
                    key = {
    
    "info", "error", "warning"}
            )
    })
    public void recrive1(String message) {
    
    
        System.out.println("message1=" + message);
    }

    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,//创建临时队列
                    exchange = @Exchange(value = "directs", type = "direct"),//指定交换机名称和类型
                    key = {
    
    "error"}
            )
    })
    public void recrive2(String message) {
    
    
        System.out.println("message2=" + message);
    }
}

FanoutConsumer

@Component
public class FanoutConsumer {
    
    
    @RabbitListener(bindings = {
    
    @QueueBinding(
            value = @Queue,//创建临时队列
            exchange = @Exchange(value = "logs", type = "fanout")//绑定的交换机
    )})
    public void receive1(String message) {
    
    
        System.out.println("message1 =" + message);
    }

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

TopicConsumer

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

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

猜你喜欢

转载自blog.csdn.net/qq_38530648/article/details/114101971