JMeter 測試 thrift RPC 介面

yangbin發表於2020-09-09

1:前言篇

1.1:什麼是 RPC 介面 - 百度的一些摘要說明 解釋一下 RPC

提到 RPC(Remote Procedure Call),就躲不開提到分散式,這個促使 RPC 誕生的領域

假設你有一個 Calculator,以及它的實現類 CalculatorImpl,那麼單體應用時,要呼叫 Calculator 的 add 方法來執行一個加運算,你可以方法中直接使用,因為在同一個地址空間,或者說在同一塊記憶體,這個稱為本地函式呼叫。

現在,將系統改造為分散式應用,介面呼叫和實現分別在兩個子系統內
服務 A 裡頭並沒有 CalculatorImpl 這個類,那它要怎樣呼叫服務 B 的 CalculatorImpl 的 add 方法呢?可以模仿 B/S 架構的呼叫方式,在 B 服務暴露一個 Restful 介面,然後 A 服務透過呼叫這個 Restful 介面來間接呼叫 CalculatorImpl 的 add 方法。
這樣,已經很接近 RPC 了,不過,像這種每次呼叫時,是不是都需要寫一串發起 http 請求的程式碼呢?比如 httpClient.sendRequest...之類的,能不能簡單一下,像本地方法呼叫一樣,去發起遠端呼叫,讓使用者感知不到遠端呼叫的過程。

遮蔽的工作,可以使用代理模式解決,生成一個代理物件,而這個代理物件的內部,就是透過 httpClient 來實現 RPC 遠端過程呼叫的。

rpc 是一種概念,http 也是 rpc 實現的一種方式。

RPC 能解耦服務

RPC:遠端過程呼叫。RPC 的核心並不在於使用什麼協議。RPC 的目的是讓你在本地呼叫遠端的方法,而對你來說這個呼叫是透明的,你並不知道這個呼叫的方法是部署哪裡。

透過 RPC 能解耦服務,這才是使用 RPC 的真正目的。RPC 的原理主要用到了動態代理模式,至於 http 協議,只是傳輸協議而已。簡單的實現可以參考 spring remoting,複雜的實現可以參考 dubbo。

rpc=socket + 動態代理

伺服器通訊原理就是一臺 socket 伺服器 A,另一臺 socket 客戶端 B,現在如果要通訊的話直接以流方式寫入或讀出。這樣能實現通訊,但有個問題。如何知道更多資訊?

比如需要傳送流大小,編碼,Ip 等。這樣就有了協議,協議就是規範,就是傳送的流中攜帶了很多的內容。那回到剛剛的問題。傳送的內容就是文字型別,客戶端就得序列化,那麼常用的就有 json,xml 之類,如果想把內容變得更小,那就有二進位制了。把文字變成二進位制傳遞。

說到 rpc 與 http 介面,不要太複雜了。rpc 協議更簡單內容更小,那麼來說效率是要高一點

1.2:RPC 介面如何去壓測

上述已經說了 RPC 跟 http 介面實際是一種方式,所有測試方法上也應該有較大的一致性,我覺得需要驗證下列幾個方面:

1:業務功能的測試;包括正常場景,異常場景
2:邊界分析測試
3:引數組合測試
4:異常情況測試:重複提交、併發測試、事務測試、分散式測試、環境異常
5:安全測試:敏感資訊加密 、前後端資料的一致性驗證
6:效能驗證:在預定的併發使用者情況下是否會出現異常問題

1.3:如何實現外掛/工具的封裝,實現對 RPC 介面的測試

針對 http 介面的壓測我想絕大多數童鞋都會使用 JMeter,但是對於 RPC 的介面,使用現有的 JMeter 的 sample 貌似無法完成這個任務;針對 RPC 的介面我們可以把它當做一個 Java 請求來處理,這樣只用開發一個 Java 請求的外掛即可完成對 RPC 介面的測試了

2:操作篇

2.1:IDEA 開發環境的搭建

1:IDEA 匯入對應的依賴

<dependencies>
    <!-- thrift依賴 -->
    <dependency>
        <groupId>org.apache.thrift</groupId>
        <artifactId>libthrift</artifactId>
        <version>0.13.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.jmeter/jorphan -->
    <dependency>
        <groupId>org.apache.jmeter</groupId>
        <artifactId>jorphan</artifactId>
        <version>5.2.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/oro/oro -->
    <dependency>
        <groupId>oro</groupId>
        <artifactId>oro</artifactId>
        <version>2.0.8</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.7</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.1.0</version>
    </dependency>
</dependencies>

2:將 JMeter ext 目錄下所有的 jar 包前部匯入 IDEA(這裡省略... 百度一大把)

3:至此 你的環境都已經基本搭建 ok 了,如果還有什麼問題 具體問題具體處理吧

2.2:RPC 請求方法的封裝

1:首先需要先生成 QueryService 這個類(具體操作方法可以百度,我下面直接貼出來)

import org.apache.thrift.transport.TTransport;

