Messaging middleware Start 2 --- --- RabbitMQ messaging patterns

The second we briefly introduce some of RabbitMQ messaging mode:

1, a direct mode (Direct)

  (1) Direct Mode Introduction

We need to send messages to use this mode when only one node, which is the simplest form.

Direct Exchange to send any message will be forwarded to RouteKey specified Queue.
1. General use rabbitMQ comes Exchange: "" (the name is an empty string Exchange, hereinafter referred to as default Exchange).
2. Exchange need not be carried out in any mode such bindings (Binding) operation.
Requires a "RouteKey" 3. messaging, it can be simply understood as the name of the queue to be sent.
4. If the queue name does not exist RouteKey vhost specified, then the message will be discarded. 

  (2) create the queue 

Before doing the following example, we first create a queue called itcast.

Durability: if done persistence Durable (long-lasting) transient (temporary)
Auto the Delete: whether to automatically delete 

  (3) Producer code that implements message ---

       First, create a project rabbitmq_demo, introduced amqp started dependence, pom.xml as follows:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring‐boot‐starter‐parent</artifactId>
    <version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
    <project.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‐amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring‐boot‐starter‐test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

      Second, write a configuration file application.yml

spring:
  rabbitmq:
    host: 192.168.184.134

      Third, start writing class 

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

      Fourth, write test classes

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class MqTest {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void testSend(){
        rabbitTemplate.convertAndSend("test","测试");
    }
}

  (4) consumer code that implements the message --- 

      First, write a message consumer class

@Component
@RabbitListener(queues="test" )
public class Customer1 {

    @RabbitHandler
    public void showMessage(String message){
        System.out.println("itcast接收到消息:"+message);
    }
}

      Second, run the startup class, you can see just sent a message in the console 

2, breakdown mode (Fanout) 

  (1) Introduction breakdown mode

When we need a message sent to multiple queues, we need to use this mode. As shown below:

Any messages sent to Fanout Exchange will be forwarded to all the Exchange bindings (Binding) of
the Queue.
1. The pattern can be understood as a routing table
2. This mode does not require RouteKey
3. This mode requires the advance Exchange Queue bind, can bind a plurality of Exchange Queue, Queue can be tied with a plurality of Exchange set.
4. If the received message and Exchange does not bind any Queue, then the message will be discarded. 

  (2) Binding queue exchanger

      First, add the queue and the queue itheima kudingyu

      Second, the new switch chuanz (clear and then review the previous section)

      Third, the two queues itheima itcast and bound to the exchanger chuanzhi 

      Click chuanzhi into the switch management interface 

      Bindings and click the Add Binding itheima kudingyu 

      After binding effect is as follows:

  (2) code that implements message producer ---

@Test
public void testSendFanout(){
    rabbitTemplate.convertAndSend("chuanzhi","", "分列模式test");
}

 (3) the consumer code that implements the message ---

      First, create a message listener class for monitoring itheima news

@Component
@RabbitListener(queues="itheima" )
public class Customer2 {

    @RabbitHandler
    public void showMessage(String message){
        System.out.println("itheima接收到消息:"+message);
    }
}

      Second, create a message listener class for monitoring kudingyu news 

@Component
@RabbitListener(queues="kudingyu" )
public class Customer3 {

    @RabbitHandler
    public void showMessage(String message){
        System.out.println("kudingyu接收到消息:"+message);
    }
}

  (4) Test, two messages can be seen in the console

3, theme (Topic) 

  (1) Themes Introduction

Any messages sent to the Topic Exchange will be forwarded to all concerned RouteKey specified topics Queue.

As shown above
such that the message switch from different sources may arrive at a column, in fact, that it is better to understand the meaning of fuzzy matching, for example: Shown in red routekey column for usa #, # representatives matches any character, but in order for the message to reach this column, usa. # must match the back of the good are free. Figure usa.news
usa.weather, can be found in red queue, the # symbol matches one or more words, symbols * matches a word more, no less. Therefore, usa. # Can be matched to usa.news.XXX, but the usa. * Only match to usa.XXX.
Note:
Switch the final analysis is a list of names and the queue is bound. When a message is posted to the switch, in fact, the message is routed to compare keys, routing messages from the last channel you are connected to the same list of bindings on the switch. Any messages sent to the Topic Exchange will be forwarded to all concerned RouteKey specified topics Queue

1. This model is more complex, in simple terms, is that each queue has its own theme concerned, all messages are presented with a "Title" (RouteKey), Exchange will forward the message to all concerned topics can blur and RouteKey matching queue.
2. This model requires RouteKey, perhaps to bind the Exchange Queue advance.
3. When making binding, to provide a topic of concern to the queue, such as "# .log. #" Indicates that the queue care of all the messages involved in log (a RouteKey as "MQ.log.error" message will be forwarded to the queue).
4. "#" Represents 0 or several keywords, "" indicates a keyword. As ". Log" can match with "log.warn", can not match the "log.warn.timeout"; however ". Log #" can be matched with both.
5. Similarly, if Exchange is not found possible to match the RouteKey Queue, this message will be discarded.

  (2) create the queue with bindings

      First, a new switch, select the type of topic

      Second, click on the new switch topictest 

      Add matching rules, add the list is as follows: 

  (2) code implementation 

      First, the preparation of test class methods:

@Test
public void testSendTopic1(){
    rabbitTemplate.convertAndSend("topictest","goods.aaa","主题模式");
}

      Second, output: itcast received message: themes 

@Test
public void testSendTopic2(){
    rabbitTemplate.convertAndSend("topictest","article.content.log","主题模式");
}

      Third, output: itheima received message: themes

@Test
public void testSendTopic3(){
    rabbitTemplate.convertAndSend("topictest","goods.log","主题模式");
}

     Fourth, the output:

itheima接收到消息:主题模式
itcast接收到消息:主题模式
kudingyu接收到消息:主题模式

At this point, RabbitMQ messaging model has three complete introduction, ladies and children's shoes hands-down operation.


 

 

 

 

 


 

 

Published 41 original articles · won praise 47 · views 30000 +

Guess you like

Origin blog.csdn.net/u014526891/article/details/87539206