Skip to content

Commit 2da3148

Browse files
author
Alexander Furer
committed
Consul health check
1 parent 929b74f commit 2da3148

File tree

13 files changed

+179
-61
lines changed

13 files changed

+179
-61
lines changed

README.adoc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,16 @@ This is the current limitation.
260260

261261
== Consul Integration
262262

263-
Starting from version `3.3.0`, the starter will auto-register the running grpc server in Consul registry if `org.springframework.cloud:spring-cloud-starter-consul-discovery` is in classpath. +
263+
Starting from version `3.3.0`, the starter will auto-register the running grpc server in Consul registry
264+
if `org.springframework.cloud:spring-cloud-starter-consul-discovery` is in classpath and
265+
`spring.cloud.service-registry.auto-registration.enabled` is *NOT* set to `false`. +
266+
264267
The registered service name will be prefixed with `grpc-` ,i.e. `grpc-${spring.application.name}` to not interfere with standard registered web-service name if you choose to run both embedded `Grpc` and `Web` servers. +
265268

269+
Setting `spring.cloud.consul.discovery.register-health-check` to true will register GRPC health check service in Consul.
270+
271+
Tags could be set by defining `spring.cloud.consul.discovery.tags` property.
272+
266273
You can find the test that demonstrates the feature link:grpc-spring-boot2-starter-demo/src/test/java/org/lognet/springboot/grpc/ConsulRegistrationTest.java[here].
267274

