RabbitMQ installs and integrates springboot in docker

docker run -d --name rabbitmq -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 -p 15671:15671 -p 15672:15672 rabbitmq:management

Port resolution:

4369, 25672 (Erlang discovery & cluster ports)

5672, 5671 (AMQP ports)

15672 (web management background port)

61613, 61614 (STOMP protocol ports)

1883, 8883 (MQTT protocol port)

Networking and RabbitMQ — RabbitMQ

RabbitMQ operating mechanism

Message Routing in AMQP

  • There are some differences between the routing process of messages in AMQP and the JMS familiar to Java developers. The roles of Exchange and Binding are added to AMQP. The producer publishes the message to the Exchange, the message finally reaches the queue and is received by the consumer, and Binding decides which queue the exchange's message should be sent to.

Exchange type

When Exchange distributes messages, there are different distribution strategies according to different types. Currently, there are four types: direct, fanout, topic, and headers. The headers match the header of the AMQP message instead of the routing key. The headers switch is exactly the same as the direct switch, but the performance is much worse, and it is almost unused at present, so directly look at the other three types:

If the routing key in the message is consistent with the binding key in Binding, the switch will send the message to the corresponding queue. The routing key exactly matches the queue name. If a queue is bound to an exchange and requires the routing key to be "dog", only messages whose routingkey is marked as "dog" will be forwarded, neither "dog.puppy" nor "dog.puppy" will be forwarded. .guard” and so on. It is an exact match, unicast mode.

Every message sent to a fanout type exchange will be assigned to all bound queues. The fanout exchange does not handle routing keys, but simply binds queues to the exchange, and every message sent to the exchange will be forwarded to all queues bound to the exchange. Much like subnet broadcasting, every host on the subnet gets a copy of the message. The fanout type forwarding messages is the fastest.

The topic exchange assigns the routing key attribute of the message through pattern matching, and matches the routing key with a certain pattern. At this time, the queue needs to be bound to a pattern. It splits the string of routing keys and binding keys into words separated by dots. It also recognizes two wildcard characters: the symbol "#" and the symbol "*". # matches 0 or more words, * matches one word.

RabbitMQ integrates springboot

1. Introduce spring-boot-starter-amqp

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

2. application.yml configuration

spring:
    rabbitmq:
      host: 192.168.159.4
      port: 5672
      virtual-host: /

3. Test RabbitMQ

@Resource
AmqpAdmin amqpAdmin;

@Resource
RabbitTemplate rabbitTemplate;

/**
 * 创建交换机
 */
@Test
void creatExchange() {
    //String name, boolean durable, boolean autoDelete
    DirectExchange directExchange = new DirectExchange("java-exchange", true, false);
    amqpAdmin.declareExchange(directExchange);
    System.out.println("xchange创建成功");
}

@Test
void creatQueue() {
    Queue queue = new Queue("java-queue",true,false,false);
    amqpAdmin.declareQueue(queue);
    System.out.println("queue创建成功");
}

/**
 * String destination 目的地
 * Binding.DestinationType destinationType 目的地类型
 * String exchange 交换机
 * String routingKey 路由键
 * @Nullable Map<String, Object> arguments
 * 将exchange交换机和destination目的地进行绑定
 */
@Test
void creatBinding() {
    Binding binding = new Binding("java-queue",
            Binding.DestinationType.QUEUE,
            "java-exchange",
            "hello.java",
            null);
    amqpAdmin.declareBinding(binding);
    System.out.println("Binding创建成功");
}

@Test
void sendMessage() {
    //1、如果发送的是对象内容必须采用序列化机制,对象必须实现Serializable接口
    DeviceEntity deviceEntity = new DeviceEntity();
    deviceEntity.setDivId(1);
    deviceEntity.setDivName("测试");
    deviceEntity.setDivCreate(new Date().toString());
    //2、如果发送的对象是一个json
    //配置config配置文件
    //测试发送消息
    rabbitTemplate.convertAndSend("java-exchange","hello.java",deviceEntity);
    log.info("消息发送成功:{}",deviceEntity);
}

1. AmqpAdmin: management component

2. RabbitTemplate: message sending processing component

3. The @RabbitListener method of listening to messages can have three parameters (regardless of quantity and order)

• Object content, Message message, Channel channel

@Slf4j
@Service
@RabbitListener(queues = {"java-queue"})
public class TestDemoImpl implements ITestDemo {

    @Autowired
    SearchFeignService searchFeignService;

    @Autowired
    TestDemoMapper testDemoMapper;

    /**
     *  消息接收
     *  class org.springframework.amqp.core.Message
     *  Message 1、原生消息详细信息
     *  2、发送的消息类型:T<发送的消息类型> DeviceEntity count
     *  3、当前传输数据的通道 Channel channel
     *
     *  Queue:可以很多人来接收消息,只要收到消息,队列删除消息,而且只能收到一个消息
     *  场景:
     *      1、同时启动多个服务,只有一个可以接收到消息
     *      2、只有当消息处理完成后才能进行下一个消息处理
     *
     *
     *  @RabbitListener 可以在类和方法上,监听哪些队列
     *  @RabbitHandler 只能在方法上面,可以重载区分不同的消息
     *
     * @param message
     */
    @RabbitHandler
    public void receiveMessage(Message message, DeviceEntity count, Channel channel) {
        byte[] body = message.getBody();
        log.info("接收到的消息为:{},消息类型为:{}",message,message.getClass());
        log.info("消息体真正内容:{}",count.toString());
    }

}

Guess you like

Origin blog.csdn.net/wzw_wwl/article/details/130987750