/**
 * Autogenerated by Thrift Compiler (0.10.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
@SuppressWarnings({"rawtypes", "serial", "unchecked", "unused"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.10.0)", date = "2017-11-18")
public class APIQueryService {

  public interface Iface {

    public String query(String uri, java.util.Map<String, String> params) throws APIQueryFailedOperation, org.apache.thrift.TException;

  }

  public interface AsyncIface {

    public void query(String uri, java.util.Map<String, String> params, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler) throws org.apache.thrift.TException;

  }

  public static class Client extends org.apache.thrift.TServiceClient implements Iface {
    public static class Factory implements org.apache.thrift.TServiceClientFactory<Client> {
      public Factory() {}
      public Client getClient(org.apache.thrift.protocol.TProtocol prot) {
        return new Client(prot);
      }
      public Client getClient(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
        return new Client(iprot, oprot);
      }
    }

    private TTransport transport;
    public TTransport getTransport() {
        return transport;
    }

    public void setTransport(TTransport transport) {
        this.transport = transport;
    }

    public void destory() {
        this.transport.close();
    }

    public Client(org.apache.thrift.protocol.TProtocol prot)
    {
      super(prot, prot);
    }

    public Client(org.apache.thrift.protocol.TProtocol iprot, org.apache.thrift.protocol.TProtocol oprot) {
      super(iprot, oprot);
    }
    /**
     * rpc查詢
     * */
    public String query(String uri, java.util.Map<String, String> params) throws APIQueryFailedOperation, org.apache.thrift.TException
    {
      send_query(uri, params);
      return recv_query();
    }

    public void send_query(String uri, java.util.Map<String, String> params) throws org.apache.thrift.TException
    {

      query_args args = new query_args();
      args.setUri(uri);
      args.setParams(params);
      sendBase("query", args);
    }

    public String recv_query() throws APIQueryFailedOperation, org.apache.thrift.TException
    {
      query_result result = new query_result();
      receiveBase(result, "query");

      if (result.isSetSuccess()) {
        return result.success;
      }
      if (result.ouch != null) {
        throw result.ouch;
      }
      throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "query failed: unknown result");
    }

  }
  public static class AsyncClient extends org.apache.thrift.async.TAsyncClient implements AsyncIface {
    public static class Factory implements org.apache.thrift.async.TAsyncClientFactory<AsyncClient> {
      private org.apache.thrift.async.TAsyncClientManager clientManager;
      private org.apache.thrift.protocol.TProtocolFactory protocolFactory;
      public Factory(org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.protocol.TProtocolFactory protocolFactory) {
        this.clientManager = clientManager;
        this.protocolFactory = protocolFactory;
      }
      public AsyncClient getAsyncClient(org.apache.thrift.transport.TNonblockingTransport transport) {
        return new AsyncClient(protocolFactory, clientManager, transport);
      }
    }

    public AsyncClient(org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.async.TAsyncClientManager clientManager, org.apache.thrift.transport.TNonblockingTransport transport) {
      super(protocolFactory, clientManager, transport);
    }

    public void query(String uri, java.util.Map<String, String> params, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler) throws org.apache.thrift.TException {
      checkReady();
      query_call method_call = new query_call(uri, params, resultHandler, this, ___protocolFactory, ___transport);
      this.___currentMethod = method_call;
      ___manager.call(method_call);
    }

    public static class query_call extends org.apache.thrift.async.TAsyncMethodCall<String> {
      private String uri;
      private java.util.Map<String, String> params;
      public query_call(String uri, java.util.Map<String, String> params, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler, org.apache.thrift.async.TAsyncClient client, org.apache.thrift.protocol.TProtocolFactory protocolFactory, org.apache.thrift.transport.TNonblockingTransport transport) throws org.apache.thrift.TException {
        super(client, protocolFactory, transport, resultHandler, false);
        this.uri = uri;
        this.params = params;
      }

      public void write_args(org.apache.thrift.protocol.TProtocol prot) throws org.apache.thrift.TException {
        prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage("query", org.apache.thrift.protocol.TMessageType.CALL, 0));
        query_args args = new query_args();
        args.setUri(uri);
        args.setParams(params);
        args.write(prot);
        prot.writeMessageEnd();
      }

      public String getResult() throws APIQueryFailedOperation, org.apache.thrift.TException {
        if (getState() != State.RESPONSE_READ) {
          throw new IllegalStateException("Method call not finished!");
        }
        org.apache.thrift.transport.TMemoryInputTransport memoryTransport = new org.apache.thrift.transport.TMemoryInputTransport(getFrameBuffer().array());
        org.apache.thrift.protocol.TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport);
        return (new Client(prot)).recv_query();
      }
    }

  }

  public static class Processor<I extends Iface> extends org.apache.thrift.TBaseProcessor<I> implements org.apache.thrift.TProcessor {

    public Processor(I iface) {
      super(iface, getProcessMap(new java.util.HashMap<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>>()));
    }

    protected Processor(I iface, java.util.Map<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {
      super(iface, getProcessMap(processMap));
    }

    private static <I extends Iface> java.util.Map<String,  org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>> getProcessMap(java.util.Map<String, org.apache.thrift.ProcessFunction<I, ? extends  org.apache.thrift.TBase>> processMap) {
      processMap.put("query", new query());
      return processMap;
    }

    public static class query<I extends Iface> extends org.apache.thrift.ProcessFunction<I, query_args> {
      public query() {
        super("query");
      }

      public query_args getEmptyArgsInstance() {
        return new query_args();
      }

      protected boolean isOneway() {
        return false;
      }

      public query_result getResult(I iface, query_args args) throws org.apache.thrift.TException {
        query_result result = new query_result();
        try {
          result.success = iface.query(args.uri, args.params);
        } catch (APIQueryFailedOperation ouch) {
          result.ouch = ouch;
        }
        return result;
      }
    }

  }

  public static class AsyncProcessor<I extends AsyncIface> extends org.apache.thrift.TBaseAsyncProcessor<I> {

    public AsyncProcessor(I iface) {
      super(iface, getProcessMap(new java.util.HashMap<String, org.apache.thrift.AsyncProcessFunction<I, ? extends org.apache.thrift.TBase, ?>>()));
    }

    protected AsyncProcessor(I iface, java.util.Map<String,  org.apache.thrift.AsyncProcessFunction<I, ? extends  org.apache.thrift.TBase, ?>> processMap) {
      super(iface, getProcessMap(processMap));
    }

    private static <I extends AsyncIface> java.util.Map<String,  org.apache.thrift.AsyncProcessFunction<I, ? extends  org.apache.thrift.TBase,?>> getProcessMap(java.util.Map<String,  org.apache.thrift.AsyncProcessFunction<I, ? extends  org.apache.thrift.TBase, ?>> processMap) {
      processMap.put("query", new query());
      return processMap;
    }

    public static class query<I extends AsyncIface> extends org.apache.thrift.AsyncProcessFunction<I, query_args, String> {
      public query() {
        super("query");
      }

      public query_args getEmptyArgsInstance() {
        return new query_args();
      }

      public org.apache.thrift.async.AsyncMethodCallback<String> getResultHandler(final org.apache.thrift.server.AbstractNonblockingServer.AsyncFrameBuffer fb, final int seqid) {
        final org.apache.thrift.AsyncProcessFunction fcall = this;
        return new org.apache.thrift.async.AsyncMethodCallback<String>() {
          public void onComplete(String o) {
            query_result result = new query_result();
            result.success = o;
            try {
              fcall.sendResponse(fb, result, org.apache.thrift.protocol.TMessageType.REPLY,seqid);
            } catch (org.apache.thrift.transport.TTransportException e) {

              fb.close();
            } catch (Exception e) {

              onError(e);
            }
          }
          public void onError(Exception e) {
            byte msgType = org.apache.thrift.protocol.TMessageType.REPLY;
            org.apache.thrift.TSerializable msg;
            query_result result = new query_result();
            if (e instanceof APIQueryFailedOperation) {
              result.ouch = (APIQueryFailedOperation) e;
              result.setOuchIsSet(true);
              msg = result;
            } else if (e instanceof org.apache.thrift.transport.TTransportException) {

              fb.close();
              return;
            } else if (e instanceof org.apache.thrift.TApplicationException) {
//              _LOGGER.error("TApplicationException inside handler", e);
              msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION;
              msg = (org.apache.thrift.TApplicationException)e;
            } else {
//              _LOGGER.error("Exception inside handler", e);
              msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION;
              msg = new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.INTERNAL_ERROR, e.getMessage());
            }
            try {
              fcall.sendResponse(fb,msg,msgType,seqid);
            } catch (Exception ex) {
//              _LOGGER.error("Exception writing to internal frame buffer", ex);
              fb.close();
            }
          }
        };
      }

      protected boolean isOneway() {
        return false;
      }

      public void start(I iface, query_args args, org.apache.thrift.async.AsyncMethodCallback<String> resultHandler) throws org.apache.thrift.TException {
        iface.query(args.uri, args.params,resultHandler);
      }
    }

  }

  public static class query_args implements org.apache.thrift.TBase<query_args, query_args._Fields>, java.io.Serializable, Cloneable, Comparable<query_args>   {
    private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("query_args");

    private static final org.apache.thrift.protocol.TField URI_FIELD_DESC = new org.apache.thrift.protocol.TField("uri", org.apache.thrift.protocol.TType.STRING, (short)1);
    private static final org.apache.thrift.protocol.TField PARAMS_FIELD_DESC = new org.apache.thrift.protocol.TField("params", org.apache.thrift.protocol.TType.MAP, (short)2);

    private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new query_argsStandardSchemeFactory();
    private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new query_argsTupleSchemeFactory();

    public String uri; // required
    public java.util.Map<String, String> params; // required

    /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
    public enum _Fields implements org.apache.thrift.TFieldIdEnum {
      URI((short)1, "uri"),
      PARAMS((short)2, "params");

      private static final java.util.Map<String, _Fields> byName = new java.util.HashMap<String, _Fields>();

      static {
        for (_Fields field : java.util.EnumSet.allOf(_Fields.class)) {
          byName.put(field.getFieldName(), field);
        }
      }

      /**
       * Find the _Fields constant that matches fieldId, or null if its not found.
       */
      public static _Fields findByThriftId(int fieldId) {
        switch(fieldId) {
          case 1: // URI
            return URI;
          case 2: // PARAMS
            return PARAMS;
          default:
            return null;
        }
      }

      /**
       * Find the _Fields constant that matches fieldId, throwing an exception
       * if it is not found.
       */
      public static _Fields findByThriftIdOrThrow(int fieldId) {
        _Fields fields = findByThriftId(fieldId);
        if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!");
        return fields;
      }

      /**
       * Find the _Fields constant that matches name, or null if its not found.
       */
      public static _Fields findByName(String name) {
        return byName.get(name);
      }

      private final short _thriftId;
      private final String _fieldName;

      _Fields(short thriftId, String fieldName) {
        _thriftId = thriftId;
        _fieldName = fieldName;
      }

      public short getThriftFieldId() {
        return _thriftId;
      }

      public String getFieldName() {
        return _fieldName;
      }
    }

    // isset id assignments
    public static final java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;
    static {
      java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new java.util.EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);
      tmpMap.put(_Fields.URI, new org.apache.thrift.meta_data.FieldMetaData("uri", org.apache.thrift.TFieldRequirementType.DEFAULT, 
          new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));
      tmpMap.put(_Fields.PARAMS, new org.apache.thrift.meta_data.FieldMetaData("params", org.apache.thrift.TFieldRequirementType.DEFAULT, 
          new org.apache.thrift.meta_data.MapMetaData(org.apache.thrift.protocol.TType.MAP, 
              new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING), 
              new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING))));
      metaDataMap = java.util.Collections.unmodifiableMap(tmpMap);
      org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(query_args.class, metaDataMap);
    }

    public query_args() {
    }

    public query_args(
      String uri,
      java.util.Map<String, String> params)
    {
      this();
      this.uri = uri;
      this.params = params;
    }

    /**
     * Performs a deep copy on <i>other</i>.
     */
    public query_args(query_args other) {
      if (other.isSetUri()) {
        this.uri = other.uri;
      }
      if (other.isSetParams()) {
        java.util.Map<String, String> __this__params = new java.util.HashMap<String, String>(other.params);
        this.params = __this__params;
      }
    }

    public query_args deepCopy() {
      return new query_args(this);
    }

    @Override
    public void clear() {
      this.uri = null;
      this.params = null;
    }

    public String getUri() {
      return this.uri;
    }

    public query_args setUri(String uri) {
      this.uri = uri;
      return this;
    }

    public void unsetUri() {
      this.uri = null;
    }

    /** Returns true if field uri is set (has been assigned a value) and false otherwise */
    public boolean isSetUri() {
      return this.uri != null;
    }

    public void setUriIsSet(boolean value) {
      if (!value) {
        this.uri = null;
      }
    }

    public int getParamsSize() {
      return (this.params == null) ? 0 : this.params.size();
    }

    public void putToParams(String key, String val) {
      if (this.params == null) {
        this.params = new java.util.HashMap<String, String>();
      }
      this.params.put(key, val);
    }

    public java.util.Map<String, String> getParams() {
      return this.params;
    }

    public query_args setParams(java.util.Map<String, String> params) {
      this.params = params;
      return this;
    }

    public void unsetParams() {
      this.params = null;
    }

    /** Returns true if field params is set (has been assigned a value) and false otherwise */
    public boolean isSetParams() {
      return this.params != null;
    }

    public void setParamsIsSet(boolean value) {
      if (!value) {
        this.params = null;
      }
    }

    public void setFieldValue(_Fields field, Object value) {
      switch (field) {
      case URI:
        if (value == null) {
          unsetUri();
        } else {
          setUri((String)value);
        }
        break;

      case PARAMS:
        if (value == null) {
          unsetParams();
        } else {
          setParams((java.util.Map<String, String>)value);
        }
        break;

      }
    }

    public Object getFieldValue(_Fields field) {
      switch (field) {
      case URI:
        return getUri();

      case PARAMS:
        return getParams();

      }
      throw new IllegalStateException();
    }

    /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */
    public boolean isSet(_Fields field) {
      if (field == null) {
        throw new IllegalArgumentException();
      }

      switch (field) {
      case URI:
        return isSetUri();
      case PARAMS:
        return isSetParams();
      }
      throw new IllegalStateException();
    }

    @Override
    public boolean equals(Object that) {
      if (that == null)
        return false;
      if (that instanceof query_args)
        return this.equals((query_args)that);
      return false;
    }

    public boolean equals(query_args that) {
      if (that == null)
        return false;
      if (this == that)
        return true;

      boolean this_present_uri = true && this.isSetUri();
      boolean that_present_uri = true && that.isSetUri();
      if (this_present_uri || that_present_uri) {
        if (!(this_present_uri && that_present_uri))
          return false;
        if (!this.uri.equals(that.uri))
          return false;
      }

      boolean this_present_params = true && this.isSetParams();
      boolean that_present_params = true && that.isSetParams();
      if (this_present_params || that_present_params) {
        if (!(this_present_params && that_present_params))
          return false;
        if (!this.params.equals(that.params))
          return false;
      }

      return true;
    }

    @Override
    public int hashCode() {
      int hashCode = 1;

      hashCode = hashCode * 8191 + ((isSetUri()) ? 131071 : 524287);
      if (isSetUri())
        hashCode = hashCode * 8191 + uri.hashCode();

      hashCode = hashCode * 8191 + ((isSetParams()) ? 131071 : 524287);
      if (isSetParams())
        hashCode = hashCode * 8191 + params.hashCode();

      return hashCode;
    }

    @Override
    public int compareTo(query_args other) {
      if (!getClass().equals(other.getClass())) {
        return getClass().getName().compareTo(other.getClass().getName());
      }

      int lastComparison = 0;

      lastComparison = Boolean.valueOf(isSetUri()).compareTo(other.isSetUri());
      if (lastComparison != 0) {
        return lastComparison;
      }
      if (isSetUri()) {
        lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.uri, other.uri);
        if (lastComparison != 0) {
          return lastComparison;
        }
      }
      lastComparison = Boolean.valueOf(isSetParams()).compareTo(other.isSetParams());
      if (lastComparison != 0) {
        return lastComparison;
      }
      if (isSetParams()) {
        lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.params, other.params);
        if (lastComparison != 0) {
          return lastComparison;
        }
      }
      return 0;
    }

    public _Fields fieldForId(int fieldId) {
      return _Fields.findByThriftId(fieldId);
    }

    public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {
      scheme(iprot).read(iprot, this);
    }

    public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
      scheme(oprot).write(oprot, this);
    }

    @Override
    public String toString() {
      StringBuilder sb = new StringBuilder("query_args(");
      boolean first = true;

      sb.append("uri:");
      if (this.uri == null) {
        sb.append("null");
      } else {
        sb.append(this.uri);
      }
      first = false;
      if (!first) sb.append(", ");
      sb.append("params:");
      if (this.params == null) {
        sb.append("null");
      } else {
        sb.append(this.params);
      }
      first = false;
      sb.append(")");
      return sb.toString();
    }

    public void validate() throws org.apache.thrift.TException {
      // check for required fields
      // check for sub-struct validity
    }

    private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
      try {
        write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));
      } catch (org.apache.thrift.TException te) {
        throw new java.io.IOException(te);
      }
    }

    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
      try {
        read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));
      } catch (org.apache.thrift.TException te) {
        throw new java.io.IOException(te);
      }
    }

    private static class query_argsStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
      public query_argsStandardScheme getScheme() {
        return new query_argsStandardScheme();
      }
    }

    private static class query_argsStandardScheme extends org.apache.thrift.scheme.StandardScheme<query_args> {

      public void read(org.apache.thrift.protocol.TProtocol iprot, query_args struct) throws org.apache.thrift.TException {
        org.apache.thrift.protocol.TField schemeField;
        iprot.readStructBegin();
        while (true)
        {
          schemeField = iprot.readFieldBegin();
          if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { 
            break;
          }
          switch (schemeField.id) {
            case 1: // URI
              if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
                struct.uri = iprot.readString();
                struct.setUriIsSet(true);
              } else { 
                org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
              }
              break;
            case 2: // PARAMS
              if (schemeField.type == org.apache.thrift.protocol.TType.MAP) {
                {
                  org.apache.thrift.protocol.TMap _map0 = iprot.readMapBegin();
                  struct.params = new java.util.HashMap<String, String>(2*_map0.size);
                  String _key1;
                  String _val2;
                  for (int _i3 = 0; _i3 < _map0.size; ++_i3)
                  {
                    _key1 = iprot.readString();
                    _val2 = iprot.readString();
                    struct.params.put(_key1, _val2);
                  }
                  iprot.readMapEnd();
                }
                struct.setParamsIsSet(true);
              } else { 
                org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
              }
              break;
            default:
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
          }
          iprot.readFieldEnd();
        }
        iprot.readStructEnd();

        // check for required fields of primitive type, which can't be checked in the validate method
        struct.validate();
      }

      public void write(org.apache.thrift.protocol.TProtocol oprot, query_args struct) throws org.apache.thrift.TException {
        struct.validate();

        oprot.writeStructBegin(STRUCT_DESC);
        if (struct.uri != null) {
          oprot.writeFieldBegin(URI_FIELD_DESC);
          oprot.writeString(struct.uri);
          oprot.writeFieldEnd();
        }
        if (struct.params != null) {
          oprot.writeFieldBegin(PARAMS_FIELD_DESC);
          {
            oprot.writeMapBegin(new org.apache.thrift.protocol.TMap(org.apache.thrift.protocol.TType.STRING, org.apache.thrift.protocol.TType.STRING, struct.params.size()));
            for (java.util.Map.Entry<String, String> _iter4 : struct.params.entrySet())
            {
              oprot.writeString(_iter4.getKey());
              oprot.writeString(_iter4.getValue());
            }
            oprot.writeMapEnd();
          }
          oprot.writeFieldEnd();
        }
        oprot.writeFieldStop();
        oprot.writeStructEnd();
      }

    }

    private static class query_argsTupleSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
      public query_argsTupleScheme getScheme() {
        return new query_argsTupleScheme();
      }
    }

    private static class query_argsTupleScheme extends org.apache.thrift.scheme.TupleScheme<query_args> {

      @Override
      public void write(org.apache.thrift.protocol.TProtocol prot, query_args struct) throws org.apache.thrift.TException {
        org.apache.thrift.protocol.TTupleProtocol oprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
        java.util.BitSet optionals = new java.util.BitSet();
        if (struct.isSetUri()) {
          optionals.set(0);
        }
        if (struct.isSetParams()) {
          optionals.set(1);
        }
        oprot.writeBitSet(optionals, 2);
        if (struct.isSetUri()) {
          oprot.writeString(struct.uri);
        }
        if (struct.isSetParams()) {
          {
            oprot.writeI32(struct.params.size());
            for (java.util.Map.Entry<String, String> _iter5 : struct.params.entrySet())
            {
              oprot.writeString(_iter5.getKey());
              oprot.writeString(_iter5.getValue());
            }
          }
        }
      }

      @Override
      public void read(org.apache.thrift.protocol.TProtocol prot, query_args struct) throws org.apache.thrift.TException {
        org.apache.thrift.protocol.TTupleProtocol iprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
        java.util.BitSet incoming = iprot.readBitSet(2);
        if (incoming.get(0)) {
          struct.uri = iprot.readString();
          struct.setUriIsSet(true);
        }
        if (incoming.get(1)) {
          {
            org.apache.thrift.protocol.TMap _map6 = new org.apache.thrift.protocol.TMap(org.apache.thrift.protocol.TType.STRING, org.apache.thrift.protocol.TType.STRING, iprot.readI32());
            struct.params = new java.util.HashMap<String, String>(2*_map6.size);
            String _key7;
            String _val8;
            for (int _i9 = 0; _i9 < _map6.size; ++_i9)
            {
              _key7 = iprot.readString();
              _val8 = iprot.readString();
              struct.params.put(_key7, _val8);
            }
          }
          struct.setParamsIsSet(true);
        }
      }
    }

    private static <S extends org.apache.thrift.scheme.IScheme> S scheme(org.apache.thrift.protocol.TProtocol proto) {
      return (org.apache.thrift.scheme.StandardScheme.class.equals(proto.getScheme()) ? STANDARD_SCHEME_FACTORY : TUPLE_SCHEME_FACTORY).getScheme();
    }
  }

  public static class query_result implements org.apache.thrift.TBase<query_result, query_result._Fields>, java.io.Serializable, Cloneable, Comparable<query_result>   {
    private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("query_result");

    private static final org.apache.thrift.protocol.TField SUCCESS_FIELD_DESC = new org.apache.thrift.protocol.TField("success", org.apache.thrift.protocol.TType.STRING, (short)0);
    private static final org.apache.thrift.protocol.TField OUCH_FIELD_DESC = new org.apache.thrift.protocol.TField("ouch", org.apache.thrift.protocol.TType.STRUCT, (short)1);

    private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new query_resultStandardSchemeFactory();
    private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new query_resultTupleSchemeFactory();

    public String success; // required
    public APIQueryFailedOperation ouch; // required

    /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
    public enum _Fields implements org.apache.thrift.TFieldIdEnum {
      SUCCESS((short)0, "success"),
      OUCH((short)1, "ouch");

      private static final java.util.Map<String, _Fields> byName = new java.util.HashMap<String, _Fields>();

      static {
        for (_Fields field : java.util.EnumSet.allOf(_Fields.class)) {
          byName.put(field.getFieldName(), field);
        }
      }

      /**
       * Find the _Fields constant that matches fieldId, or null if its not found.
       */
      public static _Fields findByThriftId(int fieldId) {
        switch(fieldId) {
          case 0: // SUCCESS
            return SUCCESS;
          case 1: // OUCH
            return OUCH;
          default:
            return null;
        }
      }

      /**
       * Find the _Fields constant that matches fieldId, throwing an exception
       * if it is not found.
       */
      public static _Fields findByThriftIdOrThrow(int fieldId) {
        _Fields fields = findByThriftId(fieldId);
        if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!");
        return fields;
      }

      /**
       * Find the _Fields constant that matches name, or null if its not found.
       */
      public static _Fields findByName(String name) {
        return byName.get(name);
      }

      private final short _thriftId;
      private final String _fieldName;

      _Fields(short thriftId, String fieldName) {
        _thriftId = thriftId;
        _fieldName = fieldName;
      }

      public short getThriftFieldId() {
        return _thriftId;
      }

      public String getFieldName() {
        return _fieldName;
      }
    }

    // isset id assignments
    public static final java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;
    static {
      java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new java.util.EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);
      tmpMap.put(_Fields.SUCCESS, new org.apache.thrift.meta_data.FieldMetaData("success", org.apache.thrift.TFieldRequirementType.DEFAULT, 
          new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));
      tmpMap.put(_Fields.OUCH, new org.apache.thrift.meta_data.FieldMetaData("ouch", org.apache.thrift.TFieldRequirementType.DEFAULT, 
          new org.apache.thrift.meta_data.StructMetaData(org.apache.thrift.protocol.TType.STRUCT, APIQueryFailedOperation.class)));
      metaDataMap = java.util.Collections.unmodifiableMap(tmpMap);
      org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(query_result.class, metaDataMap);
    }

    public query_result() {
    }

    public query_result(
      String success,
      APIQueryFailedOperation ouch)
    {
      this();
      this.success = success;
      this.ouch = ouch;
    }

    /**
     * Performs a deep copy on <i>other</i>.
     */
    public query_result(query_result other) {
      if (other.isSetSuccess()) {
        this.success = other.success;
      }
      if (other.isSetOuch()) {
        this.ouch = new APIQueryFailedOperation(other.ouch);
      }
    }

    public query_result deepCopy() {
      return new query_result(this);
    }

    @Override
    public void clear() {
      this.success = null;
      this.ouch = null;
    }

    public String getSuccess() {
      return this.success;
    }

    public query_result setSuccess(String success) {
      this.success = success;
      return this;
    }

    public void unsetSuccess() {
      this.success = null;
    }

    /** Returns true if field success is set (has been assigned a value) and false otherwise */
    public boolean isSetSuccess() {
      return this.success != null;
    }

    public void setSuccessIsSet(boolean value) {
      if (!value) {
        this.success = null;
      }
    }

    public APIQueryFailedOperation getOuch() {
      return this.ouch;
    }

    public query_result setOuch(APIQueryFailedOperation ouch) {
      this.ouch = ouch;
      return this;
    }

    public void unsetOuch() {
      this.ouch = null;
    }

    /** Returns true if field ouch is set (has been assigned a value) and false otherwise */
    public boolean isSetOuch() {
      return this.ouch != null;
    }

    public void setOuchIsSet(boolean value) {
      if (!value) {
        this.ouch = null;
      }
    }

    public void setFieldValue(_Fields field, Object value) {
      switch (field) {
      case SUCCESS:
        if (value == null) {
          unsetSuccess();
        } else {
          setSuccess((String)value);
        }
        break;

      case OUCH:
        if (value == null) {
          unsetOuch();
        } else {
          setOuch((APIQueryFailedOperation)value);
        }
        break;

      }
    }

    public Object getFieldValue(_Fields field) {
      switch (field) {
      case SUCCESS:
        return getSuccess();

      case OUCH:
        return getOuch();

      }
      throw new IllegalStateException();
    }

    /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */
    public boolean isSet(_Fields field) {
      if (field == null) {
        throw new IllegalArgumentException();
      }

      switch (field) {
      case SUCCESS:
        return isSetSuccess();
      case OUCH:
        return isSetOuch();
      }
      throw new IllegalStateException();
    }

    @Override
    public boolean equals(Object that) {
      if (that == null)
        return false;
      if (that instanceof query_result)
        return this.equals((query_result)that);
      return false;
    }

    public boolean equals(query_result that) {
      if (that == null)
        return false;
      if (this == that)
        return true;

      boolean this_present_success = true && this.isSetSuccess();
      boolean that_present_success = true && that.isSetSuccess();
      if (this_present_success || that_present_success) {
        if (!(this_present_success && that_present_success))
          return false;
        if (!this.success.equals(that.success))
          return false;
      }

      boolean this_present_ouch = true && this.isSetOuch();
      boolean that_present_ouch = true && that.isSetOuch();
      if (this_present_ouch || that_present_ouch) {
        if (!(this_present_ouch && that_present_ouch))
          return false;
        if (!this.ouch.equals(that.ouch))
          return false;
      }

      return true;
    }

    @Override
    public int hashCode() {
      int hashCode = 1;

      hashCode = hashCode * 8191 + ((isSetSuccess()) ? 131071 : 524287);
      if (isSetSuccess())
        hashCode = hashCode * 8191 + success.hashCode();

      hashCode = hashCode * 8191 + ((isSetOuch()) ? 131071 : 524287);
      if (isSetOuch())
        hashCode = hashCode * 8191 + ouch.hashCode();

      return hashCode;
    }

    @Override
    public int compareTo(query_result other) {
      if (!getClass().equals(other.getClass())) {
        return getClass().getName().compareTo(other.getClass().getName());
      }

      int lastComparison = 0;

      lastComparison = Boolean.valueOf(isSetSuccess()).compareTo(other.isSetSuccess());
      if (lastComparison != 0) {
        return lastComparison;
      }
      if (isSetSuccess()) {
        lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.success, other.success);
        if (lastComparison != 0) {
          return lastComparison;
        }
      }
      lastComparison = Boolean.valueOf(isSetOuch()).compareTo(other.isSetOuch());
      if (lastComparison != 0) {
        return lastComparison;
      }
      if (isSetOuch()) {
        lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.ouch, other.ouch);
        if (lastComparison != 0) {
          return lastComparison;
        }
      }
      return 0;
    }

    public _Fields fieldForId(int fieldId) {
      return _Fields.findByThriftId(fieldId);
    }

    public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {
      scheme(iprot).read(iprot, this);
    }

    public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
      scheme(oprot).write(oprot, this);
      }

    @Override
    public String toString() {
      StringBuilder sb = new StringBuilder("query_result(");
      boolean first = true;

      sb.append("success:");
      if (this.success == null) {
        sb.append("null");
      } else {
        sb.append(this.success);
      }
      first = false;
      if (!first) sb.append(", ");
      sb.append("ouch:");
      if (this.ouch == null) {
        sb.append("null");
      } else {
        sb.append(this.ouch);
      }
      first = false;
      sb.append(")");
      return sb.toString();
    }

    public void validate() throws org.apache.thrift.TException {
      // check for required fields
      // check for sub-struct validity
    }

    private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
      try {
        write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));
      } catch (org.apache.thrift.TException te) {
        throw new java.io.IOException(te);
      }
    }

    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
      try {
        read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));
      } catch (org.apache.thrift.TException te) {
        throw new java.io.IOException(te);
      }
    }

    private static class query_resultStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
      public query_resultStandardScheme getScheme() {
        return new query_resultStandardScheme();
      }
    }

    private static class query_resultStandardScheme extends org.apache.thrift.scheme.StandardScheme<query_result> {

      public void read(org.apache.thrift.protocol.TProtocol iprot, query_result struct) throws org.apache.thrift.TException {
        org.apache.thrift.protocol.TField schemeField;
        iprot.readStructBegin();
        while (true)
        {
          schemeField = iprot.readFieldBegin();
          if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { 
            break;
          }
          switch (schemeField.id) {
            case 0: // SUCCESS
              if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
                struct.success = iprot.readString();
                struct.setSuccessIsSet(true);
              } else { 
                org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
              }
              break;
            case 1: // OUCH
              if (schemeField.type == org.apache.thrift.protocol.TType.STRUCT) {
                struct.ouch = new APIQueryFailedOperation();
                struct.ouch.read(iprot);
                struct.setOuchIsSet(true);
              } else { 
                org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
              }
              break;
            default:
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
          }
          iprot.readFieldEnd();
        }
        iprot.readStructEnd();

        // check for required fields of primitive type, which can't be checked in the validate method
        struct.validate();
      }

      public void write(org.apache.thrift.protocol.TProtocol oprot, query_result struct) throws org.apache.thrift.TException {
        struct.validate();

        oprot.writeStructBegin(STRUCT_DESC);
        if (struct.success != null) {
          oprot.writeFieldBegin(SUCCESS_FIELD_DESC);
          oprot.writeString(struct.success);
          oprot.writeFieldEnd();
        }
        if (struct.ouch != null) {
          oprot.writeFieldBegin(OUCH_FIELD_DESC);
          struct.ouch.write(oprot);
          oprot.writeFieldEnd();
        }
        oprot.writeFieldStop();
        oprot.writeStructEnd();
      }

    }

    private static class query_resultTupleSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
      public query_resultTupleScheme getScheme() {
        return new query_resultTupleScheme();
      }
    }

    private static class query_resultTupleScheme extends org.apache.thrift.scheme.TupleScheme<query_result> {

      @Override
      public void write(org.apache.thrift.protocol.TProtocol prot, query_result struct) throws org.apache.thrift.TException {
        org.apache.thrift.protocol.TTupleProtocol oprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
        java.util.BitSet optionals = new java.util.BitSet();
        if (struct.isSetSuccess()) {
          optionals.set(0);
        }
        if (struct.isSetOuch()) {
          optionals.set(1);
        }
        oprot.writeBitSet(optionals, 2);
        if (struct.isSetSuccess()) {
          oprot.writeString(struct.success);
        }
        if (struct.isSetOuch()) {
          struct.ouch.write(oprot);
        }
      }

      @Override
      public void read(org.apache.thrift.protocol.TProtocol prot, query_result struct) throws org.apache.thrift.TException {
        org.apache.thrift.protocol.TTupleProtocol iprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
        java.util.BitSet incoming = iprot.readBitSet(2);
        if (incoming.get(0)) {
          struct.success = iprot.readString();
          struct.setSuccessIsSet(true);
        }
        if (incoming.get(1)) {
          struct.ouch = new APIQueryFailedOperation();
          struct.ouch.read(iprot);
          struct.setOuchIsSet(true);
        }
      }
    }

    private static <S extends org.apache.thrift.scheme.IScheme> S scheme(org.apache.thrift.protocol.TProtocol proto) {
      return (org.apache.thrift.scheme.StandardScheme.class.equals(proto.getScheme()) ? STANDARD_SCHEME_FACTORY : TUPLE_SCHEME_FACTORY).getScheme();
    }
  }

}

