其他分享
首页 > 其他分享> > Spring Cloud Stream RabbitMQ

Spring Cloud Stream RabbitMQ

作者:互联网

我试图理解为什么我要将Rabbit云流与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个队列并依赖于路由关键路由到这两个队列中的任何一个,如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实现(Kafka,RabbitMQ,JMS绑定器等)将应用程序连接(通过@EnableBinding)到外部消息系统,从而开发事件驱动的微服务应用程序.显然,Spring Cloud Stream使用Spring AMQP进行RabbitMQ绑定器实现.

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

例如,您需要让2个使用者根据您的条件设置相应的bindingRoutingKey,并使用上面提到的属性(routing-key-expression,destination)的单个生成器(组除外).我注意到您已为出站通道配置了组.组属性仅适用于消费者(因此是入站).

您可能还想检查这个:https://github.com/spring-cloud/spring-cloud-stream-binder-rabbit/issues/57,因为我看到有关使用routing-key-expression的一些讨论.具体来说,使用表达式值检查this.

标签:spring,spring-boot-2,spring-cloud-stream
来源: https://codeday.me/bug/20190627/1306456.html