[RabbitMQ] SpringBoot integrates RabbitMQ

Build the initial environment

Introduce dependencies

<!--引入与rabbitmq集成依赖-->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

We can also check this place directly when building quickly;
insert image description here

configuration profile

spring:
  application:
    name: springboot_rabbitmq
  rabbitmq:
    host: 10.15.0.9
    port: 5672
    username: ems
    password: 123
    virtual-host: /ems

This name has no practical significance, but it is very important in microservice projects
. The configuration in rabbitmq is to connect with our RabbitMQ service

RabbitTemplateIt is used to simplify the operation and can be used directly by injecting it into the project

The HelloWorld model uses

development producer

@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testHello(){
    
    
  rabbitTemplate.convertAndSend("hello","hello world");
}

convertAndSend method: convert and send. It is used to convert the message into byte and then send it

  • First parameter: the name of the queue
  • The second parameter: the content of the message

After we run the producer, we find that there is no queue:
insert image description here
because the creation of this queue is not created on the producer side but on the consumer side

It doesn't make sense to create a queue if there are no consumers

Develop consumers

@Component
@RabbitListener(queuesToDeclare = @Queue("hello"))
public class HelloCustomer {
    
    

    @RabbitHandler
    public void receive1(String message){
    
    
        System.out.println("message = " + message);
    }
}

The consumer must have an annotation @RabbitListener to listen on behalf of the consumer. At this point we want him to receive the messages in the hello queue, but this queue does not exist yet, so we use queuesToDeclare to declare a queue here.

Then how do we get the messages in the queue? We can create a method arbitrarily and use the @RabbitHandler annotation on it to represent the callback method for taking messages from the queue. We can get the message through the parameters of this callback method.

Then we run and find out:
insert image description here
the queue created in this mode is persistent by default, so how do we set whether it is exclusive or automatically deleted?

We can use the @Queue annotation:
insert image description here

The queue created by default is persistent, non-exclusive, and not automatically deleted.

Work model use

development producer

@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testWork(){
    
    
  for (int i = 0; i < 10; i++) {
    
    
    rabbitTemplate.convertAndSend("work","hello work!");
  }
}

Develop consumers

@Component
public class WorkCustomer {
    
    
    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive1(String message){
    
    
        System.out.println("work message1 = " + message);
    }

    @RabbitListener(queuesToDeclare = @Queue("work"))
    public void receive2(String message){
    
    
        System.out.println("work message2 = " + message);
    }
}

Here we are building multiple consumers in one class. @RabbitListener is used on classes, and @RabbitListener can also be used on methods

说明:默认在Spring AMQP实现中Work这种方式就是公平调度,如果需要实现能者多劳需要额外配置

Fanout broadcast model

development producer

@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testFanout() throws InterruptedException {
    
    
  rabbitTemplate.convertAndSend("logs","","这是日志广播");
}

convertAndSend:

  • First parameter: the name of the switch
  • The second parameter: routingkey
  • The third parameter: the generated message

Similarly, the switch is not defined in the producer but in the consumer. Only executing the above code will not create a log switch

Develop consumers

@Component
public class FanoutCustomer {
    
    

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue,  //创建临时队列
            exchange = @Exchange(name="logs",type = "fanout")  //绑定的交换机
    ))
    public void receive1(String message){
    
    
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue, //创建临时队列
            exchange = @Exchange(name="logs",type = "fanout")  //绑定交换机类型
    ))
    public void receive2(String message){
    
    
        System.out.println("message2 = " + message);
    }
}

Route routing model

development producer

@Autowired
private RabbitTemplate rabbitTemplate;

@Test
public void testDirect(){
    
    
  rabbitTemplate.convertAndSend("directs","error","error 的日志信息");
}

Develop consumers

@Component
public class DirectCustomer {
    
    

    @RabbitListener(bindings ={
    
    
            @QueueBinding(
                    value = @Queue(),
                    key={
    
    "info","error"},
                    exchange = @Exchange(type = "direct",name="directs")
            )})
    public void receive1(String message){
    
    
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings ={
    
    
            @QueueBinding(
                    value = @Queue(),
                    key={
    
    "error"},
                    exchange = @Exchange(type = "direct",name="directs")
            )})
    public void receive2(String message){
    
    
        System.out.println("message2 = " + message);
    }
}

Topic subscription model (dynamic routing model)

development producer

@Autowired
private RabbitTemplate rabbitTemplate;

//topic
@Test
public void testTopic(){
    
    
  rabbitTemplate.convertAndSend("topics","user.save.findAll","user.save.findAll 的消息");
}

Develop consumers

@Component
public class TopCustomer {
    
    
    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,
                    key = {
    
    "user.*"},
                    exchange = @Exchange(type = "topic",name = "topics")
            )
    })
    public void receive1(String message){
    
    
        System.out.println("message1 = " + message);
    }

    @RabbitListener(bindings = {
    
    
            @QueueBinding(
                    value = @Queue,
                    key = {
    
    "user.#"},
                    exchange = @Exchange(type = "topic",name = "topics")
            )
    })
    public void receive2(String message){
    
    
        System.out.println("message2 = " + message);
    }
}

Guess you like

Origin blog.csdn.net/zyb18507175502/article/details/127690703