1. summary

The old saying is good : Be flexible , Be good at thinking , Sometimes take a little turn , Maybe the problem is solved .

Get down to business , We talked about it before RabbitMQ 3.9.7 Build a mirror mode cluster , Let's talk today RabbitMQ 3.9.7 Mirror mode cluster and Springboot 2.5.5 Integrate .

2. The scene that

The server A IP:192.168.1.22

The server B IP:192.168.1.8

The server C IP:192.168.1.144

These three servers have been set up RabbitMQ Mirror mode cluster , Build a mirror mode cluster , See my last article .

3. And Springboot Integration of

3.1 Introduce dependencies

    <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 Production service configuration

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 # Enable message confirmation mode
publisher-confirm-type: correlated # Enable return Message schema
publisher-returns: true
template:
mandatory: true

3.3 Production service code

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; /**
* Confirm callback
*/
final RabbitTemplate.ConfirmCallback confirmCallback = new RabbitTemplate.ConfirmCallback() { @Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
// correlationData Unique identification
// ack mq Did you receive a message
// cause Reasons for failure
System.out.println("correlationData:" + correlationData.getId());
System.out.println("ack:" + ack);
System.out.println("cause:" + cause); }
}; /**
* Send a message
* @param messageBody Message body
* @param headers Additional attributes
* @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() { /**
* What to do after sending a message
* @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 Consumer service configuration

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:
# Set to manual ACK
acknowledge-mode: manual
concurrency: 5
prefetch: 1
max-concurrency: 10

3.5 Consumer service code

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(" Received a message :" + message.getPayload()); Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
channel.basicAck(deliveryTag, false);
}
}

3.6 Rest Test code

@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. review

Today I talked about RabbitMQ 3.9.7 Mirror mode cluster and Springboot 2.5.5 Integrate , I hope it can be helpful to everyone's work .

Welcome to help me 、 Comment on 、 forward 、 Pay more attention to :)

Pay attention to those who follow the wind Java, Update daily Java dried food .

RabbitMQ 3.9.7 Mirror mode cluster and Springboot 2.5.5 More articles on Integration

  1. RabbitMQ Image queue cluster construction 、 And SpringBoot Integrate

    Mirror mode The classic cluster model is Mirror Mirror mode , Guarantee 100% Data is not lost , In the actual work is also the most used , And the implementation of cluster is relatively simple . Mirror Image queue , The purpose is to guarantee RabbitMQ High reliability of data ...

  2. RabbitMQ Use ( Two )- RabbitMQ Services are clustered in a single machine

    RabbitMQ Use ( Two )- RabbitMQ Services are clustered in a single machine author :markjiang7m2 Original address :https://www.cnblogs.com/markjiang7m2/p/128371 ...

  3. RabbitMQ and Kafka The principle of high availability cluster

    Preface friends , By reading the previous article , I think you're right RocketMQ We have a more in-depth understanding of the basic principles of , So we are more commonly used for the current RabbitMQ and Kafka Would you like to know something about it , It's not a bad thing to know more ...

  4. Make docker-jdk7-zookeeper Mirror image ( Non cluster version )

    ## preparation Tools used , Xshell5, Xftp5, jdk-7u79-linux-x64.tar.gz, zookeeper-3.4.9.tar.gz, docker.io/centos:l ...

  5. Nodejs【 stand-alone 】 Multi process mode cluster

    Nodejs[ stand-alone ] Multi process mode cluster instance : 1. install :npm install -s cluster 2. Service code : var debug = require('debug'); var express = ...

  6. Zookeeper Deploy Zookeeper Arbitration mode cluster

    Deploy Zookeeper Arbitration mode cluster This example is deployed on a server 3 individual zk service :z1.z2.z3. 1. download Zookeeper https://zookeeper.apache.org/ 2. decompression .tar ...

  7. docker build redis Master slave cluster and sentinel The sentry cluster ,springboot Client connection

    It took two days to build redis Master slave cluster and sentinel The sentry cluster , Let's talk about springboot Client connection test redis Master-slave cluster There are two ways to view it from the Internet : One is to specify the configuration file , One is not to specify a configuration file Refer to the address ...

  8. rabbitmq+haproxy+keepalived Realize the construction of high availability cluster

    The project needs to build rabbitmq High availability cluster for , Recently I've been learning how to build , I can record here that I can communicate with you ( This is just a record of my own building process after learning , Many of the principles are not explained in detail ). Set up the environment CentOS7 64 position R ...

  9. RabbitMQ Reliable delivery and high availability cluster

    Reliable delivery : First of all, it needs to be clear , Efficiency and reliability cannot be both , If you want to make sure that every link is successful , It is bound to affect the efficiency of sending and receiving messages . If it's some occasions where the business real-time consistency requirements are not particularly high , You can sacrifice some reliability for efficiency . Make sure to eliminate ...

  10. rabbitmq System learning ( 3、 ... and ) Cluster architecture

    RabbitMQ Cluster architecture mode Main standby mode Realization RabbitMQ High availability cluster for , Generally, when the concurrency and the amount of data are not high , This model is very easy to use and simple . The active / standby mode is also called Warren Pattern HaProxy To configure listen r ...

Random recommendation

  1. Python.Module.site

    site " This module is automatically imported during initialization. The automatic import can be ...

  2. C++ String correlation function

    < turn > since :http://zhidao.baidu.com/question/173202165.html The first is memcpy Header file : #include <string.h> ...

  3. Android It monitors the broadcast of mobile network status

    Android It monitors the broadcast of mobile network status Android Monitor network status Android Use broadcast monitoring equipment to connect to the network ( Broken net ) The change of

  4. Linux Using the hard disk

    One . Hard disk partition planning : swap( In exchange for , Swap transaction , swap ) Partition : Virtual memory usage , Can't save user information . boot( guide ) Partition : Save the files related to the startup system . root( root ) Partition : The root of the system file , All files are saved in this partition ...

  5. ueditor Simple usage

    Paste unused... First ueditor Previous code : <body> <label for="input_content"> Answer area :</label> <t ...

  6. hdu 6390 Euler function + A class ( Mobius function ) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 The question : Find an equation Answer key : Look at the explanation , Write code The first line doesn't show , hinder sigma The formula will not simplify .mobius either ...

  7. SQL Server CLR Use C# Custom function

    One . brief introduction Microsoft SQL Server 2005 after , Realized with Microsoft .NET Framework The common language runtime for (CLR) Integration of .CLR Integration makes it now possible to use .NET ...

  8. 20145209 Experiment four 《android Development infrastructure 》 Experimental report

    20145209 Experiment four <android Development infrastructure > Experimental report Experimental content Android Stuidio Installation test of : Reference resources <Java and Android Develop learning guides ( The second edition )(EPUBI ...

  9. VK Cup 2016 - Qualification Round 2 A. Home Numbers Water problem

    A. Home Numbers Topic linking : http://www.codeforces.com/contest/638/problem/A Description The main street of ...

  10. EF、Dapper、NHibernate etc. ORM Comparison of frameworks and their advantages and disadvantages

    What is? ORM? ORM The full name is Object Relational Mapping, Object relation mapping . Its implementation idea is to map the data of tables in the relational database to objects , In the form of objects , In this way, developers can manipulate the database ...