Spring Actuator + Kafka Streams-将Kafka流状态添加到运行状况检查端点

时间:2019-09-19 08:56:44

标签: spring spring-boot apache-kafka

我有一个Spring Boot应用程序,我在其中使用apache kafka-streams。我不使用春季云流。我添加了执行器运行状况检查端点。我在application.yml中这样配置它:

management:
  health.db.enabled: false
  endpoints.web:
    base-path:
    path-mapping.health: /

当引发运行时异常并且日志显示时我的流已停止,但运行状况检查状态为UP。

2019-09-17 13:16:31.522 INFO 1 --- [ Thread-5] org.apache.kafka.streams.KafkaStreams : stream-client [lpp-model-stream-7e6e8fea-fcad-4033-92a4-5ede50de6e17] Streams client stopped complet伊莉

如何将kafka流状态绑定到运行状况检查端点?

我的pom.xml:

  <dependencies>
            <dependency>
                <groupId>org.springframework.kafka</groupId>
                <artifactId>spring-kafka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.kafka</groupId>
                <artifactId>kafka-streams</artifactId>
            </dependency>
            <dependency>
                <groupId>data-wizards</groupId>
                <artifactId>lpp-common-avro</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>io.confluent</groupId>
                <artifactId>kafka-streams-avro-serde</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
            </dependency>
            <dependency>
                <groupId>io.vavr</groupId>
                <artifactId>vavr</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

链接到创建流的代码:https://gist.github.com/solveretur/fc4fdd6c7663dc4d58fe72d48029f9c3

2 个答案:

答案 0 :(得分:2)

KafkaStreams维护内存中 State,该内存可以映射到执行器的健康状态。状态可以是以下之一:CREATEDERRORNOT_RUNNINGPENDING_SHUTDOWNREBALANCINGRUNNING-它们是不言自明的。有关状态转换,请参见文档https://kafka.apache.org/11/javadoc/org/apache/kafka/streams/KafkaStreams.State.html

如果您要查找完整的示例,则可以按照以下示例进行操作,并根据需要进行更新(例如,您不可以将CREATED视为UP)。确保在应用程序上下文中具有类型KafkaStreams的bean。

//Note that class name prefix before `HealthIndicator` will be camel-cased
//and used as a health component name, `kafkaStreams` here
@Component
public class KafkaStreamsHealthIndicator implements HealthIndicator {

    //if you have multiple instances, inject as Map<String, KafkaStreams>
    //Spring will map KafkaStreams instances by bean names present in context
    //so you can provide status details for each stream by name
    @Autowired
    private KafkaStreams kafkaStreams; 

    @Override
    public Health health() {
        State kafkaStreamsState = kafkaStreams.state();

        // CREATED, RUNNING or REBALANCING
        if (kafkaStreamsState == State.CREATED || kafkaStreamsState.isRunning()) {
            //set details if you need one
            return Health.up().build();
        }

        // ERROR, NOT_RUNNING, PENDING_SHUTDOWN, 
        return Health.down().withDetail("state", kafkaStreamsState.name()).build();
    }
}

然后,运行状况端点将显示为:

{
    "status": "UP",
    "kafkaStreams": {
        "status": "DOWN",
        "details": {  //not included if "UP"
            "state": "NOT_RUNNING"
        }
    }
}

答案 1 :(得分:0)

健康信息是从在您的应用程序上下文中配置的,实现了HealthIndicator接口的所有bean收集的。

您可以创建一个自定义HealthIndicator,可用于报告Kafka Streams错误。

按如下所示创建您的HealthIndicator单例bean

@Component
public class MyHealthIndicator implements HealthIndicator {
    private Exception caughtException = null;
    // Any other information you want to store.
    @Override
    public Health health() {

        if (caughtException == null) {
            return Health
                .up()
                .withDetail("status", "Kafka Streams Running")
                .build();
        }
        else {
            return Health
                .down()
                .withDetail("status", "Not Available")
                .withDetail("error", caughtException.getMessage())
                .build();
        }
    }
    private void setException(Exception caughtException) {
        this.caughtException = caughtException;
    }
}

然后,您可以在正在使用Kafka Streams的位置上自动装配该bean,并可以如下设置异常。

public class MyApp {
    @Autowire
    private MyHealthIndicator healthIndicator; // You can also use constructor injection instead. 

    // Rest of the code


    public void init() {
        // Streams initialization code here
        streams.setUncaughtExceptionHandler((Thread thread, Throwable throwable) -> {
            healthIndicator.setException(throwable);
        });
    }

}

我希望这会有所帮助,但如果没有帮助,请举一个Minimal, Verifiable, Reproducible可以供他人使用的示例