spring-cloud / spring-cloud-stream

Framework for building Event-Driven Microservices
http://cloud.spring.io/spring-cloud-stream
Apache License 2.0
969 stars 595 forks source link

Allow disabling particular binder from health #2944

Closed wyhasany closed 4 weeks ago

wyhasany commented 1 month ago

My app uses binders for rabbit and kafka. When rabbit is down my app should return 200 OK on actuator health. However when the kafka is down it should not return 200 OK. With current API I need to use nasty reflection to achieve this behavior:

import java.lang.reflect.Field;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.stream.config.BindersHealthIndicatorAutoConfiguration.BindersHealthContributor;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DisableRabbitBindingContributor implements ApplicationListener<ContextStartedEvent> {

    private final BindersHealthContributor bindersHealthContributor;

    @SuppressWarnings({"squid:S3011", "sqid:S112"})
    @Override
    public void onApplicationEvent(ContextStartedEvent event) {
        try {
            Class<? extends BindersHealthContributor> bindersHealthContributorClass = bindersHealthContributor.getClass();
            Field contributorsField = bindersHealthContributorClass.getDeclaredField("contributors");
            contributorsField.setAccessible(true);
            Object contributors = contributorsField.get(bindersHealthContributor);
            if (contributors instanceof Map contributorsMap) {
                contributorsMap.remove("rabbit");
            }
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }
}

It would be nice to add some optional bean for filtering binders contributors.

sobychacko commented 1 month ago

It sounds like you are running the app in a multi-binder environment. Did you try to disable the health indicator in the Rabbit binder's environment? https://docs.spring.io/spring-cloud-stream/reference/spring-cloud-stream/health-indicator.html.

sobychacko commented 4 weeks ago

Closing the issue due to no activity. Feel free to re-open it if you are still facing this issue.