当前位置:   article > 正文

gRPC编码初探(java)_blockingunarycall

blockingunarycall

背景:gRPC是一个高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。gRPC提供了一种简单的方法来精确地定义服务和为iOS、Android和后台支持服务自动生成可靠性很强的客户端功能库。客户端充分利用高级流和链接功能,从而有助于节省带宽、降低的TCP链接次数、节省CPU使用、和电池寿命。 以下初探编码过程:

1、安装插件Protobuf-dt最新版本,我的版本为2.2.1

2、下载protobuf

找到对应操作系统版本(我的系统为OS)直接解压到某个目录(我的目录为:/Users/peng/protoc-3.0.0-beta-2-osx-x86_64),链接:https://github.com/google/protobuf
  • 1

3、新建maven项目,过程省略

4、在pom.xml文件中添加gRPC的相关依赖

<dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-all</artifactId>
      <version>0.13.2</version>
 </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

添加maven-protobuf-plugin,在pom.xml中添加以下内容

<build> 

    <extensions> 

        <extension> 

            <groupId>kr.motd.maven</groupId> 

            <artifactId>os-maven-plugin</artifactId> 

            <version>1.4.1.Final</version> 

        </extension> 

    </extensions> 

    <plugins> 

        <plugin> 

            <groupId>org.xolstice.maven.plugins</groupId> 

            <artifactId>protobuf-maven-plugin</artifactId> 

            <version>0.5.0</version> 

            <configuration> 

                <!-- 

                  The version of protoc must match protobuf-java. If you don't depend on 

                  protobuf-java directly, you will be transitively depending on the 

                  protobuf-java version that grpc depends on. 

                --> 

                <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}</protocArtifact> 

                <pluginId>grpc-java</pluginId> 

                <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.13.2:exe:${os.detected.classifier}</pluginArtifact> 

                <protocExecutable>/Users/peng/protoc-3.0.0-beta-2-osx-x86_64/protoc</protocExecutable>

            </configuration> 

            <executions> 

                <execution> 

                    <goals> 

                        <goal>compile</goal> 

                        <goal>compile-custom</goal> 

                    </goals> 

                </execution> 

            </executions> 

        </plugin> 

    </plugins> 

</build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

注意protocExecutable节点后的目录为第2步中protobuf的安装路径

最终的pom.xml文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mzsg.demo</groupId>
  <artifactId>grpc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>grpc</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
      <groupId>io.grpc</groupId>
      <artifactId>grpc-all</artifactId>
      <version>0.13.2</version>
    </dependency>
  </dependencies>

  <build> 
        <extensions> 
            <extension> 
                <groupId>kr.motd.maven</groupId> 
                <artifactId>os-maven-plugin</artifactId> 
                <version>1.4.1.Final</version> 
            </extension> 
        </extensions> 
        <plugins> 
            <plugin> 
                <groupId>org.xolstice.maven.plugins</groupId> 
                <artifactId>protobuf-maven-plugin</artifactId> 
                <version>0.5.0</version> 
                <configuration> 
                    <!-- 
                      The version of protoc must match protobuf-java. If you don't depend on 
                      protobuf-java directly, you will be transitively depending on the 
                      protobuf-java version that grpc depends on. 
                    --> 
                    <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}</protocArtifact> 
                    <pluginId>grpc-java</pluginId> 
                    <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.13.2:exe:${os.detected.classifier}</pluginArtifact> 
                    <protocExecutable>/Users/peng/protoc-3.0.0-beta-2-osx-x86_64/protoc</protocExecutable>
                </configuration> 
                <executions> 
                    <execution> 
                        <goals> 
                            <goal>compile</goal> 
                            <goal>compile-custom</goal> 
                        </goals> 
                    </execution> 
                </executions> 
            </plugin> 
        </plugins> 
    </build> 
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

5、编写proto文件,描述入参、出参及远程方法

在src/main下面建立proto目录,protobuf-maven-plugin默认会扫描该目录以生成java文件

在proto目录下新建文件,AccountQry.proto,内容为

syntax = "proto3";
package accountService;
option java_package = "com.mzsg.demo.grpc.qryaccount";
option java_outer_classname = "QryAccountProto";

//账户查询请求
message AccountQryRequest {
  //请求流水
  string requestId = 1;
  //用户ID
  string userId = 2;
}

//账户查询响应
message AccountQryResponse {
  //请求流水
  string requestId = 1;
  //返回码,1:成功; -1:失败
  int32 rc = 2;
  //错误消息
  string msg = 3;
  //账户余额
  int32 amount = 4;
}

/**
 * 账户操查询服务
 */