下面這個類是捕獲全域性異常的

/**
 * Autogenerated by Thrift Compiler (0.10.0)
 *
 * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
 *  @generated
 */
@SuppressWarnings({"rawtypes", "serial", "unchecked"})
@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.10.0)", date = "2017-11-18")
public class APIQueryFailedOperation extends org.apache.thrift.TException implements org.apache.thrift.TBase<APIQueryFailedOperation, APIQueryFailedOperation._Fields>, java.io.Serializable, Cloneable, Comparable<APIQueryFailedOperation> {
  private static final org.apache.thrift.protocol.TStruct STRUCT_DESC = new org.apache.thrift.protocol.TStruct("APIQueryFailedOperation");

  private static final org.apache.thrift.protocol.TField OK_FIELD_DESC = new org.apache.thrift.protocol.TField("ok", org.apache.thrift.protocol.TType.I32, (short)1);
  private static final org.apache.thrift.protocol.TField MESSAGE_FIELD_DESC = new org.apache.thrift.protocol.TField("message", org.apache.thrift.protocol.TType.STRING, (short)2);

  private static final org.apache.thrift.scheme.SchemeFactory STANDARD_SCHEME_FACTORY = new APIQueryFailedOperationStandardSchemeFactory();
  private static final org.apache.thrift.scheme.SchemeFactory TUPLE_SCHEME_FACTORY = new APIQueryFailedOperationTupleSchemeFactory();

