Skip to content

Commit a407824

Browse files
author
Alexander Furer
committed
auto consule registration
1 parent 1e44bc4 commit a407824

File tree

13 files changed

+299
-18
lines changed

13 files changed

+299
-18
lines changed

grpc-spring-boot-starter/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ dependencies {
9696
compile (group: 'org.springframework.boot', name: 'spring-boot-starter', version: springBoot_1_X_Version )
9797

9898
compileOnly("org.springframework.boot:spring-boot-configuration-processor:${springBoot_1_X_Version}")
99-
99+
100+
compileOnly group: 'org.springframework.cloud', name: 'spring-cloud-consul-discovery', version: '2.1.1.RELEASE'
101+
compileOnly group: 'org.springframework.cloud', name: 'spring-cloud-starter-consul', version: '2.1.1.RELEASE'
102+
103+
100104
}
101105
compileJava.dependsOn(processResources)
102106

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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;
1011
import org.springframework.beans.factory.BeanCreationException;
1112
import org.springframework.beans.factory.DisposableBean;
1213
import org.springframework.beans.factory.annotation.Autowired;
@@ -88,7 +89,7 @@ public void run(String... args) throws Exception {
8889

8990
configurer.configure(serverBuilder);
9091
server = serverBuilder.build().start();
91-
applicationContext.publishEvent(new GRpcServerInitializedEvent(server));
92+
applicationContext.publishEvent(new GRpcServerInitializedEvent(applicationContext,server));
9293

9394
log.info("gRPC Server started, listening on port {}.", server.getPort());
9495
startDaemonAwaitThread();
@@ -153,6 +154,7 @@ public void destroy() throws Exception {
153154
server.getServices().forEach(def->healthStatusManager.clearStatus(def.getServiceDescriptor().getName()));
154155
Optional.ofNullable(server).ifPresent(Server::shutdown);
155156
log.info("gRPC server stopped.");
157+
applicationContext.publishEvent(new GRpcServerStoppedEvent(applicationContext,server));
156158
}
157159

158160
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/GRpcAutoConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ public HealthStatusManager healthStatusManager() {
5252
public GRpcServerBuilderConfigurer serverBuilderConfigurer(){
5353
return new GRpcServerBuilderConfigurer();
5454
}
55+
56+
57+
58+
59+
60+
61+
5562
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.lognet.springboot.grpc.autoconfigure.consul;
2+
3+
import org.lognet.springboot.grpc.GRpcServerRunner;
4+
import org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration;
5+
import org.springframework.beans.BeansException;
6+
import org.springframework.beans.factory.config.BeanPostProcessor;
7+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
9+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
10+
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
11+
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
12+
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
13+
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistryAutoConfiguration;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Configuration;
16+
17+
@Configuration
18+
@AutoConfigureAfter({ ConsulServiceRegistryAutoConfiguration.class, GRpcAutoConfiguration.class})
19+
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
20+
@ConditionalOnBean({ConsulServiceRegistry.class, GRpcServerRunner.class})
21+
22+
public class ConsulGrpcAutoConfiguration{
23+
24+
25+
@Bean
26+
public GrpcConsulRegistrar consulRegistrar(ServiceRegistry<ConsulRegistration> consulServiceRegistry){
27+
return new GrpcConsulRegistrar(consulServiceRegistry);
28+
}
29+
30+
// @Bean
31+
public BeanPostProcessor consuleServiceRegistryPostProcessor() {
32+
return new BeanPostProcessor() {
33+
@Override
34+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
35+
return bean;
36+
}
37+
38+
@Override
39+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
40+
if(ConsulServiceRegistry.class.isAssignableFrom(bean.getClass())){
41+
return new SmartCountingConsulServiceRegistry(ConsulServiceRegistry.class.cast(bean));
42+
}
43+
return bean;
44+
}
45+
};
46+
}
47+
}
48+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.lognet.springboot.grpc.autoconfigure.consul;
2+
3+
import com.ecwid.consul.v1.agent.model.NewService;
4+
import org.lognet.springboot.grpc.context.GRpcServerInitializedEvent;
5+
import org.lognet.springboot.grpc.context.GRpcServerStoppedEvent;
6+
import org.lognet.springboot.grpc.context.GrpcServerEvent;
7+
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
8+
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
9+
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
10+
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
11+
import org.springframework.context.ApplicationContext;
12+
import org.springframework.context.event.EventListener;
13+
14+
public class GrpcConsulRegistrar {
15+
16+
private ConsulRegistration registration;
17+
private ServiceRegistry<ConsulRegistration> consulServiceRegistry;
18+
19+
public GrpcConsulRegistrar(ServiceRegistry<ConsulRegistration> consulServiceRegistry) {
20+
this.consulServiceRegistry = consulServiceRegistry;
21+
}
22+
23+
@EventListener
24+
public void onGrpcServerStarted(GRpcServerInitializedEvent initializedEvent) {
25+
registration = getRegistration(initializedEvent);
26+
consulServiceRegistry.register(registration);
27+
}
28+
@EventListener
29+
public void onGrpcServerStopped(GRpcServerStoppedEvent initializedEvent) {
30+
31+
consulServiceRegistry.deregister(registration);
32+
consulServiceRegistry.close();
33+
}
34+
35+
36+
37+
private ConsulRegistration getRegistration(GrpcServerEvent event) {
38+
ApplicationContext applicationContext = event.getApplicationContext();
39+
40+
41+
ConsulDiscoveryProperties properties = applicationContext.getBean(ConsulDiscoveryProperties.class);
42+
43+
NewService grpcService = new NewService();
44+
grpcService.setPort(event.getServer().getPort());
45+
if (!properties.isPreferAgentAddress()) {
46+
grpcService.setAddress(properties.getHostname());
47+
}
48+
String appName = "grpc_" + ConsulAutoRegistration.getAppName(properties, applicationContext.getEnvironment());
49+
grpcService.setName(ConsulAutoRegistration.normalizeForDns(appName));
50+
grpcService.setId("grpc_" +ConsulAutoRegistration.getInstanceId(properties, applicationContext));
51+
52+
/*
53+
service.setTags(createTags(properties));
54+
setCheck(service, autoServiceRegistrationProperties, properties, context,
55+
heartbeatProperties);
56+
57+
58+
59+
60+
61+
*/
62+
63+
64+
return new ConsulRegistration(grpcService, properties);
65+
}
66+
67+
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.lognet.springboot.grpc.autoconfigure.consul;
2+
3+
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
4+
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
5+
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
8+
public class SmartCountingConsulServiceRegistry implements ServiceRegistry<ConsulRegistration> {
9+
10+
private ServiceRegistry<ConsulRegistration> target;
11+
private AtomicInteger count = new AtomicInteger(0);
12+
13+
public SmartCountingConsulServiceRegistry(ServiceRegistry<ConsulRegistration> target) {
14+
this.target = target;
15+
}
16+
17+
@Override
18+
public void register(ConsulRegistration registration) {
19+
target.register(registration);
20+
count.incrementAndGet();
21+
}
22+
23+
@Override
24+
public void deregister(ConsulRegistration registration) {
25+
target.deregister(registration);
26+
count.decrementAndGet();
27+
}
28+
29+
@Override
30+
public void close() {
31+
if(0==count.intValue()){
32+
target.close();
33+
}
34+
}
35+
36+
@Override
37+
public void setStatus(ConsulRegistration registration, String status) {
38+
target.setStatus(registration,status);
39+
}
40+
41+
@Override
42+
public <T> T getStatus(ConsulRegistration registration) {
43+
return target.getStatus(registration);
44+
}
45+
}
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package org.lognet.springboot.grpc.context;
22

33
import io.grpc.Server;
4-
import org.springframework.context.ApplicationEvent;
4+
import org.springframework.context.ApplicationContext;
55

6-
public class GRpcServerInitializedEvent extends ApplicationEvent {
7-
/**
8-
* Create a new ApplicationEvent.
9-
*
10-
* @param source the object on which the event initially occurred (never {@code null})
11-
*/
12-
public GRpcServerInitializedEvent(Server source) {
13-
super(source);
6+
public class GRpcServerInitializedEvent extends GrpcServerEvent {
7+
private Server server;
8+
9+
public GRpcServerInitializedEvent(ApplicationContext context,Server server) {
10+
super(context);
11+
this.server = server;
12+
}
13+
14+
public ApplicationContext getApplicationContext(){
15+
return (ApplicationContext) getSource();
1416
}
1517
public Server getServer(){
16-
return (Server) getSource();
18+
return server;
1719
}
1820
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.lognet.springboot.grpc.context;
2+
3+
import io.grpc.Server;
4+
import org.springframework.context.ApplicationContext;
5+
6+
public class GRpcServerStoppedEvent extends GrpcServerEvent {
7+
private Server server;
8+
9+
public GRpcServerStoppedEvent(ApplicationContext context, Server server) {
10+
super(context);
11+
this.server = server;
12+
}
13+
14+
public ApplicationContext getApplicationContext(){
15+
return (ApplicationContext) getSource();
16+
}
17+
public Server getServer(){
18+
return server;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.lognet.springboot.grpc.context;
2+
3+
import io.grpc.Server;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.ApplicationEvent;
6+
7+
public abstract class GrpcServerEvent extends ApplicationEvent {
8+
/**
9+
* Create a new ApplicationEvent.
10+
*
11+
* @param source the object on which the event initially occurred (never {@code null})
12+
*/
13+
public GrpcServerEvent(Object source) {
14+
super(source);
15+
}
16+
17+
public abstract Server getServer();
18+
public abstract ApplicationContext getApplicationContext();
19+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2-
org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration
2+
org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration,\
3+
org.lognet.springboot.grpc.autoconfigure.consul.ConsulGrpcAutoConfiguration
34

0 commit comments

Comments
 (0)