欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-10 10:01:43
目录3. 与springboot的整合3.6 rest 测试代码1. 概述老话说的好:做人要懂得变通,善于思考,有时稍微转个弯,也许问题就解决了。言归正传,之前我们聊了 rabbitmq 3.9.7...

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);

        }
    };

    /**
     * 发送消息
     * @param messagebody   消息体
     * @param headers       附加属性
     * @throws exception
     */
    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() {

            /**
             * 发送消息后做的事情
             * @param message
             * @return
             * @throws amqpexception
             */
            @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镜像模式集群内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇: 如何挑选平菇

下一篇: 2021-03-06