service QryAccountService {

  //账户查询方法
  rpc Qry(AccountQryRequest) returns (AccountQryResponse);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

注意:本例用查账户查询作为demo,因为涉及到后面的性能(包括了序列化,反序列化)对比,故不再简单采用Hello world来测试

6、运行maven-generate-source,生成proto对应的java文件

生成文件结构生成文件结构

grpc-java下的为方法,java下的为入参出参,将这两个文件copy到com.mzsg.demo.grpc.qryaccount包下

注意:QryAccountServiceGrpc.java文件会提示@Override注解报错,直接删除注解即可,另外生成的代码也不是很简洁,有点无语

QryAccountServiceGrpc.java内容

package com.mzsg.demo.grpc.qryaccount;

import static io.grpc.stub.ClientCalls.asyncUnaryCall;
import static io.grpc.stub.ClientCalls.asyncServerStreamingCall;
import static io.grpc.stub.ClientCalls.asyncClientStreamingCall;
import static io.grpc.stub.ClientCalls.asyncBidiStreamingCall;
import static io.grpc.stub.ClientCalls.blockingUnaryCall;
import static io.grpc.stub.ClientCalls.blockingServerStreamingCall;
import static io.grpc.stub.ClientCalls.futureUnaryCall;
import static io.grpc.MethodDescriptor.generateFullMethodName;
import static io.grpc.stub.ServerCalls.asyncUnaryCall;
import static io.grpc.stub.ServerCalls.asyncServerStreamingCall;
import static io.grpc.stub.ServerCalls.asyncClientStreamingCall;
import static io.grpc.stub.ServerCalls.asyncBidiStreamingCall;

@javax.annotation.Generated("by gRPC proto compiler")
public class QryAccountServiceGrpc {

  private QryAccountServiceGrpc() {}

  public static final String SERVICE_NAME = "accountService.QryAccountService";

  // Static method descriptors that strictly reflect the proto.
  @io.grpc.ExperimentalApi
  public static final io.grpc.MethodDescriptor<com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest,
      com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse> METHOD_QRY =
      io.grpc.MethodDescriptor.create(
          io.grpc.MethodDescriptor.MethodType.UNARY,
          generateFullMethodName(
              "accountService.QryAccountService", "Qry"),
          io.grpc.protobuf.ProtoUtils.marshaller(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.getDefaultInstance()),
          io.grpc.protobuf.ProtoUtils.marshaller(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.getDefaultInstance()));

  public static QryAccountServiceStub newStub(io.grpc.Channel channel) {
    return new QryAccountServiceStub(channel);
  }

  public static QryAccountServiceBlockingStub newBlockingStub(
      io.grpc.Channel channel) {
    return new QryAccountServiceBlockingStub(channel);
  }

  public static QryAccountServiceFutureStub newFutureStub(
      io.grpc.Channel channel) {
    return new QryAccountServiceFutureStub(channel);
  }

  public static interface QryAccountService {

    public void qry(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest request,
        io.grpc.stub.StreamObserver<com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse> responseObserver);
  }

  public static interface QryAccountServiceBlockingClient {

    public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse qry(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest request);
  }

  public static interface QryAccountServiceFutureClient {

    public com.google.common.util.concurrent.ListenableFuture<com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse> qry(
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest request);
  }

  public static class QryAccountServiceStub extends io.grpc.stub.AbstractStub<QryAccountServiceStub>
      implements QryAccountService {
    private QryAccountServiceStub(io.grpc.Channel channel) {
      super(channel);
    }

    private QryAccountServiceStub(io.grpc.Channel channel,
        io.grpc.CallOptions callOptions) {
      super(channel, callOptions);
    }

    @java.lang.Override
    protected QryAccountServiceStub build(io.grpc.Channel channel,
        io.grpc.CallOptions callOptions) {
      return new QryAccountServiceStub(channel, callOptions);
    }

    public void qry(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest request,
        io.grpc.stub.StreamObserver<com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse> responseObserver) {
      asyncUnaryCall(
          getChannel().newCall(METHOD_QRY, getCallOptions()), request, responseObserver);
    }
  }

  public static class QryAccountServiceBlockingStub extends io.grpc.stub.AbstractStub<QryAccountServiceBlockingStub>
      implements QryAccountServiceBlockingClient {
    private QryAccountServiceBlockingStub(io.grpc.Channel channel) {
      super(channel);
    }

    private QryAccountServiceBlockingStub(io.grpc.Channel channel,
        io.grpc.CallOptions callOptions) {
      super(channel, callOptions);
    }

    @java.lang.Override
    protected QryAccountServiceBlockingStub build(io.grpc.Channel channel,
        io.grpc.CallOptions callOptions) {
      return new QryAccountServiceBlockingStub(channel, callOptions);
    }

    public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse qry(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest request) {
      return blockingUnaryCall(
          getChannel(), METHOD_QRY, getCallOptions(), request);
    }
  }

  public static class QryAccountServiceFutureStub extends io.grpc.stub.AbstractStub<QryAccountServiceFutureStub>
      implements QryAccountServiceFutureClient {
    private QryAccountServiceFutureStub(io.grpc.Channel channel) {
      super(channel);
    }

    private QryAccountServiceFutureStub(io.grpc.Channel channel,
        io.grpc.CallOptions callOptions) {
      super(channel, callOptions);
    }

    @java.lang.Override
    protected QryAccountServiceFutureStub build(io.grpc.Channel channel,
        io.grpc.CallOptions callOptions) {
      return new QryAccountServiceFutureStub(channel, callOptions);
    }

    public com.google.common.util.concurrent.ListenableFuture<com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse> qry(
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest request) {
      return futureUnaryCall(
          getChannel().newCall(METHOD_QRY, getCallOptions()), request);
    }
  }

  private static final int METHODID_QRY = 0;

  private static class MethodHandlers<Req, Resp> implements
      io.grpc.stub.ServerCalls.UnaryMethod<Req, Resp>,
      io.grpc.stub.ServerCalls.ServerStreamingMethod<Req, Resp>,
      io.grpc.stub.ServerCalls.ClientStreamingMethod<Req, Resp>,
      io.grpc.stub.ServerCalls.BidiStreamingMethod<Req, Resp> {
    private final QryAccountService serviceImpl;
    private final int methodId;

    public MethodHandlers(QryAccountService serviceImpl, int methodId) {
      this.serviceImpl = serviceImpl;
      this.methodId = methodId;
    }

    @java.lang.SuppressWarnings("unchecked")
    public void invoke(Req request, io.grpc.stub.StreamObserver<Resp> responseObserver) {
      switch (methodId) {
        case METHODID_QRY:
          serviceImpl.qry((com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest) request,
              (io.grpc.stub.StreamObserver<com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse>) responseObserver);
          break;
        default:
          throw new AssertionError();
      }
    }

    @java.lang.SuppressWarnings("unchecked")
    public io.grpc.stub.StreamObserver<Req> invoke(
        io.grpc.stub.StreamObserver<Resp> responseObserver) {
      switch (methodId) {
        default:
          throw new AssertionError();
      }
    }
  }

  public static io.grpc.ServerServiceDefinition bindService(
      final QryAccountService serviceImpl) {
    return io.grpc.ServerServiceDefinition.builder(SERVICE_NAME)
        .addMethod(
          METHOD_QRY,
          asyncUnaryCall(
            new MethodHandlers<
              com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest,
              com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse>(
                serviceImpl, METHODID_QRY)))
        .build();
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185

QryAccountProto.java文件内容

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: AccountQry.proto

package com.mzsg.demo.grpc.qryaccount;

public final class QryAccountProto {
  private QryAccountProto() {}
  public static void registerAllExtensions(
      com.google.protobuf.ExtensionRegistry registry) {
  }
  public interface AccountQryRequestOrBuilder extends
      // @@protoc_insertion_point(interface_extends:accountService.AccountQryRequest)
      com.google.protobuf.MessageOrBuilder {

    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    java.lang.String getRequestId();
    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    com.google.protobuf.ByteString
        getRequestIdBytes();

    /**
     * <code>optional string userId = 2;</code>
     *
     * <pre>
     *用户ID
     * </pre>
     */
    java.lang.String getUserId();
    /**
     * <code>optional string userId = 2;</code>
     *
     * <pre>
     *用户ID
     * </pre>
     */
    com.google.protobuf.ByteString
        getUserIdBytes();
  }
  /**
   * Protobuf type {@code accountService.AccountQryRequest}
   *
   * <pre>
   *账户查询请求
   * </pre>
   */
  public  static final class AccountQryRequest extends
      com.google.protobuf.GeneratedMessage implements
      // @@protoc_insertion_point(message_implements:accountService.AccountQryRequest)
      AccountQryRequestOrBuilder {
    // Use AccountQryRequest.newBuilder() to construct.
    private AccountQryRequest(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
      super(builder);
    }
    private AccountQryRequest() {
      requestId_ = "";
      userId_ = "";
    }

    @java.lang.Override
    public final com.google.protobuf.UnknownFieldSet
    getUnknownFields() {
      return com.google.protobuf.UnknownFieldSet.getDefaultInstance();
    }
    private AccountQryRequest(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) {
      this();
      int mutable_bitField0_ = 0;
      try {
        boolean done = false;
        while (!done) {
          int tag = input.readTag();
          switch (tag) {
            case 0:
              done = true;
              break;
            default: {
              if (!input.skipField(tag)) {
                done = true;
              }
              break;
            }
            case 10: {
              java.lang.String s = input.readStringRequireUtf8();

              requestId_ = s;
              break;
            }
            case 18: {
              java.lang.String s = input.readStringRequireUtf8();

              userId_ = s;
              break;
            }
          }
        }
      } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        throw new RuntimeException(e.setUnfinishedMessage(this));
      } catch (java.io.IOException e) {
        throw new RuntimeException(
            new com.google.protobuf.InvalidProtocolBufferException(
                e.getMessage()).setUnfinishedMessage(this));
      } finally {
        makeExtensionsImmutable();
      }
    }
    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryRequest_descriptor;
    }

    protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryRequest_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.class, com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.Builder.class);
    }

    public static final int REQUESTID_FIELD_NUMBER = 1;
    private volatile java.lang.Object requestId_;
    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    public java.lang.String getRequestId() {
      java.lang.Object ref = requestId_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        requestId_ = s;
        return s;
      }
    }
    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    public com.google.protobuf.ByteString
        getRequestIdBytes() {
      java.lang.Object ref = requestId_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        requestId_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    public static final int USERID_FIELD_NUMBER = 2;
    private volatile java.lang.Object userId_;
    /**
     * <code>optional string userId = 2;</code>
     *
     * <pre>
     *用户ID
     * </pre>
     */
    public java.lang.String getUserId() {
      java.lang.Object ref = userId_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        userId_ = s;
        return s;
      }
    }
    /**
     * <code>optional string userId = 2;</code>
     *
     * <pre>
     *用户ID
     * </pre>
     */
    public com.google.protobuf.ByteString
        getUserIdBytes() {
      java.lang.Object ref = userId_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        userId_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    private byte memoizedIsInitialized = -1;
    public final boolean isInitialized() {
      byte isInitialized = memoizedIsInitialized;
      if (isInitialized == 1) return true;
      if (isInitialized == 0) return false;

      memoizedIsInitialized = 1;
      return true;
    }

    public void writeTo(com.google.protobuf.CodedOutputStream output)
                        throws java.io.IOException {
      if (!getRequestIdBytes().isEmpty()) {
        com.google.protobuf.GeneratedMessage.writeString(output, 1, requestId_);
      }
      if (!getUserIdBytes().isEmpty()) {
        com.google.protobuf.GeneratedMessage.writeString(output, 2, userId_);
      }
    }

    public int getSerializedSize() {
      int size = memoizedSize;
      if (size != -1) return size;

      size = 0;
      if (!getRequestIdBytes().isEmpty()) {
        size += com.google.protobuf.GeneratedMessage.computeStringSize(1, requestId_);
      }
      if (!getUserIdBytes().isEmpty()) {
        size += com.google.protobuf.GeneratedMessage.computeStringSize(2, userId_);
      }
      memoizedSize = size;
      return size;
    }

    private static final long serialVersionUID = 0L;
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(
        com.google.protobuf.ByteString data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(
        com.google.protobuf.ByteString data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(byte[] data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(
        byte[] data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseDelimitedFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseDelimitedFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(
        com.google.protobuf.CodedInputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parseFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }

    public Builder newBuilderForType() { return newBuilder(); }
    public static Builder newBuilder() {
      return DEFAULT_INSTANCE.toBuilder();
    }
    public static Builder newBuilder(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest prototype) {
      return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
    }
    public Builder toBuilder() {
      return this == DEFAULT_INSTANCE
          ? new Builder() : new Builder().mergeFrom(this);
    }

    @java.lang.Override
    protected Builder newBuilderForType(
        com.google.protobuf.GeneratedMessage.BuilderParent parent) {
      Builder builder = new Builder(parent);
      return builder;
    }
    /**
     * Protobuf type {@code accountService.AccountQryRequest}
     *
     * <pre>
     *账户查询请求
     * </pre>
     */
    public static final class Builder extends
        com.google.protobuf.GeneratedMessage.Builder<Builder> implements
        // @@protoc_insertion_point(builder_implements:accountService.AccountQryRequest)
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequestOrBuilder {
      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryRequest_descriptor;
      }

      protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryRequest_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.class, com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.Builder.class);
      }

      // Construct using com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.newBuilder()
      private Builder() {
        maybeForceBuilderInitialization();
      }

      private Builder(
          com.google.protobuf.GeneratedMessage.BuilderParent parent) {
        super(parent);
        maybeForceBuilderInitialization();
      }
      private void maybeForceBuilderInitialization() {
        if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
        }
      }
      public Builder clear() {
        super.clear();
        requestId_ = "";

        userId_ = "";

        return this;
      }

      public com.google.protobuf.Descriptors.Descriptor
          getDescriptorForType() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryRequest_descriptor;
      }

      public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest getDefaultInstanceForType() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.getDefaultInstance();
      }

      public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest build() {
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest result = buildPartial();
        if (!result.isInitialized()) {
          throw newUninitializedMessageException(result);
        }
        return result;
      }

      public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest buildPartial() {
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest result = new com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest(this);
        result.requestId_ = requestId_;
        result.userId_ = userId_;
        onBuilt();
        return result;
      }

      public Builder mergeFrom(com.google.protobuf.Message other) {
        if (other instanceof com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest) {
          return mergeFrom((com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest)other);
        } else {
          super.mergeFrom(other);
          return this;
        }
      }

      public Builder mergeFrom(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest other) {
        if (other == com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest.getDefaultInstance()) return this;
        if (!other.getRequestId().isEmpty()) {
          requestId_ = other.requestId_;
          onChanged();
        }
        if (!other.getUserId().isEmpty()) {
          userId_ = other.userId_;
          onChanged();
        }
        onChanged();
        return this;
      }

      public final boolean isInitialized() {
        return true;
      }

      public Builder mergeFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest parsedMessage = null;
        try {
          parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
          parsedMessage = (com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest) e.getUnfinishedMessage();
          throw e;
        } finally {
          if (parsedMessage != null) {
            mergeFrom(parsedMessage);
          }
        }
        return this;
      }

      private java.lang.Object requestId_ = "";
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public java.lang.String getRequestId() {
        java.lang.Object ref = requestId_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          requestId_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public com.google.protobuf.ByteString
          getRequestIdBytes() {
        java.lang.Object ref = requestId_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          requestId_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public Builder setRequestId(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }

        requestId_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public Builder clearRequestId() {

        requestId_ = getDefaultInstance().getRequestId();
        onChanged();
        return this;
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public Builder setRequestIdBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  checkByteStringIsUtf8(value);

        requestId_ = value;
        onChanged();
        return this;
      }

      private java.lang.Object userId_ = "";
      /**
       * <code>optional string userId = 2;</code>
       *
       * <pre>
       *用户ID
       * </pre>
       */
      public java.lang.String getUserId() {
        java.lang.Object ref = userId_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          userId_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>optional string userId = 2;</code>
       *
       * <pre>
       *用户ID
       * </pre>
       */
      public com.google.protobuf.ByteString
          getUserIdBytes() {
        java.lang.Object ref = userId_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          userId_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>optional string userId = 2;</code>
       *
       * <pre>
       *用户ID
       * </pre>
       */
      public Builder setUserId(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }

        userId_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional string userId = 2;</code>
       *
       * <pre>
       *用户ID
       * </pre>
       */
      public Builder clearUserId() {

        userId_ = getDefaultInstance().getUserId();
        onChanged();
        return this;
      }
      /**
       * <code>optional string userId = 2;</code>
       *
       * <pre>
       *用户ID
       * </pre>
       */
      public Builder setUserIdBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  checkByteStringIsUtf8(value);

        userId_ = value;
        onChanged();
        return this;
      }
      public final Builder setUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return this;
      }

      public final Builder mergeUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return this;
      }


      // @@protoc_insertion_point(builder_scope:accountService.AccountQryRequest)
    }

    // @@protoc_insertion_point(class_scope:accountService.AccountQryRequest)
    private static final com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest DEFAULT_INSTANCE;
    static {
      DEFAULT_INSTANCE = new com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest();
    }

    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest getDefaultInstance() {
      return DEFAULT_INSTANCE;
    }

    private static final com.google.protobuf.Parser<AccountQryRequest>
        PARSER = new com.google.protobuf.AbstractParser<AccountQryRequest>() {
      public AccountQryRequest parsePartialFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        try {
          return new AccountQryRequest(input, extensionRegistry);
        } catch (RuntimeException e) {
          if (e.getCause() instanceof
              com.google.protobuf.InvalidProtocolBufferException) {
            throw (com.google.protobuf.InvalidProtocolBufferException)
                e.getCause();
          }
          throw e;
        }
      }
    };

    public static com.google.protobuf.Parser<AccountQryRequest> parser() {
      return PARSER;
    }

    @java.lang.Override
    public com.google.protobuf.Parser<AccountQryRequest> getParserForType() {
      return PARSER;
    }

    public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest getDefaultInstanceForType() {
      return DEFAULT_INSTANCE;
    }

  }

  public interface AccountQryResponseOrBuilder extends
      // @@protoc_insertion_point(interface_extends:accountService.AccountQryResponse)
      com.google.protobuf.MessageOrBuilder {

    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    java.lang.String getRequestId();
    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    com.google.protobuf.ByteString
        getRequestIdBytes();

    /**
     * <code>optional int32 rc = 2;</code>
     *
     * <pre>
     *返回码,1:成功; -1:失败
     * </pre>
     */
    int getRc();

    /**
     * <code>optional string msg = 3;</code>
     *
     * <pre>
     *错误消息
     * </pre>
     */
    java.lang.String getMsg();
    /**
     * <code>optional string msg = 3;</code>
     *
     * <pre>
     *错误消息
     * </pre>
     */
    com.google.protobuf.ByteString
        getMsgBytes();

    /**
     * <code>optional int32 amount = 4;</code>
     *
     * <pre>
     *账户余额
     * </pre>
     */
    int getAmount();
  }
  /**
   * Protobuf type {@code accountService.AccountQryResponse}
   *
   * <pre>
   *账户查询响应
   * </pre>
   */
  public  static final class AccountQryResponse extends
      com.google.protobuf.GeneratedMessage implements
      // @@protoc_insertion_point(message_implements:accountService.AccountQryResponse)
      AccountQryResponseOrBuilder {
    // Use AccountQryResponse.newBuilder() to construct.
    private AccountQryResponse(com.google.protobuf.GeneratedMessage.Builder<?> builder) {
      super(builder);
    }
    private AccountQryResponse() {
      requestId_ = "";
      rc_ = 0;
      msg_ = "";
      amount_ = 0;
    }

    @java.lang.Override
    public final com.google.protobuf.UnknownFieldSet
    getUnknownFields() {
      return com.google.protobuf.UnknownFieldSet.getDefaultInstance();
    }
    private AccountQryResponse(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry) {
      this();
      int mutable_bitField0_ = 0;
      try {
        boolean done = false;
        while (!done) {
          int tag = input.readTag();
          switch (tag) {
            case 0:
              done = true;
              break;
            default: {
              if (!input.skipField(tag)) {
                done = true;
              }
              break;
            }
            case 10: {
              java.lang.String s = input.readStringRequireUtf8();

              requestId_ = s;
              break;
            }
            case 16: {

              rc_ = input.readInt32();
              break;
            }
            case 26: {
              java.lang.String s = input.readStringRequireUtf8();

              msg_ = s;
              break;
            }
            case 32: {

              amount_ = input.readInt32();
              break;
            }
          }
        }
      } catch (com.google.protobuf.InvalidProtocolBufferException e) {
        throw new RuntimeException(e.setUnfinishedMessage(this));
      } catch (java.io.IOException e) {
        throw new RuntimeException(
            new com.google.protobuf.InvalidProtocolBufferException(
                e.getMessage()).setUnfinishedMessage(this));
      } finally {
        makeExtensionsImmutable();
      }
    }
    public static final com.google.protobuf.Descriptors.Descriptor
        getDescriptor() {
      return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryResponse_descriptor;
    }

    protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
        internalGetFieldAccessorTable() {
      return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryResponse_fieldAccessorTable
          .ensureFieldAccessorsInitialized(
              com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.class, com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.Builder.class);
    }

    public static final int REQUESTID_FIELD_NUMBER = 1;
    private volatile java.lang.Object requestId_;
    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    public java.lang.String getRequestId() {
      java.lang.Object ref = requestId_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        requestId_ = s;
        return s;
      }
    }
    /**
     * <code>optional string requestId = 1;</code>
     *
     * <pre>
     *请求流水
     * </pre>
     */
    public com.google.protobuf.ByteString
        getRequestIdBytes() {
      java.lang.Object ref = requestId_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        requestId_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    public static final int RC_FIELD_NUMBER = 2;
    private int rc_;
    /**
     * <code>optional int32 rc = 2;</code>
     *
     * <pre>
     *返回码,1:成功; -1:失败
     * </pre>
     */
    public int getRc() {
      return rc_;
    }

    public static final int MSG_FIELD_NUMBER = 3;
    private volatile java.lang.Object msg_;
    /**
     * <code>optional string msg = 3;</code>
     *
     * <pre>
     *错误消息
     * </pre>
     */
    public java.lang.String getMsg() {
      java.lang.Object ref = msg_;
      if (ref instanceof java.lang.String) {
        return (java.lang.String) ref;
      } else {
        com.google.protobuf.ByteString bs = 
            (com.google.protobuf.ByteString) ref;
        java.lang.String s = bs.toStringUtf8();
        msg_ = s;
        return s;
      }
    }
    /**
     * <code>optional string msg = 3;</code>
     *
     * <pre>
     *错误消息
     * </pre>
     */
    public com.google.protobuf.ByteString
        getMsgBytes() {
      java.lang.Object ref = msg_;
      if (ref instanceof java.lang.String) {
        com.google.protobuf.ByteString b = 
            com.google.protobuf.ByteString.copyFromUtf8(
                (java.lang.String) ref);
        msg_ = b;
        return b;
      } else {
        return (com.google.protobuf.ByteString) ref;
      }
    }

    public static final int AMOUNT_FIELD_NUMBER = 4;
    private int amount_;
    /**
     * <code>optional int32 amount = 4;</code>
     *
     * <pre>
     *账户余额
     * </pre>
     */
    public int getAmount() {
      return amount_;
    }

    private byte memoizedIsInitialized = -1;
    public final boolean isInitialized() {
      byte isInitialized = memoizedIsInitialized;
      if (isInitialized == 1) return true;
      if (isInitialized == 0) return false;

      memoizedIsInitialized = 1;
      return true;
    }

    public void writeTo(com.google.protobuf.CodedOutputStream output)
                        throws java.io.IOException {
      if (!getRequestIdBytes().isEmpty()) {
        com.google.protobuf.GeneratedMessage.writeString(output, 1, requestId_);
      }
      if (rc_ != 0) {
        output.writeInt32(2, rc_);
      }
      if (!getMsgBytes().isEmpty()) {
        com.google.protobuf.GeneratedMessage.writeString(output, 3, msg_);
      }
      if (amount_ != 0) {
        output.writeInt32(4, amount_);
      }
    }

    public int getSerializedSize() {
      int size = memoizedSize;
      if (size != -1) return size;

      size = 0;
      if (!getRequestIdBytes().isEmpty()) {
        size += com.google.protobuf.GeneratedMessage.computeStringSize(1, requestId_);
      }
      if (rc_ != 0) {
        size += com.google.protobuf.CodedOutputStream
          .computeInt32Size(2, rc_);
      }
      if (!getMsgBytes().isEmpty()) {
        size += com.google.protobuf.GeneratedMessage.computeStringSize(3, msg_);
      }
      if (amount_ != 0) {
        size += com.google.protobuf.CodedOutputStream
          .computeInt32Size(4, amount_);
      }
      memoizedSize = size;
      return size;
    }

    private static final long serialVersionUID = 0L;
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(
        com.google.protobuf.ByteString data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(
        com.google.protobuf.ByteString data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(byte[] data)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(
        byte[] data,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws com.google.protobuf.InvalidProtocolBufferException {
      return PARSER.parseFrom(data, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseDelimitedFrom(java.io.InputStream input)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseDelimitedFrom(
        java.io.InputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseDelimitedFrom(input, extensionRegistry);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(
        com.google.protobuf.CodedInputStream input)
        throws java.io.IOException {
      return PARSER.parseFrom(input);
    }
    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parseFrom(
        com.google.protobuf.CodedInputStream input,
        com.google.protobuf.ExtensionRegistryLite extensionRegistry)
        throws java.io.IOException {
      return PARSER.parseFrom(input, extensionRegistry);
    }

    public Builder newBuilderForType() { return newBuilder(); }
    public static Builder newBuilder() {
      return DEFAULT_INSTANCE.toBuilder();
    }
    public static Builder newBuilder(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse prototype) {
      return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
    }
    public Builder toBuilder() {
      return this == DEFAULT_INSTANCE
          ? new Builder() : new Builder().mergeFrom(this);
    }

    @java.lang.Override
    protected Builder newBuilderForType(
        com.google.protobuf.GeneratedMessage.BuilderParent parent) {
      Builder builder = new Builder(parent);
      return builder;
    }
    /**
     * Protobuf type {@code accountService.AccountQryResponse}
     *
     * <pre>
     *账户查询响应
     * </pre>
     */
    public static final class Builder extends
        com.google.protobuf.GeneratedMessage.Builder<Builder> implements
        // @@protoc_insertion_point(builder_implements:accountService.AccountQryResponse)
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponseOrBuilder {
      public static final com.google.protobuf.Descriptors.Descriptor
          getDescriptor() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryResponse_descriptor;
      }

      protected com.google.protobuf.GeneratedMessage.FieldAccessorTable
          internalGetFieldAccessorTable() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryResponse_fieldAccessorTable
            .ensureFieldAccessorsInitialized(
                com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.class, com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.Builder.class);
      }

      // Construct using com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.newBuilder()
      private Builder() {
        maybeForceBuilderInitialization();
      }

      private Builder(
          com.google.protobuf.GeneratedMessage.BuilderParent parent) {
        super(parent);
        maybeForceBuilderInitialization();
      }
      private void maybeForceBuilderInitialization() {
        if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) {
        }
      }
      public Builder clear() {
        super.clear();
        requestId_ = "";

        rc_ = 0;

        msg_ = "";

        amount_ = 0;

        return this;
      }

      public com.google.protobuf.Descriptors.Descriptor
          getDescriptorForType() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.internal_static_accountService_AccountQryResponse_descriptor;
      }

      public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse getDefaultInstanceForType() {
        return com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.getDefaultInstance();
      }

      public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse build() {
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse result = buildPartial();
        if (!result.isInitialized()) {
          throw newUninitializedMessageException(result);
        }
        return result;
      }

      public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse buildPartial() {
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse result = new com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse(this);
        result.requestId_ = requestId_;
        result.rc_ = rc_;
        result.msg_ = msg_;
        result.amount_ = amount_;
        onBuilt();
        return result;
      }

      public Builder mergeFrom(com.google.protobuf.Message other) {
        if (other instanceof com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse) {
          return mergeFrom((com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse)other);
        } else {
          super.mergeFrom(other);
          return this;
        }
      }

      public Builder mergeFrom(com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse other) {
        if (other == com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse.getDefaultInstance()) return this;
        if (!other.getRequestId().isEmpty()) {
          requestId_ = other.requestId_;
          onChanged();
        }
        if (other.getRc() != 0) {
          setRc(other.getRc());
        }
        if (!other.getMsg().isEmpty()) {
          msg_ = other.msg_;
          onChanged();
        }
        if (other.getAmount() != 0) {
          setAmount(other.getAmount());
        }
        onChanged();
        return this;
      }

      public final boolean isInitialized() {
        return true;
      }

      public Builder mergeFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws java.io.IOException {
        com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse parsedMessage = null;
        try {
          parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry);
        } catch (com.google.protobuf.InvalidProtocolBufferException e) {
          parsedMessage = (com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse) e.getUnfinishedMessage();
          throw e;
        } finally {
          if (parsedMessage != null) {
            mergeFrom(parsedMessage);
          }
        }
        return this;
      }

      private java.lang.Object requestId_ = "";
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public java.lang.String getRequestId() {
        java.lang.Object ref = requestId_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          requestId_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public com.google.protobuf.ByteString
          getRequestIdBytes() {
        java.lang.Object ref = requestId_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          requestId_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public Builder setRequestId(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }

        requestId_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public Builder clearRequestId() {

        requestId_ = getDefaultInstance().getRequestId();
        onChanged();
        return this;
      }
      /**
       * <code>optional string requestId = 1;</code>
       *
       * <pre>
       *请求流水
       * </pre>
       */
      public Builder setRequestIdBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  checkByteStringIsUtf8(value);

        requestId_ = value;
        onChanged();
        return this;
      }

      private int rc_ ;
      /**
       * <code>optional int32 rc = 2;</code>
       *
       * <pre>
       *返回码,1:成功; -1:失败
       * </pre>
       */
      public int getRc() {
        return rc_;
      }
      /**
       * <code>optional int32 rc = 2;</code>
       *
       * <pre>
       *返回码,1:成功; -1:失败
       * </pre>
       */
      public Builder setRc(int value) {

        rc_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional int32 rc = 2;</code>
       *
       * <pre>
       *返回码,1:成功; -1:失败
       * </pre>
       */
      public Builder clearRc() {

        rc_ = 0;
        onChanged();
        return this;
      }

      private java.lang.Object msg_ = "";
      /**
       * <code>optional string msg = 3;</code>
       *
       * <pre>
       *错误消息
       * </pre>
       */
      public java.lang.String getMsg() {
        java.lang.Object ref = msg_;
        if (!(ref instanceof java.lang.String)) {
          com.google.protobuf.ByteString bs =
              (com.google.protobuf.ByteString) ref;
          java.lang.String s = bs.toStringUtf8();
          msg_ = s;
          return s;
        } else {
          return (java.lang.String) ref;
        }
      }
      /**
       * <code>optional string msg = 3;</code>
       *
       * <pre>
       *错误消息
       * </pre>
       */
      public com.google.protobuf.ByteString
          getMsgBytes() {
        java.lang.Object ref = msg_;
        if (ref instanceof String) {
          com.google.protobuf.ByteString b = 
              com.google.protobuf.ByteString.copyFromUtf8(
                  (java.lang.String) ref);
          msg_ = b;
          return b;
        } else {
          return (com.google.protobuf.ByteString) ref;
        }
      }
      /**
       * <code>optional string msg = 3;</code>
       *
       * <pre>
       *错误消息
       * </pre>
       */
      public Builder setMsg(
          java.lang.String value) {
        if (value == null) {
    throw new NullPointerException();
  }

        msg_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional string msg = 3;</code>
       *
       * <pre>
       *错误消息
       * </pre>
       */
      public Builder clearMsg() {

        msg_ = getDefaultInstance().getMsg();
        onChanged();
        return this;
      }
      /**
       * <code>optional string msg = 3;</code>
       *
       * <pre>
       *错误消息
       * </pre>
       */
      public Builder setMsgBytes(
          com.google.protobuf.ByteString value) {
        if (value == null) {
    throw new NullPointerException();
  }
  checkByteStringIsUtf8(value);

        msg_ = value;
        onChanged();
        return this;
      }

      private int amount_ ;
      /**
       * <code>optional int32 amount = 4;</code>
       *
       * <pre>
       *账户余额
       * </pre>
       */
      public int getAmount() {
        return amount_;
      }
      /**
       * <code>optional int32 amount = 4;</code>
       *
       * <pre>
       *账户余额
       * </pre>
       */
      public Builder setAmount(int value) {

        amount_ = value;
        onChanged();
        return this;
      }
      /**
       * <code>optional int32 amount = 4;</code>
       *
       * <pre>
       *账户余额
       * </pre>
       */
      public Builder clearAmount() {

        amount_ = 0;
        onChanged();
        return this;
      }
      public final Builder setUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return this;
      }

      public final Builder mergeUnknownFields(
          final com.google.protobuf.UnknownFieldSet unknownFields) {
        return this;
      }


      // @@protoc_insertion_point(builder_scope:accountService.AccountQryResponse)
    }

    // @@protoc_insertion_point(class_scope:accountService.AccountQryResponse)
    private static final com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse DEFAULT_INSTANCE;
    static {
      DEFAULT_INSTANCE = new com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse();
    }

    public static com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse getDefaultInstance() {
      return DEFAULT_INSTANCE;
    }

    private static final com.google.protobuf.Parser<AccountQryResponse>
        PARSER = new com.google.protobuf.AbstractParser<AccountQryResponse>() {
      public AccountQryResponse parsePartialFrom(
          com.google.protobuf.CodedInputStream input,
          com.google.protobuf.ExtensionRegistryLite extensionRegistry)
          throws com.google.protobuf.InvalidProtocolBufferException {
        try {
          return new AccountQryResponse(input, extensionRegistry);
        } catch (RuntimeException e) {
          if (e.getCause() instanceof
              com.google.protobuf.InvalidProtocolBufferException) {
            throw (com.google.protobuf.InvalidProtocolBufferException)
                e.getCause();
          }
          throw e;
        }
      }
    };

    public static com.google.protobuf.Parser<AccountQryResponse> parser() {
      return PARSER;
    }

    @java.lang.Override
    public com.google.protobuf.Parser<AccountQryResponse> getParserForType() {
      return PARSER;
    }

    public com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse getDefaultInstanceForType() {
      return DEFAULT_INSTANCE;
    }

  }

  private static com.google.protobuf.Descriptors.Descriptor
    internal_static_accountService_AccountQryRequest_descriptor;
  private static
    com.google.protobuf.GeneratedMessage.FieldAccessorTable
      internal_static_accountService_AccountQryRequest_fieldAccessorTable;
  private static com.google.protobuf.Descriptors.Descriptor
    internal_static_accountService_AccountQryResponse_descriptor;
  private static
    com.google.protobuf.GeneratedMessage.FieldAccessorTable
      internal_static_accountService_AccountQryResponse_fieldAccessorTable;

  public static com.google.protobuf.Descriptors.FileDescriptor
      getDescriptor() {
    return descriptor;
  }
  private static com.google.protobuf.Descriptors.FileDescriptor
      descriptor;
  static {
    java.lang.String[] descriptorData = {
      "\n\020AccountQry.proto\022\016accountService\"6\n\021Ac" +
      "countQryRequest\022\021\n\trequestId\030\001 \001(\t\022\016\n\006us" +
      "erId\030\002 \001(\t\"P\n\022AccountQryResponse\022\021\n\trequ" +
      "estId\030\001 \001(\t\022\n\n\002rc\030\002 \001(\005\022\013\n\003msg\030\003 \001(\t\022\016\n\006" +
      "amount\030\004 \001(\0052a\n\021QryAccountService\022L\n\003Qry" +
      "\022!.accountService.AccountQryRequest\032\".ac" +
      "countService.AccountQryResponseB0\n\035com.m" +
      "zsg.demo.grpc.qryaccountB\017QryAccountProt" +
      "ob\006proto3"
    };
    com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
        new com.google.protobuf.Descriptors.FileDescriptor.    InternalDescriptorAssigner() {
          public com.google.protobuf.ExtensionRegistry assignDescriptors(
              com.google.protobuf.Descriptors.FileDescriptor root) {
            descriptor = root;
            return null;
          }
        };
    com.google.protobuf.Descriptors.FileDescriptor
      .internalBuildGeneratedFileFrom(descriptorData,
        new com.google.protobuf.Descriptors.FileDescriptor[] {
        }, assigner);
    internal_static_accountService_AccountQryRequest_descriptor =
      getDescriptor().getMessageTypes().get(0);
    internal_static_accountService_AccountQryRequest_fieldAccessorTable = new
      com.google.protobuf.GeneratedMessage.FieldAccessorTable(
        internal_static_accountService_AccountQryRequest_descriptor,
        new java.lang.String[] { "RequestId", "UserId", });
    internal_static_accountService_AccountQryResponse_descriptor =
      getDescriptor().getMessageTypes().get(1);
    internal_static_accountService_AccountQryResponse_fieldAccessorTable = new
      com.google.protobuf.GeneratedMessage.FieldAccessorTable(
        internal_static_accountService_AccountQryResponse_descriptor,
        new java.lang.String[] { "RequestId", "Rc", "Msg", "Amount", });
  }

  // @@protoc_insertion_point(outer_class_scope)
}


7、编写接口实现类QryAccountServiceImpl.java

package com.mzsg.demo.grpc.qryaccount.impl;

import com.mzsg.demo.grpc.qryaccount.QryAccountProto;
import com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest;
import com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse;
import com.mzsg.demo.grpc.qryaccount.QryAccountServiceGrpc.QryAccountService;

import io.grpc.stub.StreamObserver;

public class QryAccountServiceImpl implements QryAccountService {

    public void qry(AccountQryRequest request, StreamObserver<AccountQryResponse> responseObserver) {
        System.out.println("qry " + request.getUserId());
        AccountQryResponse response = QryAccountProto.AccountQryResponse.newBuilder().setRc(1).setAmount(666).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929
  • 930
  • 931
  • 932
  • 933
  • 934
  • 935
  • 936
  • 937
  • 938
  • 939
  • 940
  • 941
  • 942
  • 943
  • 944
  • 945
  • 946
  • 947
  • 948
  • 949
  • 950
  • 951
  • 952
  • 953
  • 954
  • 955
  • 956
  • 957
  • 958
  • 959
  • 960
  • 961
  • 962
  • 963
  • 964
  • 965
  • 966
  • 967
  • 968
  • 969
  • 970
  • 971
  • 972
  • 973
  • 974
  • 975
  • 976
  • 977
  • 978
  • 979
  • 980
  • 981
  • 982
  • 983
  • 984
  • 985
  • 986
  • 987
  • 988
  • 989
  • 990
  • 991
  • 992
  • 993
  • 994
  • 995
  • 996
  • 997
  • 998
  • 999
  • 1000
  • 1001
  • 1002
  • 1003
  • 1004
  • 1005
  • 1006
  • 1007
  • 1008
  • 1009
  • 1010
  • 1011
  • 1012
  • 1013
  • 1014
  • 1015
  • 1016
  • 1017
  • 1018
  • 1019
  • 1020
  • 1021
  • 1022
  • 1023
  • 1024
  • 1025
  • 1026
  • 1027
  • 1028
  • 1029
  • 1030
  • 1031
  • 1032
  • 1033
  • 1034
  • 1035
  • 1036
  • 1037
  • 1038
  • 1039
  • 1040
  • 1041
  • 1042
  • 1043
  • 1044
  • 1045
  • 1046
  • 1047
  • 1048
  • 1049
  • 1050
  • 1051
  • 1052
  • 1053
  • 1054
  • 1055
  • 1056
  • 1057
  • 1058
  • 1059
  • 1060
  • 1061
  • 1062
  • 1063
  • 1064
  • 1065
  • 1066
  • 1067
  • 1068
  • 1069
  • 1070
  • 1071
  • 1072
  • 1073
  • 1074
  • 1075
  • 1076
  • 1077
  • 1078
  • 1079
  • 1080
  • 1081
  • 1082
  • 1083
  • 1084
  • 1085
  • 1086
  • 1087
  • 1088
  • 1089
  • 1090
  • 1091
  • 1092
  • 1093
  • 1094
  • 1095
  • 1096
  • 1097
  • 1098
  • 1099
  • 1100
  • 1101
  • 1102
  • 1103
  • 1104
  • 1105
  • 1106
  • 1107
  • 1108
  • 1109
  • 1110
  • 1111
  • 1112
  • 1113
  • 1114
  • 1115
  • 1116
  • 1117
  • 1118
  • 1119
  • 1120
  • 1121
  • 1122
  • 1123
  • 1124
  • 1125
  • 1126
  • 1127
  • 1128
  • 1129
  • 1130
  • 1131
  • 1132
  • 1133
  • 1134
  • 1135
  • 1136
  • 1137
  • 1138
  • 1139
  • 1140
  • 1141
  • 1142
  • 1143
  • 1144
  • 1145
  • 1146
  • 1147
  • 1148
  • 1149
  • 1150
  • 1151
  • 1152
  • 1153
  • 1154
  • 1155
  • 1156
  • 1157
  • 1158
  • 1159
  • 1160
  • 1161
  • 1162
  • 1163
  • 1164
  • 1165
  • 1166
  • 1167
  • 1168
  • 1169
  • 1170
  • 1171
  • 1172
  • 1173
  • 1174
  • 1175
  • 1176
  • 1177
  • 1178
  • 1179
  • 1180
  • 1181
  • 1182
  • 1183
  • 1184
  • 1185
  • 1186
  • 1187
  • 1188
  • 1189
  • 1190
  • 1191
  • 1192
  • 1193
  • 1194
  • 1195
  • 1196
  • 1197
  • 1198
  • 1199
  • 1200
  • 1201
  • 1202
  • 1203
  • 1204
  • 1205
  • 1206
  • 1207
  • 1208
  • 1209
  • 1210
  • 1211
  • 1212
  • 1213
  • 1214
  • 1215
  • 1216
  • 1217
  • 1218
  • 1219
  • 1220
  • 1221
  • 1222
  • 1223
  • 1224
  • 1225
  • 1226
  • 1227
  • 1228
  • 1229
  • 1230
  • 1231
  • 1232
  • 1233
  • 1234
  • 1235
  • 1236
  • 1237
  • 1238
  • 1239
  • 1240
  • 1241
  • 1242
  • 1243
  • 1244
  • 1245
  • 1246
  • 1247
  • 1248
  • 1249
  • 1250
  • 1251
  • 1252
  • 1253
  • 1254
  • 1255
  • 1256
  • 1257
  • 1258
  • 1259
  • 1260
  • 1261
  • 1262
  • 1263
  • 1264
  • 1265
  • 1266
  • 1267
  • 1268
  • 1269
  • 1270
  • 1271
  • 1272
  • 1273
  • 1274
  • 1275
  • 1276
  • 1277
  • 1278
  • 1279
  • 1280
  • 1281
  • 1282
  • 1283
  • 1284
  • 1285
  • 1286
  • 1287
  • 1288
  • 1289
  • 1290
  • 1291
  • 1292
  • 1293
  • 1294
  • 1295
  • 1296
  • 1297
  • 1298
  • 1299
  • 1300
  • 1301
  • 1302
  • 1303
  • 1304
  • 1305
  • 1306
  • 1307
  • 1308
  • 1309
  • 1310
  • 1311
  • 1312
  • 1313
  • 1314
  • 1315
  • 1316
  • 1317
  • 1318
  • 1319
  • 1320
  • 1321
  • 1322
  • 1323
  • 1324
  • 1325
  • 1326
  • 1327
  • 1328
  • 1329
  • 1330
  • 1331
  • 1332
  • 1333
  • 1334
  • 1335
  • 1336
  • 1337
  • 1338
  • 1339
  • 1340
  • 1341
  • 1342
  • 1343
  • 1344
  • 1345
  • 1346
  • 1347
  • 1348
  • 1349
  • 1350
  • 1351
  • 1352
  • 1353
  • 1354
  • 1355
  • 1356
  • 1357
  • 1358
  • 1359
  • 1360
  • 1361
  • 1362
  • 1363
  • 1364
  • 1365
  • 1366
  • 1367
  • 1368
  • 1369
  • 1370
  • 1371
  • 1372
  • 1373
  • 1374
  • 1375
  • 1376
  • 1377
  • 1378
  • 1379
  • 1380
  • 1381
  • 1382
  • 1383
  • 1384
  • 1385
  • 1386
  • 1387
  • 1388
  • 1389
  • 1390
  • 1391
  • 1392
  • 1393
  • 1394
  • 1395
  • 1396
  • 1397
  • 1398
  • 1399
  • 1400
  • 1401
  • 1402
  • 1403
  • 1404
  • 1405
  • 1406
  • 1407
  • 1408
  • 1409
  • 1410
  • 1411
  • 1412
  • 1413
  • 1414
  • 1415
  • 1416
  • 1417
  • 1418
  • 1419
  • 1420
  • 1421
  • 1422
  • 1423
  • 1424
  • 1425
  • 1426
  • 1427
  • 1428
  • 1429
  • 1430
  • 1431
  • 1432
  • 1433
  • 1434
  • 1435
  • 1436
  • 1437
  • 1438
  • 1439
  • 1440
  • 1441
  • 1442
  • 1443
  • 1444
  • 1445
  • 1446
  • 1447
  • 1448
  • 1449
  • 1450
  • 1451
  • 1452
  • 1453
  • 1454
  • 1455
  • 1456
  • 1457
  • 1458
  • 1459
  • 1460
  • 1461
  • 1462
  • 1463
  • 1464
  • 1465
  • 1466
  • 1467
  • 1468
  • 1469
  • 1470
  • 1471
  • 1472
  • 1473
  • 1474
  • 1475
  • 1476
  • 1477
  • 1478
  • 1479
  • 1480
  • 1481
  • 1482
  • 1483
  • 1484
  • 1485
  • 1486
  • 1487
  • 1488
  • 1489
  • 1490
  • 1491
  • 1492
  • 1493
  • 1494
  • 1495
  • 1496
  • 1497
  • 1498
  • 1499
  • 1500
  • 1501
  • 1502
  • 1503
  • 1504
  • 1505
  • 1506
  • 1507
  • 1508
  • 1509
  • 1510
  • 1511
  • 1512
  • 1513
  • 1514
  • 1515
  • 1516
  • 1517
  • 1518
  • 1519
  • 1520
  • 1521
  • 1522
  • 1523
  • 1524
  • 1525
  • 1526
  • 1527
  • 1528
  • 1529
  • 1530
  • 1531
  • 1532
  • 1533
  • 1534
  • 1535
  • 1536
  • 1537
  • 1538
  • 1539
  • 1540
  • 1541
  • 1542
  • 1543
  • 1544
  • 1545
  • 1546
  • 1547
  • 1548
  • 1549
  • 1550
  • 1551
  • 1552
  • 1553
  • 1554
  • 1555
  • 1556
  • 1557
  • 1558
  • 1559
  • 1560
  • 1561
  • 1562
  • 1563
  • 1564
  • 1565
  • 1566
  • 1567
  • 1568
  • 1569
  • 1570
  • 1571

7、编写接口实现类QryAccountServiceImpl.java

package com.mzsg.demo.grpc.qryaccount.impl;

import com.mzsg.demo.grpc.qryaccount.QryAccountProto;
import com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest;
import com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse;
import com.mzsg.demo.grpc.qryaccount.QryAccountServiceGrpc.QryAccountService;

import io.grpc.stub.StreamObserver;

public class QryAccountServiceImpl implements QryAccountService {

    public void qry(AccountQryRequest request, StreamObserver<AccountQryResponse> responseObserver) {
        System.out.println("qry " + request.getUserId());
        AccountQryResponse response = QryAccountProto.AccountQryResponse.newBuilder().setRc(1).setAmount(666).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

8、编码Server端代码Server.java

package com.mzsg.demo.grpc;

import java.io.IOException;

import com.mzsg.demo.grpc.qryaccount.QryAccountServiceGrpc;
import com.mzsg.demo.grpc.qryaccount.impl.QryAccountServiceImpl;

public class Server {

       private static int port = 8883;
        private static io.grpc.Server server;

        public void run() {
            QryAccountServiceGrpc.QryAccountService modifyAccountServiceImpl = new QryAccountServiceImpl();
            server = io.grpc.ServerBuilder.forPort(port).addService(QryAccountServiceGrpc.bindService(modifyAccountServiceImpl)).build();
            try {
                server.start();
                System.out.println("Server start success on port:" + port);
                server.awaitTermination();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            Server server = new Server();
            server.run();
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

9、编码Client端代码Client.java

package com.mzsg.demo.grpc;

import java.io.FileNotFoundException;
import java.io.IOException;

import com.mzsg.demo.grpc.qryaccount.QryAccountProto;
import com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryRequest;
import com.mzsg.demo.grpc.qryaccount.QryAccountProto.AccountQryResponse;
import com.mzsg.demo.grpc.qryaccount.QryAccountServiceGrpc;
import com.mzsg.demo.grpc.qryaccount.QryAccountServiceGrpc.QryAccountServiceBlockingStub;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class Client {
    public static void main( String[] args ) throws FileNotFoundException, IOException{
        AccountQryRequest request = QryAccountProto.AccountQryRequest.newBuilder().setUserId("20012").setRequestId("123").build();
        ManagedChannel channel = ManagedChannelBuilder
                  .forAddress("localhost", 8883)
                  .usePlaintext(true)
                  .build();
        QryAccountServiceBlockingStub stub = QryAccountServiceGrpc.newBlockingStub(channel);
        for (int j = 0; j < 20; j++) {
            long start = System.currentTimeMillis();
                for(int i=0; i<10000; i++){
                    AccountQryResponse rsp = stub.qry(request);
                    //System.out.println(rsp);
                }
                System.out.println(System.currentTimeMillis() - start + " MS");
            }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

10、运行Server.java,再运行Client.java
简单性能对比(每万次调用消耗时间比):

实现时间(毫秒)备注
HTTP接口13000springboot+json,客户端采用HttpClient
Google Grpc2100本demo
Facebook swift(Thrift)1500Thrift实现

转载:https://blog.csdn.net/tuxingchen6/article/details/55222951
https://blog.csdn.net/xueyepiaoling/article/details/72551860

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/430231
推荐阅读
相关标签
  

闽ICP备14008679号