springboot使用rabbitmq的简单教程

1.在pom.xml引入rabbmq依赖,如下:

 2.在yml中配置rabbitmq基础数据

3.编写rabbitmq配置类

@Configuration
@Slf4j
 public  class RabbitConfig {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.rabbitmq.host}")
    private String host;

    @Value("${spring.rabbitmq.port}")
    private int port;

    @Value("${spring.rabbitmq.username}")
    private String username;

    @Value("${spring.rabbitmq.password}")
    private String password;

    @Value("${spring.rabbitmq.virtual-host:}")
    public String vhost;

    //直连交换机
    public static final String DIRECT_EXCHANGE = "direct_exchange";


    //队列
    public static final String BANKNET_QUEUE = "bankNet_queue";

    //唯一key值
    public static final String DIRECT_ROUTINGKEY = "direct_routingKey";


    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Queue DirectQueue() {
        //Durable:是否持久化(重启rabbitmq之后,队列是否还存在)
        return new Queue(BANKNET_QUEUE, true); //队列持久
    }

    @Bean
    public Binding directBinding() {
        //队列绑定交换机,with(RabbitConfig.ROUTINGKEY_A)路由key
        return BindingBuilder.bind(DirectQueue()).to(directExchange()).with(DIRECT_ROUTINGKEY);
    }

   
    @Bean
    @Primary
    public ConnectionFactory connectionFactory() {
        //连接工厂
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.host,this.port);
        connectionFactory.setUsername(this.username);
        connectionFactory.setPassword(this.password);
        //设置指定虚拟机:/为默认
        connectionFactory.setVirtualHost(vhost);
        //开启发送方消息确认机制!
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    //注入RabbitTemplate实例
    @Bean
    @Primary
    //@Scope注解是springIoc容器中的一个作用域,基本作用域singleton(单例)、prototype(多例)
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    //必须是prototype类型
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory());
        //设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if (ack) {
                    logger.info("消息发送成功:{}",correlationData.getId());
                } else {
                    logger.info("消息发送失败: {}" ,cause);
                }
            }
        });
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                logger.info("ReturnCallback:     "+"消息:"+returnedMessage.getMessage());
                logger.info("ReturnCallback:     "+"回应码:"+returnedMessage.getReplyCode());
                logger.info("ReturnCallback:     "+"回应信息:"+returnedMessage.getReplyText());
                logger.info("ReturnCallback:     "+"交换机:"+returnedMessage.getExchange());
                logger.info("ReturnCallback:     "+"路由键:"+returnedMessage.getRoutingKey());
            }
        });
        return rabbitTemplate;
    }

 4.业务引用rabbitmq

5.生产者发送消息

 6.消费者监听消息

 

 7.获取到的消息进行业务处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值