码迷,mamicode.com
首页 > 编程语言 > 详细

RabbitMQ使用——基于SpringBoot

时间:2019-08-13 00:18:37      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:context   boot   ide   onclick   cat   port   ons   mic   password   

一、简单示例:

  1)创建一个spring boot项目:

  技术图片

  2)配置application.yml

spring:
  application:
    name: spring-boot-amqp
  rabbitmq:
    host: 192.168.80.140
    port: 5672
    username: rabbit
    password: 123456

  3)创建列队配置:

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfiguration {
    @Bean
    public Queue queue() {
        return new Queue("helloRabbit");
    }
}

  4)消费者:

@Component
public class HelloRabbitConsumer {
    @RabbitListener(queues = "helloRabbit")
    public void process(String message) {
        System.out.println("Consumer: " + message);
    }
}

  5)生产者:

@Component
public class HelloRabbitProvider {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String message) {
        rabbitTemplate.convertAndSend("helloRabbit", message);
    }
}

  6)测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
    @Autowired
    private HelloRabbitProvider helloRabbitProvider;

    @Test
    public void testSender() {
        for (int i = 0; i < 10; i++) {
            helloRabbitProvider.send("hello motor");
        }
    }
}

二、路由模式:

  1)路由/列队配置:

技术图片
@Configuration
public class RabbitMQConfiguration {
    @Bean
    public Queue getQueue() {
        return new Queue("simple_queue");
    }

    @Bean
    public FanoutExchange getFanoutExchange(){
        return  new FanoutExchange("fanout_exchange_new");
    }

    @Bean
    public Binding getBinding(Queue getQueue, FanoutExchange getFanoutExchange){
        return BindingBuilder.bind(getQueue).to(getFanoutExchange);
    }
}
RabbitMQConfiguration

  2)生产者:

技术图片
@Component
public class RabbitProvider {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String message) {
        //rabbitTemplate.convertAndSend("helloRabbit", message);
        rabbitTemplate.convertAndSend("fanout_exchange_new","", message);
    }
}
RabbitProvider

  3)消费者:

技术图片
@Component
public class RabbitConsumer {
    @RabbitListener(queues = "simple_queue")
    public void process(String message) {
        System.out.println("Consumer: " + message);
    }
}
RabbitConsumer

  4)测试:

技术图片
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqSpringbootApplicationTests {
    @Autowired
    private RabbitProvider RabbitProvider;

    @Test
    public void testSender() {
        for (int i = 0; i < 10; i++) {
            RabbitProvider.send("hello motor");
        }
    }
}
test

三、通配符模式:

  1)路由/列队配置:

@Configuration
public class RabbitMQConfiguration {
    @Bean
    public Queue getQueue() {
        return new Queue("simple_queue");
    }

    @Bean
    public FanoutExchange getFanoutExchange(){
        return  new FanoutExchange("fanout_exchange_new");
    }

    @Bean
    public Binding getBinding(Queue getQueue, FanoutExchange getFanoutExchange){
        return BindingBuilder.bind(getQueue).to(getFanoutExchange);
    }

    @Bean
    public TopicExchange getTopicExchange(){
        return new TopicExchange("topic_exchange_new");
    }

    @Bean
    public Binding getBinding2(Queue getQueue,TopicExchange getTopicExchange){
        return BindingBuilder.bind(getQueue).to(getTopicExchange).with("product.*");
    }
}

  2)生产者:

@Component
public class RabbitProvider {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(String message) {
        //rabbitTemplate.convertAndSend("helloRabbit", message);
        //rabbitTemplate.convertAndSend("fanout_exchange_new","", message);
        rabbitTemplate.convertAndSend("topic_exchange_new","product.add","topic模式消息");
    }
}

 

RabbitMQ使用——基于SpringBoot

标签:context   boot   ide   onclick   cat   port   ons   mic   password   

原文地址:https://www.cnblogs.com/Tractors/p/11343245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!