springboot篇】十九. springboot整合RabbitMQ

中国加油,武汉加油!

篇幅较长,配合目录观看

项目准备
  1. 新建springboot项目nz1904-springboot-10-rabbitmq
  2. 导入lombox和web依赖

1. 案例

1.1 导包

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

1.2 application.properties

#给应用取名字
spring.application.name=springboot-rabbitmq
#ip地址
spring.rabbitmq.host=
#端口
spring.rabbitmq.port=5672
#用户名
spring.rabbitmq.username=
#密码
spring.rabbitmq.password=
#开启消息确认机制
spring.rabbitmq.publisher-confirms=true
#开始return机制
spring.rabbitmq.publisher-returns=true
#整个虚拟机
spring.rabbitmq.virtual-host=/

1.3 定义AppConfig和RabbitMQConfig

package com.wpj.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * 程序配置
 */
@SpringBootConfiguration
@ComponentScan(basePackages = {"com.wpj"})
public class AppConfig {
}
package com.wpj.config;

import org.springframework.amqp.core.Queue;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

@SpringBootConfiguration
public class RabbitMQConfig {

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

}

1.4 写业务下沉

package com.wpj.manager;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * rabbitmq访问队列的方法
 */
@Component
public class RabbitMQManager {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送消息到工作模型中
     */
    public void sendWork(){
        amqpTemplate.convertAndSend("queueWork","我是工作模型测试的值");
    }
}

1.5 写Service及其impl

package com.wpj.service;

public interface IRabbitMQService {
    void sendMsg();
}
package com.wpj.service.impl;

import com.wpj.manager.RabbitMQManager;
import com.wpj.service.IRabbitMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitMQServiceImpl implements IRabbitMQService {

    @Autowired
    private RabbitMQManager rabbitMQManager;

    @Override
    public void sendMsg() {
        rabbitMQManager.sendWork();
    }

}

1.6 写Controller

package com.wpj.controller;

import com.wpj.service.IRabbitMQService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitMQController {

    @Autowired
    private IRabbitMQService rabbitMQService;

    /**
     * 测试工作模型的发消息
     * @return
     */
    @RequestMapping("sendMsg")
    public Object sendMsg(){
        rabbitMQService.sendMsg();
        return "发送成功....";
    }
}

1.7 写消费者

package com.wpj.listener;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
@RabbitListener(queues = {"queueWork"})
public class WorkReceiveListener {
    @RabbitHandler
    public void receiveMessage(String message){
        System.out.println("收到消息了:"+message);
    }
}

1.8 启动程序入口

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. 扩展

2.1 手动确认

spring.rabbitmq.listener.direct.acknowledge-mode=manual
channel.basicAck(message2.getMessageProperties().getDeliveryTag(), false);
channel.basicNack(); // 重回队列

2.2 确认机制

    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    rabbitTemplate.setConfirmCallback(confirmCallback);
    
    /**
     * Confirm机制
     */
    final RabbitTemplate.ConfirmCallback confirmCallback=new RabbitTemplate.ConfirmCallback(){
        @Override
        public void confirm(CorrelationData correlationData, boolean b, String s) {
            System.out.println("确认机制........");
        }
    };
spring.rabbitmq.publisher-confirms=true

2.3 死信队列

new Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, @Nullable Map<String, Object> arguments)

2.4 削流

#消费者最小数量
spring.rabbitmq.listener.simple.concurrency=1
#消费之最大数量
spring.rabbitmq.listener.simple.max-concurrency=10

2.5 return机制

    @Autowired
    private RabbitTemplate rabbitTemplate;
    
    rabbitTemplate.setMandatory(true);
    
       /**
     * return机制
     */
    RabbitTemplate.ReturnCallback returnCallback=new RabbitTemplate.ReturnCallback(){
        @Override
        public void returnedMessage(Message message, int i, String s, String s1, String s2) {
            System.out.println("return机制收到消息了.....");
        }
    };
