Skip to content

Commit 41e5802

Browse files
committed
#8: Codec support
1 parent 85de440 commit 41e5802

File tree

4 files changed

+84
-23
lines changed

4 files changed

+84
-23
lines changed

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcChannelFactoryConfigurations.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.springframework.grpc.autoconfigure.client;
1717

18-
import java.net.URI;
1918
import java.util.List;
2019

2120
import javax.net.ssl.SSLException;
@@ -78,16 +77,11 @@ public GrpcChannelConfigurer secureChannelConfigurer(GrpcClientProperties channe
7877
}
7978

8079
private static io.grpc.netty.shaded.io.grpc.netty.NegotiationType of(final NegotiationType negotiationType) {
81-
switch (negotiationType) {
82-
case PLAINTEXT:
83-
return io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT;
84-
case PLAINTEXT_UPGRADE:
85-
return io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
86-
case TLS:
87-
return io.grpc.netty.shaded.io.grpc.netty.NegotiationType.TLS;
88-
default:
89-
throw new IllegalArgumentException("Unsupported NegotiationType: " + negotiationType);
90-
}
80+
return switch (negotiationType) {
81+
case PLAINTEXT -> io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT;
82+
case PLAINTEXT_UPGRADE -> io.grpc.netty.shaded.io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
83+
case TLS -> io.grpc.netty.shaded.io.grpc.netty.NegotiationType.TLS;
84+
};
9185
}
9286

9387
}
@@ -128,16 +122,11 @@ public GrpcChannelConfigurer secureChannelConfigurer(GrpcClientProperties channe
128122
}
129123

130124
private static io.grpc.netty.NegotiationType of(final NegotiationType negotiationType) {
131-
switch (negotiationType) {
132-
case PLAINTEXT:
133-
return io.grpc.netty.NegotiationType.PLAINTEXT;
134-
case PLAINTEXT_UPGRADE:
135-
return io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
136-
case TLS:
137-
return io.grpc.netty.NegotiationType.TLS;
138-
default:
139-
throw new IllegalArgumentException("Unsupported NegotiationType: " + negotiationType);
140-
}
125+
return switch (negotiationType) {
126+
case PLAINTEXT -> io.grpc.netty.NegotiationType.PLAINTEXT;
127+
case PLAINTEXT_UPGRADE -> io.grpc.netty.NegotiationType.PLAINTEXT_UPGRADE;
128+
case TLS -> io.grpc.netty.NegotiationType.TLS;
129+
};
141130
}
142131

143132
}
@@ -168,8 +157,7 @@ static class NamedChannelVirtualTargets implements VirtualTargets {
168157
@Override
169158
public String getTarget(String authority) {
170159
NamedChannel channel = this.channels.getChannel(authority);
171-
String address = channels.getTarget(channel.getAddress());
172-
return address;
160+
return channels.getTarget(channel.getAddress());
173161
}
174162

175163
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/client/GrpcClientAutoConfiguration.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import java.util.concurrent.TimeUnit;
1919

20+
import io.grpc.CompressorRegistry;
21+
import io.grpc.DecompressorRegistry;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2023
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2124
import org.springframework.boot.ssl.SslBundle;
2225
import org.springframework.boot.ssl.SslBundles;
@@ -81,4 +84,16 @@ else if (ShadedNettyChannelFactoryHelper.isAvailable()) {
8184
};
8285
}
8386

87+
@ConditionalOnBean(CompressorRegistry.class)
88+
@Bean
89+
public GrpcChannelConfigurer compressionServerConfigurer(final CompressorRegistry registry) {
90+
return (name, builder) -> builder.compressorRegistry(registry);
91+
}
92+
93+
@ConditionalOnBean(DecompressorRegistry.class)
94+
@Bean
95+
public GrpcChannelConfigurer decompressionServerConfigurer(final DecompressorRegistry registry) {
96+
return (name, builder) -> builder.decompressorRegistry(registry);
97+
}
98+
8499
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.springframework.grpc.autoconfigure.common.codec;
2+
3+
import io.grpc.Codec;
4+
import io.grpc.CompressorRegistry;
5+
import io.grpc.DecompressorRegistry;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
/**
12+
* The autoconfiguration that contains all codec related beans for clients/servers.
13+
*
14+
* @author Andrei Lisa
15+
*/
16+
@Configuration(proxyBeanMethods = false)
17+
@ConditionalOnClass(Codec.class)
18+
public class GrpcCodecAutoConfiguration {
19+
20+
@ConditionalOnMissingBean
21+
@Bean
22+
public Codec gzipCompressor() {
23+
return new Codec.Gzip();
24+
}
25+
26+
@ConditionalOnMissingBean
27+
@Bean
28+
public CompressorRegistry defaultCompressorRegistry(Codec codec) {
29+
final CompressorRegistry registry = CompressorRegistry.getDefaultInstance();
30+
registry.register(codec);
31+
return registry;
32+
}
33+
34+
@ConditionalOnMissingBean
35+
@Bean
36+
public DecompressorRegistry defaultDecompressorRegistry(Codec codec) {
37+
DecompressorRegistry registry = DecompressorRegistry.getDefaultInstance();
38+
registry = registry.with(codec, false);
39+
return registry;
40+
}
41+
42+
}

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import io.grpc.BindableService;
1919

20+
import io.grpc.CompressorRegistry;
21+
import io.grpc.DecompressorRegistry;
22+
import io.grpc.netty.NettyServerBuilder;
2023
import org.springframework.beans.factory.ObjectProvider;
2124
import org.springframework.boot.autoconfigure.AutoConfiguration;
2225
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
@@ -70,4 +73,17 @@ ServerBuilderCustomizers serverBuilderCustomizers(ObjectProvider<ServerBuilderCu
7073
return new ServerBuilderCustomizers(customizers.orderedStream().toList());
7174
}
7275

76+
@ConditionalOnBean(CompressorRegistry.class)
77+
@Bean
78+
public ServerBuilderCustomizer<NettyServerBuilder> compressionServerConfigurer(final CompressorRegistry registry) {
79+
return builder -> builder.compressorRegistry(registry);
80+
}
81+
82+
@ConditionalOnBean(DecompressorRegistry.class)
83+
@Bean
84+
public ServerBuilderCustomizer<NettyServerBuilder> decompressionServerConfigurer(
85+
final DecompressorRegistry registry) {
86+
return builder -> builder.decompressorRegistry(registry);
87+
}
88+
7389
}

0 commit comments

Comments
 (0)