SpringBoot-RabbitMQ

Mac中RabbitMQ安装:
  终端命令:

 brew install rabbitmq

 进入安装目录:/usr/local/Cellar/rabbitmq/3.7.3

sbin/rabbitmq-server

 登录 http://localhost:15672 ,用户名:guest   密码:guest

  1. pom.xml添加依赖
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-amqp</artifactId>
    		</dependency>
    
  2. application.properties配置
    spring.rabbitmq.host=127.0.0.1
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=guest
    spring.rabbitmq.password=guest
    
  3. 根据不同的交换机创建队列
    1.   direct
      @Configuration
      public class RabbitConfig {
      
          @Bean
          public Queue helloQueue(){
              return new Queue("helloQueue");
          }
      
          @Bean
          public Queue manyQueue(){
              return new Queue("manyQueue");
          }
      
          @Bean
          public Queue objectQueue(){
              return new Queue("objectQueue");
          }
      
      }
      
    2. topic
      @Configuration
      public class TopicExchangeConfig {
      
          private static final String message = "topic.message";
          private static final String messages = "topic.messages";
      
          // 创建两个队列
          @Bean
          public Queue queueMessage(){
              return new Queue(TopicExchangeConfig.message);
          }
      
          @Bean
          public Queue queueMessages(){
              return new Queue(TopicExchangeConfig.messages);
          }
      
          // topic 交换机,可以根据routing_key自由的绑定不同的队列
          @Bean
          TopicExchange topicExchange(){
              return new TopicExchange("exchange");
          }
      
          // 将队列与交换机,根据不同的路由模式(规则) routing_key  绑定在一起
          @Bean
          Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
              return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
          }
      
          @Bean
          Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
              return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
          }
      
      }
      
    3. fanout
      @Configuration
      public class FanoutExchangeConfig {
      
          // 定义三个队列
          @Bean
          public Queue fanoutOne(){
              return new Queue("fanout.One");
          }
      
          @Bean
          public Queue fanoutTwo(){
              return new Queue("fanout.Two");
          }
      
          @Bean
          public Queue fanoutThree(){
              return new Queue("fanout.Three");
          }
      
          // 定义交换机
          @Bean
          public FanoutExchange fanoutExchange(){
              return new FanoutExchange("fanoutExchange");
          }
      
          // 将交换机与队列绑定,绑定了改交换机的队列都会收到消息
          @Bean
          public Binding bindingFanoutOne(Queue fanoutOne, FanoutExchange fanoutExchange) {
              return BindingBuilder.bind(fanoutOne).to(fanoutExchange);
          }
      
          @Bean
          public Binding bindingFanoutTwo(Queue fanoutTwo, FanoutExchange fanoutExchange) {
              return BindingBuilder.bind(fanoutTwo).to(fanoutExchange);
          }
      
          @Bean
          public Binding bindingFanoutThree(Queue fanoutThree, FanoutExchange fanoutExchange) {
              return BindingBuilder.bind(fanoutThree).to(fanoutExchange);
          }
      
      
      }
      
  4. 发送者
    1.   fanout
      @Component
      public class FanoutSender {
      
          @Autowired
          private AmqpTemplate rabbitTemplate;
      
          public void send(){
              String context = "hi, fanout msg ";
              System.out.println("Sender : " + context);
              // 参数1:交换机
              // 参数2:routing_key
              // 参数3:消息,可以是对象
              this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
          }
      
      }
      
    2. topic
      @Component
      public class TopicSender {
      
          @Autowired
          private AmqpTemplate rabbitTemplate;
      
          public void send(){
              String context = "topic message";
              System.out.println("Sender : " + context);
              rabbitTemplate.convertAndSend("exchange","topic.1", context);
          }
      
          public void send1(){
              String context = "topic message";
              System.out.println("Sender : " + context);
              rabbitTemplate.convertAndSend("exchange","topic.message", context);
          }
      
          public void send2(){
              String context = "topic message";
              System.out.println("Sender : " + context);
              rabbitTemplate.convertAndSend("exchange","topic.messages", context);
          }
      
      }
      
  5. 接受者
    @Component
    // 监听队列
    @RabbitListener(queues = "helloQueue")
    public class HelloReciver {
        // 接受消息后处理
        @RabbitHandler
        public void process(String hello){
            System.out.println("Receiver : " + hello);
        }
    
    }
    


原文地址:https://www.cnblogs.com/king-peng/p/10277293.html