【实战】springboot引入rabbitMQ,解决问题

本文档详细介绍了如何在SpringBoot应用中集成并使用RabbitMQ,包括添加依赖、配置连接、设置交换机和队列,以及在生产者和消费者服务中调用和配置。特别指出,配置公共common服务会导致消费者启动失败,需避免此类问题。
摘要由CSDN通过智能技术生成

具体rabbitMQ的使用以及下载安装,详见上篇帖子

这里想把生产者和消费者各自建立一个服务

步骤

①pom中添加依赖

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

②配置文件新增链接rabbitmq的配置

生产者:

server:
  port: 6001

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

  application:
    name: producer

消费者:

server:
  port: 6000

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

  application:
    name: consumer

③配置config文件

生产者和消费者各自配置config文件(这两个文件内容是相同的),或者写一个公共common服务,把配置文件放进去:

PS:不能写公共common.否则consumer启动会报错,缺少那个监听队列的注入。别问我为啥这么

清楚,问就是踩过。

配置了四种类型的交换机,并配置了队列

package com.hyj.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    //配置direct交换机
    @Bean
    public DirectExchange directExchange() {

        return new DirectExchange("directExchange");
    }

    //配置direct队列
    @Bean
    public Queue directQueue() {
        return new Queue("directQueue");
    }

    //将direct队列绑定到交换机上
    @Bean
    public Binding directBinding(Queue directQueue, DirectExchange directExchange) {

        // 参数 1 为需要绑定的队列
        // 参数 2 为需要绑定的交换机
        // 参数 3绑定时的RoutingKey
        return BindingBuilder.bind(directQueue).to(directExchange).with("directRouting");
    }


    //配置Fanout交换机
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    //配置Topic交换机
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

}

绑定多个队列用法

package com.gwmfc.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    @Autowired
    private AppConfig appConfig;

    @Bean
    public DirectExchange directExchange() {

        return new DirectExchange(appConfig.exchangeName, true, false);
    }

    @Bean
    public Queue directQueue01() {
        return new Queue(appConfig.callbackQueueName);
    }

    @Bean
    public Queue directQueue02() {
        return new Queue(appConfig.thumbQueueName);
    }

    @Bean
    public Queue directQueue03() {
        return new Queue(appConfig.fastDfsQueueName);
    }

    //callback队列绑定到交换机,001
    @Bean
    public Binding directBinding01() {

        return BindingBuilder.bind(directQueue01()).to(directExchange()).with(appConfig.callbackKey);
    }


    //thumbPic队列绑定到交换机,002
    @Bean
    public Binding directBinding02() {
        return BindingBuilder.bind(directQueue02()).to(directExchange()).with(appConfig.thumbKey);
    }


    //fastDfs队列绑定到交换机,003
    @Bean
    public Binding directBinding03() {
        return BindingBuilder.bind(directQueue03()).to(directExchange()).with(appConfig.fastDfsKey);
    }
}

④调用

生产者:

SpringBoot提供了AmqpTemplate来操作RabbitMQ

@RestController
@Slf4j
public class TestController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @GetMapping(value = "/setMsg")
    public void SetMsg(@RequestParam("msg") String msg) {
        amqpTemplate.convertAndSend("directExchange", "directRouting", msg);
        log.info("生产消息:{}", msg);
    }
}

消费者:

@Service
@Slf4j
public class MsgService {
    @RabbitListener(queues = "directQueue")
    public void GetMsg(String msg){
        log.info("消费消息:{}",msg);
    }
}

⑤结果

完成。

@RabbitListener注解:

当RabbitListener注解在方法上时,对应的方式就是Rabbit消息的监听器。

当RabbitListener注解在类上时,和RabbitHandle注解配合使用,可以实现不同类型的消息的分

发,类中被RabbitHandle注解的方法就是Rabbit消息的监听器。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值