268275
== Eureka Integration

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ buildscript {
22
ext {
33
springBoot_1_X_Version = '1.5.13.RELEASE'
44
springBoot_2_X_Version = '2.1.3.RELEASE'
5-
grpcVersion = '1.29.0'
5+
grpcVersion = '1.30.0'
66
}
77
repositories {
88
mavenCentral()

grpc-spring-boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/DemoApp.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,16 @@
11
package org.lognet.springboot.grpc.demo;
22

33

4-
import io.grpc.examples.CalculatorGrpc;
5-
import io.grpc.examples.CalculatorOuterClass;
6-
import io.grpc.stub.StreamObserver;
7-
import org.lognet.springboot.grpc.GRpcService;
84
import org.springframework.boot.SpringApplication;
95
import org.springframework.boot.autoconfigure.SpringBootApplication;
10-
import org.springframework.context.annotation.Bean;
116

127
/**
138
* Created by alexf on 28-Jan-16.
149
*/
1510
@SpringBootApplication
1611
public class DemoApp {
1712

18-
@Bean
19-
public GreeterService greeterService() {
20-
return new GreeterService();
21-
}
2213

23-
@GRpcService(interceptors = NotSpringBeanInterceptor.class)
24-
public static class CalculatorService extends CalculatorGrpc.CalculatorImplBase{
25-
@Override
26-
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
27-
CalculatorOuterClass.CalculatorResponse.Builder resultBuilder = CalculatorOuterClass.CalculatorResponse.newBuilder();
28-
switch (request.getOperation()){
29-
case ADD:
30-
resultBuilder.setResult(request.getNumber1()+request.getNumber2());
31-
break;
32-
case SUBTRACT:
33-
resultBuilder.setResult(request.getNumber1()-request.getNumber2());
34-
break;
35-
case MULTIPLY:
36-
resultBuilder.setResult(request.getNumber1()*request.getNumber2());
37-
break;
38-
case DIVIDE:
39-
resultBuilder.setResult(request.getNumber1()/request.getNumber2());
40-
break;
41-
case UNRECOGNIZED:
42-
break;
43-
}
44-
responseObserver.onNext(resultBuilder.build());
45-
responseObserver.onCompleted();
46-
47-
48-
}
49-
50-
51-
}
5214

5315
public static void main(String[] args) {
5416
SpringApplication.run(DemoApp.class,args);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.lognet.springboot.grpc.demo;
2+
3+
import io.grpc.examples.CalculatorGrpc;
4+
import io.grpc.examples.CalculatorOuterClass;
5+
import io.grpc.stub.StreamObserver;
6+
import org.lognet.springboot.grpc.GRpcService;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
@Configuration
11+
public class DemoAppConfiguration {
12+
@Bean
13+
public GreeterService greeterService() {
14+
return new GreeterService();
15+
}
16+
17+
@GRpcService(interceptors = NotSpringBeanInterceptor.class)
18+
public static class CalculatorService extends CalculatorGrpc.CalculatorImplBase{
19+
@Override
20+
public void calculate(CalculatorOuterClass.CalculatorRequest request, StreamObserver<CalculatorOuterClass.CalculatorResponse> responseObserver) {
21+
CalculatorOuterClass.CalculatorResponse.Builder resultBuilder = CalculatorOuterClass.CalculatorResponse.newBuilder();
22+
switch (request.getOperation()){
23+
case ADD:
24+
resultBuilder.setResult(request.getNumber1()+request.getNumber2());
25+
break;
26+
case SUBTRACT:
27+
resultBuilder.setResult(request.getNumber1()-request.getNumber2());
28+
break;
29+
case MULTIPLY:
30+
resultBuilder.setResult(request.getNumber1()*request.getNumber2());
31+
break;
32+
case DIVIDE:
33+
resultBuilder.setResult(request.getNumber1()/request.getNumber2());
34+
break;
35+
case UNRECOGNIZED:
36+
break;
37+
}
38+
responseObserver.onNext(resultBuilder.build());
39+
responseObserver.onCompleted();
40+
41+
42+
}
43+
44+
45+
}
46+
}

grpc-spring-boot-starter-demo/src/main/protoGen/io/grpc/examples/CalculatorGrpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/**
1111
*/
1212
@javax.annotation.Generated(
13-
value = "by gRPC proto compiler (version 1.29.0)",
13+
value = "by gRPC proto compiler (version 1.30.0)",
1414
comments = "Source: calculator.proto")
1515
public final class CalculatorGrpc {
1616

grpc-spring-boot-starter-demo/src/main/protoGen/io/grpc/examples/GreeterGrpc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* </pre>
1414
*/
1515
@javax.annotation.Generated(
16-
value = "by gRPC proto compiler (version 1.29.0)",
16+
value = "by gRPC proto compiler (version 1.30.0)",
1717
comments = "Source: greeter.proto")
1818
public final class GreeterGrpc {
1919

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/DemoAppTestAop.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Test;
55
import org.junit.runner.RunWith;
66
import org.lognet.springboot.grpc.demo.DemoApp;
7+
import org.lognet.springboot.grpc.demo.DemoAppConfiguration.CalculatorService;
78
import org.lognet.springboot.grpc.demo.GreeterService;
89
import org.springframework.aop.support.AopUtils;
910
import org.springframework.beans.factory.annotation.Autowired;
@@ -33,7 +34,7 @@ public class DemoAppTestAop extends GrpcServerTestBase{
3334
private GreeterService greeterService;
3435

3536
@Autowired
36-
private DemoApp.CalculatorService calculatorService;
37+
private CalculatorService calculatorService;
3738

3839
@Rule
3940
public OutputCapture outputCapture = new OutputCapture();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.lognet.springboot.grpc.autoconfigure.consul;
2+
3+
import com.ecwid.consul.v1.agent.model.NewService;
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.*;
6+
7+
/**
8+
* created by Yehor Kravchenko
9+
* 04/02/2020 - 16:20
10+
*/
11+
@Getter
12+
@Setter
13+
@EqualsAndHashCode(callSuper = true)
14+
@ToString
15+
@Builder
16+
public class GRpcConsulHealthCheck extends NewService.Check {
17+
18+
/**
19+
* Host address + port
20+
*/
21+
@SerializedName("grpc")
22+
private String socketAddr;
23+
24+
/**
25+
* interval for health check polling
26+
*/
27+
@Builder.Default
28+
private String interval = "30s";
29+
30+
/**
31+
* Is server used TLS
32+
*/
33+
@SerializedName("grpc_use_tls")
34+
private boolean grpcUseTlc;
35+
36+
/**
37+
* timeout on healthcheck polling
38+
*/
39+
private String timeout;
40+
}

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/consul/GrpcConsulRegistrar.java

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.lognet.springboot.grpc.autoconfigure.consul;
22

33
import com.ecwid.consul.v1.agent.model.NewService;
4+
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
45
import org.lognet.springboot.grpc.context.GRpcServerInitializedEvent;
56
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
67
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
@@ -10,6 +11,9 @@
1011
import org.springframework.context.SmartLifecycle;
1112
import org.springframework.context.event.EventListener;
1213

14+
import java.util.Objects;
15+
import java.util.stream.Collectors;
16+
1317
public class GrpcConsulRegistrar implements SmartLifecycle {
1418

1519
private final ConsulServiceRegistry consulServiceRegistry;
@@ -30,30 +34,40 @@ private ConsulRegistration getRegistration(GRpcServerInitializedEvent event) {
3034
ApplicationContext applicationContext = event.getApplicationContext();
3135

3236

33-
ConsulDiscoveryProperties properties = applicationContext.getBean(ConsulDiscoveryProperties.class);
37+
ConsulDiscoveryProperties consulProperties = applicationContext.getBean(ConsulDiscoveryProperties.class);
38+
GRpcServerProperties gRpcServerProperties = event.getApplicationContext().getBean(GRpcServerProperties.class);
3439

3540
NewService grpcService = new NewService();
3641
grpcService.setPort(event.getServer().getPort());
37-
if (!properties.isPreferAgentAddress()) {
38-
grpcService.setAddress(properties.getHostname());
42+
if (!consulProperties.isPreferAgentAddress()) {
43+
grpcService.setAddress(consulProperties.getHostname());
3944
}
40-
String appName = "grpc-" + ConsulAutoRegistration.getAppName(properties, applicationContext.getEnvironment());
45+
String appName = "grpc-" + ConsulAutoRegistration.getAppName(consulProperties, applicationContext.getEnvironment());
4146
grpcService.setName(ConsulAutoRegistration.normalizeForDns(appName));
42-
grpcService.setId("grpc-" + ConsulAutoRegistration.getInstanceId(properties, applicationContext));
43-
44-
/*
45-
service.setTags(createTags(properties));
46-
setCheck(service, autoServiceRegistrationProperties, properties, context,
47-
heartbeatProperties);
47+
grpcService.setId("grpc-" + ConsulAutoRegistration.getInstanceId(consulProperties, applicationContext));
48+
grpcService.setTags(ConsulAutoRegistration.createTags(consulProperties)
49+
.stream()
50+
.filter(t->!t.startsWith("secure="))
51+
.collect(Collectors.toList())
52+
);
4853

4954

55+
if(consulProperties.isRegisterHealthCheck()) {
56+
GRpcConsulHealthCheck health = GRpcConsulHealthCheck.builder()
57+
.socketAddr(consulProperties.getHostname() + ":" + event.getServer().getPort())
58+
.grpcUseTlc(Objects.nonNull(gRpcServerProperties.getSecurity()))
59+
.interval(consulProperties.getHealthCheckInterval())
60+
.timeout(consulProperties.getHealthCheckTimeout())
61+
.build();
5062

63+
health.setDeregisterCriticalServiceAfter(consulProperties.getHealthCheckCriticalTimeout());
5164

65+
grpcService.setCheck(health);
66+
}
5267

53-
*/
5468

5569

56-
return new ConsulRegistration(grpcService, properties);
70+
return new ConsulRegistration(grpcService, consulProperties);
5771
}
5872

5973

grpc-spring-boot2-starter-demo/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ ext {
2323
}
2424
dependencies {
2525

26-
testCompile project(':grpc-spring-boot-starter-demo')
26+
27+
compile project(':grpc-spring-boot-starter-demo')
2728

2829
testCompile project(':grpc-spring-boot-starter-demo').sourceSets.test.output
2930

@@ -36,8 +37,9 @@ dependencies {
3637
testRuntime('org.springframework.boot:spring-boot-starter-web')
3738

3839

39-
testRuntime('org.springframework.cloud:spring-cloud-starter-consul-discovery')
40-
testCompile 'com.pszymczyk.consul:embedded-consul:2.0.0'
40+
testCompile('org.springframework.cloud:spring-cloud-starter-consul-discovery')
41+
runtime('org.springframework.cloud:spring-cloud-starter-consul-discovery')
42+
testCompile 'com.pszymczyk.consul:embedded-consul:2.1.4'
4143

4244
testCompile "org.springframework.cloud:spring-cloud-config-server:2.1.1.RELEASE"
4345
testCompile "org.springframework.cloud:spring-cloud-config-client:2.1.1.RELEASE"

0 commit comments

Comments
 (0)