【问题标题】:SpringBoot actuator/health/ to reflect Kinesis streams not presentSpring Boot actuator/health/ 以反映 Kinesis 流不存在
【发布时间】:2022-07-08 22:25:00
【问题描述】:

我正在尝试为 SpringBoot 应用程序构建自定义 KinesisBinderHealthIndicator
当出现在 AWS 中的 Kinesis 流的实际列表与作为 spring.cloud.stream.bindings 目标的 application.properties 文件中声明的流不匹配时,我的自定义实现应该在 /actuator/health 端点下显示健康 DOWN 状态(例如:当流具有已删除或未自动创建到 Kinesis 中)
application.properties:

spring.cloud.stream.bindings.my-first-stream-in-0.destination=my-first-stream
spring.cloud.stream.bindings.my-second-stream-in-0.destination=my-second-stream

aws kinesis 列表流:

aws --endpoint-url=http://localhost:4566 kinesis list-streams
{
    "StreamNames": [
        "my-first-stream",
    ]
}

我知道如何覆盖默认的 KinesisBinderHealthIndicator 实现以及如何获取可用 Kinesis 流的实际列表:

@Primary
@Component("kinesisBinderHealthIndicator")
@ComponentScan(basePackages = "org.springframework.cloud.stream.binder.kinesis")
@RequiredArgsConstructor
public class CustomKinesisBinderHealthIndicator implements HealthIndicator {

    private final KinesisMessageChannelBinder kinesisMessageChannelBinder;
    private final KinesisExtendedBindingProperties kinesisExtendedBindingProperties;

    @Override
    public Health health() {
        try {
            List<String> actualKinesisStreams = new ArrayList<>(this.kinesisMessageChannelBinder.getStreamsInUse());

            //code to retrieve list of kinesis streams from destination bindings
        } catch (Exception e) {
            return Health.down(e)).build();
        }
    }
}

您能否告诉我有关从 Spring Cloud 流绑定目标检索流列表的任何提示?

非常感谢!

【问题讨论】:

    标签: java spring spring-boot amazon-kinesis spring-boot-actuator


    【解决方案1】:

    设法弄清楚这一点,以防万一其他人需要这个: 为了获取已定义流的列表,您将需要一个 KinesisExtendedBindingProperties 对象来从 application.properties 文件中提取在 spring.cloud.stream.bindings 下定义的绑定。
    之后,只需遍历 kinesisExtendedBindingProperties.getBindings() 返回的 Map 并使用 BindingServiceProperties 对象来提取绑定属性(在我们的例子中,destination 代表流名称)。

            List<String> destinationStreams = new ArrayList<>();
    
            kinesisExtendedBindingProperties.getBindings().forEach((k, v) -> {
                Optional<BindingProperties> streamNameForBinding = Optional.ofNullable(bindingServiceProperties.getBindings().get(k));
                streamNameForBinding.ifPresent(bindingProperties -> destinationStreams.add(bindingProperties.getDestination()));
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 2019-08-05
      • 1970-01-01
      • 2020-03-25
      相关资源
      最近更新 更多