SpringBoot整合RabbitMQ

1.Hello World模型

1.导包

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

2.编写properties文件

#ip地址
spring.rabbitmq.host=114.55.219.117
#端口
spring.rabbitmq.port=5672
#用户名
spring.rabbitmq.username=xbb
#密码
spring.rabbitmq.password=123456

3.编写配置文件

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

4.controller

/**
     * hello world模型
     * @return
     */
    @RequestMapping("sendMsg")
    public Object sendMsg(){
        rabbitMQService.sendMsg();
        return "发送成功";
    }

5.service

@Service
public class RabbitMQService implements IRabbitMQService {

    @Autowired
    private RabbitMQManager rabbitMQManager;

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

6.编写manager

@Component
public class RabbitMQManager {
    
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送消息到工作模型
     */
    public void sendWork(){
        amqpTemplate.convertAndSend("queueWork","hello world");
    }
}

7.编写消费者

@Component
public class WorkReceiveListener {

    /**
     * hello world模型
     * @param msg
     */
    @RabbitListener(queues = {"queueHelloWorld"})
    public void receiveMsg(String msg, Channel channel, Message message){
        System.out.println("消费者1收到:"+msg);
    }
}

2.work模型

生产者:

/**
     * work模型发送消息
     */
    public void sendWorkMsg(){
        for (int i = 0; i < 5; i++) {
            amqpTemplate.convertAndSend("queueWork","hello,work");
        }
    }

消费者:

/**
     * work模型消费者1
     * @param msg
     * @param channel
     * @param message
     */
    @RabbitListener(queues = {"queueWork"})
    public void receiveMsg1(String msg, Channel channel, Message message){
        System.out.println("消费者1收到"+msg);
        System.out.println("通道是:"+channel);
        System.out.println("传输数据封装:"+message);

    }

    /**
     * work模型消费者2
     * @param msg
     * @param channel
     * @param message
     */
    @RabbitListener(queues = {"queueWork"})
    public void receiveMsg2(String msg, Channel channel, Message message){
        System.out.println("消费者2收到"+msg);
        System.out.println("通道是:"+channel);
        System.out.println("传输数据封装:"+message);

    }

3.发布订阅模型

1.配置文件

/**
     * 发布订阅模型队列1
     * @return
     */
    @Bean
    public Queue queueFanout1(){
        return new Queue("queueFanout1");
    }

    /**
     * 发布订阅模型队列2
     * @return
     */
    @Bean
    public Queue queueFanout2(){
        return new Queue("queueFanout2");
    }

    /**
     * 发布订阅模型交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("exchange-fanout");
    }

    /**
     * 将发布订阅队列1绑定到交换机
     * @param queueFanout1
     * @param fanoutExchange
     * @return
     */
    @Bean
    public Binding bindingExchange1(Queue queueFanout1,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queueFanout1).to(fanoutExchange);
    }

    /**
     * 将发布订阅队列2绑定到交换机
     * @param queueFanout2
     * @param fanoutExchange
     * @return
     */
    @Bean
    public Binding bindingExchange2(Queue queueFanout2,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(queueFanout2).to(fanoutExchange);
    }

2.生产者:

/**
     * 模拟发布订阅模型发送消息
     */
    public void sendFanoutMsg(){
        amqpTemplate.convertAndSend("exchange-fanout","","hello.fanout");
    }

3.消费者:

/**
     * 发布订阅模型消费者1
     * @param msg
     */
    @RabbitListener(queues = {"queueFanout1"})
    public void receiveFanoutMsg1(String msg){
        System.out.println("发布订阅模型消费者1收到:"+msg);
    }

    /**
     * 发布订阅模型消费者2
     * @param msg
     */
    @RabbitListener(queues = {"queueFanout2"})
    public void receiveFanoutMsg2(String msg){
        System.out.println("发布订阅模型消费者2收到:"+msg);
    }

4.路由模型

1.配置

/**
     * 路由模型队列1
     * @return
     */
    @Bean
    public Queue queueRouting1(){
        return new Queue("queueRouting1");
    }

    /**
     * 路由模型队列2
     * @return
     */
    @Bean
    public Queue queueRouting2(){
        return new Queue("queueRouting2");
    }

    /**
     * 路由类型交换机
     * @return
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("exchange-direct");
    }

    /**
     * 将路由队列1绑定到交换机
     * @param queueRouting1
     * @param directExchange
     * @return
     */
    @Bean
    public Binding bindingDirectExchange1(Queue queueRouting1,DirectExchange directExchange){
        return BindingBuilder.bind(queueRouting1).to(directExchange).with("hello");
    }

    /**
     * 将路由队列2绑定到交换机
     * @param queueRouting2
     * @param directExchange
     * @return
     */
    @Bean
    public Binding bindingDirectExchange2(Queue queueRouting2,DirectExchange directExchange){
        return BindingBuilder.bind(queueRouting2).to(directExchange).with("world");
    }

2.生产者:

/**
     * 模拟路由模型发送消息
     */
    public void sendRoutingMsg(){
        amqpTemplate.convertAndSend("exchange-direct","world","hello world");
    }

3.消费者:

