Springboot结合RabbitMQ

pom.xml

 <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

application.yaml

spring:
  rabbitmq:
    host: 127.0.0.1
    username: guest
    password: guest
    port: 5672
    virtual-host: test_vhost
    connection-timeout: 600000
    # 关闭自动ack,设置成手动ack
    listener:
      simple:
        acknowledge-mode: manual
        prefetch: 10
        concurrency: 2
        max-concurrency: 5

配置文件,也可以使用@PostConstruct

@Configuration
public class RabbitMQConfig {
	/**
	* 绑定
	*/
    public static final String EXCHANGE_NAME="springboot_topic_exchange";
    public static final String QUEUE_NAME="springboot_queue";

    @Bean("exchange")
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
    }

    @Bean("queue")
    public Queue queue(){
        return QueueBuilder.durable(QUEUE_NAME).build();
    }

    @Bean()
    public Binding bindQueueExchange(@Qualifier("exchange")Exchange exchange,
                                     @Qualifier("queue")Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
    }
	
   /*
	* Fanout 模式
	*/
	public static final String EXCHANGE_NAME2="my_topic_exchange";
    public static final String QUEUE_NAME2="my_queue";
    //创建交换机
    @Bean
    public FanoutExchange fannoutExchange(){
        return new FanoutExchange(EXCHANGE_NAME2,true,false);
    }

    //创建队列
    @Bean
    public Queue fannoutQueue(){
        return new Queue(QUEUE_NAME2,true,false,false);
    }
    //声明绑定关系
    @Bean
    public Binding bindQueue(@Qualifier("exchange")Exchange exchange,
                                     @Qualifier("queue")Queue queue){
        return BindingBuilder.bind(fannoutQueue()).to(fannoutExchange());
    }
    
   /*
	* topic 模式
	*/
	@Bean
    public Queue topicQueue(){
        return new Queue(QUEUE_NAME,true,false,false);
    }
    //创建交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(EXCHANGE_NAME,true,false);
    }
    //声明绑定关系
    @Bean
    public Binding bindTopicQueue(){
        return BindingBuilder.bind(topicQueue()).to(topicExchange()).with("yuyang.#");
    }


消费者代码

@Component
@Slf4j
public class MessageListener  {
	@RabbitListener(queues="springboot_queue")
    public void process(Message message) {
        log.info("helloWorld模式 received message : {}",new String(message.getBody()));
    }


}

生产者代码

	@Autowired
    private RabbitTemplate rabbitTemplate;

    // pub/sub 发布订阅模式   交换机类型 fanout
    @GetMapping("/fanoutSend")
    public String fanoutSend() throws AmqpException, UnsupportedEncodingException {
        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue
        rabbitMQService.sendMessage("my_topic_exchange","*","fanoutSend");
        return "message sended : "+message;
    }

	//topic 工作模式   交换机类型 topic
    @GetMapping("/topicSend")
    public String topicSend() throws AmqpException, UnsupportedEncodingException {
        //fanout模式只往exchange里发送消息。分发到exchange下的所有queue
        rabbitTemplate.convertAndSend("spring_topic_exchange","yuyang.test","sadedf");
        return "ok";
    }

手动ack

	@RabbitListener(queues = "springboot_queue")
    public void listenerQueue(Message message, Channel channel) throws IOException {
         log.info(new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值