RabbitMq insight

                                                                                    ---- only add up to pay homage, like me, slowly advancing human children

The question: What is RabbitMq

  

Here are some personal feelings,

rabbitmq AMQP protocol is a follows ( this is valid and is not clear ) to achieve the message queue for the connection between the server and the server,

producer can send a message to the message queue inside, Customer may consume the message queue

There are many ways between the two, such as the common point (1V1) (technical term: Direct ), point to multipoint (1V poly) (technical term: fanmout )

Into vernacular: A sends a message, B can only receive this message, this is the point

                          A sends a message, the message B, C, D can receive, this is the point to multipoint

 

So the question is: Why should I use rabbitMq (What is the use of scenes)

Asynchronous processing , application of decoupling , traffic clipping , etc.

https://www.cnblogs.com/zhao-yi/p/9299126.html

 

How to use springBoot in it?

docker run my own use rabbitmq the mirror to do, so use rabbitmq server specific installation is not clear

In springBoot main steps:

      1, rely on the introduction of 

  <!-- 引入rabbitmq 消息中间件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

      2, the configuration rabbitmq connection (Rabbit itself is a server, it is connected to the data connection as jdbc general)

# Configure rabbitmq (you can see for yourself rabbitProperties to view items need to be configured, and the default configuration items) 
Sring:
  rabbitmq:
    Host: 192.168.3.48
    Port: 5672
    Virtual-Host: /
    username: the Guest
    password: the Guest

     3, send messages and receive messages Case

 

    # News release test
@Test public void the Test () { // like most of the previous template tool is operating on rabbitmq brought together the System.out.println ( "HELLO"); // send the following parameters which need but after the inquiry message, the message is a class a little trouble, it is not recommended to use this // rabbitTemplate.send ( "qf.direct", "Remember that you are directly connected"); the Map <String, Object > the Map = new new HashMap <> (); map.put ( "First", "hello"); map.put ( "SECOND,", "hello everybody"); rabbitTemplate.convertAndSend ( "exchange.direct", "QF ", the Map); System.out.println (" successfully sent "); }

# test consumer news
  @Test 
public void the receive () {
// Get the specified message to a message queue inside
Object rabbitTemplate.receiveAndConvert QF = ( "QF");
System.out.println (qf.getClass ());
System.out.println ( QF);
System.out.println ( "successfully acquired");
}

  

4, using @RabbitListener annotations ( detailed logic, and using the principle of this part of the ignorant )

Use @RabbitListener annotations need to turn rabbitmq comment

@EnableRabbit
@EnableCaching
@MapperScan("com.qf.springboot01.dao")
@SpringBootApplication
public class Springboot01Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot01Application.class, args);
    }
}

 Use case are as follows:

    @RabbitListener(queues = "qf")
    public void receive(Object person){
        if(person == null){
            System.out.println("person里面是空的");
        }
        System.out.println();
        System.out.println("接受人" + person);
        System.out.println(person.toString());
    }
} 

 The annotation queues queue parameter specifies the listener may listen to multiple queues

    The method of labeling the annotation parameters for matching inside the message queue, if there is the same type of data acquisition to be encapsulated directly, if not the same

    No encapsulation, but also consume a message vernacular is: I get a message that I would give parameters correspond with you, if it does not give you the same, but the message

    I have also taken out.

 

5, Attention:

      When introducing dependent, wherein RabbitAutoConfiguration class automatically configured to help us a lot, where the message body is stored in a sequence of the embodiment is also configured the queue

But the default serialization is serialized as a byte array stored in the queue, resulting in dyslexia.

So, if you configure his MessageConverter, as follows:

@Configuration
public class MyAMQPConfig {
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

  

 

Currently only know because relatively shallow, so the only record of these simple things, to be back after the understanding,

Hope you come back to add, these data

 

Guess you like

Origin www.cnblogs.com/helloqiufei/p/11536473.html