Spring Cloud Stream Rabbit 3.2.1 消息路由

1.简介

在之前的RabbitMQ入门之常见模式一文中,我们已经介绍过Routing模式,当QueueExchange绑定的时候,设置了RoutingKey,只有RoutingKey完全匹配的消息才会被路由到对应的Queue中。Routing的模式的模型如下:

Spring Cloud Stream Rabbit 3.2.1 消息路由_第1张图片

2.Spring Cloud Stream Rabbit实现消息路由

2.1 配置文件

# binder
spring.cloud.stream.binders.rabbitmqbr.type=rabbit
spring.cloud.stream.binders.rabbitmqbr.environment.spring.rabbitmq.host=${rabbitmq.host}
spring.cloud.stream.binders.rabbitmqbr.environment.spring.rabbitmq.port=${rabbitmq.port}
spring.cloud.stream.binders.rabbitmqbr.environment.spring.rabbitmq.username=${rabbitmq.username}
spring.cloud.stream.binders.rabbitmqbr.environment.spring.rabbitmq.password=${rabbitmq.password}
spring.cloud.stream.binders.rabbitmqbr.environment.spring.rabbitmq.virtual-host=${rabbitmq.virtual-host}
spring.cloud.stream.default-binder=rabbitmqbr

# producer
spring.cloud.stream.bindings.producerQueue-out-0.binder=rabbitmqbr
spring.cloud.stream.bindings.producerQueue-out-0.destination=notify.exchange
spring.cloud.stream.bindings.producerQueue-out-0.contentType=application/json

# consumer1
spring.cloud.stream.bindings.consumer1Queue-in-0.binder=rabbitmqbr
spring.cloud.stream.bindings.consumer1Queue-in-0.group=consumer1.group1
spring.cloud.stream.bindings.consumer1Queue-in-0.destination=notify.exchange
spring.cloud.stream.bindings.consumer1Queue-in-0.content-type=application/json

# consumer2
spring.cloud.stream.bindings.consumer2Queue-in-0.binder=rabbitmqbr
spring.cloud.stream.bindings.consumer2Queue-in-0.group=consumer2.group1
spring.cloud.stream.bindings.consumer2Queue-in-0.destination=notify.exchange
spring.cloud.stream.bindings.consumer2Queue-in-0.content-type=application/json

#routing
spring.cloud.stream.rabbit.bindings.producerQueue-out-0.producer.routing-key-expression=headers.type
spring.cloud.stream.rabbit.bindings.producerQueue-out-0.producer.exchange-type=direct
spring.cloud.stream.rabbit.bindings.consumer1Queue-in-0.consumer.binding-routing-key=consumer1
spring.cloud.stream.rabbit.bindings.consumer1Queue-in-0.consumer.exchange-type=direct
spring.cloud.stream.rabbit.bindings.consumer2Queue-in-0.consumer.binding-routing-key=consumer2
spring.cloud.stream.rabbit.bindings.consumer2Queue-in-0.consumer.exchange-type=direct

# queue name
spring.cloud.function.definition=consumer1Queue;consumer2Queue

spring.cloud.stream.rabbit.bindings.producerQueue-out-0.producer.exchange-type=direct:表示Exchange的类型为direct,这种类型的Exchange,当我们使用routingKey时,消息的routingKey完全匹配的情况下,消息才会路由到指定的queue

spring.cloud.stream.rabbit.bindings.producerQueue-out-0.producer.routing-key-expression=headers.type:表示在消息路由时,判断消息的headerstype属性的值。

2.2 生产者

@Autowired
private StreamBridge streamBridge;

public void sendMsg(){
    Map<String, Long> payload = Map.of("msg", "consumer1消息");
    Message<Map<String, Long>> message = MessageBuilder.withPayload(payload).setHeader("type", "consumer1").build();
    streamBridge.send("producerQueue-out-0", message);
    payload = Map.of("msg", “consumer2消息);
    message = MessageBuilder.withPayload(payload).setHeader("type", "consumer2").build();
    streamBridge.send("producerQueue-out-0", message);
}

2.3 消费者

@Component("consumer1Queue")
public class Consumer1QueueListener implements Consumer<JSONObject> {

    @Override
    public void accept(JSONObject jsonObject) {
        String text = jsonObject.getString("msg");
        log.debug("收到consumer1消息通知:{}", text);
    }
}

@Component("consumer2Queue")
public class Consumer2QueueListener implements Consumer<JSONObject> {

    @Override
    public void accept(JSONObject jsonObject) {
        String text = jsonObject.getString("msg");
        log.debug("收到consumer2消息通知:{}", text);
    }
}

需要注意的是,消费者接受消息时使用的类型需要与生产者发送的消息体类型一致,否则会出现消息无法消费并丢失。

你可能感兴趣的:(Spring,Cloud,RabbitMQ,spring,cloud,rabbitmq,java)