springboot2 整合 rabbitmq3.8

一、简配版

导入Spring for RabbitMQ

项目结构

application.yml

spring:
    rabbitmq:
        addresses: amqp://admin:admin@192.168.0.201:5672

address,以逗号分隔的客户端应连接到的地址列表。admin:admin是用户名密码。

整合rabbitmq还有哪些属性,请参阅 RabbitProperties

RabbiMQConfig.java

package com.xin.xpfs.springbootrabbitmqsingle.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
//@EnableRabbit
public class RabbiMQConfig {

    @Bean
    public Queue myQueue() {
        return new Queue("myQueue");
    }
}

在Config类里创建了Queue,没有创建交换器,使用默认交换器

Exchange: (AMQP default) 默认交换隐式绑定到每个队列,路由密钥等于队列名称。无法显式绑定到默认exchange或从默认exchange解除绑定。也不能删除。

在消费者@RabbitListener注解里,也可以创建交换器、队列、绑定。

RabbiMQConfig.java 创建了交换器和绑定的

package com.xin.xpfs.springbootrabbitmqsingle.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
//@EnableRabbit
public class RabbiMQConfig {

    @Bean
    public Queue myQueue() {
        return new Queue("myQueue", true);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("myDirectExchange", true, false);
    }

    @Bean
    public Binding binding(Queue myQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(myQueue).to(directExchange).with("myQueue");
    }

}

ProducerController.java

package com.xin.xpfs.springbootrabbitmqsingle.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/send")
    public String send() {
        String content = UUID.randomUUID().toString();
        rabbitTemplate.convertAndSend("myQueue",content);
        return content;
    }
}

MyConsumer.java

package com.xin.xpfs.springbootrabbitmqsingle.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MyConsumer {

    @RabbitListener(queues = "myQueue")
    public void onMessage(String content) {
        log.info("收到消息:{}", content);
    }

}

浏览器访问:http://localhost:8080/send

控制台输出:收到消息:dd799baa-d805-42dd-a7a3-fa854240a0be

RabbitMQ配置属性表

Spring Cloud中RabbitMQ配置属性表

https://blog.csdn.net/en_joker/article/details/80103519

Spring Boot + RabbitMQ 配置参数解释

https://www.cnblogs.com/qts-hope/p/11242559.html

参考:

spring boot 2.x 系列 —— spring boot 整合 RabbitMQ

https://blog.csdn.net/m0_37809146/article/details/86680920

文档:

AMQP中文文档

https://docshome.gitbooks.io/springboot/content/pages/spring-boot-features.html#boot-features-amqp

AMQP英文文档

https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/htmlsingle/#boot-features-amqp

spring-amqp 英文

https://docs.spring.io/spring-amqp/docs/2.2.5.RELEASE/reference/html/#amqp

Spring AMQP 中文文档

https://www.docs4dev.com/docs/zh/spring-amqp/2.1.2.RELEASE/reference

其他:

RabbitMQ 核心概念
RabbitMQ 客户端开发
基于 HAProxy + KeepAlived 搭建 RabbitMQ 高可用集群
spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值