06-RabbitMQ之SpringBoot使用

由于SpringBoot官方支持RabbitMQ,所以RabbitMQ与SpringBoot的集成是非常简单的。不过SpringBoot集成RabbitMQ的方式是按照Spring的一套统一的MQ模型创建的,因此SpringBoot集成插件中对于生产者、消息、消费者等重要的对象模型,与RabbitMQ原生的各个组件有对应关系,但是并不完全相同。
一、引入依赖

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

二、配置RabbitMQ的信息
在application.properties文件中配置以下信息

server.port=8080
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/mirror

三、声明队列
所有的exchange、queue、binding的配置,都需要以对象的方式声明并注入到spring容器中。默认情况下这些对象一经声明后,就会在RabbitMQ上绑定已有exchange等或新建exchange。
1、直连模式

/**
 * 直连模式只需要声明队列,所有消息都通过队列转发。
 */
@Configuration
public class DirectConfig {
	@Bean
	public Queue directQueue() {
		return new Queue("directqueue");
	}
}

2、Quorum队列

@Configuration
public class QuorumConfig {
    @Bean
    public Queue quorumQueue() {
        Map<String,Object> params = new HashMap<>();
        params.put("x-queue-type","quorum");
        return new Queue("quorumQueue",true,false,false,params);
    }
}

3、Stream队列

@Configuration
public class StreamConfig {
    @Bean
    public Queue streamQueue() {
        Map<String,Object> params = new HashMap<>();
        params.put("x-queue-type","stream");
        params.put("x-max-length-bytes", 20_000_000_000L);
        params.put("x-stream-max-segment-size-bytes", 100_000_000); 
        return new Queue("streamQueue",true,false,false,params);
    }
}

4、Fanout/Topic模式

/**
 * Fanout模式需要声明exchange,并绑定queue,由exchange负责转发到queue上。
 */
@Configuration
public class FanoutConfig {
	//声明队列
	@Bean
	public Queue fanoutQ1() {
		return new Queue("fanout.q1");
	}
	//声明exchange
	@Bean
	public FanoutExchange setFanoutExchange() {
		return new FanoutExchange("fanoutExchange");
	}
	//声明Binding,exchange与queue的绑定关系
	@Bean
	public Binding bindQ1() {
		return BindingBuilder.bind(fanoutQ1()).to(setFanoutExchange());
		//Topic模式需要加上routingKey
		//return BindingBuilder.bind(topicQ1()).to(setTopicExchange()).with("hunan.*");
	}
}

四、配置生产者
生产者的所有属性都已经在application.properties配置文件中进行配置。项目启动时会在Spring容器中初始化一个RabbitmqTemplate对象,所有的发送消息操作都通过这个对象来进行。

@RestController
public class ProducerController {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@GetMapping(value = "/directSend")
	public Object directSend(String message) throws AmqpException, UnsupportedEncodingException {
		//设置部分请求参数
		MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType("text/plain");
		messageProperties.setPriority(2);
		//设置消息转换器,如json
		rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
		//将对象转换成json再发送。
//		rabbitTemplate.convertandsend("",Object);
		//发消息
		rabbitTemplate.send("directqueue", new Message(message.getBytes("UTF-8"), messageProperties));
		return "message sended : " + message;
	}
}

五、配置消费者
消费者都是通过@RabbitListener注解来声明,注解中包含了声明消费者队列时所需要的重点参数。

@Component
public class DirectReceiver {
	@RabbitListener(queues = "directqueue")
	public void directReceive2(String message) {
		System.out.println("consumer2 received message : " + message);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值