1.安装参考:https://blog.youkuaiyun.com/weixin_41004350/article/details/83046842
注意:RabbiMq 和 Erlang 的版本兼容,官网:https://www.rabbitmq.com/which-erlang.html
2.SpringBoot整合
springBoot和rabbitMQ已经有很好的的集成了,使用amqp
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
yml配置
spring:
application:
name: RabbitMQ_Demo
rabbitmq:
host: 111.xxx.xxx.175
port: 5672
username: guest
password: guest
创建消息队列(名称为hello的queue)
// import org.springframework.amqp.core.Queue;
@Bean
public Queue helloQueue(){
return new Queue("hello");
}
配置消息消费者
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @author jie
* @date 2020/7/22 15:00
*/
@Component
@RabbitListener(queues = "hello")
public class RabbitMQConsumer {
@RabbitHandler
public void onMessage(String hello){
System.out.println("收到消息 :"+hello);
}
}
使用AmqpTemplate 向hello队列发送消息
@RequestMapping("api/v1/pro/proFunds")
public class ProFundsRest {
@Autowired
private AmqpTemplate amqpTemplate;
@GetMapping("send/{msg}")
public InvokeResult send(@PathVariable("msg") String msg) {
amqpTemplate.convertAndSend("hello",msg);
return InvokeResult.success();
}
}