Docker部署RabbitMQ及整合Spring Boot示例
在docker中安装RabbitMQ,一行搞定
docker run -d --name myactivemq -p 61616:61616 -p 8161:8161 webcenter/activemq
安装完成后,打开RabbitMQ管理系统
http://ip:15672
创建示例程序
打开idea,使用spring initialzr快速创建Spring boot项目,并打开pom,集成RabbitMQ
<!--rabbitMq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
修改application.properties 添加以下配置
spring.rabbitmq.host=ip
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.listener.concurrency=10
spring.rabbitmq.listener.max-concurrency=20
spring.rabbitmq.listener.prefetch=5
创建SenderConf类
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Created by ccwant on 2019-04-04.
*/
@Configuration
public class SenderConf {
@Bean(name="Amessage")
public Queue AMessage() {
return new Queue("fanout.A",true);//true表示持久化该队列
}
@Bean(name="Bmessage")
public Queue BMessage() {
return new Queue("fanout.B",true);
}
@Bean(name="Cmessage")
public Queue CMessage() {
return new Queue("fanout.C",true);
}
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("topicExchange");//配置广播路由器
}
@Bean
public Binding bindingExchangeA(@Qualifier("Amessage") Queue AMessage,TopicExchange topicExchange) {
return BindingBuilder.bind(AMessage).to(topicExchange).with("123");
}
@Bean
public Binding bindingExchangeB(@Qualifier("Bmessage") Queue BMessage, TopicExchange topicExchange) {
return BindingBuilder.bind(BMessage).to(topicExchange).with("456");
}
@Bean
public Binding bindingExchangeC(@Qualifier("Cmessage") Queue CMessage, TopicExchange topicExchange) {
return BindingBuilder.bind(CMessage).to(topicExchange).with("789");
}
}
创建HelloReceive类
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Created by ccwant on 2019-04-04.
*/
@Component
public class HelloReceive {
@RabbitListener(queues="fanout.A")
public void processA(String str1) {
System.out.println("ReceiveA:"+str1);
}
@RabbitListener(queues="fanout.B")
public void processB(String str) {
System.out.println("ReceiveB:"+str);
}
@RabbitListener(queues="fanout.C")
public void processC(String str) {
System.out.println("ReceiveC:"+str);
}
}
创建HelloSender类
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Created by ccwant on 2019-04-04.
*/
@Component
public class HelloSender {
@Autowired
private AmqpTemplate template;
public void send() {
System.out.println("发送消息");
template.convertAndSend("topicExchange","123","hello,rabbit~ 你好");
}
}
最后写一个单元测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RabbitMqDemoApplication.class)
public class RabbitMqDemoApplicationTests {
@Test
public void contextLoads() {
}
@Autowired
private HelloSender helloSender;
@Test
public void testRabbit() {
helloSender.send();
}
}
先运行Application,然后再运行单元测试发送消息
到此RabbitMQ已经跑通了