Skip to content

Commit c7f8845

Browse files
committed
auto configuration
1 parent d253312 commit c7f8845

File tree

7 files changed

+120
-19
lines changed

7 files changed

+120
-19
lines changed

grpc-server-spring-boot-starter/src/main/java/net/devh/boot/grpc/server/autoconfigure/GrpcActuatoHealthServiceAutoConfiguration.java

Lines changed: 0 additions & 10 deletions
This file was deleted.

grpc-server-spring-boot-starter/src/main/java/net/devh/boot/grpc/server/autoconfigure/GrpcHealthServiceAutoConfiguration.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
package net.devh.boot.grpc.server.autoconfigure;
1818

19+
import net.devh.boot.grpc.server.health.ActuatorGrpcHealth;
20+
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration;
21+
import org.springframework.boot.actuate.health.HealthEndpoint;
22+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
1923
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2025
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2126
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2227
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -36,6 +41,7 @@
3641
@ConditionalOnClass(HealthStatusManager.class)
3742
@ConditionalOnProperty(prefix = "grpc.server", name = "health-service-enabled", matchIfMissing = true)
3843
@AutoConfigureBefore(GrpcServerFactoryAutoConfiguration.class)
44+
@AutoConfigureAfter(HealthEndpointAutoConfiguration.class)
3945
public class GrpcHealthServiceAutoConfiguration {
4046

4147
/**
@@ -51,8 +57,16 @@ HealthStatusManager healthStatusManager() {
5157

5258
@Bean
5359
@GrpcService
60+
@ConditionalOnProperty(prefix = "grpc.server", name = "health-service-type", havingValue = "grpc", matchIfMissing = true)
5461
BindableService grpcHealthService(final HealthStatusManager healthStatusManager) {
5562
return healthStatusManager.getHealthService();
5663
}
5764

65+
@Bean
66+
@GrpcService
67+
@ConditionalOnProperty(prefix = "grpc.server", name = "health-service-type", havingValue = "actuator")
68+
@ConditionalOnBean(HealthEndpoint.class)
69+
BindableService grpcHealthServiceActuator(final HealthEndpoint healthStatusManager) {
70+
return new ActuatorGrpcHealth(healthStatusManager);
71+
}
5872
}

grpc-server-spring-boot-starter/src/main/java/net/devh/boot/grpc/server/config/GrpcServerProperties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ public class GrpcServerProperties {
228228
*/
229229
private boolean healthServiceEnabled = true;
230230

231+
/**
232+
* @TODO
233+
*/
234+
private String healthServiceType = "grpc";
235+
231236
/**
232237
* Whether proto reflection service is enabled or not. Defaults to {@code true}.
233238
*

grpc-server-spring-boot-starter/src/test/java/net/devh/boot/grpc/server/autoconfigure/GrpcHealthServiceDefaultAutoConfigurationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
@ImportAutoConfiguration({
3737
GrpcServerAutoConfiguration.class,
3838
GrpcServerFactoryAutoConfiguration.class,
39-
GrpcHealthServiceAutoConfiguration.class,
40-
GrpcActuatoHealthServiceAutoConfiguration.class
39+
GrpcHealthServiceAutoConfiguration.class
4140
})
4241
@DirtiesContext
4342
class GrpcHealthServiceDefaultAutoConfigurationTest {

grpc-server-spring-boot-starter/src/test/java/net/devh/boot/grpc/server/autoconfigure/GrpcHealthServiceFalseAutoConfigurationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
@ImportAutoConfiguration({
3535
GrpcServerAutoConfiguration.class,
3636
GrpcServerFactoryAutoConfiguration.class,
37-
GrpcHealthServiceAutoConfiguration.class,
38-
GrpcActuatoHealthServiceAutoConfiguration.class
37+
GrpcHealthServiceAutoConfiguration.class
3938
})
4039
@DirtiesContext
4140
class GrpcHealthServiceFalseAutoConfigurationTest extends GrpcHealthServiceDefaultAutoConfigurationTest {

grpc-server-spring-boot-starter/src/test/java/net/devh/boot/grpc/server/autoconfigure/GrpcHealthServiceTrueActuatorConfigurationTest.java

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,116 @@
1616

1717
package net.devh.boot.grpc.server.autoconfigure;
1818

19+
import io.grpc.ManagedChannel;
20+
import io.grpc.ManagedChannelBuilder;
21+
import io.grpc.Status;
22+
import io.grpc.StatusRuntimeException;
23+
import io.grpc.health.v1.HealthCheckRequest;
24+
import io.grpc.health.v1.HealthCheckResponse;
25+
import io.grpc.health.v1.HealthGrpc;
26+
import io.grpc.health.v1.HealthGrpc.HealthStub;
27+
import org.junit.jupiter.api.Test;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration;
30+
import org.springframework.boot.actuate.health.Health;
31+
import org.springframework.boot.actuate.health.HealthIndicator;
1932
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2033
import org.springframework.boot.test.context.SpringBootTest;
34+
import org.springframework.context.annotation.Bean;
35+
import org.springframework.context.annotation.Configuration;
2136
import org.springframework.test.annotation.DirtiesContext;
2237

23-
@SpringBootTest(classes = GrpcHealthServiceDefaultAutoConfigurationTest.TestConfig.class,
38+
import static org.junit.jupiter.api.Assertions.*;
39+
40+
@SpringBootTest(classes = {
41+
GrpcHealthServiceDefaultAutoConfigurationTest.TestConfig.class,
42+
GrpcHealthServiceTrueActuatorConfigurationTest.TestConfig.class
43+
},
2444
properties = {
25-
"grpc.server.health-service-enabled=true",
2645
"grpc.server.health-service-type=actuator",
2746
})
2847
@ImportAutoConfiguration({
2948
GrpcServerAutoConfiguration.class,
3049
GrpcServerFactoryAutoConfiguration.class,
3150
GrpcHealthServiceAutoConfiguration.class,
32-
GrpcActuatoHealthServiceAutoConfiguration.class
51+
HealthEndpointAutoConfiguration.class
3352
})
3453
@DirtiesContext
3554
class GrpcHealthServiceTrueActuatorConfigurationTest extends GrpcHealthServiceDefaultAutoConfigurationTest {
55+
@Configuration
56+
static class TestConfig {
57+
@Bean
58+
TestIndicator customIndicator() {
59+
return new TestIndicator();
60+
}
61+
}
62+
63+
static class TestIndicator implements HealthIndicator {
64+
Health health = Health.up().build();
65+
66+
@Override
67+
public Health health() {
68+
return health;
69+
}
70+
}
71+
72+
@Autowired
73+
TestIndicator customIndicator;
74+
75+
@Test
76+
void testUnhealthyService() {
77+
final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:9090").usePlaintext().build();
78+
try {
79+
final HealthStub stub = HealthGrpc.newStub(channel);
80+
81+
customIndicator.health = Health.down().build();
82+
final AwaitableStreamObserver<HealthCheckResponse> resultObserver = new AwaitableStreamObserver<>();
83+
stub.check(HealthCheckRequest.getDefaultInstance(), resultObserver);
84+
85+
final HealthCheckResponse response = assertDoesNotThrow(resultObserver::getSingle);
86+
assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, response.getStatus());
87+
} finally {
88+
channel.shutdown();
89+
}
90+
}
91+
92+
@Test
93+
void testSpecificUnhealthyService() {
94+
final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:9090").usePlaintext().build();
95+
try {
96+
final HealthStub stub = HealthGrpc.newStub(channel);
97+
98+
customIndicator.health = Health.down().build();
99+
final AwaitableStreamObserver<HealthCheckResponse> resultObserver = new AwaitableStreamObserver<>();
100+
stub.check(HealthCheckRequest.newBuilder()
101+
.setService("customIndicator")
102+
.build(), resultObserver);
103+
104+
final HealthCheckResponse response = assertDoesNotThrow(resultObserver::getSingle);
105+
assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, response.getStatus());
106+
} finally {
107+
channel.shutdown();
108+
}
109+
}
110+
111+
@Test
112+
void testNotFoundService() throws InterruptedException {
113+
final ManagedChannel channel = ManagedChannelBuilder.forTarget("localhost:9090").usePlaintext().build();
114+
try {
115+
final HealthStub stub = HealthGrpc.newStub(channel);
116+
117+
customIndicator.health = Health.down().build();
118+
final AwaitableStreamObserver<HealthCheckResponse> resultObserver = new AwaitableStreamObserver<>();
119+
stub.check(HealthCheckRequest.newBuilder()
120+
.setService("someservice")
121+
.build(), resultObserver);
122+
123+
var error = resultObserver.getError();
124+
assertInstanceOf( StatusRuntimeException.class, error);
125+
assertEquals(Status.NOT_FOUND.getCode(),((StatusRuntimeException) error).getStatus().getCode());
126+
} finally {
127+
channel.shutdown();
128+
}
129+
}
130+
36131
}

grpc-server-spring-boot-starter/src/test/java/net/devh/boot/grpc/server/autoconfigure/GrpcHealthServiceTrueAutoConfigurationTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
@ImportAutoConfiguration({
2626
GrpcServerAutoConfiguration.class,
2727
GrpcServerFactoryAutoConfiguration.class,
28-
GrpcHealthServiceAutoConfiguration.class,
29-
GrpcActuatoHealthServiceAutoConfiguration.class
28+
GrpcHealthServiceAutoConfiguration.class
3029
})
3130
@DirtiesContext
3231
class GrpcHealthServiceTrueAutoConfigurationTest extends GrpcHealthServiceDefaultAutoConfigurationTest {

0 commit comments

Comments
 (0)