目录
0、前言
需要已经安装RabbitMQ,并且启动、配置好用户。参考《基于CentOS6.5安装RabbitMQ》,《基于CentOS6.5使用RabbitMQ(二)》,《基于CentOS6.5使用RabbitMQ(三)》。
1、消息流程
生产者,消费者,消息
内部消息:Exchange,Binding,Queues
2、添加依赖
在pom.xml中添加rabbitmq的依赖:
<!-- 引入rabbitmq依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
3、添加配置
# rabbitmq配置
mq.rabbit.host=**.**.**.**
mq.rabbit.port=5672
mq.rabbit.virtualHost=/
mq.rabbit.username=admin
mq.rabbit.password=admin
4、新建配置类
RabbitMQConfig.java
/**
* @Auther: chisj chisj@foxmal.com
* @Date: 2018-11-12 15:35
* @Description:
*/
@Component
@Slf4j
@ConfigurationProperties
public class RabbitMQConfig {
@Value("${mq.rabbit.host}")
public String mqRabbitHost;
@Value("${mq.rabbit.port}")
public int mqRabbitPort;
@Value("${mq.rabbit.username}")
public String mqRabbitUserName;
@Value("${mq.rabbit.password}")
public String mqRabbitPassword;
@Value("${mq.rabbit.virtualHost}")
public String mqRabbitVirtualHost;
public static String EXCHANGE_NAME = "exchange-name";
public static String QUEUE_NAME = "queue-name";
public static String ROUTING_KEY = "routing-key";
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.mqRabbitHost,this.mqRabbitPort);
connectionFactory.setUsername(this.mqRabbitUserName);
connectionFactory.setPassword(this.mqRabbitPassword);
connectionFactory.setVirtualHost(this.mqRabbitVirtualHost);
connectionFactory.setPublisherConfirms(true);
return connectionFactory;
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
}
@Bean
public DirectExchange defaultExchange() {
return new DirectExchange(EXCHANGE_NAME);
}
@Bean
public Queue queue() {
return new Queue(QUEUE_NAME, true);
}
@Bean
public Binding binding() {
return BindingBuilder.bind(queue()).to(defaultExchange()).with(ROUTING_KEY);
}
@Bean
public SimpleMessageListenerContainer messageContainer() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory());
container.setQueues(queue());
container.setExposeListenerChannel(true);
container.setMaxConcurrentConsumers(1);
container.setConcurrentConsumers(1);
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
container.setMessageListener(new ChannelAwareMessageListener() {
public void onMessage(Message message, com.rabbitmq.client.Channel channel) throws Exception {
byte[] body = message.getBody();
log.info("消费端接收到消息 : " + new String(body));
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
});
return container;
}
}
5、新建生产者接口
ProductService.java
/**
* @Auther: chisj chisj@foxmal.com
* @Date: 2018-11-12 15:37
* @Description:
*/
public interface ProductService {
/**
* 保存
*/
public int save(String productName);
}
6、新建生产者实现类
ProductServiceImpl.java
/**
* @Auther: chisj chisj@foxmal.com
* @Date: 2018-11-12 15:39
* @Description:
*/
@Service("productServiceImpl")
@Slf4j
public class ProductServiceImpl implements ProductService, RabbitTemplate.ConfirmCallback {
private RabbitTemplate rabbitTemplate;
public ProductServiceImpl(RabbitTemplate rabbitTemplate){
this.rabbitTemplate=rabbitTemplate;
this.rabbitTemplate.setConfirmCallback(this);
}
/**
* 保存
*
* @param productName
*/
@Override
public int save(String productName) {
//执行保存
String uuid = UUID.randomUUID().toString();
CorrelationData correlationId = new CorrelationData(uuid);
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, RabbitMQConfig.ROUTING_KEY, productName, correlationId);
return 0;
}
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
log.info("消息id:" + correlationData);
if (ack) {
log.info("消息发送确认成功");
} else {
log.info("消息发送确认失败:" + cause);
}
}
}
7、新建生产者控制器类
ProductController.java
/**
* @Auther: chisj chisj@foxmal.com
* @Date: 2018-11-12 15:42
* @Description:
*/
@RestController
@RequestMapping("/Product")
public class ProductController {
@Resource
private ProductService productService;
@RequestMapping("/save")
public String save(String productName) {
productService.save(productName);
return "product save success.";
}
}
8、测试
在postman中请求{{url}}/Product/save这个端子
查看后台log