【问题标题】:Micrometer integration with Spring Cloud Stream千分尺与 Spring Cloud Stream 的集成
【发布时间】:2021-09-09 08:08:03
【问题描述】:

我正在将 Spring Cloud Stream 应用程序迁移到函数式方法。到目前为止,我使用的是提供 Spring Integration 的千分尺度量。见Spring Integration - Micrometer Integration

自迁移以来,使用 StreamBridge 生成的消息不再生成指标。这似乎是因为 Spring Integration 在将通道定义为 bean 时创建了它的指标,而 StreamBridge 则动态创建了通道。

话虽如此,我的问题是:

  • SCS 是否提供任何其他类型的指标?
  • 如果没有,有什么方法可以强制应用程序事先将通道声明为 bean?

提前致谢。

更新

以下测试表明,即使在属性中定义绑定目标,也不会在启动时创建 bean。

用于测试的版本是:

弹簧启动:2.5.4

spring-cloud-stream: 3.1.3

class MessageChannelBeanTests {

    @Test
    void declaredOutputBindingsCreatesMessageChannelBeans() {

        TestChannelBinderConfiguration.applicationContextRunner(MessageChannelBeanTestsConfiguration.class)
                .withPropertyValues("spring.cloud.stream.bindings.myBinding.destination=myDestination",
                        "spring.cloud.stream.bindings.syso-in-0.destination=my-topic")
                .run(context -> {
                    final Map<String, MessageChannel> channels = context.getBeansOfType(MessageChannel.class);


                    /*
                     * Just to see which channels are declared as a bean
                     * The ouput is:
                     *
                     * nullChannel
                     * errorChannel
                     * syso-in-0
                     * my-topic.anonymous.errors
                     */
                    channels.entrySet()
                            .stream()
                            .map(Entry::getKey)
                            .forEach(System.out::println);

                    // Asserts that the channel is created as a bean even if the binding is set by properties
                    assertThat(channels)
                            .containsKey("myBinding");


                });

    }


    @EnableAutoConfiguration
    static class MessageChannelBeanTestsConfiguration {

        @Bean
        public Consumer<String> syso() {
            return System.out::println;
        }
    }
}

请注意,创建的 MessageChannel bean 是:

nullChannel 错误频道 系统输入-0 我的主题.anonymous.errors

但是没有'myBinding'的踪迹

【问题讨论】:

    标签: spring-cloud-stream micrometer


    【解决方案1】:

    是的,如果您为不存在的绑定定义了一个属性,它将提前创建为一个 bean。例如,假设在消息传递系统中,您有一个名为myDestination 的目的地(队列、主题等)。比你可以拥有这样的东西

    spring.cloud.stream.bindings.myBinding.destination=myDestination

    并使用网桥发送给myBinding

    【讨论】:

    • 它似乎对我不起作用。我不知道我是否做错了什么......我刚刚更新了问题以显示一个测试来检查它(我的应用程序中也发生了同样的事情)。感谢 Oleg 看一看。
    猜你喜欢
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2020-10-24
    • 2018-04-28
    相关资源
    最近更新 更多