Skip to content

Commit 0771303

Browse files
committed
Improve channel factory API
This commit improves the GrpcChannelFactory createChannel API by introducing `ChannelBuilderOptions` that can be specified during channel creation. Additionally, concrete Netty channel factory implementations have been added as well as adding type to the GrpcChannelBuilderCustomizer which helps match customizers to channel factories.
1 parent b3271be commit 0771303

File tree

24 files changed

+1194
-260
lines changed

24 files changed

+1194
-260
lines changed

samples/grpc-server-netty-shaded/src/test/java/com/example/demo/DemoApplicationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import org.apache.commons.logging.Log;
66
import org.apache.commons.logging.LogFactory;
77
import org.junit.jupiter.api.Test;
8+
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.SpringApplication;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.boot.test.context.TestConfiguration;
1213
import org.springframework.context.annotation.Bean;
1314
import org.springframework.context.annotation.Lazy;
15+
import org.springframework.grpc.client.ChannelBuilderOptions;
1416
import org.springframework.grpc.client.GrpcChannelFactory;
1517
import org.springframework.grpc.test.LocalGrpcPort;
1618
import org.springframework.test.annotation.DirtiesContext;
@@ -51,7 +53,8 @@ static class TestListener {
5153
@Bean
5254
@Lazy
5355
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
54-
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
56+
return SimpleGrpc
57+
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
5558
}
5659

5760
}

samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import org.apache.commons.logging.Log;
66
import org.apache.commons.logging.LogFactory;
77
import org.junit.jupiter.api.Test;
8+
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.builder.SpringApplicationBuilder;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.boot.test.context.TestConfiguration;
1213
import org.springframework.context.annotation.Bean;
1314
import org.springframework.context.annotation.Lazy;
15+
import org.springframework.grpc.client.ChannelBuilderOptions;
1416
import org.springframework.grpc.client.GrpcChannelFactory;
1517
import org.springframework.grpc.sample.proto.HelloReply;
1618
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -50,7 +52,8 @@ static class ExtraConfiguration {
5052
@Bean
5153
@Lazy
5254
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
53-
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
55+
return SimpleGrpc
56+
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
5457
}
5558

5659
}

samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerHealthIntegrationTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.boot.test.context.SpringBootTest;
3333
import org.springframework.boot.test.context.TestConfiguration;
3434
import org.springframework.context.annotation.Bean;
35+
import org.springframework.grpc.client.ChannelBuilderOptions;
3536
import org.springframework.grpc.client.GrpcChannelFactory;
3637
import org.springframework.grpc.sample.proto.HelloReply;
3738
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -62,7 +63,7 @@ class WithClientHealthEnabled {
6263
@Test
6364
void loadBalancerRespectsServerHealth(@Autowired GrpcChannelFactory channels,
6465
@Autowired HealthStatusManager healthStatusManager) {
65-
ManagedChannel channel = channels.createChannel("health-test").build();
66+
ManagedChannel channel = channels.createChannel("health-test", ChannelBuilderOptions.defaults());
6667
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc.newBlockingStub(channel);
6768

6869
// put the service up (SERVING) and give load balancer time to update
@@ -117,7 +118,7 @@ class WithActuatorHealthAdapter {
117118

118119
@Test
119120
void healthIndicatorsAdaptedToGrpcHealthStatus(@Autowired GrpcChannelFactory channels) {
120-
var channel = channels.createChannel("0.0.0.0:0").build();
121+
var channel = channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults());
121122
var healthStub = HealthGrpc.newBlockingStub(channel);
122123
var serviceName = "custom";
123124

samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerIntegrationTests.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.api.condition.EnabledOnOs;
2525
import org.junit.jupiter.api.condition.OS;
26+
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.boot.test.context.SpringBootTest;
2829
import org.springframework.grpc.autoconfigure.server.GrpcServerProperties;
30+
import org.springframework.grpc.client.ChannelBuilderOptions;
2931
import org.springframework.grpc.client.GrpcChannelFactory;
3032
import org.springframework.grpc.sample.proto.HelloReply;
3133
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -36,8 +38,9 @@
3638
import org.springframework.test.context.ActiveProfiles;
3739

3840
import io.grpc.ManagedChannel;
39-
import io.grpc.StatusRuntimeException;
4041
import io.grpc.Status.Code;
42+
import io.grpc.StatusRuntimeException;
43+
import io.grpc.netty.NettyChannelBuilder;
4144

4245
/**
4346
* More detailed integration tests for {@link GrpcServerFactory gRPC server factories} and
@@ -51,7 +54,7 @@ class ServerWithInProcessChannel {
5154

5255
@Test
5356
void servesResponseToClient(@Autowired GrpcChannelFactory channels) {
54-
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:0").build());
57+
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
5558
}
5659

5760
}
@@ -63,7 +66,7 @@ class ServerWithException {
6366
@Test
6467
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
6568
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
66-
.newBlockingStub(channels.createChannel("0.0.0.0:0").build());
69+
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
6770
assertThat(assertThrows(StatusRuntimeException.class,
6871
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
6972
.getStatus()
@@ -73,7 +76,7 @@ void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
7376
@Test
7477
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
7578
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
76-
.newBlockingStub(channels.createChannel("0.0.0.0:0").build());
79+
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
7780
assertThat(assertThrows(StatusRuntimeException.class,
7881
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
7982
.getStatus()
@@ -89,7 +92,7 @@ class ServerWithUnhandledException {
8992
@Test
9093
void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
9194
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
92-
.newBlockingStub(channels.createChannel("0.0.0.0:0").build());
95+
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
9396
assertThat(assertThrows(StatusRuntimeException.class,
9497
() -> client.sayHello(HelloRequest.newBuilder().setName("error").build()))
9598
.getStatus()
@@ -99,7 +102,7 @@ void specificErrorResponse(@Autowired GrpcChannelFactory channels) {
99102
@Test
100103
void defaultErrorResponseIsUnknown(@Autowired GrpcChannelFactory channels) {
101104
SimpleGrpc.SimpleBlockingStub client = SimpleGrpc
102-
.newBlockingStub(channels.createChannel("0.0.0.0:0").build());
105+
.newBlockingStub(channels.createChannel("0.0.0.0:0", ChannelBuilderOptions.defaults()));
103106
assertThat(assertThrows(StatusRuntimeException.class,
104107
() -> client.sayHello(HelloRequest.newBuilder().setName("internal").build()))
105108
.getStatus()
@@ -116,7 +119,8 @@ class ServerWithAnyIPv4AddressAndRandomPort {
116119
@Test
117120
void servesResponseToClientWithAnyIPv4AddressAndRandomPort(@Autowired GrpcChannelFactory channels,
118121
@LocalGrpcPort int port) {
119-
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port).build());
122+
assertThatResponseIsServedToChannel(
123+
channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
120124
}
121125

122126
}
@@ -129,7 +133,8 @@ class ServerWithAnyIPv6AddressAndRandomPort {
129133
@Test
130134
void servesResponseToClientWithAnyIPv4AddressAndRandomPort(@Autowired GrpcChannelFactory channels,
131135
@LocalGrpcPort int port) {
132-
assertThatResponseIsServedToChannel(channels.createChannel("0.0.0.0:" + port).build());
136+
assertThatResponseIsServedToChannel(
137+
channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
133138
}
134139

135140
}
@@ -142,7 +147,8 @@ class ServerWithLocalhostAndRandomPort {
142147
@Test
143148
void servesResponseToClientWithLocalhostAndRandomPort(@Autowired GrpcChannelFactory channels,
144149
@LocalGrpcPort int port) {
145-
assertThatResponseIsServedToChannel(channels.createChannel("127.0.0.1:" + port).build());
150+
assertThatResponseIsServedToChannel(
151+
channels.createChannel("127.0.0.1:" + port, ChannelBuilderOptions.defaults()));
146152
}
147153

148154
}
@@ -156,7 +162,8 @@ class ServerConfiguredWithStaticClientChannel {
156162

157163
@Test
158164
void servesResponseToClientWithConfiguredChannel(@Autowired GrpcChannelFactory channels) {
159-
assertThatResponseIsServedToChannel(channels.createChannel("test-channel").build());
165+
assertThatResponseIsServedToChannel(
166+
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
160167
}
161168

162169
}
@@ -169,7 +176,8 @@ class ServerWithUnixDomain {
169176

170177
@Test
171178
void clientChannelWithUnixDomain(@Autowired GrpcChannelFactory channels) {
172-
assertThatResponseIsServedToChannel(channels.createChannel("unix:unix-test-channel").build());
179+
assertThatResponseIsServedToChannel(channels.createChannel("unix:unix-test-channel",
180+
ChannelBuilderOptions.defaults().<NettyChannelBuilder>withCustomizer((__, b) -> b.usePlaintext())));
173181
}
174182

175183
}
@@ -185,7 +193,8 @@ class ServerWithSsl {
185193

186194
@Test
187195
void clientChannelWithSsl(@Autowired GrpcChannelFactory channels) {
188-
assertThatResponseIsServedToChannel(channels.createChannel("test-channel").build());
196+
assertThatResponseIsServedToChannel(
197+
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
189198
}
190199

191200
}
@@ -203,7 +212,8 @@ class ServerWithClientAuth {
203212

204213
@Test
205214
void clientChannelWithSsl(@Autowired GrpcChannelFactory channels) {
206-
assertThatResponseIsServedToChannel(channels.createChannel("test-channel").build());
215+
assertThatResponseIsServedToChannel(
216+
channels.createChannel("test-channel", ChannelBuilderOptions.defaults()));
207217
}
208218

209219
}

samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import org.apache.commons.logging.Log;
66
import org.apache.commons.logging.LogFactory;
77
import org.junit.jupiter.api.Test;
8+
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.builder.SpringApplicationBuilder;
1011
import org.springframework.boot.test.context.SpringBootTest;
1112
import org.springframework.boot.test.context.TestConfiguration;
1213
import org.springframework.boot.test.web.server.LocalServerPort;
1314
import org.springframework.context.annotation.Bean;
1415
import org.springframework.context.annotation.Lazy;
16+
import org.springframework.grpc.client.ChannelBuilderOptions;
1517
import org.springframework.grpc.client.GrpcChannelFactory;
1618
import org.springframework.grpc.sample.proto.HelloReply;
1719
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -49,7 +51,8 @@ static class ExtraConfiguration {
4951
@Bean
5052
@Lazy
5153
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalServerPort int port) {
52-
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
54+
return SimpleGrpc
55+
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
5356
}
5457

5558
}

samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/ListenOnTwoPortsTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.springframework.boot.test.context.TestConfiguration;
1212
import org.springframework.context.annotation.Bean;
1313
import org.springframework.context.annotation.Lazy;
14+
import org.springframework.grpc.client.ChannelBuilderOptions;
1415
import org.springframework.grpc.client.GrpcChannelFactory;
1516
import org.springframework.grpc.sample.proto.HelloReply;
1617
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -51,7 +52,8 @@ static class ExtraConfiguration {
5152
@Bean
5253
@Lazy
5354
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
54-
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
55+
return SimpleGrpc
56+
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
5557
}
5658

5759
}

samples/grpc-webflux/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.boot.test.context.TestConfiguration;
1313
import org.springframework.context.annotation.Bean;
1414
import org.springframework.context.annotation.Lazy;
15+
import org.springframework.grpc.client.ChannelBuilderOptions;
1516
import org.springframework.grpc.client.GrpcChannelFactory;
1617
import org.springframework.grpc.sample.proto.HelloReply;
1718
import org.springframework.grpc.sample.proto.HelloRequest;
@@ -51,7 +52,8 @@ static class ExtraConfiguration {
5152
@Bean
5253
@Lazy
5354
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
54-
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
55+
return SimpleGrpc
56+
.newBlockingStub(channels.createChannel("0.0.0.0:" + port, ChannelBuilderOptions.defaults()));
5557
}
5658

5759
}

0 commit comments

Comments
 (0)