Cly*_*row 5 spring apache-kafka spring-boot
我有一个使用 apache kafka-streams 的 Spring Boot 应用程序。我不使用 spring 云流。我添加了执行器健康检查端点。我是application.yml这样配置的:
management:
health.db.enabled: false
endpoints.web:
base-path:
path-mapping.health: /
Run Code Online (Sandbox Code Playgroud)
当抛出运行时异常并且我的流被停止时,日志显示但运行状况检查状态为 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>
Run Code Online (Sandbox Code Playgroud)
链接到我创建流的代码:https : //gist.github.com/solveretur/fc4fdd6c7663dc4d58fe72d48029f9c3
KafkaStreams 维护一个内存中 State,可以映射到执行器的健康状态。状态可以是以下之一:CREATED, ERROR, NOT_RUNNING, PENDING_SHUTDOWN, REBALANCING, RUNNING- 它们是不言自明的。请参阅状态转换的文档https://kafka.apache.org/11/javadoc/org/apache/kafka/streams/KafkaStreams.State.html
如果您正在寻找一个完整的示例,您可以使用以下示例并根据您的需要更新它(例如,您可能不计CREATED为状态 UP)。确保KafkaStreams在应用程序上下文中有一个类型为 bean 的 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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后健康端点将显示如下:
{
"status": "UP",
"kafkaStreams": {
"status": "DOWN",
"details": { //not included if "UP"
"state": "NOT_RUNNING"
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5122 次 |
| 最近记录: |