Skip to content

Commit 293597a

Browse files
author
Alexander Furer
committed
closes #214
1 parent 031fa04 commit 293597a

File tree

12 files changed

+213
-5
lines changed

12 files changed

+213
-5
lines changed

README.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ By default, starter pulls `io.grpc:grpc-netty-shaded` as transitive dependency
5555
----
5656
<1> Make sure to pull the version that matches the release.
5757

58+
Both libraries' presence on classpath is also xref:_netty_server[supported] with `grpc.netty-server.on-collision-prefer-shaded-netty` property.
59+
5860
If you are using Spring Boot Dependency Management plugin, it might pull not the same version as the version this started was compiled against, causing binary incompatibility issue. +
5961
In this case you'll need to forcibly and explicitly set the `grpc` version to use (see link:ReleaseNotes.md[version matrix here^] ):
6062

@@ -124,6 +126,7 @@ Default value is `0` (means don't wait).
124126
* link:grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/GRpcServerProperties.java[Netty-specific server properties] can be specified under `grpc.netty-server` prefix. +
125127
By configuring one of the `grpc.netty-server.xxxx` values you are implicitly setting transport to be Netty-based.
126128

129+
[[_netty_server]]
127130
[source,yaml]
128131
----
129132
grpc:
@@ -133,7 +136,7 @@ grpc:
133136
primary-listen-address: 10.10.15.23:0 <3>
134137
additional-listen-addresses:
135138
- 192.168.0.100:6767 <4>
136-
139+
on-collision-prefer-shaded-netty: false <5>
137140
138141
----
139142
<1> `Duration` type properties can be configured with string value format described https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java[here].
@@ -143,6 +146,7 @@ grpc:
143146
* `host:port` (if `port` value is less than 1, uses random value)
144147
* `host:` (uses default grpc port, `6565` )
145148
<4> Exposed on internal network IP as well with predefined port `6767`.
149+
<5> In case you have both `shaded` and `pure` netty libraries in dependencies, pick the `NettyServerBuilder` type that should be created. This is the type that will be passed to `GRpcServerBuilderConfigurer` (see <<Custom gRPC Server Configuration>>), defaults to `true`(i.e. `io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder`; `io.grpc.netty.NettyServerBuilder` if `false`)
146150

147151
The starter supports also the `in-process server`, which should be used for testing purposes :
148152

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ apply plugin: "nebula.facet"
1616
facets {
1717
pureNettyTest
1818
customSecurityTest
19+
bothPureAndShadedNettyTest
1920
}
2021

2122
dependencyManagement {
@@ -45,6 +46,9 @@ configurations {
4546

4647
customSecurityTestCompile.extendsFrom( testCompile)
4748
customSecurityTestRuntime.extendsFrom(testRuntime)
49+
50+
bothPureAndShadedNettyTestCompile.extendsFrom( testCompile)
51+
bothPureAndShadedNettyTestRuntime.extendsFrom( testRuntime)
4852
}
4953

5054

@@ -90,10 +94,22 @@ dependencies {
9094

9195
pureNettyTestCompile sourceSets.test.output
9296
pureNettyTestCompile "io.grpc:grpc-netty"
97+
98+
99+
bothPureAndShadedNettyTestCompile sourceSets.test.output
100+
bothPureAndShadedNettyTestCompile "io.grpc:grpc-netty"
93101
//testCompile "org.testcontainers:junit-jupiter:1.14.3"
94102

95103

96104
}
105+
sourceSets.test.output.each {println it.absolutePath}
106+
//sourceSets {
107+
// bothPureAndShadedNettyTest {
108+
// resources {
109+
// srcDir sourceSets.test.output
110+
// }
111+
// }
112+
//}
97113

98114
configurations.all {
99115
resolutionStrategy.eachDependency { details ->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import io.grpc.ServerBuilder;
4+
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.lognet.springboot.grpc.demo.DemoApp;
8+
import org.mockito.Mockito;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.context.TestConfiguration;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Import;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
import org.springframework.util.ClassUtils;
16+
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.Assert.assertThrows;
20+
import static org.junit.Assert.assertTrue;
21+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
22+
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE)
25+
@Import(DefaultNettyCollisionTest.TestConfig.class)
26+
public class DefaultNettyCollisionTest extends GrpcServerTestBase {
27+
28+
@TestConfiguration
29+
static class TestConfig {
30+
@Bean
31+
public GRpcServerBuilderConfigurer customConfigurer(){
32+
return Mockito.mock(GRpcServerBuilderConfigurer.class);
33+
}
34+
}
35+
36+
@Autowired
37+
private GRpcServerBuilderConfigurer configurer;
38+
39+
@Test
40+
public void contextLoads() {
41+
assertNettyBuilderClass(io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder.class);
42+
43+
}
44+
45+
protected void assertNettyBuilderClass(Class<? extends ServerBuilder<?>> nettyServerBuilderClass){
46+
ClassLoader classLoader = getClass().getClassLoader();
47+
48+
assertThat("PureNetty is on classpath",ClassUtils.isPresent(io.grpc.netty.NettyServerBuilder.class.getName(), classLoader));
49+
assertThat("ShadedNetty is on classpath",ClassUtils.isPresent(io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder.class.getName(), classLoader));
50+
51+
Mockito.verify(configurer,Mockito.times(1))
52+
.configure(Mockito.any(nettyServerBuilderClass));
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.lognet.springboot.grpc.demo.DemoApp;
6+
import org.mockito.Mockito;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.context.TestConfiguration;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Import;
12+
import org.springframework.test.context.ActiveProfiles;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
import org.springframework.util.ClassUtils;
15+
16+
import java.io.InputStream;
17+
18+
import static org.hamcrest.MatcherAssert.assertThat;
19+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
20+
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE)
23+
@ActiveProfiles("prefer-pure-netty")
24+
@Import(DefaultNettyCollisionTest.TestConfig.class)
25+
public class PreferPureNettyCollisionTest extends DefaultNettyCollisionTest {
26+
27+
28+
@Test
29+
public void contextLoads() {
30+
assertNettyBuilderClass(io.grpc.netty.NettyServerBuilder.class);
31+
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.lognet.springboot.grpc.demo.DemoApp;
6+
import org.mockito.Mockito;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.context.TestConfiguration;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Import;
12+
import org.springframework.test.context.ActiveProfiles;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
import org.springframework.util.ClassUtils;
15+
16+
import java.io.InputStream;
17+
18+
import static org.hamcrest.MatcherAssert.assertThat;
19+
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
20+
21+
@RunWith(SpringRunner.class)
22+
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE)
23+
@ActiveProfiles("prefer-shaded-netty")
24+
@Import(DefaultNettyCollisionTest.TestConfig.class)
25+
public class PreferShadedNettyCollisionTest extends DefaultNettyCollisionTest {
26+
27+
28+
@Test
29+
public void contextLoads() {
30+
assertNettyBuilderClass(io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder.class);
31+
32+
33+
34+
}
35+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
grpc:
2+
netty-server:
3+
on-collision-prefer-shaded-netty: false
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
grpc:
2+
netty-server:
3+
on-collision-prefer-shaded-netty: true

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33

44
import org.springframework.boot.SpringApplication;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.builder.SpringApplicationBuilder;
67

78
/**
89
* Created by alexf on 28-Jan-16.
910
*/
1011
@SpringBootApplication
1112
public class DemoApp {
1213

13-
14-
1514
public static void main(String[] args) {
16-
SpringApplication.run(DemoApp.class,args);
15+
new SpringApplicationBuilder(DemoApp.class)
16+
.profiles("demo")
17+
.run(args);
1718
}
1819

1920
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
@AutoConfigureAfter(ValidationAutoConfiguration.class)
3838
@ConditionalOnBean(annotation = GRpcService.class)
3939
@EnableConfigurationProperties({GRpcServerProperties.class})
40-
@Import({GRpcValidationConfiguration.class,PureNettyConfiguration.class,ShadedNettyConfiguration.class})
40+
@Import({GRpcValidationConfiguration.class,
41+
NettyServerBuilderSelector.class
42+
})
4143
@Slf4j
4244
public class GRpcAutoConfiguration {
4345

0 commit comments

Comments
 (0)