1 RABBITMQ Knowledge Point 1.1 overall architecture diagram
Message producers post messages to Exchange, and Exchange routes producer-delivered messages to a queue in some sort of routing mechanism, and the message consumer gets messages from the queue for consumption .
1.2 Core Concepts 1.2.1 Server
Also known as broker, receiving client connections, implementing AMQP entity Services
1.2.2 Connection
Connection, application services with broker's network connection (PS: Similar to connection in JDBC)
1.2.3 Channel
Network channel, almost all operations in the channel, channel is to carry out the message read and write channels. The client can establish multiple channel, with each channel representing one session. "PS: statement equivalent to JDBC"
1.2.4 Message
Messages, the data passed between the server and the application are composed of properties and body, and properties can modify the message, such as the priority of the message, delay and other advanced features, the body is the content of the message.
1.2.5 Virtual Host
Virtual address, the topmost message route that is used for logical isolation. a There can be several exchange and queue in virtual host, the same virtual host cannot have Exchange or queue with the same name, the default is/, Virtual host is typically used for project partitioning, where a project-related exchange and queue are in one virtual host.
1.2.6 Exchange
A switch for receiving messages; forwarding a message to a bound queue based on a routing key
1.2.7 Binding
Virtual connection between Exchange and Queue, the Binding can contain routing key.
1.2.8 Routing Key
A routing rule that a virtual machine can use to determine how to route a specific message, which is the routing rule for Exchange and queue
1.2.9 Queue
Also called the message queue, messages queues, saving messages and forwarding them to consumers
1.3 The flow of RABBITMQ messages
Message publishers send a message, Exchange, routeing Key to Rabbtimq, they go through Virtual Host, then Exchange, to queue, and the message consumer gets the message in the queue.
2 Docker Download RABBIMQ 2.1 Docker Environment setup
See another blog post: Click to
The Docker version is:
2.2 Installing RABBITMQ 2.2.1 Pull RABBITMQ Mirror
Docker Pull Rabbitmq:3.7.7-management
Tips 01:RABBITMQ version can go to the official website to view
2.2.2 View the list of images pulled
2.2.3 Creating and starting the RABBITMQ container
Docker run-d--hostname my-rabbit-p 5672:5672-p 15672:15672 rabbitmq:3.7.7-management
The trick 01:-p is to map the port of the virtual machine to the port in Docker so that the port accessing the virtual machine is equivalent to accessing the port in Docker
Tip 02:5,672 is the port that connects the RABBTIMQ, and 15672 is the port that connects the RABBITMQ management page
2.2.4 Viewing the Boot RABBITMQ container
2.3 Connecting the RABBITMQ Management page
Tip 01: Access the virtual machine IP + RABBTIMQ Management page Port directly in the browser, for example:
http://192.168.233.135:15672/
Tip 02: You need to enter the user name and user password for the first visit, the default user name and user password are guest
Tip 03: The interface after successful access is as follows
3 Springboot Integration RABBITMQ Consumer Publisher 3.1 Create a Springboot project
Introducing dependencies: Web, AMQP, others are secondary dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-web</artifactid> </dependency> <!--https:// MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK.BOOT/SPRING-BOOT-STARTER-AMQP- <dependency> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPRING-BOOT-STARTER-AMQP </artifactId> </dependency>
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.xunyji</groupId> <artifactId>rabbitmq01</artifactId> & Lt;version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>rabbitmq01</ Name> <description>demo Project forSpring boot</description> <parent> <groupId>org.springframework.boot</groupId> &L T;artifactid>spring-boot-starter-parent</artifactid> <version>2.0.4.RELEASE</version> & Lt;relativepath/> <!--lookup parent from repository to </parent> <properties> <proj Ect.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputEncoding> Utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--https://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK.BOOT/SPRING-BOOT-STARTER-AMQP --<dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring- Boot-starter-amqp</artifactid> </dependency> <!--https://Mvnrepository.com/artifact/org.apache.commons/commons-lang3 --<dependency> <groupId>org.apache.commons</groupId> <artifactid>commons-lang3 </artifactId> </dependency> <!--https://Mvnrepository.com/artifact/commons-io/commons-io --<dependency> <groupId>commons-io</groupId> <artifactid>commons-io</artifa Ctid> <version>2.4</version> </dependency> <!--https://Mvnrepository.com/artifact/com.alibaba/fastjson --<dependency> <groupId>com.alibaba</groupId> <ARTIFACTID>FASTJSON</ARTIFAC Tid> <version>1.2.47</version> </dependency> <!--https://Mvnrepository.com/artifact/javax.servlet/javax.servlet-api --<dependency> <groupId>javax.servlet</groupId> <artifactid>javax.servlet-api& lt;/artifactid> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-devtools</arti factid> <scope>runtime</scope> </dependency> <dependency> & Lt;groupid>org.projectlombok</groupid> <artifactId>lombok</artifactId> <opti Onal>true</optional> </dependency> <dependency> <groupid>org.springframework.boot& Lt;/groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</ scope> </dependency> </dependencies> <build> <plugins> <plugi N> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot- maven-plugin</artifactid> </plugin> </plugins> </build></project>
Pom.xml3.2 Configuration Files
# RABBITMQ Basic configuration spring.rabbitmq.addresses=192.168.233.135:5672spring.rabbitmq.username= Guestspring.activemq.password=guestspring.rabbitmq.virtual-host=/spring.rabbitmq.connection -timeout=15000server.servlet.path=/server.port=8001spring.http.encoding.charset =utf-8spring.jackson.date-format=yyyy-mm-dd HH:mm:ssspring.jackson.time-zone=gmt+8 Spring.jackson. default-property-inclusion=non_null
application.properties3.3 Publishing messages
To rely on an instance of injection rabbittemplate and invoke an instance method Convertandsend publish the message
Tip 01: You need to set up a responsive exchange and queue in RABBITMQ before you publish
3.3.1 Adding Exchange in RABBITMQ
Tip 01: New Exchange information appears in the Exchange list when you are finished adding
3.3.2 Adding a queue in RABBITMQ
Tip 01: When you finish adding a queue, you will see the newly added queue in the queue list
3.3.3 Setting routing rules for Exchange and queue
Both the 01:exchange and the queue can be set, only one side is set.
3.4 Testing
Publish a message to RABBITMQ
PackageCom.xunyji.rabbitmq01.producer;Importcom.xunyji.rabbitmq01.Rabbitmq01ApplicationTests;ImportCom.xunyji.rabbitmq01.entity.Order;Importlombok.extern.slf4j.Slf4j;Importorg.junit.Test;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Component;ImportJava.util.UUID;Import Staticorg.junit.assert.*; @Component @slf4j Public classOrdersendertestextendsrabbitmq01applicationtests {@AutowiredPrivateOrdersender Ordersender; @Test Public voidTEST01 ()throwsException {Order order=Order.builder (). ID ("20180830"). Name ("Test Order 01"). MessageId (System.currenttimemillis ()+ "$" +Uuid.randomuuid (). toString ()). build (); Ordersender.sendorder (order); }}
View Code
Tip 01: After a successful delivery, there will be a message in the queue that is consumed and will be removed from the queue after the message consumer has consumed it.
4 Springboot Integration RABBITMQ Consumer Subscribers 4.1 Create a Springboot project
Introducing the necessary dependencies: the Web, AMPQ, and the rest are secondary dependencies
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-web</artifactid> </dependency> <!--https:// MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK.BOOT/SPRING-BOOT-STARTER-AMQP- <dependency> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPRING-BOOT-STARTER-AMQP </artifactId> </dependency>
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.xunyji</groupId> <artifactid>rabbitmq02-consumer</ artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>r Abbitmq02-consumer</name> <description>demo Project forSpring boot</description> <parent> <groupId>org.springframework.boot</groupId> &L T;artifactid>spring-boot-starter-parent</artifactid> <version>2.0.4.RELEASE</version> & Lt;relativepath/> <!--lookup parent from repository to </parent> <properties> <proj Ect.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputEncoding> Utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--https://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK.BOOT/SPRING-BOOT-STARTER-AMQP --<dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring- Boot-starter-amqp</artifactid> </dependency> <!--https://Mvnrepository.com/artifact/org.apache.commons/commons-lang3 --<dependency> <groupId>org.apache.commons</groupId> <artifactid>commons-lang3 </artifactId> </dependency> <!--https://Mvnrepository.com/artifact/commons-io/commons-io --<dependency> <groupId>commons-io</groupId> <artifactid>commons-io</artifa Ctid> <version>2.4</version> </dependency> <!--https://Mvnrepository.com/artifact/com.alibaba/fastjson --<dependency> <groupId>com.alibaba</groupId> <ARTIFACTID>FASTJSON</ARTIFAC Tid> <version>1.2.47</version> </dependency> <!--https://Mvnrepository.com/artifact/javax.servlet/javax.servlet-api --<dependency> <groupId>javax.servlet</groupId> <artifactid>javax.servlet-api& lt;/artifactid> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-devtools</arti factid> <scope>runtime</scope> </dependency> <dependency> & Lt;groupid>org.projectlombok</groupid> <artifactId>lombok</artifactId> <opti Onal>true</optional> </dependency> <dependency> <groupid>org.springframework.boot& Lt;/groupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</ scope> </dependency> </dependencies> <build> <plugins> <plugi N> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot- maven-plugin</artifactid> </plugin> </plugins> </build></project>
Pom.xml4.2 Configuration Files
#springboot整合mq的基本配置spring. Rabbitmq.addresses=192.168.233.135:5672Spring.rabbitmq.username=Guestspring.activemq.password=guestspring.rabbitmq.virtual-host=/spring.rabbitmq.connection-timeout=15000#springboot整合mq的消费配置spring. Rabbitmq.listener.simple.concurrency=5Spring.rabbitmq.listener.simple.max-concurrency=10Spring.rabbitmq.listener.simple.acknowledge-mode=Manualspring.rabbitmq.listener.simple.prefetch=1Server.servlet.path=/Server.port=8002Spring.http.encoding.charset=utf-8spring.jackson.date-format=yyyy-mm-DD HH:mm:ssspring.jackson.time-zone=gmt+8Spring.jackson.default-property-inclusion=non_null
View Code4.3 Writing of consumption methods
Use annotations on the consumption method to turn on the listener and specify the listener for that queue
Tip: @RabbitHandler and @RabbitListener annotations can automatically generate Exchange and queue bindings in RABBITMQ
4.4 Getting messages
As there is already a message in RABBITMQ to be processed, so the direct start of the project can complete the consumption of messages, after consumption of RABBITMQ in the number of messages to be consumed is 0
5 This blog source code
Click to go
SpringBoot28 RABBITMQ knowledge points, Docker download RABBITMQ, Springboot integration Rabbtimq