1.FanoutController
package com.bctc.servlet.rabbitmq.fanout; 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.RestController; @RestController @RequestMapping("/FanoutController") public class FanoutController { @Autowired private FanoutSender sender; @GetMapping(value = "/fanout") public void fanoutSender(String a) throws Exception { sender.send(a); sender.send2(a+a); } }
2.FanoutRabbitConfig
package com.bctc.servlet.rabbitmq.fanout; 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; /** *fanout类型的exchange会把消息推到所有跟fanoutExchange绑定的queue中,所以不需要指定routingkey,指定了也没用! */ @Configuration public class FanoutRabbitConfig { @Bean public Queue AMessage() { return new Queue("fanout.A"); } @Bean public Queue BMessage() { return new Queue("fanout.B"); } @Bean public Queue CMessage() { return new Queue("fanout.C"); } @Bean FanoutExchange fanoutExchange() { return new FanoutExchange("fanoutExchange"); } @Bean FanoutExchange fanoutExchange2() { return new FanoutExchange("fanoutExchange2"); } @Bean Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) { return BindingBuilder.bind(AMessage).to(fanoutExchange); } @Bean Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange2) { return BindingBuilder.bind(BMessage).to(fanoutExchange2); } @Bean Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange2) { return BindingBuilder.bind(CMessage).to(fanoutExchange2); } }
3.FanoutSender生产者
package com.bctc.servlet.rabbitmq.fanout; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class FanoutSender { @Autowired private AmqpTemplate rabbitTemplate; public void send(String a) { this.rabbitTemplate.convertAndSend("fanoutExchange","", a); } public void send2(String a) { this.rabbitTemplate.convertAndSend("fanoutExchange2","", a); } }4.消费者FanoutReceiverA
package com.bctc.servlet.rabbitmq.fanout; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "fanout.A") public class FanoutReceiverA { @RabbitHandler public void process(String message) { System.out.println("fanout Receiver A : " + message); } }
5.消费者FanoutReceiverB
package com.bctc.servlet.rabbitmq.fanout; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "fanout.B") public class FanoutReceiverB { @RabbitHandler public void process(String message) { System.out.println("fanout Receiver B: " + message); } }
6.消费者FanoutReceiverC
package com.bctc.servlet.rabbitmq.fanout; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component @RabbitListener(queues = "fanout.C") public class FanoutReceiverC { @RabbitHandler public void process(String message) { System.out.println("fanout Receiver C: " + message); } }
http://localhost:9081/FanoutController/fanout?a=niuhao1