标签:context boot ide onclick cat port ons mic password
一、简单示例:
1)创建一个spring boot项目:
2)配置
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); } }
2)生产者:
@Component public class RabbitProvider { @Autowired private RabbitTemplate rabbitTemplate; public void send(String message) { //rabbitTemplate.convertAndSend("helloRabbit", message); rabbitTemplate.convertAndSend("fanout_exchange_new","", message); } }
3)消费者:
@Component public class RabbitConsumer { @RabbitListener(queues = "simple_queue") public void process(String message) { System.out.println("Consumer: " + message); } }
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"); } } }
三、通配符模式:
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模式消息"); } }
标签:context boot ide onclick cat port ons mic password
原文地址:https://www.cnblogs.com/Tractors/p/11343245.html