springboot 集成 RabbitMQ

SpringBoot 集成RabbitMQ 其实非常简单,具体步骤如下:

1. pom.xml 引入依赖

<dependency>
		 <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2 编写配置文件 (我这里只写一种默认的方式direct 其余的方式类同)

## application.yml 配置 
spring:
 rabbitmq:
  host: 154.8.141.51
  port: 5672
  username: root
  password: root

### 配置队列 Queue
@Configuration
public class RabbitConfig {
	@Bean
	public Queue Queue() {
		return new Queue("demo");
	}
}

3 编写消息提供方

	@Autowired
    private AmqpTemplate rabbitTemplate;
    
	/**
	 * 发送消息
	 * @author zhangh
	 * @date 2018年11月6日下午4:15:22
	 * @param message
	 * @throws IOException
	 * @throws TimeoutException
	 */
	@ApiOperation(value = "MQ 发送接口", notes = "发送消息")
	@RequestMapping("/sendMQ")
	@ResponseBody
	public void sendMessage(String message) throws IOException, TimeoutException {
        rabbitTemplate.convertAndSend("demo","hello " + new Date()+ message);
	}

4 编写消息消费方

@Component
@RabbitListener(queues = "demo")
public class ReceiveMQ {
	
	@RabbitHandler
	public void receive(String str) {
		System.out.println("Receiver  : " + str);
	}
}

5 .启动项目

先执行消息提供方   然后再控制台可以看到有消息输出

输出

以上就是SpringBoot 集成RabbitMQ 的完整步骤,如有疑问请留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值