springboot使用rabbitmq的简单教程

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

springboot使用rabbitmq的简单教程_第1张图片

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

springboot使用rabbitmq的简单教程_第2张图片

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

springboot使用rabbitmq的简单教程_第3张图片

5.生产者发送消息

springboot使用rabbitmq的简单教程_第4张图片

 6.消费者监听消息

 

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

你可能感兴趣的:(java-rabbitmq,rabbitmq,spring,boot)