当前位置:   article > 正文

springboot 整合rabbit订阅广播消息_springboot rabbitmq 广播消息

springboot rabbitmq 广播消息

安装rabbitmq

docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 -d rabbitmq:3.9-management
  • 1
端口号说明
4369erlang发现端口
5672client端通信口
15672管理界面ui端口
25672server间内部通信口

springboot项目添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4

application.properties添加配置

#rabbitmq 服务地址
spring.rabbitmq.host=192.168.1.66
#rabbitmq client端口
spring.rabbitmq.port=5672
#登录账号
spring.rabbitmq.username=guest
#登录密码
spring.rabbitmq.password=guest
spring.rabbitmq.listener.direct.acknowledge-mode=manual
#虚拟服务
spring.rabbitmq.virtual-host=/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

添加消费监听类MQConsumer


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 消息队列消费
 */
@Slf4j
@Component
public class MQConsumer {

    /**
     * 监听
     */
    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "队列名",
                durable="false"), exchange = @Exchange(value = "交换机",
                durable="false",
                type= ExchangeTypes.TOPIC,
                ignoreDeclarationExceptions = "true"),
                key = "路由key"
            )
    })
    public void redirect(Message message) throws IOException {
        log.info("消息内容:{}",new String(message.getBody()));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/742533
推荐阅读
相关标签
  

闽ICP备14008679号