  public int ok; // required
  public String message; // required

  /** The set of fields this struct contains, along with convenience methods for finding and manipulating them. */
  public enum _Fields implements org.apache.thrift.TFieldIdEnum {
    OK((short)1, "ok"),
    MESSAGE((short)2, "message");

    private static final java.util.Map<String, _Fields> byName = new java.util.HashMap<String, _Fields>();

    static {
      for (_Fields field : java.util.EnumSet.allOf(_Fields.class)) {
        byName.put(field.getFieldName(), field);
      }
    }

    /**
     * Find the _Fields constant that matches fieldId, or null if its not found.
     */
    public static _Fields findByThriftId(int fieldId) {
      switch(fieldId) {
        case 1: // OK
          return OK;
        case 2: // MESSAGE
          return MESSAGE;
        default:
          return null;
      }
    }

    /**
     * Find the _Fields constant that matches fieldId, throwing an exception
     * if it is not found.
     */
    public static _Fields findByThriftIdOrThrow(int fieldId) {
      _Fields fields = findByThriftId(fieldId);
      if (fields == null) throw new IllegalArgumentException("Field " + fieldId + " doesn't exist!");
      return fields;
    }

    /**
     * Find the _Fields constant that matches name, or null if its not found.
     */
    public static _Fields findByName(String name) {
      return byName.get(name);
    }

