1 客户端api的使用
1.1 依赖引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1.2 在配置类中初始化queue exchange binding
@Bean(name = "mac")
public Queue queue() {
Queue macQueue = new Queue("topic.mac");
return macQueue;
}
@Bean()
public TopicExchange topicExchange() {
TopicExchange topicExchange = new TopicExchange("topic");
return topicExchange;
}
@Bean
Binding bindingExchange(@Qualifier("mac") Queue queue, TopicExchange topicExchange) {
Binding binding = BindingBuilder.bind(queue).to(topicExchange).with("topic.*");
return binding;
}
如果没有显示的声明,spring会自动的声明配置的队列,交换器和绑定队列和交换器,不需要自己实例化rabbitAdmin去声明1.3 设置消息推送工具
1.3 设置消息推送工具
@Repository
public class RabbitRepository implements MessageQueue {
@Autowired
RabbitTemplate rabbitTemplate;
@Override
public void write(List<String> messages) {
for (String msg : messages) {
rabbitTemplate.convertAndSend("topic", "topic.mac", msg);
}
}
}
2 rabbitmq management ui的使用
management ui 工具需要安装rabbitmq后,启动插件,就可以使用。
该工具可以对rabbitmq的一系列东西进行查看,比如queue,connection,channel,exchange等。它可以手动的创建queue,exchange。我们主要是看自己的业务程序中声明的queue是否创建成功,exchange是否创建成功,还有对应的队列和exchange是否绑定上。如图1可以看到我们的队列topic.mac已经创建成功
如图2可以看到我们交换器topic已经创建成功
假如我们想要查看业务程序是否成功推动消息到rabbitmq,我们可以在ui工具的queue中查看,首先找到我们消息被写到的队列,然后点击队列名,跳到新的操作界面,里面有一个get messages可以查看rabbitmq中的消息,如图4所示