Rabbitmq中綁定
exchange:flow
routing-key:user
bind-queue:flow_user
白話文就是,把user綁定到flow_user序列
發送方使用routing-key推送:
//把routing-key發送給名為flow的exchenge,然后exchenge負責向綁定的這個Queue推送
amqpTemplate.convertAndSend("flow","user", context);
Rabbit配置
- 添加exchange(這里類型type應該是topic,截圖時候沒有注意)

添加exchange
- 添加Queue

添加Queue
- 添加這個User 到exchange(注意routing-key)

SpringBoot集成Rabbitmq
- 注冊配置bean
@Configurable
public class TopicRabbitConfig {
public final static String FLOW = "flow";
public final static String USER = "user";
public final static String USER_QUEUE = "flow_user";
@Bean
public Queue queueMessages3() {
return new Queue(USER_QUEUE);
}
@Bean
TopicExchange exchange() {
return new TopicExchange(FLOW);
}
@Bean
Binding bindingExchangeMessages3(Queue queueMessages3, TopicExchange exchange) {
return BindingBuilder.bind(queueMessages3).to(exchange).with(FLOW);
}
}
- 發送方代碼
/**
* @Package: pterosaur.account.service.impl
* @Description: 模擬發送消息,測試使用
* @author: liuxin
* @date: 17/4/19 下午3:17
*/
@Component
public class AccountSentImpl {
@Autowired
private AmqpTemplate amqpTemplate;
private ExecutorService threadPool = Executors.newFixedThreadPool(8);
public void send() {
for (int i=0;i<10;i++){
String context = "hello :" + DateUtil.formatDatetime(System.currentTimeMillis())+",當前線程:"+Thread.currentThread().getName();
System.out.println("Sender : " + context);
threadPool.execute(new Runnable() {
@Override
public void run() {
amqpTemplate.convertAndSend(TopicRabbitConfig.FLOW,TopicRabbitConfig.USER, context);
}
});
}
}
}
- 接受方代碼
/**
* @Package: pterosaur.account.service.impl
* @Description: mq信息處理實現類
* @author: liuxin
* @date: 17/4/19 下午2:55
*/
@Component
public class AccountReceiverImpl implements AccountReceiver {
private static final Logger logger = LoggerFactory.getLogger(AccountReceiverImpl.class);
@Autowired
ExecutorService threadPool;
/**
* 用戶流水
*
* @param message
*/
@RabbitListener(queues = TopicRabbitConfig.USER_QUEUE)
@RabbitHandler
public void processUser(String message) {
threadPool.execute(new Runnable() {
@Override
public void run() {
logger.info("用戶側流水:{}",message);
}
});
}
}
- 測試代碼
Sender : hello :2017-04-25 17:44:15,當前線程:main
Sender : hello :2017-04-25 17:44:20,當前線程:main
2017-04-25 17:44:25.754 INFO 67685 --- [pool-1-thread-1] p.a.service.impl.AccountReceiverImpl : 用戶側流水:hello :2017-04-25 17:44:20,當前線程:main
Sender : hello :2017-04-25 17:44:25,當前線程:main
Sender : hello :2017-04-25 17:44:30,當前線程:main
2017-04-25 17:44:32.048 INFO 67685 --- [pool-1-thread-2] p.a.service.impl.AccountReceiverImpl : 用戶側流水:hello :2017-04-25 17:44:30,當前線程:main
Sender : hello :2017-04-25 17:44:32,當前線程:main
Sender : hello :2017-04-25 17:44:33,當前線程:main
2017-04-25 17:44:35.556 INFO 67685 --- [pool-1-thread-3] p.a.service.impl.AccountReceiverImpl : 用戶側流水:hello :2017-04-25 17:44:33,當前線程:main
Sender : hello :2017-04-25 17:44:35,當前線程:main
Sender : hello :2017-04-25 17:44:37,當前線程:main
2017-04-25 17:44:38.797 INFO 67685 --- [pool-1-thread-1] p.a.service.impl.AccountReceiverImpl : 用戶側流水:hello :2017-04-25 17:44:37,當前線程:main