|
15 | 15 | */
|
16 | 16 | package org.springframework.grpc.autoconfigure.client;
|
17 | 17 |
|
| 18 | +import java.net.URI; |
18 | 19 | import java.util.List;
|
| 20 | +import java.util.concurrent.TimeUnit; |
19 | 21 |
|
20 | 22 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
| 23 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 24 | +import org.springframework.boot.ssl.SslBundle; |
| 25 | +import org.springframework.boot.ssl.SslBundles; |
21 | 26 | import org.springframework.context.annotation.Bean;
|
22 | 27 | import org.springframework.context.annotation.Configuration;
|
| 28 | +import org.springframework.grpc.autoconfigure.client.GrpcClientProperties.NamedChannel; |
23 | 29 | import org.springframework.grpc.client.DefaultGrpcChannelFactory;
|
24 | 30 | import org.springframework.grpc.client.GrpcChannelConfigurer;
|
25 | 31 | import org.springframework.grpc.client.GrpcChannelFactory;
|
26 | 32 |
|
| 33 | +import io.grpc.ManagedChannelBuilder; |
| 34 | + |
27 | 35 | @Configuration(proxyBeanMethods = false)
|
| 36 | +@EnableConfigurationProperties(GrpcClientProperties.class) |
28 | 37 | public class GrpcClientAutoConfiguration {
|
29 | 38 |
|
30 | 39 | @Bean
|
31 | 40 | @ConditionalOnMissingBean(GrpcChannelFactory.class)
|
32 |
| - public DefaultGrpcChannelFactory defaultGrpcChannelFactory(final List<GrpcChannelConfigurer> configurers) { |
33 |
| - return new DefaultGrpcChannelFactory(configurers); |
| 41 | + public DefaultGrpcChannelFactory defaultGrpcChannelFactory(final List<GrpcChannelConfigurer> configurers, |
| 42 | + GrpcClientProperties channels) { |
| 43 | + return new DefaultGrpcChannelFactory(configurers) { |
| 44 | + @Override |
| 45 | + public ManagedChannelBuilder<?> newChannel(String authority) { |
| 46 | + if (channels.getChannels().containsKey(authority)) { |
| 47 | + NamedChannel channel = channels.getChannels().get(authority); |
| 48 | + URI address = channel.getAddress(); |
| 49 | + if (address.getScheme().equals("static") || address.getScheme().equals("tcp")) { |
| 50 | + return super.newChannel(address.getAuthority()); |
| 51 | + } |
| 52 | + return super.newChannel(address.toString()); |
| 53 | + } |
| 54 | + return super.newChannel(authority); |
| 55 | + } |
| 56 | + }; |
| 57 | + } |
| 58 | + |
| 59 | + @Bean |
| 60 | + public GrpcChannelConfigurer sslGrpcChannelConfigurer(GrpcClientProperties channels, SslBundles bundles) { |
| 61 | + return (authority, builder) -> { |
| 62 | + for (String name : channels.getChannels().keySet()) { |
| 63 | + if (authority.equals(name)) { |
| 64 | + NamedChannel channel = channels.getChannels().get(name); |
| 65 | + if (channel.getSsl().isEnabled() && channel.getSsl().getBundle() != null) { |
| 66 | + SslBundle bundle = bundles.getBundle(channel.getSsl().getBundle()); |
| 67 | + if (NettyChannelFactoryHelper.isAvailable()) { |
| 68 | + NettyChannelFactoryHelper.sslContext(builder, bundle); |
| 69 | + } |
| 70 | + else if (ShadedNettyChannelFactoryHelper.isAvailable()) { |
| 71 | + ShadedNettyChannelFactoryHelper.sslContext(builder, bundle); |
| 72 | + } |
| 73 | + else { |
| 74 | + throw new IllegalStateException("Netty is not available"); |
| 75 | + } |
| 76 | + } |
| 77 | + else { |
| 78 | + // builder.usePlaintext(); |
| 79 | + } |
| 80 | + if (channel.getUserAgent() != null) { |
| 81 | + builder.userAgent(channel.getUserAgent()); |
| 82 | + } |
| 83 | + if (channel.getDefaultLoadBalancingPolicy() != null) { |
| 84 | + builder.defaultLoadBalancingPolicy(channel.getDefaultLoadBalancingPolicy()); |
| 85 | + } |
| 86 | + if (channel.getMaxInboundMessageSize() != null) { |
| 87 | + builder.maxInboundMessageSize((int) channel.getMaxInboundMessageSize().toBytes()); |
| 88 | + } |
| 89 | + if (channel.getMaxInboundMetadataSize() != null) { |
| 90 | + builder.maxInboundMetadataSize((int) channel.getMaxInboundMetadataSize().toBytes()); |
| 91 | + } |
| 92 | + if (channel.getKeepAliveTime() != null) { |
| 93 | + builder.keepAliveTime(channel.getKeepAliveTime().toNanos(), TimeUnit.NANOSECONDS); |
| 94 | + } |
| 95 | + if (channel.getKeepAliveTimeout() != null) { |
| 96 | + builder.keepAliveTimeout(channel.getKeepAliveTimeout().toNanos(), TimeUnit.NANOSECONDS); |
| 97 | + } |
| 98 | + builder.keepAliveWithoutCalls(channel.isKeepAliveWithoutCalls()); |
| 99 | + if (channel.getIdleTimeout() != null) { |
| 100 | + builder.idleTimeout(channel.getIdleTimeout().toNanos(), TimeUnit.NANOSECONDS); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
34 | 105 | }
|
35 | 106 |
|
36 | 107 | }
|
0 commit comments