Detailed RabbitMQ message queue basis and entry Demo

1 Why use RabbitMQ

1.1 RabbitMQ basis

AMQP, I.e. Advanced Message Queuing Protocol, Advanced Message Queuing Protocol, is an open standard application-layer protocol for message-oriented middleware design.
Message broker between the components is mainly used for 解耦the sender, the message need not be aware of the presence of the user message, and vice versa.
AMQPThe main feature is message-oriented, queues, routing (including point and publish / subscribe), reliability and safety.
RabbitMQIs an open source AMQPimplementation, with a server-side Erlanglanguage, supports a variety of clients, such as: Python、Ruby、.NET、Java、JMS、C、PHP、ActionScript、XMPP、STOMP, and support AJAX. In a distributed system for store and forward messages, in terms of ease of use, scalability, high availability, and so doing well.

1.2 synchronous mutation step

1.2.1 process one by one

Is shown below, the process is successively transmitted, namely: an order -> Service Order -> texting -> Hair Email-> made push, so a lot of time
Here Insert Picture Description

1.2.2 Thread Pool

As shown below in order open a service thread pool, the thread does not wait for the results of the other threads, it may be converted to synchronous asynchronous, but not decoupled
Here Insert Picture Description

1.2.3 with MQ System

As shown below, using message queues, MQthe system may become asynchronous to synchronous: when the user orders, MQthe system returns the order id, not in the pipe MQsystem, in fact, MQthe system will interact with the system and no other affected services and order
Here Insert Picture Description

1.3 decoupled service

Use MQ service can also decouple between them
Here Insert Picture Description

1.4 Flow cut front

Internet service spike, if the huge volume of requests sent to the spike service that could allow server to its knees, this time with a message queue, receiving and processing a huge request, because it does not deal with spike request, therefore we have to spike the service. This time can be set in the message queue inside a threshold, if it reaches a certain request, do not send to the spike in service, and sent to the other services
Here Insert Picture Description

2 Message Queue Basics

2.1 Provider

News producer, is delivering the message program.

2.2 Consumer

Consumer news, is that the program accepts messages.

2.3 Message passing is not used when the message queue

Here Insert Picture Description

2.4 Message passing the message queue

Here Insert Picture Description

2.5 What is a queue

Like a queue to store goods warehouse or store, a transit point between the factory production of goods and users to buy goods

2.6 What is stored in the queue

In rabbitMQthe flow of information from your application to Rabbitmqthe queue, all information can be stored only in a queue. Queue can store a lot of information, because it is basically an unlimited buffer, provided that your machine has enough storage space.

2.7 Relations queues and applications

A plurality of producers can send messages to the same queue, the plurality of messages may be only one queue by receiving the same data from the

Case 3 Getting Started

3.1 RabbitMQ profile

3.1.1 RabbitMQ coordinates

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

RabbitMQ 3.1.2 added the relevant configuration

# 给应用起一个名字
spring.applicatin.name=springcloud-mq
# RabbitMQ的安装地址
spring.rabbitmq.host=192.168.126.140
# RabbitMQ的访问端口,和页面的15672端口不一样
spring.rabbitmq.port=5672
# RabbitMQ的用户名
spring.rabbitmq.username=admin
# RabbitMQ的密码
spring.rabbitmq.password=123456

3.2 Code section

3.2.1 Creating queue

@Configuration
public class QueueConfig {
	//@Bean 方法的名字是默认的id名字,因此不要使用get+方法名
	@Bean
	// 注意:Queue要使用:org.springframework.amqp.core.Queue
	public Queue createQueue() {
		return new Queue("test_mq");
	}
}

3.2.2 Creating messaging provider

@Component
public class QueueSender {

	@Autowired
	private AmqpTemplate amqpTemplate;
	
	public void send (String msg) {
		//向消息队列发送消息
		//参数一: 队列的名称。
		//参数二: 消息
		this.amqpTemplate.convertAndSend("test_mq",msg);
	}
}

3.2.3 message recipient

@Component
public class QueueReciver {
	//此处的注解用来检测某一个特定队列是否变化
	@RabbitListener(queues="test_mq")
	public void reciver(String msg) {
		System.out.println("=====================");
		System.out.println(msg);
		System.out.println("=====================");
	}
}

3.2.4 Test class

@RunWith(SpringRunner.class)
@SpringBootTest(classes=AppAction.class)
public class QueueTest {
	@Autowired
	private QueueSender sender;
	/*
	* 测试消息队列
	*/
	@Test
	public void test(){
		this.sender.send("Hello RabbitMQ");
	}
}

3.3 Links refused error

3.3.1 User name Password wrong

Error:
ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
Solution:

  • Under carefully check the account password is correct
  • After logging in to the following Can access virtual hostsmodifications to/
    Here Insert Picture Description

Hostname problem 3.3.2 Centos virtual machine

3.3.2.1 Centos7 change the host name in two places consistent

For Centos7To modify the host name remains, then use hostnamectl
Here Insert Picture Description
as well as in /etc/hoststhis document have to change it
Here Insert Picture Description

3.3.2.2 Centos6 change the host name in two places consistent

Modify /etc/hostsfiles in
Here Insert Picture Description
there /etc/sysconfig/networknext file
Here Insert Picture Description

Published 334 original articles · won praise 186 · views 310 000 +

Guess you like

Origin blog.csdn.net/u012060033/article/details/104220535