Skip to content

Commit dcc83df

Browse files
authored
[#1519] Optimized the implementation of the actuator health check interface (#1520) (#1532)
1 parent 71b8603 commit dcc83df

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/check/RegistryHealthIndicator.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,44 @@
1717

1818
package com.huaweicloud.servicecomb.discovery.check;
1919

20-
import com.google.common.eventbus.Subscribe;
21-
import com.huaweicloud.common.event.EventManager;
20+
import org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceInstanceRegistrationEvent;
21+
import org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceRegistrationEvent;
2222
import org.springframework.boot.actuate.health.Health;
2323
import org.springframework.boot.actuate.health.HealthIndicator;
24-
import org.apache.servicecomb.service.center.client.RegistrationEvents.MicroserviceInstanceRegistrationEvent;
24+
25+
import com.google.common.eventbus.Subscribe;
26+
import com.huaweicloud.common.event.EventManager;
2527

2628
public class RegistryHealthIndicator implements HealthIndicator {
2729

28-
private boolean isSuccess = false;
30+
private boolean isSuccess = false;
2931

30-
private static final String REGISTRATION_NOT_READY = "registration not ready";
32+
private static final String REGISTRATION_NOT_READY = "registration not ready";
3133

32-
public RegistryHealthIndicator() {
33-
EventManager.register(this);
34-
}
34+
public RegistryHealthIndicator() {
35+
EventManager.register(this);
36+
}
3537

36-
@Override
37-
public Health health() {
38-
if (isSuccess) {
39-
return Health.up().build();
40-
}
41-
return Health.down().withDetail("Error Message", REGISTRATION_NOT_READY).build();
38+
@Override
39+
public Health health() {
40+
if (isSuccess) {
41+
return Health.up().build();
4242
}
43-
44-
@Subscribe
45-
public void registryListener(MicroserviceInstanceRegistrationEvent event) {
46-
this.isSuccess = event.isSuccess();
43+
return Health.down().withDetail("Error Message", REGISTRATION_NOT_READY).build();
44+
}
45+
46+
@Subscribe
47+
public void registryInstanceListener(MicroserviceInstanceRegistrationEvent event) {
48+
this.isSuccess = event.isSuccess();
49+
}
50+
51+
@Subscribe
52+
public void serviceRegistryListener(MicroserviceRegistrationEvent event) {
53+
// Considering the engine shutdown scenario
54+
// Successful service registration does not necessarily mean that the microservice is successfully registered.
55+
// However, if service registration fails, the microservice definitely fails to be registered.
56+
if (!event.isSuccess()) {
57+
this.isSuccess = event.isSuccess();
4758
}
59+
}
4860
}

spring-cloud-huawei-service-engine/service-engine-discovery/src/main/java/com/huaweicloud/servicecomb/discovery/check/RegistryHealthIndicatorConfiguration.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
@Configuration
2626
@ConditionalOnClass(name = {"org.springframework.boot.actuate.health.HealthIndicator"})
2727
public class RegistryHealthIndicatorConfiguration {
28-
@Bean
29-
@Order(100)
30-
public RegistryHealthIndicator registryHealthIndicator() {
31-
return new RegistryHealthIndicator();
32-
}
28+
@Bean
29+
@Order(100)
30+
public RegistryHealthIndicator registryHealthIndicator() {
31+
return new RegistryHealthIndicator();
32+
}
3333
}

0 commit comments

Comments
 (0)