springboot2.0集成rabbitmq
最近需要用上rabbitmq,項目是用springboot搭建的颜启,所以就要在springboot環(huán)境下單獨集成rabbitmq,寫這篇文章用來記錄下。不多說了,進入正題晌该。
?1.首先引入Maven依賴:spring-boot-starter-amqp
<dependency>
? ? ? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? ? ? <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
? 2.application.yml中引入配置項:
spring:
????? rabbitmq:
? ? ? ? ? ? host: 127.0.0.1
? ? ? ? ? ? port: 5672
? ? ? ? ? ? username: root
? ? ? ? ? ? password: root
? ? ? ? ? ? virtual-host: vhost_one
? ? ? ? ? ?publisher-confirms: true? #開啟發(fā)送確認
? ? ? ? ? ?publisher-returns: true? ?#開啟發(fā)送失敗退回
3.生產者:
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.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
public class RabbitMqConfig {
@Bean
public RabbitTemplate setRabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate();
rabbitTemplate.setConnectionFactory(connectionFactory);
rabbitTemplate.setExchange("topicExchange");
// 設置消息內容序列化方式
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
return rabbitTemplate;
}
/**
* 生成主題路由器
* @return
*/
@Bean("topicExchange")
public TopicExchange topicExchange() {
return new TopicExchange("topicExchange", true, false);
}
/**
* 定義發(fā)送短信的隊列
* @return
*/
@Bean("sendSMSQueue")
public Queue sendSMSQueue() {
return new Queue("sendSMS", true, false, false);
}
/**
* 創(chuàng)建發(fā)送短信的隊列和主題交換機的綁定關系
* @param queue
* @param exchange
* @return
*/
@Bean
public Binding bingExchangeANDqueue(@Qualifier("sendSMSQueue")Queue queue, @Qualifier("topicExchange")TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("common.sendSMS.#");
}
}
? ? ? 上面配置好了RabbitTemplate和路由和隊列以及綁定關系,在項目中直接調用RabbitTemplate的發(fā)送消息的方法即可:
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void send() {
rabbitTemplate.convertAndSend("common.sendSMS.msg", "rabbitmq發(fā)送短信1給你了");
}
4.消費者:
? ? application.yml中引入配置項和生產者一樣:
spring:
? rabbitmq:
? ? ? host: 127.0.0.1
? ? ? port: 5672
? ? ? username: root
? ? ? password: root
? ? ? virtual-host: vhost_one
? ? ? ? 同時也需要設置RabbitConfiguration來設置序列化的方式您机,就用spring提供的Jackson2JsonMessageConverter類來方序列化和序列化,RabbitConfiguration類如下:
? ? import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
? ? import org.springframework.amqp.rabbit.connection.ConnectionFactory;
? ? import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
? ? import? org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
? ? import org.springframework.context.annotation.Bean;
? ? import org.springframework.context.annotation.Configuration;
? ? @Configuration
? ? public class RabbitConfiguration {
? ? @Bean
? ? public SimpleRabbitListenerContainerFactory myFactory(
? ? ? ? ? ?SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
? ? ? ? ? ? ? ?SimpleRabbitListenerContainerFactory factory =
? ? ? ? ? ? ? new SimpleRabbitListenerContainerFactory();
? ? ? ? ? ? ? ?configurer.configure(factory, connectionFactory);
? ? ? ? ? ? ? ?factory.setMessageConverter(new Jackson2JsonMessageConverter());
? ? ? ? ? ? ? ?return factory;
? ? ? ? ? ?}
? ? }
? 調用示例:
? ? import org.springframework.amqp.rabbit.annotation.EnableRabbit;
? ? import org.springframework.amqp.rabbit.annotation.RabbitListener;
? ? import org.springframework.stereotype.Component;
? ? @Component
? ? @EnableRabbit
? ? public class Consumer {
? ? ? ? @RabbitListener(queues = "sendSMS", containerFactory = "myFactory")
? ? ? ? public void sendMsg(String content) {
? ? ? ? ? ? ? ? ? ? ?System.out.println(content);
? ? ? ? ?}
? ? }