Article Directory
- 1 Why use RabbitMQ
- 2 Message Queue Basics
- 2.1 Provider
- 2.2 Consumer
- 2.3 Message passing is not used when the message queue
- 2.4 Message passing the message queue
- 2.5 What is a queue
- 2.6 What is stored in the queue
- 2.7 Relations queues and applications
- Case 3 Getting Started
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.
AMQP
The main feature is message-oriented, queues, routing (including point and publish / subscribe), reliability and safety.
RabbitMQ
Is an open source AMQP
implementation, with a server-side Erlang
language, 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
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
1.2.3 with MQ System
As shown below, using message queues, MQ
the system may become asynchronous to synchronous: when the user orders, MQ
the system returns the order id, not in the pipe MQ
system, in fact, MQ
the system will interact with the system and no other affected services and order
1.3 decoupled service
Use MQ service can also decouple between them
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
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
2.4 Message passing the message queue
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 rabbitMQ
the flow of information from your application to Rabbitmq
the 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 hosts
modifications to/
Hostname problem 3.3.2 Centos virtual machine
3.3.2.1 Centos7 change the host name in two places consistent
For Centos7
To modify the host name remains, then use hostnamectl
as well as in /etc/hosts
this document have to change it
3.3.2.2 Centos6 change the host name in two places consistent
Modify /etc/hosts
files in
there /etc/sysconfig/network
next file