当前位置:网站首页>Rabbitmq (1) - basic introduction

Rabbitmq (1) - basic introduction

2020-11-08 13:00:00 Big brother Jian

Original address :https://www.jianshu.com/p/e186a7fce8cc

Before learning something , Let's start with a methodology , Know how to learn . Learning a thing generally follows the following steps :

  1. xxx What is it? , Cause of birth , What problems can be solved .
  2. How to install , How to use ( Get up and start one demo).
  3. Some of the basic concepts involved and basic introduction to use .
  4. Middle stage 、 High level features use .
  5. Highly available deployment scenarios .
  6. An in-depth understanding of the principles .

This article is mainly at the first 、 Two ring node , Let the novice get a better start .

1、rabbitmq What is it?

rabbitmq It is a very popular message middleware .Rabbit So the name , It's because rabbits move so fast and breed so crazy , RabbitMQ The founders of this distributed software think that it's the right name for this distributed software .

RabbitMQ Is to use Erlang Language implementation AMQP (Advanced Message Queuing Protocol, Advanced message queue protocol ) Message middleware , It originated from Financial system , therefore rabbitmq Is characterized by Reliable transmission , Performance is behind reliability

2、 Why use mq, Why? rabbitmq

Why use mq?

mq There are generally two modes
One is point-to-point mode : Mainly for peak clipping and asynchronous .
The other is broadcast mode : It is mainly for decoupling between businesses .

In short, it is the scene where the business needs to cut the peak of traffic , There is a need to decouple business scenarios .

Why? rabbitmq?

rabbitmq The features are as follows :
1、 reliability : RabbitMQ It was born for the financial system , So some mechanisms are specially used to ensure reliability , Such as persistence 、 Transmission confirmation and release confirmation, etc .
2、 Flexible router :4 Kind of router
3、 Extensibility : Multiple rabbitmq Nodes can form a cluster , You can also dynamically expand the nodes in the cluster according to the actual business situation .
4、 High availability : Have a mirror queue to prevent the loss of disaster recovery messages .

Let's use mq Products do important business processing ( For example, the amount 、 Order ) When , Our most basic statement is
1、 Reliable and stable , Message not lost .
2、 There are cluster disaster recovery solutions .
3、 Flexible features .
4、 Performance is based on reliability
rabbitmq Can be perfectly competent .

4、rabbitmq Installation

This is just an introduction docker Installation mode
docker run -d --name myrabbit1 -p 15672:15672 -p 5672:5672 rabbitmq:3.6.15-management

Access after installation localhost:15672(localhost Change the server ip) See the following interface on behalf of the successful installation .

Use default account guest/guest Sign in

The interface of the management desk will be explained in detail one place after another .

4、 Quick to use demo
Here we use springboot Show examples of how to use rabbitmq
4.1 newly build springboot The project also adds configuration

spring.rabbitmq.host=localhost
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

4.2 introduce ampq Of springboot-start rely on

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

4.3 New configuration file , Initialize queue

@Configuration
public class RabbitMqConfig {
    @Bean
    public Queue demo(){
        Queue demo = new Queue("demo");
        return demo;
    }
}

4.4 New consumers 、 producer .

@RestController
@RequestMapping
public class Controller {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @GetMapping("sendMq")
    public String sendMq(String msg){
        rabbitTemplate.convertAndSend("demo",msg);
        return msg;
    }
}
@Component
public class DemoLister {
    @RabbitListener(queues = "demo")
    public void Lister(Message message){
        byte[] body = message.getBody();
        System.out.println(" Received message body :"+new String(body));
    }
}

4.5 Send a message to see the effect
Carry out orders curl localhost:8080/sendMq?msg=abc
See the effect

5、 Last
git clone -b teacher-1 https://gitee.com/guoeryyj/rabbitmq-teacher.git Download the sample code x

版权声明
本文为[Big brother Jian]所创,转载请带上原文链接,感谢