    private final short _thriftId;
    private final String _fieldName;

    _Fields(short thriftId, String fieldName) {
      _thriftId = thriftId;
      _fieldName = fieldName;
    }

    public short getThriftFieldId() {
      return _thriftId;
    }

    public String getFieldName() {
      return _fieldName;
    }
  }

  // isset id assignments
  private static final int __OK_ISSET_ID = 0;
  private byte __isset_bitfield = 0;
  public static final java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> metaDataMap;
  static {
    java.util.Map<_Fields, org.apache.thrift.meta_data.FieldMetaData> tmpMap = new java.util.EnumMap<_Fields, org.apache.thrift.meta_data.FieldMetaData>(_Fields.class);
    tmpMap.put(_Fields.OK, new org.apache.thrift.meta_data.FieldMetaData("ok", org.apache.thrift.TFieldRequirementType.DEFAULT, 
        new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I32)));
    tmpMap.put(_Fields.MESSAGE, new org.apache.thrift.meta_data.FieldMetaData("message", org.apache.thrift.TFieldRequirementType.DEFAULT, 
        new org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));
    metaDataMap = java.util.Collections.unmodifiableMap(tmpMap);
    org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(APIQueryFailedOperation.class, metaDataMap);
  }

  public APIQueryFailedOperation() {
  }

  public APIQueryFailedOperation(
    int ok,
    String message)
  {
    this();
    this.ok = ok;
    setOkIsSet(true);
    this.message = message;
  }

  /**
   * Performs a deep copy on <i>other</i>.
   */
  public APIQueryFailedOperation(APIQueryFailedOperation other) {
    __isset_bitfield = other.__isset_bitfield;
    this.ok = other.ok;
    if (other.isSetMessage()) {
      this.message = other.message;
    }
  }

  public APIQueryFailedOperation deepCopy() {
    return new APIQueryFailedOperation(this);
  }

  @Override
  public void clear() {
    setOkIsSet(false);
    this.ok = 0;
    this.message = null;
  }

  public int getOk() {
    return this.ok;
  }

  public APIQueryFailedOperation setOk(int ok) {
    this.ok = ok;
    setOkIsSet(true);
    return this;
  }

  public void unsetOk() {
    __isset_bitfield = org.apache.thrift.EncodingUtils.clearBit(__isset_bitfield, __OK_ISSET_ID);
  }

  /** Returns true if field ok is set (has been assigned a value) and false otherwise */
  public boolean isSetOk() {
    return org.apache.thrift.EncodingUtils.testBit(__isset_bitfield, __OK_ISSET_ID);
  }

  public void setOkIsSet(boolean value) {
    __isset_bitfield = org.apache.thrift.EncodingUtils.setBit(__isset_bitfield, __OK_ISSET_ID, value);
  }

  public String getMessage() {
    return this.message;
  }

  public APIQueryFailedOperation setMessage(String message) {
    this.message = message;
    return this;
  }

  public void unsetMessage() {
    this.message = null;
  }

  /** Returns true if field message is set (has been assigned a value) and false otherwise */
  public boolean isSetMessage() {
    return this.message != null;
  }

  public void setMessageIsSet(boolean value) {
    if (!value) {
      this.message = null;
    }
  }

  public void setFieldValue(_Fields field, Object value) {
    switch (field) {
    case OK:
      if (value == null) {
        unsetOk();
      } else {
        setOk((Integer)value);
      }
      break;

    case MESSAGE:
      if (value == null) {
        unsetMessage();
      } else {
        setMessage((String)value);
      }
      break;

    }
  }

  public Object getFieldValue(_Fields field) {
    switch (field) {
    case OK:
      return getOk();

    case MESSAGE:
      return getMessage();

    }
    throw new IllegalStateException();
  }

  /** Returns true if field corresponding to fieldID is set (has been assigned a value) and false otherwise */
  public boolean isSet(_Fields field) {
    if (field == null) {
      throw new IllegalArgumentException();
    }

    switch (field) {
    case OK:
      return isSetOk();
    case MESSAGE:
      return isSetMessage();
    }
    throw new IllegalStateException();
  }

  @Override
  public boolean equals(Object that) {
    if (that == null)
      return false;
    if (that instanceof APIQueryFailedOperation)
      return this.equals((APIQueryFailedOperation)that);
    return false;
  }

  public boolean equals(APIQueryFailedOperation that) {
    if (that == null)
      return false;
    if (this == that)
      return true;

    boolean this_present_ok = true;
    boolean that_present_ok = true;
    if (this_present_ok || that_present_ok) {
      if (!(this_present_ok && that_present_ok))
        return false;
      if (this.ok != that.ok)
        return false;
    }

    boolean this_present_message = true && this.isSetMessage();
    boolean that_present_message = true && that.isSetMessage();
    if (this_present_message || that_present_message) {
      if (!(this_present_message && that_present_message))
        return false;
      if (!this.message.equals(that.message))
        return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int hashCode = 1;

    hashCode = hashCode * 8191 + ok;

    hashCode = hashCode * 8191 + ((isSetMessage()) ? 131071 : 524287);
    if (isSetMessage())
      hashCode = hashCode * 8191 + message.hashCode();

    return hashCode;
  }

  @Override
  public int compareTo(APIQueryFailedOperation other) {
    if (!getClass().equals(other.getClass())) {
      return getClass().getName().compareTo(other.getClass().getName());
    }

    int lastComparison = 0;

    lastComparison = Boolean.valueOf(isSetOk()).compareTo(other.isSetOk());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetOk()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.ok, other.ok);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    lastComparison = Boolean.valueOf(isSetMessage()).compareTo(other.isSetMessage());
    if (lastComparison != 0) {
      return lastComparison;
    }
    if (isSetMessage()) {
      lastComparison = org.apache.thrift.TBaseHelper.compareTo(this.message, other.message);
      if (lastComparison != 0) {
        return lastComparison;
      }
    }
    return 0;
  }

  public _Fields fieldForId(int fieldId) {
    return _Fields.findByThriftId(fieldId);
  }

  public void read(org.apache.thrift.protocol.TProtocol iprot) throws org.apache.thrift.TException {
    scheme(iprot).read(iprot, this);
  }

  public void write(org.apache.thrift.protocol.TProtocol oprot) throws org.apache.thrift.TException {
    scheme(oprot).write(oprot, this);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder("APIQueryFailedOperation(");
    boolean first = true;

    sb.append("ok:");
    sb.append(this.ok);
    first = false;
    if (!first) sb.append(", ");
    sb.append("message:");
    if (this.message == null) {
      sb.append("null");
    } else {
      sb.append(this.message);
    }
    first = false;
    sb.append(")");
    return sb.toString();
  }

  public void validate() throws org.apache.thrift.TException {
    // check for required fields
    // check for sub-struct validity
  }

  private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
    try {
      write(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(out)));
    } catch (org.apache.thrift.TException te) {
      throw new java.io.IOException(te);
    }
  }

  private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException {
    try {
      // it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor.
      __isset_bitfield = 0;
      read(new org.apache.thrift.protocol.TCompactProtocol(new org.apache.thrift.transport.TIOStreamTransport(in)));
    } catch (org.apache.thrift.TException te) {
      throw new java.io.IOException(te);
    }
  }

  private static class APIQueryFailedOperationStandardSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
    public APIQueryFailedOperationStandardScheme getScheme() {
      return new APIQueryFailedOperationStandardScheme();
    }
  }

  private static class APIQueryFailedOperationStandardScheme extends org.apache.thrift.scheme.StandardScheme<APIQueryFailedOperation> {

    public void read(org.apache.thrift.protocol.TProtocol iprot, APIQueryFailedOperation struct) throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TField schemeField;
      iprot.readStructBegin();
      while (true)
      {
        schemeField = iprot.readFieldBegin();
        if (schemeField.type == org.apache.thrift.protocol.TType.STOP) { 
          break;
        }
        switch (schemeField.id) {
          case 1: // OK
            if (schemeField.type == org.apache.thrift.protocol.TType.I32) {
              struct.ok = iprot.readI32();
              struct.setOkIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          case 2: // MESSAGE
            if (schemeField.type == org.apache.thrift.protocol.TType.STRING) {
              struct.message = iprot.readString();
              struct.setMessageIsSet(true);
            } else { 
              org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
            }
            break;
          default:
            org.apache.thrift.protocol.TProtocolUtil.skip(iprot, schemeField.type);
        }
        iprot.readFieldEnd();
      }
      iprot.readStructEnd();

      // check for required fields of primitive type, which can't be checked in the validate method
      struct.validate();
    }

    public void write(org.apache.thrift.protocol.TProtocol oprot, APIQueryFailedOperation struct) throws org.apache.thrift.TException {
      struct.validate();

      oprot.writeStructBegin(STRUCT_DESC);
      oprot.writeFieldBegin(OK_FIELD_DESC);
      oprot.writeI32(struct.ok);
      oprot.writeFieldEnd();
      if (struct.message != null) {
        oprot.writeFieldBegin(MESSAGE_FIELD_DESC);
        oprot.writeString(struct.message);
        oprot.writeFieldEnd();
      }
      oprot.writeFieldStop();
      oprot.writeStructEnd();
    }

  }

  private static class APIQueryFailedOperationTupleSchemeFactory implements org.apache.thrift.scheme.SchemeFactory {
    public APIQueryFailedOperationTupleScheme getScheme() {
      return new APIQueryFailedOperationTupleScheme();
    }
  }

  private static class APIQueryFailedOperationTupleScheme extends org.apache.thrift.scheme.TupleScheme<APIQueryFailedOperation> {

    @Override
    public void write(org.apache.thrift.protocol.TProtocol prot, APIQueryFailedOperation struct) throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TTupleProtocol oprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
      java.util.BitSet optionals = new java.util.BitSet();
      if (struct.isSetOk()) {
        optionals.set(0);
      }
      if (struct.isSetMessage()) {
        optionals.set(1);
      }
      oprot.writeBitSet(optionals, 2);
      if (struct.isSetOk()) {
        oprot.writeI32(struct.ok);
      }
      if (struct.isSetMessage()) {
        oprot.writeString(struct.message);
      }
    }

    @Override
    public void read(org.apache.thrift.protocol.TProtocol prot, APIQueryFailedOperation struct) throws org.apache.thrift.TException {
      org.apache.thrift.protocol.TTupleProtocol iprot = (org.apache.thrift.protocol.TTupleProtocol) prot;
      java.util.BitSet incoming = iprot.readBitSet(2);
      if (incoming.get(0)) {
        struct.ok = iprot.readI32();
        struct.setOkIsSet(true);
      }
      if (incoming.get(1)) {
        struct.message = iprot.readString();
        struct.setMessageIsSet(true);
      }
    }
  }

  private static <S extends org.apache.thrift.scheme.IScheme> S scheme(org.apache.thrift.protocol.TProtocol proto) {
    return (org.apache.thrift.scheme.StandardScheme.class.equals(proto.getScheme()) ? STANDARD_SCHEME_FACTORY : TUPLE_SCHEME_FACTORY).getScheme();
  }
}


