Skip to content

Commit 693a69a

Browse files
author
Alexander Furer
committed
closes #30
1 parent 421120c commit 693a69a

File tree

9 files changed

+149
-36
lines changed

9 files changed

+149
-36
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
apply plugin: 'idea'
22
buildscript {
33
ext {
4-
springBootVersion = '1.4.0.RELEASE'
5-
grpcVersion = '1.0.0'
4+
springBootVersion = '1.4.2.RELEASE'
5+
grpcVersion = '1.0.2'
66
}
77
repositories {
88
mavenCentral()

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
11
package io.grpc.examples;
22

3-
import static io.grpc.stub.ClientCalls.asyncUnaryCall;
4-
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;
5-
import static io.grpc.stub.ClientCalls.asyncClientStreamingCall;
6-
import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall;
7-
import static io.grpc.stub.ClientCalls.blockingUnaryCall;
8-
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
9-
import static io.grpc.stub.ClientCalls.futureUnaryCall;
103
import static io.grpc.MethodDescriptor.generateFullMethodName;
4+
import static io.grpc.stub.ClientCalls.*;
115
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
12-
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
13-
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
14-
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;
156
import static io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;
16-
import static io.grpc.stub.ServerCalls.asyncUnimplementedStreamingCall;
177

188
/**
199
*/
2010
@javax.annotation.Generated(
21-
value = "by gRPC proto compiler (version 1.0.0)",
11+
value = "by gRPC proto compiler (version 1.0.2)",
2212
comments = "Source: calculator.proto")
2313
public class CalculatorGrpc {
2414

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
@@ -11,7 +11,7 @@
1111
* </pre>
1212
*/
1313
@javax.annotation.Generated(
14-
value = "by gRPC proto compiler (version 1.0.0)",
14+
value = "by gRPC proto compiler (version 1.0.2)",
1515
comments = "Source: greeter.proto")
1616
public class GreeterGrpc {
1717

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package org.lognet.springboot.grpc;
22

3-
import static org.junit.Assert.*;
4-
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
5-
6-
import java.util.concurrent.ExecutionException;
7-
8-
import io.grpc.*;
3+
import io.grpc.ManagedChannel;
4+
import io.grpc.ManagedChannelBuilder;
5+
import io.grpc.ServerInterceptor;
96
import io.grpc.examples.CalculatorGrpc;
107
import io.grpc.examples.CalculatorOuterClass;
8+
import io.grpc.examples.GreeterGrpc;
9+
import io.grpc.examples.GreeterOuterClass;
1110
import org.hamcrest.CoreMatchers;
12-
import org.junit.After;
13-
import org.junit.Before;
14-
import org.junit.Rule;
15-
import org.junit.Test;
11+
import org.junit.*;
1612
import org.junit.runner.RunWith;
1713
import org.lognet.springboot.grpc.demo.DemoApp;
1814
import org.mockito.Mockito;
@@ -21,12 +17,15 @@
2117
import org.springframework.boot.test.context.SpringBootTest;
2218
import org.springframework.boot.test.rule.OutputCapture;
2319
import org.springframework.boot.test.web.client.TestRestTemplate;
20+
import org.springframework.context.ApplicationContext;
2421
import org.springframework.http.HttpStatus;
2522
import org.springframework.http.ResponseEntity;
2623
import org.springframework.test.context.junit4.SpringRunner;
2724

28-
import io.grpc.examples.GreeterGrpc;
29-
import io.grpc.examples.GreeterOuterClass;
25+
import java.util.concurrent.ExecutionException;
26+
27+
import static org.junit.Assert.*;
28+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
3029

3130
/**
3231
* Created by alexf on 28-Jan-16.
@@ -44,6 +43,8 @@ public class DemoAppTest {
4443
@Qualifier("globalInterceptor")
4544
private ServerInterceptor globalInterceptor;
4645

46+
@Autowired
47+
private ApplicationContext context;
4748

4849

4950
@Before
@@ -101,4 +102,12 @@ public void actuatorTest() throws ExecutionException, InterruptedException {
101102
}
102103

103104

105+
@Test
106+
public void testDefaultConfigurer(){
107+
Assert.assertEquals("Default configurer should be picked up",
108+
context.getBean(GRpcServerBuilderConfigurer.class).getClass(),
109+
GRpcServerBuilderConfigurer.class);
110+
}
111+
112+
104113
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import io.grpc.ManagedChannel;
4+
import io.grpc.ManagedChannelBuilder;
5+
import io.grpc.examples.CalculatorGrpc;
6+
import io.grpc.examples.CalculatorOuterClass;
7+
import org.hamcrest.CoreMatchers;
8+
import org.junit.*;
9+
import org.junit.runner.RunWith;
10+
import org.lognet.springboot.grpc.demo.DemoApp;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.boot.test.rule.OutputCapture;
14+
import org.springframework.test.context.ActiveProfiles;
15+
import org.springframework.test.context.junit4.SpringRunner;
16+
17+
import java.util.concurrent.ExecutionException;
18+
19+
import static org.lognet.springboot.grpc.TestConfig.CUSTOM_EXECUTOR_MESSAGE;
20+
21+
22+
/**
23+
* Created by 310242212 on 21-Dec-16.
24+
*/
25+
@RunWith(SpringRunner.class)
26+
@SpringBootTest(classes = {DemoApp.class,TestConfig.class},
27+
webEnvironment = SpringBootTest.WebEnvironment.NONE
28+
,properties = "grpc.port=7777")
29+
@ActiveProfiles(profiles = {"customServerBuilder"})
30+
public class GRpcServerBuilderConfigurerTest {
31+
32+
private ManagedChannel channel;
33+
34+
@Autowired
35+
private GRpcServerBuilderConfigurer configurer;
36+
37+
@Rule
38+
public OutputCapture outputCapture = new OutputCapture();
39+
40+
@Before
41+
public void setup() {
42+
channel = ManagedChannelBuilder.forAddress("localhost", 7777)
43+
.usePlaintext(true)
44+
.build();
45+
}
46+
47+
@After
48+
public void tearDown() {
49+
channel.shutdown();
50+
}
51+
52+
@Test
53+
public void customServerBuilderTest() throws ExecutionException, InterruptedException {
54+
55+
56+
Assert.assertNotEquals("Custom configurer should be picked up", configurer.getClass(),GRpcServerBuilderConfigurer.class);
57+
58+
double result = CalculatorGrpc.newFutureStub(channel)
59+
.calculate(CalculatorOuterClass.CalculatorRequest.newBuilder()
60+
.setNumber1(1.0)
61+
.setNumber2(1.0)
62+
.setOperation(CalculatorOuterClass.CalculatorRequest.OperationType.ADD)
63+
.build())
64+
.get().getResult();
65+
Assert.assertEquals(2.0,result,0.0);
66+
67+
// log interceptor should be invoked only on GreeterService and not CalculatorService
68+
outputCapture.expect(CoreMatchers.containsString(CUSTOM_EXECUTOR_MESSAGE));
69+
}
70+
}

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package org.lognet.springboot.grpc;
22

3-
import io.grpc.Metadata;
4-
import io.grpc.ServerCall;
5-
import io.grpc.ServerCallHandler;
6-
import io.grpc.ServerInterceptor;
7-
import static org.mockito.Mockito.*;
8-
3+
import io.grpc.*;
94
import org.mockito.invocation.InvocationOnMock;
105
import org.mockito.stubbing.Answer;
116
import org.springframework.context.annotation.Bean;
127
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.context.annotation.Profile;
139

10+
import static org.mockito.Mockito.*;
1411

1512

1613
/**
@@ -20,6 +17,7 @@
2017
public class TestConfig {
2118

2219

20+
public static final String CUSTOM_EXECUTOR_MESSAGE="Hello from custom executor.";
2321

2422
@Bean(name = "globalInterceptor")
2523
@GRpcGlobalInterceptor
@@ -37,6 +35,22 @@ public ServerCall.Listener answer(InvocationOnMock invocation) throws Throwable
3735
return mock;
3836
}
3937

38+
@Bean
39+
@Profile("customServerBuilder")
40+
public GRpcServerBuilderConfigurer grpcServerWithPredefinedPortConfigurer(){
41+
return new GRpcServerBuilderConfigurer(){
42+
@Override
43+
public ServerBuilder<?> configure(ServerBuilder<?> serverBuilder){
44+
return serverBuilder.executor(command -> {
45+
System.out.println(CUSTOM_EXECUTOR_MESSAGE);
46+
command.run();
47+
}
48+
);
49+
50+
}
51+
};
52+
}
53+
4054

4155

4256

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import io.grpc.ServerBuilder;
4+
5+
/**
6+
* Created by 310242212 on 21-Dec-16.
7+
*/
8+
public class GRpcServerBuilderConfigurer {
9+
public ServerBuilder<?> configure(ServerBuilder<?> serverBuilder){
10+
return serverBuilder;
11+
}
12+
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ public class GRpcServerRunner implements CommandLineRunner,DisposableBean {
3232
@Autowired
3333
private GRpcServerProperties gRpcServerProperties;
3434

35+
36+
private GRpcServerBuilderConfigurer configurer;
37+
3538
private Server server;
3639

40+
public GRpcServerRunner(GRpcServerBuilderConfigurer configurer) {
41+
this.configurer = configurer;
42+
}
43+
3744
@Override
3845
public void run(String... args) throws Exception {
3946
log.info("Starting gRPC Server ...");
@@ -57,7 +64,7 @@ public void run(String... args) throws Exception {
5764
});
5865

5966

60-
server = serverBuilder.build().start();
67+
server = configurer.configure(serverBuilder).build().start();
6168
log.info("gRPC Server started, listening on port {}.", gRpcServerProperties.getPort());
6269
startDaemonAwaitThread();
6370

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package org.lognet.springboot.grpc.autoconfigure;
22

3+
import org.lognet.springboot.grpc.GRpcServerBuilderConfigurer;
34
import org.lognet.springboot.grpc.GRpcServerRunner;
45
import org.lognet.springboot.grpc.GRpcService;
6+
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
57
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
8+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
69
import org.springframework.boot.context.properties.EnableConfigurationProperties;
710
import org.springframework.context.annotation.Bean;
811
import org.springframework.context.annotation.Configuration;
@@ -12,10 +15,18 @@
1215
*/
1316
@Configuration
1417
@EnableConfigurationProperties(GRpcServerProperties.class)
18+
@AutoConfigureOrder
1519
public class GRpcAutoConfiguration {
20+
1621
@Bean
1722
@ConditionalOnBean(annotation = GRpcService.class)
18-
public GRpcServerRunner grpcServerRunner(){
19-
return new GRpcServerRunner();
23+
public GRpcServerRunner grpcServerRunner(GRpcServerBuilderConfigurer configurer){
24+
return new GRpcServerRunner(configurer);
25+
}
26+
27+
@Bean
28+
@ConditionalOnMissingBean( GRpcServerBuilderConfigurer.class)
29+
public GRpcServerBuilderConfigurer serverBuilderConfigurer(){
30+
return new GRpcServerBuilderConfigurer();
2031
}
2132
}

0 commit comments

Comments
 (0)