返回顶部
首页 > 资讯 > 后端开发 > Python >RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合
  • 855
分享到

RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合

2024-04-02 19:04:59 855人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录1. 概述2. 场景说明3. 与SpringBoot的整合3.1 引入依赖3.2 生产服务配置3.3 生产服务代码3.4 消费服务配置3.5 消费服务代码3.6 Rest 测试代

1. 概述

老话说的好:做人要懂得变通,善于思考,有时稍微转个弯,也许问题就解决了。

言归正传,之前我们聊了 RabbitMQ 3.9.7 镜像模式集群的搭建,今天我们来聊聊 RabbitMQ 3.9.7 镜像模式集群与springboot 2.5.5 整合。

2. 场景说明

服务器A IP:192.168.1.22

服务器B IP:192.168.1.8

服务器C IP:192.168.1.144

此三台服务器上已搭建好了 RabbitMQ镜像模式集群,镜像模式集群的搭建,可参见我的上一篇文章。

3. 与Springboot的整合

3.1 引入依赖


<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

3.2 生产服务配置


spring:
  rabbitmq:
    addresses: 192.168.1.22:5672,192.168.1.8:5672,192.168.1.144:5672
    username: guest
    passWord: guest
    virtual-host: /
    connection-timeout: 16000

    # 启用消息确认模式
    publisher-confirm-type: correlated

    # 启用 return 消息模式
    publisher-returns: true
    template:
      mandatory: true

3.3 生产服务代码


import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class Producer {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    
    final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() {

        @Override
        public void confirm(CorrelationData correlationData, boolean ack, String cause) {
            // correlationData 唯一标识
            // ack mq是否收到消息
            // cause 失败原因
            System.out.println("correlationData:" + correlationData.getId());
            System.out.println("ack:" + ack);
            System.out.println("cause:" + cause);

        }
    };

    
    public void sendMessage(String messageBody, Map<String, Object> headers, String id) throws Exception {

        MessageHeaders messageHeaders = new MessageHeaders(headers);

        Message<String> message = MessageBuilder.createMessage(messageBody, messageHeaders);

        rabbitTemplate.setConfirmCallback(confirmCallback);

        String exchangeName = "exchange-hello";
        String routingKey = "test.123";

        CorrelationData correlationData = new CorrelationData(id);

        rabbitTemplate.convertAndSend(exchangeName, routingKey, message, new MessagePostProcessor() {

            
            @Override
            public org.springframework.amqp.core.Message postProceSSMessage(org.springframework.amqp.core.Message message) throws AmqpException {
                return message;
            }
        }, correlationData);
    }
}

3.4 消费服务配置


spring:
  rabbitmq:
    addresses: 192.168.1.22:5672,192.168.1.8:5672,192.168.1.144:5672
    username: guest
    password: guest
    virtual-host: /
    connection-timeout: 16000

    listener:
      simple:
        # 设置为手工ACK
        acknowledge-mode: manual
        concurrency: 5
        prefetch: 1
        max-concurrency: 10

3.5 消费服务代码


import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue-hello", durable = "true"),
            exchange = @Exchange(value = "exchange-hello" , durable = "true", type = "topic"),
            key = "test.*"
    ))
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws Exception {

        System.out.println("收到消息:" + message.getPayload());

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag, false);
    }
}

3.6 Rest 测试代码


@RestController
@RequestMapping("/mq")
public class RabbitmqController {

    @Autowired
    private Producer producer;

    @GetMapping("/sendMessage")
    public String sendMessage(@RequestParam String messageBody, @RequestParam String id) throws Exception {
        Map<String, Object> headers = new HashMap<>();
        producer.sendMessage(messageBody, headers, id);
        return "success";
    }
}

4. 综述

到此这篇关于RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合的文章就介绍到这了,更多相关RabbitMQ镜像模式集群内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: RabbitMQ 3.9.7 镜像模式集群与Springboot 2.5.5 整合

本文链接: https://lsjlt.com/news/137333.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作