4交换机的第三种类型:fanout

文章目录

  • 1 fanout
    • 1.1 生产者
      • 1.1.1 引入依赖
      • 1.1.2 编写配置文件
      • 1.1.3 编写配置类(指定了交换机)
      • 1.1.4 编写controller(发送到哪个交换机,以及指定key)
    • 1.2消费者
      • 1.2.1 引入依赖
      • 1.2.2 编写配置文件
      • 1.2.3 编写配置类(指定了交换机,多个消息队列)
      • 1.2.4 编写service(@RabbitListener进行监听每一个队列)
    • 1.3 测试

1 fanout

1.1 生产者

1.1.1 引入依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

1.1.2 编写配置文件

spring:
  application:
    name: biz-publisher
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
server:
  port: 8071


1.1.3 编写配置类(指定了交换机)

package fastwave.cloud.demo.fastwavebizpublisher.config;

import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FanoutConfig {

    @Bean
    FanoutExchange FanoutExchange()
    {
        return new FanoutExchange("FanoutExchange");
    }
}

1.1.4 编写controller(发送到哪个交换机,以及指定key)

package fastwave.cloud.demo.fastwavebizpublisher.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("amqp")
public class AmqpController {

    @Autowired
    RabbitTemplate template;

       @GetMapping("/fanout")
    public String sendFanout(@RequestParam Map<String, Object> params)
    {
        String msg = params.get("msg").toString();
        template.convertAndSend("FanoutExchange", null, msg);
        return "OK";
    }

}

1.2消费者

1.2.1 引入依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

1.2.2 编写配置文件

spring:
  application:
    name: biz-subscriber
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
server:
  port: 8072


1.2.3 编写配置类(指定了交换机,多个消息队列)

一个交换机和每一个消息队列进行绑定

package fastwave.cloud.demo.fastwavebizsubscriber.config;

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

@Configuration
public class FanoutConfig {

    @Bean
    FanoutExchange FanoutExchange()
    {
        return new FanoutExchange("FanoutExchange");
    }


    @Bean
    Queue FanoutQueue()
    {
        return new Queue("FanoutQueue");
    }

    @Bean
    Queue FanoutQueue2()
    {
        return new Queue("FanoutQueue2");
    }

    @Bean
    Binding BindFanout()
    {
        return BindingBuilder.bind(FanoutQueue()).to(FanoutExchange());
    }

    @Bean
    Binding BindFanout2()
    {
        return BindingBuilder.bind(FanoutQueue2()).to(FanoutExchange());
    }

}


1.2.4 编写service(@RabbitListener进行监听每一个队列)

package fastwave.cloud.demo.fastwavebizsubscriber.services;

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

@Component
public class FanoutReceiver {

    @RabbitListener(queues = "FanoutQueue")
    public void receiver(String msg)
    {
        System.out.println("收到的消息是:" + msg);
    }

    @RabbitListener(queues = "FanoutQueue2")
    public void receiver2(String msg)
    {
        System.out.println("收到的消息是:" + msg);
    }
}

1.3 测试

1 启动消费者 和生产者

2 调用接口:

4交换机的第三种类型:fanout_第1张图片
3 结果: 所有的消费者都可以接受到。
4交换机的第三种类型:fanout_第2张图片

你可能感兴趣的:(springboot,整合rabbitmq)