/**
     * 路由key为hello的消费者
     * @param msg
     */
    @RabbitListener(queues = {"queueRouting1"})
    public void receiveDirectMsg1(String msg){
        System.out.println("路由key为hello的消费者收到:"+msg);
    }

    /**
     * 路由key为world的消费者
     * @param msg
     */
    @RabbitListener(queues = {"queueRouting2"})
    public void receiveDirectMsg2(String msg){
        System.out.println("路由key为world的消费者收到:"+msg);
    }

5.topic模型

1.配置

/**
     * topic模型队列1
     * @return
     */
    @Bean
    public Queue queueTopic1(){
        return new Queue("queue-topic1");
    }

    /**
     * topic模型队列2
     * @return
     */
    @Bean
    public Queue queueTopic2(){
        return new Queue("queue-topic2");
    }

    /**
     * topic模型交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("exchange-topic");
    }

    /**
     * 将topic模型队列1绑定到交换机
     * @param queueTopic1
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding bindingTopicExchange1(Queue queueTopic1,TopicExchange topicExchange){
        return BindingBuilder.bind(queueTopic1).to(topicExchange).with("#.hello");
    }

    /**
     * 将topic模型队列2绑定到交换机
     * @param queueTopic2
     * @param topicExchange
     * @return
     */
    @Bean
    public Binding bindingTopicExchange2(Queue queueTopic2,TopicExchange topicExchange){
        return BindingBuilder.bind(queueTopic2).to(topicExchange).with("#.world");
    }

2.生产者:

/**
     * 模拟topic模型发送消息
     */
    public void sendTopicMsg(){
        amqpTemplate.convertAndSend("exchange-topic","hello.world","hello world");
    }

3.消费者:

@RabbitListener(queues = {"queue-topic1"})
    public void receiveTopicMsg1(String msg){
        System.out.println("路由为#.hello的消费者收到:"+msg);
    }

    @RabbitListener(queues = {"queue-topic2"})
    public void receiveTopicMsg2(String msg){
        System.out.println("路由为#.world的消费者收到:"+msg);
    }

6.confirm机制

在properties开启confirm机制

#开启confirm机制
spring.rabbitmq.publisher-confirm-type=correlated

生产者:

	RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {
        @Override
        public void confirm(CorrelationData correlationData, boolean b, String s) {
            System.out.println("确认机制");
        }
    };
	/**
     * confirm 机制
     */
    public void sendConfirmMsg(){
        rabbitTemplate.setConfirmCallback(confirmCallback);
        rabbitTemplate.convertAndSend("hello world");
    }

消费者:

@RabbitListener(queues = {"queue-confirm"})
    public void receiveConfirmMsg(String msg){
        System.out.println("收到:"+msg);
    }

7.return机制

#开启return机制
spring.rabbitmq.publisher-returns=true

配置

/**
     * return机制队列1
     * @return
     */
    @Bean
    public Queue queueReturn1(){
        return new Queue("queue-return1");
    }

    /**
     * return机制队列2
     * @return
     */
    @Bean
    public Queue queueReturn2(){
        return new Queue("queue-return2");
    }

    /**
     * return机制交换机
     * @return
     */
    @Bean
    public FanoutExchange fanoutReturnExchange(){
        return new FanoutExchange("exchange-return");
    }

    /**
     * return机制将队列1绑定到交换机
     * @param queueReturn1
     * @param fanoutReturnExchange
     * @return
     */
    @Bean
    public Binding bindingReturnExchange1(Queue queueReturn1,FanoutExchange fanoutReturnExchange){
        return BindingBuilder.bind(queueReturn1).to(fanoutReturnExchange);
    }

    /**
     * return机制将队列2绑定到交换机
     * @param queueReturn2
     * @param fanoutReturnExchange
     * @return
     */
    @Bean
    public Binding bindingReturnExchange2(Queue queueReturn2,FanoutExchange fanoutReturnExchange){
        return BindingBuilder.bind(queueReturn2).to(fanoutReturnExchange);
    }

生产者:

	RabbitTemplate.ReturnCallback returnCallback = new RabbitTemplate.ReturnCallback() {
        @Override
        public void returnedMessage(Message message, int i, String s, String s1, String s2) {
            System.out.println("监听到不可达的消息");
            System.out.println("状态码:"+i+"---文本信息:"+s+"---交换机名字:"+s1+"----路由的key:s2");
        }
    };
    
	/**
     * return机制
     */
    public void sendReturnMsg(){
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnCallback(returnCallback);
        rabbitTemplate.convertAndSend("exchange-return","","hello world");
    }

消费者:

	@RabbitListener(queues = {"queue-return1"})
    public void receiveReturnMsg1(String msg){
        System.out.println("return消费者1收到:"+msg);
    }

    @RabbitListener(queues = {"queue-return2"})
    public void receiveReturnMsg2(String msg){
        System.out.println("return消费者2收到:"+msg);
    }

8.ttl队列

编写配置

	/**
     * ttl队列
     * @return
     */
    @Bean
    public Queue queueTTL(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-message-ttl",5000);
        return new Queue("queue-TTL",false,false,false,map);
    }

生产者:

/**
     * ttl队列
     */
    public void sendTTLMsg(){
        rabbitTemplate.convertAndSend("queue-TTL","wolegequ");
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值