Spring 云流 RabbitMQ

Spring Cloud Stream RabbitMQ

我想了解为什么我想将 Spring 云流与 RabbitMQ 一起使用。我看过 RabbitMQ Spring 教程 4 (https://www.rabbitmq.com/tutorials/tutorial-four-spring-amqp.html),这基本上就是我想要做的。它创建了一个带有 2 个附加队列的直接交换,并且根据路由键将消息路由到 Q1 或 Q2。

如果您查看教程,整个过程非常简单,您创建所有部件,将它们绑定在一起,然后就可以开始了。

我想知道使用 Sing Cloud Stream 我会得到什么好处,如果这就是它的用例。创建一个简单的交换很容易,甚至定义目的地和组也可以直接使用流。所以我想为什么不更进一步,尝试用流处理教程案例。

我看到 Stream 有一个 BinderAwareChannelResolver 似乎做同样的事情。但我正在努力将它们放在一起以实现与 RabbitMQ Spring 教程中相同的效果。我不确定这是否是一个依赖性问题,但我似乎从根本上误解了一些东西,我想是这样的:

spring.cloud.stream.bindings.output.destination=myDestination
spring.cloud.stream.bindings.output.group=consumerGroup
spring.cloud.stream.rabbit.bindings.output.producer.routing-key-expression='key'

应该就可以了。

有没有人提供源和汇的最小示例,它基本上创建直接交换,将 2 个队列绑定到它,并根据路由关键路由到这 2 个队列中的任何一个,如 https://www.rabbitmq.com/tutorials/tutorial-four-spring-amqp.html

编辑:

下面是一组最小的代码,演示了如何按照我的要求进行操作。我没有附上 build.gradle 因为它很简单(但如果有人感兴趣,请告诉我)

application.properties: 设置生产者

spring.cloud.stream.bindings.output.destination=tut.direct
spring.cloud.stream.rabbit.bindings.output.producer.exchangeType=direct
spring.cloud.stream.rabbit.bindings.output.producer.routing-key-expression=headers.type

Sources.class: 设置生产者频道

public interface Sources {

    String OUTPUT = "output";

    @Output(Sources.OUTPUT)
    MessageChannel output();
}

StatusController.class: 响应休息呼叫并使用特定路由键发送消息

/**
 * Status endpoint for the health-check service.
 */
@RestController
@EnableBinding(Sources.class)
public class StatusController {

    private int index;

    private int count;

    private final String[] keys = {"orange", "black", "green"};

    private Sources sources;

    private StatusService status;

    @Autowired
    public StatusController(Sources sources, StatusService status) {
        this.sources = sources;
        this.status = status;
    }

    /**
     * Service available, service returns "OK"'.
     * @return The Status of the service.
     */
    @RequestMapping("/status")
    public String status() {
        String status = this.status.getStatus();

        StringBuilder builder = new StringBuilder("Hello to ");
        if (++this.index == 3) {
            this.index = 0;
        }
        String key = keys[this.index];
        builder.append(key).append(' ');
        builder.append(Integer.toString(++this.count));
        String payload = builder.toString();
        log.info(payload);

        // add kv pair - routingkeyexpression (which matches 'type') will then evaluate
        // and add the value as routing key
        Message<String> msg = new GenericMessage<>(payload, Collections.singletonMap("type", key));
        sources.output().send(msg);

        // return rest call
        return status;
    }
}

事物的消费者方面,属性:

spring.cloud.stream.bindings.input.destination=tut.direct
spring.cloud.stream.rabbit.bindings.input.consumer.exchangeType=direct
spring.cloud.stream.rabbit.bindings.input.consumer.bindingRoutingKey=orange
spring.cloud.stream.bindings.inputer.destination=tut.direct
spring.cloud.stream.rabbit.bindings.inputer.consumer.exchangeType=direct
spring.cloud.stream.rabbit.bindings.inputer.consumer.bindingRoutingKey=black

Sinks.class:

public interface Sinks {

    String INPUT = "input";

    @Input(Sinks.INPUT)
    SubscribableChannel input();

    String INPUTER = "inputer";

    @Input(Sinks.INPUTER)
    SubscribableChannel inputer();
}

ReceiveStatus.class:接收状态:

@EnableBinding(Sinks.class)
public class ReceiveStatus {
    @StreamListener(Sinks.INPUT)
    public void receiveStatusOrange(String msg) {
       log.info("I received a message. It was orange number: {}", msg);
    }

    @StreamListener(Sinks.INPUTER)
    public void receiveStatusBlack(String msg) {
        log.info("I received a message. It was black number: {}", msg);
    }
}

Spring Cloud Stream 允许您使用 Spring Cloud Stream Binder 实现使应用程序连接(通过 @EnableBinding)到外部消息传递系统,从而开发事件驱动的微服务应用程序(Kafka、RabbitMQ、JMS 绑定器等)。显然,Spring Cloud Stream 使用 Spring AMQP 实现 RabbitMQ 活页夹。

BinderAwareChannelResolver 适用于对生产者的动态绑定支持,我认为在您的情况下,它是关于配置交换以及消费者与该交换的绑定。

例如,您需要根据您的标准设置 2 个具有适当 bindingRoutingKey 的消费者,以及一个具有上述属性(路由键表达式、目标)的生产者(组除外).我注意到您已经为出站通道配置了 groupgroup 属性 仅适用于消费者(因此是入站)。

您可能还想检查这个:https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit/issues/57 as I see some discussion around using routing-key-expression. Specifically, check this 关于使用表达式值的一个。