Springboot结合RabbitMQ

pom.xml

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

application.yaml

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

java 复制代码
@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.#");
    }

消费者代码

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


}

生产者代码

java 复制代码
	@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

java 复制代码
	@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);
    }
相关推荐
、我是男生。8 分钟前
钨粉与小烛树蜡的熔融实验
rabbitmq
计算机毕设指导61 小时前
基于微信小程序的积分制零食自选平台【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
QK芒果小洛1 小时前
Springboot 接口校验数组中的对象的方式
java·spring boot·后端
qq_12498707531 小时前
基于微信小程序的民宿预订系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·毕业设计
GEM的左耳返2 小时前
互联网大厂Java求职面试题解析与实战指导:涵盖核心技术栈与业务场景
java·数据库·spring boot·安全·微服务·消息队列·面试题
Percep_gan2 小时前
Linux中安装rabbitmq,很详细
linux·运维·rabbitmq
Wang's Blog2 小时前
RabbitMQ:消息可靠性保障之消费端 ACK 机制与限流策略解析
分布式·rabbitmq
憧憬少2 小时前
通过切换Service实现类来切换看板数据来源
java·spring boot
千寻技术帮2 小时前
10400_基于Springboot的职业教育管理系统
java·spring boot·后端·毕设·文档·职业教育
武子康2 小时前
Java-194 RabbitMQ 分布式通信怎么选:SOA/Dubbo、微服务 OpenFeign、同步重试与 MQ 异步可靠性落地
大数据·分布式·微服务·消息队列·rabbitmq·dubbo·异步