一、安装Rabbit
备注:Erlang与RabbitMQ版本对照 http://www.rabbitmq.com/which-erlang.html
1、安装Erlang环境,下载安装包,网址:http://erlang.org/download/,如我下载的版本是:otp_win64_21.0.exe
安装比较简单,双击安装包,然后一直点击下一步,需要权限提示点击是即可。
2、安装RabbitMQ,下载安装包,网站:http://www.rabbitmq.com/install-windows.html,如我下载的版本是:rabbitmq-server-3.7.7.exe
安装比较简单,双击安装包,然后一直点击下一步,需要权限提示点击是即可。
3、安装RabbitMQ管理界面
a、以管理员身份打开cmd,进入RabbitMQ安装目录下sbin文件,如我的安装目录为:D:\Work\RabbitMQ Server\rabbitmq_server-3.7.7\sbin
b、在sbin目录下,执行命令,安装管理插件
rabbitmq-plugins.bat enable rabbitmq_management
c、在sbin目录下,执行命令,重启RabbitMQ服务(注意:如果不以管理员运行cmd则会报拒绝访问错误!)
net stop RabbitMQ && net start RabbitMQ
d、打开浏览器,输入网址http://localhost:15672/,输入用户名和密码:guest/guest,进入管理界面则代表安装成功。
二、整合
1.依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.配置文件:
spring.application.name=Spring-boot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3.队列配置
@Configuration
public class RabbitConfig {
@Bean
public Queue queue(){
return new Queue("hello");
}
}
4. 生产者:
rabbitTemplate是springboot 提供的默认实现
@Component
public class HelloSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public class HelloSender {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String string = new Date().toString();
System.out.println("Sender : " + string);
this.rabbitTemplate.convertAndSend("hello", string);
}
public void sendData(Object object) {
System.out.println("Sender : " + object.toString());
this.rabbitTemplate.convertAndSend("hello", object);
}
}
}
5. 消费者
@Component
@RabbitListener(queues = "hello")
public class HelloReceiver {
@RabbitHandler
public void process(Object object){
System.out.println("消费者1 : " + object);
}
}
@Component
@RabbitListener(queues = "hello")
public class HelloReceiver2 {
@RabbitHandler
public void process(Object object){
System.err.println("消费者2 : " + object);
}
}
6.交换机和路由绑定
@Configuration
public class TopicRabbitConfig {
final static String message = "topic.message";
final static String messages = "topic.messages";
@Bean
public Queue queueMessage(){
return new Queue(TopicRabbitConfig.message);
}
@Bean
public Queue queueMessages(){
return new Queue(TopicRabbitConfig.messages);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("exchange");
}
@Bean
Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange){
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
}
@Bean
Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}
}
7. 测试:
@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMqHelloTest {
@Autowired
private HelloSender helloSender;
@Test
public void hello() throws Exception{
helloSender.send();
}
@Test
public void oneToMany(){
for (int i=0;i<10;i++){
helloSender.sendData(i);
}
}
}
运行oneToMany(),可见两个消费者均匀地消费了10条消息。
原文链接:https://www.cnblogs.com/moy25/p/8448179.html,
http://www.ityouknow.com/springboot/2016/11/30/spring-boot-rabbitMQ.html