spring boot集成Rabbit MQ

1. pom.xml

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

2. application.properties

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

3. RabbitConfig.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
	
	private static final Logger log = LoggerFactory.getLogger(RabbitConfig.class);

	@Bean
	public Queue newsQueue() {
		return new Queue("newsQueue");
	}
	
	@Bean
    public Queue news1Queue() {
        return new Queue("news1");
    }

    @Bean
    public Queue news2Queue() {
        return new Queue("news2");
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("newsTopic");
    }

    @Bean
    public Binding bindingNews1Queue(Queue news1Queue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(news1Queue).to(fanoutExchange);
    }

    @Bean
    public Binding bindingNews2Queue(Queue news2Queue, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(news2Queue).to(fanoutExchange);
    }
	
}

4. 发送消息

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Producer {

	private static final Logger log = LoggerFactory.getLogger(Producer.class);
	
	@Autowired
	private AmqpTemplate rabbitTemplate;
	
	public void sendNewsQueue(String msg) {
		rabbitTemplate.convertAndSend("newsQueue", msg);
	}
	
	// 参数可以直接传对象
	public void sendStockTopic(String msg) {
		rabbitTemplate.convertAndSend("newsTopic", "", msg);
	}

}

5. 接收消息

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class News1Consumer {

	private static final Logger log = LoggerFactory.getLogger(News1Consumer.class);
	
	public static long count = 0;
	
	@RabbitHandler
	@RabbitListener(queues = "news1", containerFactory = "rabbitListenerContainerFactory")
    public void process(String msg) {
		log.info("msg:" + msg);
    }
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值