Spring Cloud Streams ..源和接收器的多个动态目标

时间:2019-03-07 00:38:41

标签: spring spring-boot spring-integration spring-cloud spring-cloud-stream

我的系统上有一个更改请求,该请求当前侦听多个通道并将消息也发送到多个通道,但是现在目标名称将在数据库中并且可以随时更改。 我很难相信自己是第一个遇到这个问题的人,但是我看到的信息有限。

我发现的只是这两个...
动态接收器目标: https://github.com/spring-cloud-stream-app-starters/router/tree/master/spring-cloud-starter-stream-sink-router,但是如何像@StreamListener一样主动监听那些频道呢?

动态源目标: https://github.com/spring-cloud/spring-cloud-stream-samples/blob/master/source-samples/dynamic-destination-source/,该操作

@Bean
    @ServiceActivator(inputChannel = "sourceChannel")
    public ExpressionEvaluatingRouter router() {
        ExpressionEvaluatingRouter router = new ExpressionEvaluatingRouter(new SpelExpressionParser().parseExpression("payload.id"));
        router.setDefaultOutputChannelName("default-output");
        router.setChannelResolver(resolver);
        return router;
    }

但是“ payload.id”是什么?在哪里指定目的地?

1 个答案:

答案 0 :(得分:0)

随时改善我的答案,希望对其他人有帮助。

现在代码(在我的调试器中有效)。这是一个示例,尚未准备好生产!

这是向动态目的地发送消息的方法

import org.springframework.messaging.MessageChannel;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.binding.BinderAwareChannelResolver;


@Service
@EnableBinding
public class MessageSenderService {

    @Autowired
    private BinderAwareChannelResolver resolver;

    @Transactional
    public void sendMessage(final String topicName, final String payload) {
        final MessageChannel messageChannel = resolver.resolveDestination(topicName);
        messageChannel.send(new GenericMessage<String>(payload));
    }
}

Spring Cloud Stream的配置。

spring:
  cloud:
    stream:
      dynamicDestinations: output.topic.1,output.topic2,output.topic.3

我在这里找到 https://docs.spring.io/spring-cloud-stream/docs/Elmhurst.RELEASE/reference/htmlsingle/index.html#dynamicdestination 它将在Spring Cloud Stream 2+版本中运行。我使用2.1.2

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream</artifactId>
    <version>2.1.2.RELEASE</version>
</dependency>

这是从动态目标接收消息的方法

https://stackoverflow.com/a/56148190/4587961

配置

spring:
  cloud:
    stream:
      default:
        consumer:
          concurrency: 2
          partitioned: true
      bindings:
        # inputs
        input:
          group: application_name_group
          destination: topic-1,topic-2
          content-type: application/json;charset=UTF-8

Java使用者。

@Component
@EnableBinding(Sink.class)
public class CommonConsumer {

    private final static Logger logger = LoggerFactory.getLogger(CommonConsumer.class);

    @StreamListener(target = Sink.INPUT)
    public void consumeMessage(final Message<Object> message) {
        logger.info("Received a message: \nmessage:\n{}", message.getPayload());
        // Here I define logic which handles messages depending on message headers and topic.
        // In my case I have configuration which forwards these messages to webhooks, so I need to have mapping topic name -> webhook URI.
    }
}