Skip to content

Commit 31d3dda

Browse files
author
Alexander Furer
committed
graceful shutdown
1 parent 911315b commit 31d3dda

File tree

7 files changed

+65
-72
lines changed

7 files changed

+65
-72
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import lombok.extern.slf4j.Slf4j;
88
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
99
import org.lognet.springboot.grpc.context.GRpcServerInitializedEvent;
10-
import org.lognet.springboot.grpc.context.GRpcServerStoppedEvent;
1110
import org.springframework.beans.factory.BeanCreationException;
1211
import org.springframework.beans.factory.DisposableBean;
1312
import org.springframework.beans.factory.annotation.Autowired;
@@ -154,7 +153,6 @@ public void destroy() throws Exception {
154153
server.getServices().forEach(def->healthStatusManager.clearStatus(def.getServiceDescriptor().getName()));
155154
Optional.ofNullable(server).ifPresent(Server::shutdown);
156155
log.info("gRPC server stopped.");
157-
applicationContext.publishEvent(new GRpcServerStoppedEvent(applicationContext,server));
158156
}
159157

160158
private <T> Stream<String> getBeanNamesByTypeWithAnnotation(Class<? extends Annotation> annotationType, Class<T> beanType) throws Exception {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
@Configuration
1515
@ConditionalOnClass(ConsulServiceRegistry.class)
1616
@AutoConfigureAfter({ ConsulServiceRegistryAutoConfiguration.class, GRpcAutoConfiguration.class})
17+
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
18+
@ConditionalOnBean({ConsulServiceRegistry.class, GRpcServerRunner.class})
1719
public class ConsulGrpcAutoConfiguration{
1820

1921

20-
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
21-
@ConditionalOnBean({ConsulServiceRegistry.class, GRpcServerRunner.class})
22+
2223
@Bean
2324
public GrpcConsulRegistrar consulRegistrar(ConsulServiceRegistry consulServiceRegistry){
2425
return new GrpcConsulRegistrar(consulServiceRegistry);

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

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import com.ecwid.consul.v1.agent.model.NewService;
44
import org.lognet.springboot.grpc.context.GRpcServerInitializedEvent;
5-
import org.lognet.springboot.grpc.context.GRpcServerStoppedEvent;
6-
import org.lognet.springboot.grpc.context.GrpcServerEvent;
75
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
86
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
97
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
108
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
119
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.SmartLifecycle;
1211
import org.springframework.context.event.EventListener;
1312

14-
public class GrpcConsulRegistrar {
13+
public class GrpcConsulRegistrar implements SmartLifecycle {
1514

1615
private ConsulRegistration registration;
1716
private ConsulServiceRegistry consulServiceRegistry;
@@ -25,16 +24,9 @@ public void onGrpcServerStarted(GRpcServerInitializedEvent initializedEvent) {
2524
registration = getRegistration(initializedEvent);
2625
consulServiceRegistry.register(registration);
2726
}
28-
@EventListener
29-
public void onGrpcServerStopped(GRpcServerStoppedEvent initializedEvent) {
30-
31-
consulServiceRegistry.deregister(registration);
32-
consulServiceRegistry.close();
33-
}
34-
3527

3628

37-
private ConsulRegistration getRegistration(GrpcServerEvent event) {
29+
private ConsulRegistration getRegistration(GRpcServerInitializedEvent event) {
3830
ApplicationContext applicationContext = event.getApplicationContext();
3931

4032

@@ -47,7 +39,7 @@ private ConsulRegistration getRegistration(GrpcServerEvent event) {
4739
}
4840
String appName = "grpc_" + ConsulAutoRegistration.getAppName(properties, applicationContext.getEnvironment());
4941
grpcService.setName(ConsulAutoRegistration.normalizeForDns(appName));
50-
grpcService.setId("grpc_" +ConsulAutoRegistration.getInstanceId(properties, applicationContext));
42+
grpcService.setId("grpc_" + ConsulAutoRegistration.getInstanceId(properties, applicationContext));
5143

5244
/*
5345
service.setTags(createTags(properties));
@@ -65,4 +57,38 @@ private ConsulRegistration getRegistration(GrpcServerEvent event) {
6557
}
6658

6759

60+
@Override
61+
public boolean isAutoStartup() {
62+
return false;
63+
}
64+
65+
@Override
66+
public void stop(Runnable callback) {
67+
stop();
68+
callback.run();
69+
}
70+
71+
@Override
72+
public void start() {
73+
74+
}
75+
76+
@Override
77+
public synchronized void stop() {
78+
79+
consulServiceRegistry.deregister(registration);
80+
consulServiceRegistry.close();
81+
registration = null;
82+
83+
}
84+
85+
@Override
86+
public synchronized boolean isRunning() {
87+
return null != registration;
88+
}
89+
90+
@Override
91+
public int getPhase() {
92+
return 0;
93+
}
6894
}

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/context/GRpcServerInitializedEvent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import io.grpc.Server;
44
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.ApplicationEvent;
56

6-
public class GRpcServerInitializedEvent extends GrpcServerEvent {
7+
public class GRpcServerInitializedEvent extends ApplicationEvent {
78
private Server server;
89

910
public GRpcServerInitializedEvent(ApplicationContext context,Server server) {
@@ -14,6 +15,7 @@ public GRpcServerInitializedEvent(ApplicationContext context,Server server) {
1415
public ApplicationContext getApplicationContext(){
1516
return (ApplicationContext) getSource();
1617
}
18+
1719
public Server getServer(){
1820
return server;
1921
}

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/context/GRpcServerStoppedEvent.java

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

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/context/GrpcServerEvent.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
package org.lognet.springboot.grpc;
22

3-
import com.pszymczyk.consul.junit.ConsulResource;
43
import io.grpc.ManagedChannel;
54
import io.grpc.ManagedChannelBuilder;
65
import io.grpc.examples.GreeterGrpc;
76
import io.grpc.examples.GreeterOuterClass;
8-
import org.junit.AfterClass;
9-
import org.junit.ClassRule;
107
import org.junit.Test;
118
import org.junit.runner.RunWith;
129
import org.lognet.springboot.grpc.demo.DemoApp;
1310
import org.springframework.beans.factory.annotation.Autowired;
1411
import org.springframework.boot.test.context.SpringBootTest;
1512
import org.springframework.cloud.client.ServiceInstance;
1613
import org.springframework.cloud.client.discovery.DiscoveryClient;
14+
import org.springframework.context.ConfigurableApplicationContext;
1715
import org.springframework.test.context.junit4.SpringRunner;
18-
import org.springframework.util.SocketUtils;
1916

2017
import java.util.List;
2118
import java.util.concurrent.ExecutionException;
@@ -25,23 +22,30 @@
2522

2623

2724
@RunWith(SpringRunner.class)
28-
@SpringBootTest(classes = DemoApp.class, properties = {"spring.cloud.config.enabled:false"})
25+
@SpringBootTest(classes = DemoApp.class, properties = {"spring.cloud.config.enabled:false"}
26+
,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
27+
)
28+
2929
public class ConsulRegistrationTest {
30+
// @ClassRule
31+
// public static final ConsulResource consul(){
32+
// int port = SocketUtils.findAvailableTcpPort();
33+
// ConsulResource consulResource = new ConsulResource(port);
34+
// System.setProperty("spring.cloud.consul.port",String.valueOf(port));
35+
// return consulResource;
36+
// }
37+
// @AfterClass
38+
// public static void clear(){
39+
// System.clearProperty("spring.cloud.consul.port");
40+
// }
41+
42+
3043

31-
@ClassRule
32-
public static final ConsulResource consul(){
33-
int port = SocketUtils.findAvailableTcpPort();
34-
ConsulResource consulResource = new ConsulResource(port);
35-
System.setProperty("spring.cloud.consul.port",String.valueOf(port));
36-
return consulResource;
37-
}
38-
@AfterClass
39-
public static void clear(){
40-
System.clearProperty("spring.cloud.consul.port");
41-
}
4244

4345
@Autowired
4446
private DiscoveryClient discoveryClient;
47+
@Autowired
48+
ConfigurableApplicationContext applicationContext;
4549

4650
@Test
4751
public void contextLoads() throws ExecutionException, InterruptedException {
@@ -57,5 +61,6 @@ public void contextLoads() throws ExecutionException, InterruptedException {
5761
final GreeterOuterClass.HelloRequest helloRequest =GreeterOuterClass.HelloRequest.newBuilder().setName("Bob").build();
5862
final String reply = greeterFutureStub.sayHello(helloRequest).get().getMessage();
5963
assertNotNull("Replay should not be null",reply);
64+
applicationContext.stop();
6065
}
6166
}

0 commit comments

Comments
 (0)