springboot+rabbitmq的整合
2018-08-01 10:20
459 查看
1.queue的代码整合:什么意思呢?通俗的讲就是当一处生产只有一处消费。
2.exchange的整合:俗称广播,就是一处生产多处收到消息。
第一种的queue是可以负载均衡的。会平均分配到每一个消费者。
第二种类似于发送邮件,消息指定发送的人都会收到邮件。
好了,我不爱说文书卷的概念,就喜欢simple demo和quick start。干货,干货!!
代码有能运行的springboot为基础的整合,作为程序员如果整不出一个springboot,就别谈rabbitmq了。
1.queue的整合
@RequestMapping("/send") public User send(){ User user = new User(); user.setName("张三"); user.setId(1L); user.setAge(12); user.setSex(1); rabbitTemplate.convertAndSend("hailang",user); return user; }
实体bean
public class User implements Serializable{ private Long id; private String name; private Integer age; private Integer sex; public User(){}; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", sex=" + sex + '}'; }
@Component public class Receiver { @RabbitListener(queues = "hailang") public void receiver1(User user){ System.out.println("hailang1:"+user); } }
queue的代码测试执行接口,控制台看到消费的结果。
而且读过底层代码发现,类都是通过反射发送,不用考虑序列化问题。框架高度集成方便我们的开发。
2.广播的集成
import org.springframework.amqp.core.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @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 Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) { return BindingBuilder.bind(AMessage).to(fanoutExchange); } @Bean Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) { return BindingBuilder.bind(BMessage).to(fanoutExchange); } @Bean Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) { return BindingBuilder.bind(CMessage).to(fanoutExchange); } }
发送消息
注意发送方法第一个参数
@RequestMapping("/send/group") public User sendGroup(){ User user = new User(); user.setName("张三"); user.setId(1L); user.setAge(12); user.setSex(1); rabbitTemplate.convertAndSend("fanoutExchange","",user); return user; }
接收消息
import com.demo.model.User; 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 Receiver1 { @RabbitHandler public void receiver4(User user){ System.out.println("group1:"+user); } }
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 Recevier2 { @RabbitHandler public void receiver4(User user){ System.out.println("group2:"+user); } }
浏览器,执行controller的url,就可以看到同时得到消费。
阅读更多相关文章推荐
- springboot+rabbitmq整合示例程
- SpringBoot RabbitMQ 整合进阶版
- 5.SpringBoot RabbitMQ整合
- springboot+rabbitMq整合开发实战一
- spring boot整合activemq rabbitmq
- springboot+rabbitmq整合
- Springboot+RabbitMQ整合示例
- springboot+rabbitmq整合
- springboot+rabbitmq整合示例程
- springboot+rabbitMq整合开发实战二:模拟用户下单的过程
- springboot+rabbitmq 整合实例
- Springboot+RabbitMQ整合
- springboot+rabbitmq整合示例程
- springboot rabbitmq整合
- spring boot rabbitmq 多MQ配置 自动 创建 队列 RPC
- SpringBoot ActiveMQ 整合使用
- 带着新人学springboot的应用06(springboot+RabbitMQ 中)
- Springboot + rabbitMQ实现延迟消费以及spring与策略模式联合处理不同的业务(一)
- SpringBoot + RabbitMQ 使用Demo
- 【学习笔记】spring boot + zookeeper + dubbo + rabbitMq + mysql + thymeleaf/freemarker + mybaits 代码试验