|
18 | 18 |
|
19 | 19 | import io.netty.bootstrap.Bootstrap;
|
20 | 20 | import io.netty.channel.Channel;
|
| 21 | +import io.netty.channel.ChannelDuplexHandler; |
21 | 22 | import io.netty.channel.ChannelFuture;
|
22 | 23 | import io.netty.channel.ChannelHandler;
|
| 24 | +import io.netty.channel.ChannelHandlerContext; |
| 25 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
23 | 26 | import io.netty.channel.ChannelInitializer;
|
24 | 27 | import io.netty.channel.ChannelOption;
|
25 | 28 | import io.netty.channel.ChannelPipeline;
|
|
28 | 31 | import io.netty.channel.epoll.EpollMode;
|
29 | 32 | import io.netty.channel.nio.NioEventLoopGroup;
|
30 | 33 | import io.netty.channel.socket.SocketChannel;
|
| 34 | +import io.netty.handler.codec.http2.Http2FrameCodecBuilder; |
| 35 | +import io.netty.handler.codec.http2.Http2MultiplexHandler; |
| 36 | +import io.netty.handler.codec.http2.Http2StreamChannelBootstrap; |
31 | 37 | import io.netty.handler.timeout.IdleStateHandler;
|
32 | 38 | import io.netty.util.concurrent.DefaultEventExecutorGroup;
|
33 | 39 | import io.netty.util.concurrent.EventExecutorGroup;
|
34 | 40 | import io.netty.util.internal.PlatformDependent;
|
35 | 41 | import org.apache.seata.common.exception.FrameworkException;
|
36 | 42 | import org.apache.seata.common.thread.NamedThreadFactory;
|
| 43 | +import org.apache.seata.core.protocol.Protocol; |
37 | 44 | import org.apache.seata.core.rpc.RemotingBootstrap;
|
| 45 | +import org.apache.seata.core.rpc.netty.grpc.GrpcDecoder; |
| 46 | +import org.apache.seata.core.rpc.netty.grpc.GrpcEncoder; |
38 | 47 | import org.apache.seata.core.rpc.netty.v1.ProtocolDecoderV1;
|
39 | 48 | import org.apache.seata.core.rpc.netty.v1.ProtocolEncoderV1;
|
40 | 49 | import org.slf4j.Logger;
|
@@ -130,14 +139,18 @@ public void start() {
|
130 | 139 | @Override
|
131 | 140 | public void initChannel(SocketChannel ch) {
|
132 | 141 | ChannelPipeline pipeline = ch.pipeline();
|
133 |
| - pipeline |
134 |
| - .addLast(new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(), |
135 |
| - nettyClientConfig.getChannelMaxWriteIdleSeconds(), |
136 |
| - nettyClientConfig.getChannelMaxAllIdleSeconds())) |
137 |
| - .addLast(new ProtocolDecoderV1()) |
138 |
| - .addLast(new ProtocolEncoderV1()); |
139 |
| - if (channelHandlers != null) { |
140 |
| - addChannelPipelineLast(ch, channelHandlers); |
| 142 | + if (nettyClientConfig.getProtocol().equals(Protocol.GPRC.value)) { |
| 143 | + pipeline.addLast(Http2FrameCodecBuilder.forClient().build()) |
| 144 | + .addLast(new Http2MultiplexHandler(new ChannelDuplexHandler())); |
| 145 | + } else { |
| 146 | + pipeline.addLast(new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(), |
| 147 | + nettyClientConfig.getChannelMaxWriteIdleSeconds(), |
| 148 | + nettyClientConfig.getChannelMaxAllIdleSeconds())); |
| 149 | + pipeline.addLast(new ProtocolDecoderV1()) |
| 150 | + .addLast(new ProtocolEncoderV1()); |
| 151 | + if (channelHandlers != null) { |
| 152 | + addChannelPipelineLast(ch, channelHandlers); |
| 153 | + } |
141 | 154 | }
|
142 | 155 | }
|
143 | 156 | });
|
@@ -177,9 +190,30 @@ public Channel getNewChannel(InetSocketAddress address) {
|
177 | 190 | } else {
|
178 | 191 | channel = f.channel();
|
179 | 192 | }
|
| 193 | + |
| 194 | + if (nettyClientConfig.getProtocol().equals(Protocol.GPRC.value)) { |
| 195 | + Http2StreamChannelBootstrap bootstrap = new Http2StreamChannelBootstrap(channel); |
| 196 | + bootstrap.handler(new ChannelInboundHandlerAdapter() { |
| 197 | + @Override |
| 198 | + public void handlerAdded(ChannelHandlerContext ctx) { |
| 199 | + Channel channel = ctx.channel(); |
| 200 | + channel.pipeline().addLast(new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(), |
| 201 | + nettyClientConfig.getChannelMaxWriteIdleSeconds(), |
| 202 | + nettyClientConfig.getChannelMaxAllIdleSeconds())); |
| 203 | + channel.pipeline().addLast(new GrpcDecoder()); |
| 204 | + channel.pipeline().addLast(new GrpcEncoder()); |
| 205 | + if (channelHandlers != null) { |
| 206 | + addChannelPipelineLast(channel, channelHandlers); |
| 207 | + } |
| 208 | + } |
| 209 | + }); |
| 210 | + channel = bootstrap.open().get(); |
| 211 | + } |
| 212 | + |
180 | 213 | } catch (Exception e) {
|
181 | 214 | throw new FrameworkException(e, "can not connect to services-server.");
|
182 | 215 | }
|
| 216 | + |
183 | 217 | return channel;
|
184 | 218 | }
|
185 | 219 |
|
|
0 commit comments