如何在Akka grpc中实现管道

时间:2019-12-17 01:42:20

标签: java akka protocol-buffers akka-stream akka-grpc

我在Java中有一个示例性protobuf项目,该项目将protobuf请求消息发送到服务器,并使用netty通道处理程序来获取响应,类似于:

 public NettyClient(String host, int port) {
        this.host = host;
        this.port = port;
        this.msgFactory = new OA2ProtoMessageFactory();
        this.protoChannelMessageDecoder = new ProtoMessageToChannelMessageDecoder(msgFactory);
        this.protoChannelMessageEncoder = new ChannelMessageToProtoMessageEncoder(msgFactory);
        this.protoMessageReceiverHandler = new ProtoMessageReceiverHandler(msgFactory);
        connect();
    }

    public void connect() {
        workerGroup = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    initPipelineForChannel(ch);
                }
            });
            // Start the client.
            channelFuture = b.connect(host, port).sync();

        } catch (Exception ex) {
            closeConnection();
        }
    }

    private void initPipelineForChannel(Channel ch) throws SSLException {
        ChannelPipeline pipeline = ch.pipeline();
        SslEngineFactory sslEngineFactory = new ClientSslEngineFactory();
        pipeline.addLast("ssl", sslEngineFactory.newHandler(ch));

        pipeline.addLast("idleState", new IdleStateHandler(INACTIVITY_READ_MILLIS,
                PING_INTERVAL_MILLIS, 0, TimeUnit.MILLISECONDS));

        pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MAX_FRAME_LENGTH, 0, LENGTH_FIELD_LENGTH, 0,
                LENGTH_FIELD_LENGTH));
        pipeline.addLast("protobufDecoder", protobufDecoder);
        pipeline.addLast("protoChannelMessageDecoder", protoChannelMessageDecoder);
        pipeline.addLast("lengthFieldPrepender", lengthFieldPrepender);
        pipeline.addLast("protobufEncoder", protobufEncoder);
        pipeline.addLast("protoChannelMessageEncoder", protoChannelMessageEncoder);
        pipeline.addLast("heartbeatOnIdle", HeartbeatOnIdleHandler.DEFAULT);
        pipeline.addLast(ProtoMessageReceiverHandler.NAME, protoMessageReceiverHandler);
        pipeline.addLast("closeOnException", CloseOnExceptionHandler.DEFAULT);
    }

因此,如果我理解正确的话,这基本上会创建一个netty Bootstrap并在创建频道时添加一条管道。

如何在Akka grpc(解码器等)中设置此管道?

0 个答案:

没有答案