Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
7cd19822
Commit
7cd19822
authored
Feb 08, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Add Kafka health indicator"
Closes gh-11515
parent
0dbd9429
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
102 additions
and
96 deletions
+102
-96
pom.xml
...g-boot-project/spring-boot-actuator-autoconfigure/pom.xml
+5
-5
KafkaHealthIndicatorAutoConfiguration.java
...onfigure/kafka/KafkaHealthIndicatorAutoConfiguration.java
+22
-26
KafkaHealthIndicatorProperties.java
...e/autoconfigure/kafka/KafkaHealthIndicatorProperties.java
+2
-1
package-info.java
...mework/boot/actuate/autoconfigure/kafka/package-info.java
+1
-1
additional-spring-configuration-metadata.json
...es/META-INF/additional-spring-configuration-metadata.json
+6
-6
spring.factories
...utoconfigure/src/main/resources/META-INF/spring.factories
+1
-1
pom.xml
spring-boot-project/spring-boot-actuator/pom.xml
+9
-9
KafkaHealthIndicator.java
...ingframework/boot/actuate/kafka/KafkaHealthIndicator.java
+13
-13
package-info.java
.../org/springframework/boot/actuate/kafka/package-info.java
+1
-1
KafkaHealthIndicatorTests.java
...amework/boot/actuate/kafka/KafkaHealthIndicatorTests.java
+37
-33
appendix-application-properties.adoc
...cs/src/main/asciidoc/appendix-application-properties.adoc
+2
-0
production-ready-features.adoc
...oot-docs/src/main/asciidoc/production-ready-features.adoc
+3
-0
No files found.
spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml
View file @
7cd19822
...
@@ -193,11 +193,6 @@
...
@@ -193,11 +193,6 @@
<artifactId>
elasticsearch
</artifactId>
<artifactId>
elasticsearch
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.kafka
</groupId>
<artifactId>
spring-kafka
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.flywaydb
</groupId>
<groupId>
org.flywaydb
</groupId>
<artifactId>
flyway-core
</artifactId>
<artifactId>
flyway-core
</artifactId>
...
@@ -311,6 +306,11 @@
...
@@ -311,6 +306,11 @@
<artifactId>
spring-integration-core
</artifactId>
<artifactId>
spring-integration-core
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.kafka
</groupId>
<artifactId>
spring-kafka
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.security
</groupId>
<groupId>
org.springframework.security
</groupId>
<artifactId>
spring-security-config
</artifactId>
<artifactId>
spring-security-config
</artifactId>
...
...
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/kafka/KafkaHealthIndicatorAutoConfiguration.java
View file @
7cd19822
...
@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
...
@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.AutoConfigureBefore
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
;
import
org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
...
@@ -41,40 +42,35 @@ import org.springframework.kafka.core.KafkaAdmin;
...
@@ -41,40 +42,35 @@ import org.springframework.kafka.core.KafkaAdmin;
* @author Juan Rada
* @author Juan Rada
*/
*/
@Configuration
@Configuration
@ConditionalOnClass
(
KafkaAdmin
.
class
)
@ConditionalOnBean
(
KafkaAdmin
.
class
)
@ConditionalOnEnabledHealthIndicator
(
"kafka"
)
@ConditionalOnEnabledHealthIndicator
(
"kafka"
)
@AutoConfigureBefore
(
HealthIndicatorAutoConfiguration
.
class
)
@AutoConfigureBefore
(
HealthIndicatorAutoConfiguration
.
class
)
@AutoConfigureAfter
(
KafkaAutoConfiguration
.
class
)
@AutoConfigureAfter
(
KafkaAutoConfiguration
.
class
)
public
class
KafkaHealthIndicatorAutoConfiguration
{
@EnableConfigurationProperties
(
KafkaHealthIndicatorProperties
.
class
)
public
class
KafkaHealthIndicatorAutoConfiguration
extends
CompositeHealthIndicatorConfiguration
<
KafkaHealthIndicator
,
KafkaAdmin
>
{
@Configuration
private
final
Map
<
String
,
KafkaAdmin
>
admins
;
@ConditionalOnBean
(
KafkaAdmin
.
class
)
@EnableConfigurationProperties
(
KafkaHealthIndicatorProperties
.
class
)
static
class
KafkaClientHealthIndicatorConfiguration
extends
CompositeHealthIndicatorConfiguration
<
KafkaHealthIndicator
,
KafkaAdmin
>
{
private
final
Map
<
String
,
KafkaAdmin
>
admin
s
;
private
final
KafkaHealthIndicatorProperties
propertie
s
;
private
final
KafkaHealthIndicatorProperties
properties
;
KafkaHealthIndicatorAutoConfiguration
(
Map
<
String
,
KafkaAdmin
>
admins
,
KafkaHealthIndicatorProperties
properties
)
{
KafkaClientHealthIndicatorConfiguration
(
Map
<
String
,
KafkaAdmin
>
admins
,
this
.
admins
=
admins
;
KafkaHealthIndicatorProperties
properties
)
{
this
.
properties
=
properties
;
this
.
admins
=
admins
;
}
this
.
properties
=
properties
;
}
@Bean
@ConditionalOnMissingBean
(
name
=
"kafkaHealthIndicator"
)
public
HealthIndicator
kafkaHealthIndicator
()
{
return
createHealthIndicator
(
this
.
admins
);
}
@Override
@Bean
protected
KafkaHealthIndicator
createHealthIndicator
(
KafkaAdmin
source
)
{
@ConditionalOnMissingBean
(
name
=
"kafkaHealthIndicator"
)
Duration
responseTimeout
=
this
.
properties
.
getResponseTimeout
();
public
HealthIndicator
kafkaHealthIndicator
()
{
return
createHealthIndicator
(
this
.
admins
);
}
return
new
KafkaHealthIndicator
(
source
,
@Override
responseTimeout
==
null
?
100L
:
responseTimeout
.
toMillis
());
protected
KafkaHealthIndicator
createHealthIndicator
(
KafkaAdmin
source
)
{
}
Duration
responseTimeout
=
this
.
properties
.
getResponseTimeout
();
return
new
KafkaHealthIndicator
(
source
,
responseTimeout
.
toMillis
());
}
}
}
}
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/kafka/KafkaHealthIndicatorProperties.java
View file @
7cd19822
...
@@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
...
@@ -25,6 +25,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
* Configuration properties for {@link KafkaHealthIndicator}.
* Configuration properties for {@link KafkaHealthIndicator}.
*
*
* @author Juan Rada
* @author Juan Rada
* @since 2.0.0
*/
*/
@ConfigurationProperties
(
prefix
=
"management.health.kafka"
,
ignoreUnknownFields
=
false
)
@ConfigurationProperties
(
prefix
=
"management.health.kafka"
,
ignoreUnknownFields
=
false
)
public
class
KafkaHealthIndicatorProperties
{
public
class
KafkaHealthIndicatorProperties
{
...
@@ -32,7 +33,7 @@ public class KafkaHealthIndicatorProperties {
...
@@ -32,7 +33,7 @@ public class KafkaHealthIndicatorProperties {
/**
/**
* Time to wait for a response from the cluster description operation.
* Time to wait for a response from the cluster description operation.
*/
*/
private
Duration
responseTimeout
=
Duration
.
ofMillis
(
100
);
private
Duration
responseTimeout
=
Duration
.
ofMillis
(
100
0
);
public
Duration
getResponseTimeout
()
{
public
Duration
getResponseTimeout
()
{
return
this
.
responseTimeout
;
return
this
.
responseTimeout
;
...
...
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/kafka/package-info.java
View file @
7cd19822
...
@@ -15,6 +15,6 @@
...
@@ -15,6 +15,6 @@
*/
*/
/**
/**
* Auto-configuration for actuator
k
afka support.
* Auto-configuration for actuator
Apache K
afka support.
*/
*/
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
kafka
;
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
.
kafka
;
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json
View file @
7cd19822
...
@@ -103,6 +103,12 @@
...
@@ -103,6 +103,12 @@
"description"
:
"Whether to enable JMS health check."
,
"description"
:
"Whether to enable JMS health check."
,
"defaultValue"
:
true
"defaultValue"
:
true
},
},
{
"name"
:
"management.health.kafka.enabled"
,
"type"
:
"java.lang.Boolean"
,
"description"
:
"Whether to enable Kafka health check."
,
"defaultValue"
:
true
},
{
{
"name"
:
"management.health.ldap.enabled"
,
"name"
:
"management.health.ldap.enabled"
,
"type"
:
"java.lang.Boolean"
,
"type"
:
"java.lang.Boolean"
,
...
@@ -145,12 +151,6 @@
...
@@ -145,12 +151,6 @@
"description"
:
"Whether to enable Neo4j health check."
,
"description"
:
"Whether to enable Neo4j health check."
,
"defaultValue"
:
true
"defaultValue"
:
true
},
},
{
"name"
:
"management.health.kafka.enabled"
,
"type"
:
"java.lang.Boolean"
,
"description"
:
"Whether to enable kafka health check."
,
"defaultValue"
:
true
},
{
{
"name"
:
"management.info.build.enabled"
,
"name"
:
"management.info.build.enabled"
,
"type"
:
"java.lang.Boolean"
,
"type"
:
"java.lang.Boolean"
,
...
...
spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories
View file @
7cd19822
...
@@ -11,7 +11,6 @@ org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationP
...
@@ -11,7 +11,6 @@ org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationP
org.springframework.boot.actuate.autoconfigure.context.ShutdownEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.context.ShutdownEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.couchbase.CouchbaseHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.couchbase.CouchbaseHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.elasticsearch.ElasticsearchHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.elasticsearch.ElasticsearchHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.kafka.KafkaHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration,\
...
@@ -25,6 +24,7 @@ org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguratio
...
@@ -25,6 +24,7 @@ org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguratio
org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.jms.JmsHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.jms.JmsHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.kafka.KafkaHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.ldap.LdapHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.ldap.LdapHealthIndicatorAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.liquibase.LiquibaseEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.liquibase.LiquibaseEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointAutoConfiguration,\
org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointAutoConfiguration,\
...
...
spring-boot-project/spring-boot-actuator/pom.xml
View file @
7cd19822
...
@@ -172,11 +172,6 @@
...
@@ -172,11 +172,6 @@
<artifactId>
spring-rabbit
</artifactId>
<artifactId>
spring-rabbit
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.kafka
</groupId>
<artifactId>
spring-kafka
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.data
</groupId>
<groupId>
org.springframework.data
</groupId>
<artifactId>
spring-data-cassandra
</artifactId>
<artifactId>
spring-data-cassandra
</artifactId>
...
@@ -230,6 +225,11 @@
...
@@ -230,6 +225,11 @@
<artifactId>
spring-integration-core
</artifactId>
<artifactId>
spring-integration-core
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.kafka
</groupId>
<artifactId>
spring-kafka
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.security
</groupId>
<groupId>
org.springframework.security
</groupId>
<artifactId>
spring-security-core
</artifactId>
<artifactId>
spring-security-core
</artifactId>
...
@@ -263,13 +263,13 @@
...
@@ -263,13 +263,13 @@
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.
kafka
</groupId>
<groupId>
org.springframework.
boot
</groupId>
<artifactId>
spring-
kafka-test
</artifactId>
<artifactId>
spring-
boot-autoconfigure
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.
boot
</groupId>
<groupId>
org.springframework.
kafka
</groupId>
<artifactId>
spring-
boot-autoconfigure
</artifactId>
<artifactId>
spring-
kafka-test
</artifactId>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<dependency>
...
...
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/kafka/KafkaHealthIndicator.java
View file @
7cd19822
...
@@ -30,6 +30,7 @@ import org.apache.kafka.common.config.ConfigResource.Type;
...
@@ -30,6 +30,7 @@ import org.apache.kafka.common.config.ConfigResource.Type;
import
org.springframework.boot.actuate.health.AbstractHealthIndicator
;
import
org.springframework.boot.actuate.health.AbstractHealthIndicator
;
import
org.springframework.boot.actuate.health.Health.Builder
;
import
org.springframework.boot.actuate.health.Health.Builder
;
import
org.springframework.boot.actuate.health.HealthIndicator
;
import
org.springframework.boot.actuate.health.HealthIndicator
;
import
org.springframework.boot.actuate.health.Status
;
import
org.springframework.kafka.core.KafkaAdmin
;
import
org.springframework.kafka.core.KafkaAdmin
;
import
org.springframework.util.Assert
;
import
org.springframework.util.Assert
;
...
@@ -43,37 +44,35 @@ public class KafkaHealthIndicator extends AbstractHealthIndicator {
...
@@ -43,37 +44,35 @@ public class KafkaHealthIndicator extends AbstractHealthIndicator {
static
final
String
REPLICATION_PROPERTY
=
"transaction.state.log.replication.factor"
;
static
final
String
REPLICATION_PROPERTY
=
"transaction.state.log.replication.factor"
;
private
final
KafkaAdmin
kafkaAdmin
;
private
final
KafkaAdmin
kafkaAdmin
;
private
final
DescribeClusterOptions
describeOptions
;
private
final
DescribeClusterOptions
describeOptions
;
/**
/**
* Create a new {@link KafkaHealthIndicator} instance.
* Create a new {@link KafkaHealthIndicator} instance.
*
*
* @param kafkaAdmin the kafka admin
* @param kafkaAdmin the kafka admin
* @param re
sponseTimeout the describe cluster
request timeout in milliseconds
* @param re
questTimeout the
request timeout in milliseconds
*/
*/
public
KafkaHealthIndicator
(
KafkaAdmin
kafkaAdmin
,
long
re
sponse
Timeout
)
{
public
KafkaHealthIndicator
(
KafkaAdmin
kafkaAdmin
,
long
re
quest
Timeout
)
{
Assert
.
notNull
(
kafkaAdmin
,
"KafkaAdmin must not be null"
);
Assert
.
notNull
(
kafkaAdmin
,
"KafkaAdmin must not be null"
);
this
.
kafkaAdmin
=
kafkaAdmin
;
this
.
kafkaAdmin
=
kafkaAdmin
;
this
.
describeOptions
=
new
DescribeClusterOptions
()
this
.
describeOptions
=
new
DescribeClusterOptions
()
.
timeoutMs
((
int
)
re
sponse
Timeout
);
.
timeoutMs
((
int
)
re
quest
Timeout
);
}
}
@Override
@Override
protected
void
doHealthCheck
(
Builder
builder
)
throws
Exception
{
protected
void
doHealthCheck
(
Builder
builder
)
throws
Exception
{
try
(
AdminClient
adminClient
=
AdminClient
.
create
(
this
.
kafkaAdmin
.
getConfig
()))
{
try
(
AdminClient
adminClient
=
AdminClient
.
create
(
this
.
kafkaAdmin
.
getConfig
()))
{
DescribeClusterResult
result
=
adminClient
.
describeCluster
(
this
.
describeOptions
);
DescribeClusterResult
result
=
adminClient
.
describeCluster
(
this
.
describeOptions
);
String
brokerId
=
result
.
controller
().
get
().
idString
();
String
brokerId
=
result
.
controller
().
get
().
idString
();
int
replicationFactor
=
getReplicationFactor
(
brokerId
,
adminClient
);
int
replicationFactor
=
getReplicationFactor
(
brokerId
,
adminClient
);
int
nodes
=
result
.
nodes
().
get
().
size
();
int
nodes
=
result
.
nodes
().
get
().
size
();
if
(
nodes
>=
replicationFactor
)
{
Status
status
=
nodes
>=
replicationFactor
?
Status
.
UP
:
Status
.
DOWN
;
builder
.
up
();
builder
.
status
(
status
)
}
.
withDetail
(
"clusterId"
,
result
.
clusterId
().
get
())
else
{
.
withDetail
(
"brokerId"
,
brokerId
)
builder
.
down
();
.
withDetail
(
"nodes"
,
nodes
);
}
builder
.
withDetail
(
"clusterId"
,
result
.
clusterId
().
get
());
builder
.
withDetail
(
"brokerId"
,
brokerId
);
builder
.
withDetail
(
"nodes"
,
nodes
);
}
}
}
}
...
@@ -85,5 +84,6 @@ public class KafkaHealthIndicator extends AbstractHealthIndicator {
...
@@ -85,5 +84,6 @@ public class KafkaHealthIndicator extends AbstractHealthIndicator {
Config
brokerConfig
=
kafkaConfig
.
get
(
configResource
);
Config
brokerConfig
=
kafkaConfig
.
get
(
configResource
);
return
Integer
.
parseInt
(
brokerConfig
.
get
(
REPLICATION_PROPERTY
).
value
());
return
Integer
.
parseInt
(
brokerConfig
.
get
(
REPLICATION_PROPERTY
).
value
());
}
}
}
}
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/kafka/package-info.java
View file @
7cd19822
...
@@ -15,6 +15,6 @@
...
@@ -15,6 +15,6 @@
*/
*/
/**
/**
* Actuator support for Kafka.
* Actuator support for
Apache
Kafka.
*/
*/
package
org
.
springframework
.
boot
.
actuate
.
kafka
;
package
org
.
springframework
.
boot
.
actuate
.
kafka
;
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/kafka/KafkaHealthIndicatorTests.java
View file @
7cd19822
...
@@ -20,78 +20,82 @@ import java.util.Collections;
...
@@ -20,78 +20,82 @@ import java.util.Collections;
import
java.util.Map
;
import
java.util.Map
;
import
org.apache.kafka.clients.producer.ProducerConfig
;
import
org.apache.kafka.clients.producer.ProducerConfig
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.actuate.health.Health
;
import
org.springframework.boot.actuate.health.Health
;
import
org.springframework.boot.actuate.health.Status
;
import
org.springframework.boot.actuate.health.Status
;
import
org.springframework.kafka.core.KafkaAdmin
;
import
org.springframework.kafka.core.KafkaAdmin
;
import
org.springframework.kafka.test.rule.KafkaEmbedded
;
import
org.springframework.kafka.test.rule.KafkaEmbedded
;
import
org.springframework.util.SocketUtils
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
/**
/**
* Test
for {@link KafkaHealthIndicator}
* Test
s for {@link KafkaHealthIndicator}.
*
*
* @author Juan Rada
* @author Juan Rada
* @author Stephane Nicoll
*/
*/
public
class
KafkaHealthIndicatorTests
{
public
class
KafkaHealthIndicatorTests
{
private
static
final
Long
RESPONSE_TIME
=
1000L
;
private
KafkaEmbedded
kafkaEmbedded
;
private
KafkaEmbedded
kafkaEmbedded
;
private
KafkaAdmin
kafkaAdmin
;
private
void
startKafka
(
int
replicationFactor
)
throws
Exception
{
private
KafkaAdmin
kafkaAdmin
;
this
.
kafkaEmbedded
=
new
KafkaEmbedded
(
1
,
true
);
this
.
kafkaEmbedded
.
brokerProperties
(
Collections
.
singletonMap
(
KafkaHealthIndicator
.
REPLICATION_PROPERTY
,
String
.
valueOf
(
replicationFactor
)));
this
.
kafkaEmbedded
.
before
();
this
.
kafkaAdmin
=
new
KafkaAdmin
(
Collections
.
singletonMap
(
ProducerConfig
.
BOOTSTRAP_SERVERS_CONFIG
,
this
.
kafkaEmbedded
.
getBrokersAsString
()));
}
private
void
shutdownKafka
()
throws
Exception
{
@After
this
.
kafkaEmbedded
.
destroy
();
public
void
shutdownKafka
()
throws
Exception
{
if
(
this
.
kafkaEmbedded
!=
null
)
{
this
.
kafkaEmbedded
.
destroy
();
}
}
}
@Test
@Test
public
void
kafkaIsUp
()
throws
Exception
{
public
void
kafkaIsUp
()
throws
Exception
{
startKafka
(
1
);
startKafka
(
1
);
KafkaHealthIndicator
healthIndicator
=
KafkaHealthIndicator
healthIndicator
=
new
KafkaHealthIndicator
(
this
.
kafkaAdmin
,
RESPONSE_TIME
);
new
KafkaHealthIndicator
(
this
.
kafkaAdmin
,
1000L
);
Health
health
=
healthIndicator
.
health
();
Health
health
=
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
UP
);
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
UP
);
assertDetails
(
health
.
getDetails
());
assertDetails
(
health
.
getDetails
());
shutdownKafka
();
}
}
private
void
assertDetails
(
Map
<
String
,
Object
>
details
)
{
@Test
assertThat
(
details
).
containsEntry
(
"brokerId"
,
"0"
);
public
void
kafkaIsDown
()
{
assertThat
(
details
).
containsKey
(
"clusterId"
);
int
freePort
=
SocketUtils
.
findAvailableTcpPort
();
assertThat
(
details
).
containsEntry
(
"nodes"
,
1
);
this
.
kafkaAdmin
=
new
KafkaAdmin
(
Collections
.
singletonMap
(
ProducerConfig
.
BOOTSTRAP_SERVERS_CONFIG
,
"127.0.0.1:"
+
freePort
));
KafkaHealthIndicator
healthIndicator
=
new
KafkaHealthIndicator
(
this
.
kafkaAdmin
,
1L
);
Health
health
=
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
assertThat
((
String
)
health
.
getDetails
().
get
(
"error"
)).
isNotEmpty
();
}
}
@Test
@Test
public
void
notEnoughNodesForReplicationFactor
()
throws
Exception
{
public
void
notEnoughNodesForReplicationFactor
()
throws
Exception
{
startKafka
(
2
);
startKafka
(
2
);
KafkaHealthIndicator
healthIndicator
=
KafkaHealthIndicator
healthIndicator
=
new
KafkaHealthIndicator
(
this
.
kafkaAdmin
,
RESPONSE_TIME
);
new
KafkaHealthIndicator
(
this
.
kafkaAdmin
,
1000L
);
Health
health
=
healthIndicator
.
health
();
Health
health
=
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
assertDetails
(
health
.
getDetails
());
assertDetails
(
health
.
getDetails
());
shutdownKafka
();
}
}
@Test
private
void
assertDetails
(
Map
<
String
,
Object
>
details
)
{
public
void
kafkaIsDown
()
throws
Exception
{
assertThat
(
details
).
containsEntry
(
"brokerId"
,
"0"
);
assertThat
(
details
).
containsKey
(
"clusterId"
);
assertThat
(
details
).
containsEntry
(
"nodes"
,
1
);
}
private
void
startKafka
(
int
replicationFactor
)
throws
Exception
{
this
.
kafkaEmbedded
=
new
KafkaEmbedded
(
1
,
true
);
this
.
kafkaEmbedded
.
brokerProperties
(
Collections
.
singletonMap
(
KafkaHealthIndicator
.
REPLICATION_PROPERTY
,
String
.
valueOf
(
replicationFactor
)));
this
.
kafkaEmbedded
.
before
();
this
.
kafkaAdmin
=
new
KafkaAdmin
(
Collections
.
singletonMap
(
this
.
kafkaAdmin
=
new
KafkaAdmin
(
Collections
.
singletonMap
(
ProducerConfig
.
BOOTSTRAP_SERVERS_CONFIG
,
"127.0.0.1:34987"
));
ProducerConfig
.
BOOTSTRAP_SERVERS_CONFIG
,
KafkaHealthIndicator
healthIndicator
=
this
.
kafkaEmbedded
.
getBrokersAsString
()));
new
KafkaHealthIndicator
(
this
.
kafkaAdmin
,
RESPONSE_TIME
);
Health
health
=
healthIndicator
.
health
();
assertThat
(
health
.
getStatus
()).
isEqualTo
(
Status
.
DOWN
);
assertThat
((
String
)
health
.
getDetails
().
get
(
"error"
)).
isNotEmpty
();
}
}
}
}
spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc
View file @
7cd19822
...
@@ -1271,6 +1271,8 @@ content into your application. Rather, pick only the properties that you need.
...
@@ -1271,6 +1271,8 @@ content into your application. Rather, pick only the properties that you need.
management.health.elasticsearch.response-timeout=100ms # The time to wait for a response from the cluster.
management.health.elasticsearch.response-timeout=100ms # The time to wait for a response from the cluster.
management.health.influxdb.enabled=true # Whether to enable InfluxDB health check.
management.health.influxdb.enabled=true # Whether to enable InfluxDB health check.
management.health.jms.enabled=true # Whether to enable JMS health check.
management.health.jms.enabled=true # Whether to enable JMS health check.
management.health.kafka.enabled=true # Whether to enable Kafka health check.
management.health.kafka.response-timeout=1000ms # Time to wait for a response from the cluster description operation.
management.health.ldap.enabled=true # Whether to enable LDAP health check.
management.health.ldap.enabled=true # Whether to enable LDAP health check.
management.health.mail.enabled=true # Whether to enable Mail health check.
management.health.mail.enabled=true # Whether to enable Mail health check.
management.health.mongo.enabled=true # Whether to enable MongoDB health check.
management.health.mongo.enabled=true # Whether to enable MongoDB health check.
...
...
spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc
View file @
7cd19822
...
@@ -572,6 +572,9 @@ The following `HealthIndicators` are auto-configured by Spring Boot when appropr
...
@@ -572,6 +572,9 @@ The following `HealthIndicators` are auto-configured by Spring Boot when appropr
|{sc-spring-boot-actuator}/jms/JmsHealthIndicator.{sc-ext}[`JmsHealthIndicator`]
|{sc-spring-boot-actuator}/jms/JmsHealthIndicator.{sc-ext}[`JmsHealthIndicator`]
|Checks that a JMS broker is up.
|Checks that a JMS broker is up.
|{sc-spring-boot-actuator}/kafka/KafkaHealthIndicator.{sc-ext}[`KafkaHealthIndicator`]
|Checks that a Kafka server is up.
|{sc-spring-boot-actuator}/mail/MailHealthIndicator.{sc-ext}[`MailHealthIndicator`]
|{sc-spring-boot-actuator}/mail/MailHealthIndicator.{sc-ext}[`MailHealthIndicator`]
|Checks that a mail server is up.
|Checks that a mail server is up.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment