RabbitMq之二——简单使用(集成springboot)
上一篇我们大概介绍了一下关于linux版的rabbitmq的安装,那么这里我们简述一下rabbitMq的使用,当然,rabbitMq一共有六中模式,这里我们只简单的拿一种出来说一说,其他的demo我在下面附带一份别人写的博文,供大家参考!
一、添加依赖
任何第三方服务集成到springboot中我们都需要做的一件事就是添加相关的api依赖以供我们操作该服务,众所周知,spring为我们提供了很多关于操作第三方服务的Template,例如redisTemplate,jdbcTemplate,AmqpTemplate等等,在这里我们就使用spring为我们提供的AmqpTemplate ,依赖如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二、添加yml配置
我们在操作rabbit时必须要指定rabbit的ip和对外开放的端口,这里要注意的是,rabbit占用了很多端口,每个端口都有不同的作用,大致列举如下(默认):
4369 – erlang发现口
5672 – client端通信口
15672 – 管理界面ui端口
25672 – server间内部通信口
在这里,毫无疑问,我们通过java代码访问rabbit需要的是5672端口;配置非常简单,如下:
spring:
rabbitmq:
host: ip
username: 用户名
password: 密码
port: 端口
三、添加配置类
与其说成为配置类,倒不如说是利用springIOC管理的单例对象实在启动时创建的特性,创建队列和交换机以及声明绑定关系,代码如下:
package com.wwy.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* rabbitMq配置类
* @author wwy
* @date 2019年12月9日
* @version v0.0.1
*/
@Configuration
public class RabbitMqConfig {
// 队列名
public static final String QUEUE_NAME = "order-send";
public static final String QUEUE_NAME_2 = "order-send-2";
// 交换机名
public static final String QUEUE_EXCHANGE_NAME = "order-send-exchange";
/**
* 创建队列1
* @return
*/
@Bean
public Queue queue() {
// 是否持久化
boolean durable = true;
//仅创建者可以使用的私有队列,断开后自动删除
boolean exclusive = false;
// 当所有消费客户端连接断开后,是否自动删除队列
boolean autoDelete = false;
return new Queue(QUEUE_NAME, durable, exclusive,autoDelete);
}
/**
* 创建队列2
* @return
*/
@Bean("queue2")
public Queue queue2() {
// 是否持久化
boolean durable = true;
//仅创建者可以使用的私有队列,断开后自动删除
boolean exclusive = false;
// 当所有消费客户端连接断开后,是否自动删除队列
boolean autoDelete = false;
return new Queue(QUEUE_NAME_2, durable, exclusive,autoDelete);
}
/**
* 创建交换机
* @return
*/
@Bean
public TopicExchange exchange() {
// 是否持久化
boolean durable = true;
//当所有消费客户端连接断开后,是否自动删除队列
boolean autoDelete = false;
return new TopicExchange(QUEUE_EXCHANGE_NAME, durable, autoDelete);
}
/**
* 声明路由和队列1的绑定关系
* @param queue
* @param exchange
* @return
*/
@Bean
public Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);
}
/**
* 声明路由和队列2的绑定关系
*
* @param queue
* @param exchange
* @return
*/
@Bean
public Binding binding2(@Qualifier("queue2") Queue queue2, TopicExchange exchange) {
return BindingBuilder.bind(queue2).to(exchange).with(QUEUE_NAME_2);
}
}
四、发送端
package com.wwy.test.send;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wwy.config.RabbitMqConfig;
import com.wwy.entry.APIEntry;
import com.wwy.entry.Order;
import lombok.extern.slf4j.Slf4j;
/**
* rabbitMq发送端
* @author wwy
* @date 2019年12月11日
* @version v0.0.1
*
*/
@Service
@Slf4j
public class RabbitService {
@Autowired
private AmqpTemplate rabbitTemplate;
/**
* 发送消息
* @param order
* @return
*/
public APIEntry send(Order order) {
log.info("开始发送消息");
try {
rabbitTemplate.convertAndSend(RabbitMqConfig.QUEUE_NAME_2,order);
}catch(Exception e) {
log.error("发送失败"+e);
return APIEntry.ERROR("发送失败");
}
log.info("发送成功");
return APIEntry.OK(null);
}
}
我这里是使用的controller调用的,通过浏览器访问接口发送消息,controller的代码就不必贴出来了,太简单!
五、接收端
package com.wwy.test.serviceImpl;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import com.wwy.config.RabbitMqConfig;
import com.wwy.entry.Order;
import lombok.extern.slf4j.Slf4j;
/**
* rabbitMq接收端
* @author wwy
* @date 2019年12月11日
* @version v0.0.1
*
*/
@Slf4j
@Service
public class RabbitMqServcie {
/**
* 监听队列1消息
* @param message
*/
@RabbitListener(queues=RabbitMqConfig.QUEUE_NAME)
public void re(Order message) {
log.info("队列1接收成功");
log.info(message.toString());
}
/**
* 监听队列2消息
* @param message
*/
@RabbitListener(queues=RabbitMqConfig.QUEUE_NAME_2)
public void re2(Order message) {
log.info("队列2接收成功");
log.info(message.toString());
}
}
好了,接下来我们就可以启动项目,访问接口,发送消息了!当然,再实际项目使用中,我们接收端的代码不仅仅是打印日志,我们还需要将数据入库等操作。
注意:
1.使用rabbitMq发送的数据为对象时,该对象必须要实现序列化接口!
2.如果你的项目是微服务项目,发送端和接收端不在同一个服务当中,最好在发送端和接收端都声明队列,否则,如果先启动没有声明队列的服务就有可能报错
另外,我之前在网上看到的一篇博文,上面详细的介绍了各种模式的具体使用!连接如下:
https://www.cnblogs.com/linyufeng/p/9885645.html