2:封裝 RPC 介面處理的類方法

import com.RPCUtils.APIQueryService;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import java.util.Map;

public class ProcessRPC {

    public String RunRPC(String IP,int Port,int timeOut,String url,Map<String, String> map){

        TTransport transport = null;
        String result = null;
        try {
            transport = new TSocket(IP, Port, timeOut);
            transport.open();
            TProtocol protocol = new TCompactProtocol(transport);
            APIQueryService.Client client = new APIQueryService.Client(protocol);
            client.setTransport(transport);
            result = client.query(url,map);
            //System.out.println(result);
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }

        return result;
    }
}

2.3:封裝 RPC 的 JMeter 外掛

import com.alibaba.fastjson.JSON;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import java.util.Map;

/**
 * JMeter RPC測試外掛
 *
 */
public class RPCPluginsForJMeter implements JavaSamplerClient {

    private String IP = "IP";
    private String Port = "Port";
    private String timeOut = "timeOut";
    private String URL = "URL";
    private String Params = "Params";
    /**
     * 設定可用引數及的預設值
     */
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.addArgument("IP", ""); //RPC伺服器的IP地址
        arguments.addArgument("Port", "");//埠
        arguments.addArgument("timeOut", "");//設定超時時間
        arguments.addArgument("URL", "");//RPC介面的URl
        arguments.addArgument("Params", "{'key1':'value1','key2':'value2'}"); //RPC介面的引數
        return arguments;
    }

    /**
     * 獲取arguments引數進行測試驗證
     */
    public SampleResult runTest(JavaSamplerContext context) {

        IP = context.getParameter("IP");
        Port = context.getParameter("Port");
        timeOut = context.getParameter("timeOut");
        URL = context.getParameter("URL");
        Params = context.getParameter("Params");

        SampleResult sr = new SampleResult();
        try {
            sr.sampleStart();// jmeter 開始統計響應時間標記

            //建立物件執行
            Map<String, String> map = (Map<String, String>) JSON.parseObject(Params, Map.class);
            ProcessRPC processRPC = new ProcessRPC();
            String result = processRPC.RunRPC(IP,Integer.parseInt(Port),Integer.parseInt(timeOut),URL,map);

            //設定響應
            sr.setResponseData(result,"UTF-8");
            //sr.setResponseMessage(result);
            sr.setDataType(SampleResult.TEXT);
            sr.setSuccessful(true);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sr.sampleEnd();// jmeter 結束統計響應時間標記
        }
        return sr;
    }

    /**
     * 每個執行緒測試前執行一次,做一些初始化工作
     */
    public void setupTest(JavaSamplerContext context) {

    }

    /**
     * 測試結束時呼叫;
     */
    public void teardownTest(JavaSamplerContext context) {

    }
/**打包前需要將main函式里面的內容註釋掉**/
    public static void main(String[] args) {
//        Arguments params = new Arguments();
//        params.addArgument("IP", "xx.xx.xx.xx");//設定引數,並賦予預設值1
//        params.addArgument("Port", "xxxx");//設定引數,並賦予預設值2
//        params.addArgument("timeOut", "xxxx");//設定引數,並賦予預設值2
//        params.addArgument("URL", "/xx/xx/xxx");//設定引數,並賦予預設值2
//        params.addArgument("Params", "{"xx":"xx","xx":"xx"}");//設定引數,並賦予預設值2
//        JavaSamplerContext arg0 = new JavaSamplerContext(params);
//        RPCPluginsForJMeter test = new RPCPluginsForJMeter();
//        test.setupTest(arg0);
//        test.runTest(arg0);
//        test.teardownTest(arg0);
    }
}

2.4:打包 jar、驗證和測試

注意:需要打包成可執行的 jar 包

2.5:測試與資料收集

這裡的測試跟壓測 HTTP 的操作幾乎一樣的

3:結束篇

RPC 不可怕,百度是最好的老師,多研究、多思考一下... 問題自然就迎刃而解了

相關文章