spring.rabbitmq.publisher-returns=true

3. 发布订阅模式

3.1 RabbitMQConfig

//首先得申明两个队列
@Bean
public Queue queueFanout1(){
    return new Queue("queueFanout1");
}
@Bean
public Queue queueFanout2(){
    return new Queue("queueFanout2");
}
//准备一个交换机
@Bean
public FanoutExchange fanoutExchange1(){
    return new FanoutExchange("fanoutExchange1");
}
//将队列绑定到交换机
@Bean
public Binding bindingExchange1(Queue queueFanout1, FanoutExchange fanoutExchange1){
    return BindingBuilder.bind(queueFanout1).to(fanoutExchange1);
}
@Bean
public Binding bindingExchange2(Queue queueFanout2,FanoutExchange fanoutExchange1){
    return BindingBuilder.bind(queueFanout2).to(fanoutExchange1);
}

3.2 RabbitManager

/**
 * 向发布订阅模式里面发送消息
 */
public void sendPublishWork(){
    rabbitTemplate.convertSendAndReceive("fanoutExchange1","","发布订阅模式的值");
}

3.2 service及其impl

void sendPublishWork();
@Override
public void sendPublishWork() {
    rabbitMQManager.sendPublishWork();
}

3.4 PublishReceiveListener

package com.wpj.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;


@Component
public class PublishReceiveListener {

    @RabbitListener(queues = {"queueFanout1"})
    public void receiveMessage(String str, Channel channel, Message message2){
        System.out.println("queueFanout1收到消息了:"+str);

    }

    @RabbitListener(queues = {"queueFanout2"})
    public void receiveMessage1(String str, Channel channel, Message message2){
        System.out.println("queueFanout2收到消息了:"+str);
    }

}

3.5 RabbitMQController

/**
 * 向发布订阅模式 发送值
 * @return
 */
@RequestMapping("sendPublishData")
public Object sendPublishData(){
    rabbitMQService.sendPublishWork();
    return "发送成功....";
}

4. 发布路由模式

3.1 RabbitMQConfig

//首先要准备两个队列
@Bean
public Queue queueRouting1(){
    return new Queue("queueRouting1");
}
@Bean
public Queue queueRouting2(){
    return new Queue("queueRouting2");
}
//准备一个交换机
@Bean
public DirectExchange directExchange1(){
    return new DirectExchange("directExchange1");
}
//接下来进行绑定
@Bean
public Binding bindingDirectExchange1(Queue queueRouting1,DirectExchange directExchange1){
    return BindingBuilder.bind(queueRouting1).to(directExchange1).with("jiekami");
}
@Bean
public Binding bindingDirectExchange2(Queue queueRouting2,DirectExchange directExchange1){
    return BindingBuilder.bind(queueRouting2).to(directExchange1).with("wpj");
}

3.2 RabbitManager

/**
 * 向路由模型里面发送请求
 */
public void sendDirectExchange(){
    rabbitTemplate.convertSendAndReceive("directExchange1","wpj","路由模型的值");
}

3.3 service及其impl

void sendDirectExchange();
@Override
public void sendDirectExchange() {
    rabbitMQManager.sendDirectExchange();
}

3.4 DirectReceiveListener

package com.wpj.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class DirectReceiveListener {

    @RabbitListener(queues = {"queueRouting1"})
    public void receiveMessage(String str, Channel channel, Message message2){
        System.out.println("queueRouting1收到消息了:"+str);

    }

    @RabbitListener(queues = {"queueRouting2"})
    public void receiveMessage1(String str, Channel channel, Message message2){
        System.out.println("queueRouting2收到消息了:"+str);
    }

}

3.5 RabbitMQController

/**
 * 向发布订阅模式 发送值
 * @return
 */
@RequestMapping("sendDirectData")
public Object sendDirectData(){
    rabbitMQService.sendDirectExchange();
    return "发送成功....";
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值