2.thrift使用

本文详述了使用Thrift创建一个名为Helloservice的服务,该服务包含sayHello方法,接收一个word参数并返回添加了'hello'的字符串。从编写idl文件开始,到使用Thrift编译器生成代码,打包成jar供服务端和客户端共享,接着实现服务端的Iface接口和服务器的IO操作,最后构建客户端并展示启动服务端和客户端后的运行效果。
摘要由CSDN通过智能技术生成

我们使用thrift,写一个helloservice,它有一个方法sayHello,这个方法有一个参数word,它的功能就是在word后面添加一个hello然后返回,这也是服务端要做的事情;客户端拿到返回结果后打印出来。

我们一步一步写一下:

  1. 编写idl

    service HelloService{
            string sayHello(1: string word);
    }
  2. 我们用thrift编译器编译一下,这里有两种选择,我们可以使用thrift官方提供的编译器:http://thrift.apache.org/docs/install,也可以使用maven的编译插件等

    编译后的代码:

    /**
     * Autogenerated by Thrift Compiler (0.8.0)
     *
     * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
     *  @generated
     */
    import org.apache.thrift.protocol.TTupleProtocol;
    import org.apache.thrift.scheme.IScheme;
    import org.apache.thrift.scheme.SchemeFactory;
    import org.apache.thrift.scheme.StandardScheme;
    import org.apache.thrift.scheme.TupleScheme;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.BitSet;
    import java.util.Collections;
    import java.util.EnumMap;
    import java.util.EnumSet;
    import java.util.HashMap;
    import java.util.Map;
    
    public class HelloService {
    
      public interface Iface {
    
        public String sayHello(String word) throws org.apache.thrift.TException;
    
      }
    
      public interface AsyncIface {
    
        public void sayHello(String word, org.apache.thrift.async.AsyncMethodCallback<AsyncClient.sayHello_call> 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);
          }
        }
    
        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);
        }
    
        public String sayHello(String word) throws org.apache.thrift.TException
        {
          send_sayHello(word);
          return recv_sayHello();
        }
    
        public void send_sayHello(String word) throws org.apache.thrift.TException
        {
          sayHello_args args = new sayHello_args();
          args.setWord(word);
          sendBase("sayHello", args);
        }
    
        public String recv_sayHello() throws org.apache.thrift.TException
        {
          sayHello_result result = new sayHello_result();
          receiveBase(result, "sayHello");
          if (result.isSetSuccess()) {
            return result.success;
          }
          throw new org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.MISSING_RESULT, "sayHello 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 sayHello(String word, org.apache.thrift.async.AsyncMethodCallback<sayHello_call> resultHandler) throws org.apache.thrift.TException {
          checkReady();
          sayHello_call method_call = new sayHello_call(word, resultHandler, this, ___protocolFactory, ___transport);
          this.___currentMethod = method_call;
          ___manager.call(method_call);
        }
    
        public static class sayHello_call extends org.apache.thrift.async.TAsyncMethodCall {
          private String word;
          public sayHello_call(String word, org.apache.thrift.async.AsyncMethodCallback<sayHello_call> 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.word = word;
          }
    
          public void write_args(org.apache.thrift.protocol.TProtocol prot) throws org.apache.thrift.TException {
            prot.writeMessageBegin(new org.apache.thrift.protocol.TMessage("sayHello", org.apache.thrift.protocol.TMessageType.CALL, 0));
            sayHello_args args = new sayHello_args();
            args.setWord(word);
            args.write(prot);
            prot.writeMessageEnd();
          }
    
          public String getResult() throws org.apache.thrift.TException {
            if (getState() != org.apache.thrift.async.TAsyncMethodCall.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_sayHello();
          }
        }
    
      }
    
      public static class Processor<I extends Iface> extends org.apache.thrift.TBaseProcessor<I> implements org.apache.thrift.TProcessor {
        private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());
        public Processor(I iface) {
          super(iface, getProcessMap(new HashMap<String, org.apache.thrift.ProcessFunction<I, ? extends org.apache.thrift.TBase>>()));
        }
    
        protected Processor(I iface, Map<String,  org.apache.thrift.ProcessFunction<I, ? extends  org.apache.thrift.TBase>> processMap) {
          super(iface, getProcessMap(processMap));
        }
    
        private static <I extends Iface
org.apache.thrift.transport.TTransportException是一个异常,它表示在使用Apache Thrift传输时发生了问题。这个异常通常是由于网络连接问题引起的。在引用和引用中,都提到了这个异常。其中,引用中的错误信息显示了一个SocketTimeoutException,这意味着在读取数据时发生了超时。而引用中的错误信息显示了一个GSS initiate failed异常,这可能是由于在与Kerberos进行身份验证时出现了问题导致的。 在处理这个异常时,可能还会遇到其他异常被抑制的情况。在引用中,提到了一个被抑制的SQLException异常,它发生在清理服务器资源时出错。 要解决org.apache.thrift.transport.TTransportException异常,可以考虑以下几个步骤: 1. 检查网络连接是否正常。确保网络连接稳定,并且没有任何阻塞或超时问题。 2. 检查Kerberos配置是否正确。确保在与Hive进行连接时,Kerberos身份验证过程能够正常进行。 3. 检查服务器资源清理过程是否出错。如果在关闭Hive连接时出现异常,可能需要检查关闭过程中是否有任何资源未正确释放。 以上是解决org.apache.thrift.transport.TTransportException异常的一些建议。具体的解决方法可能还需要根据具体情况进行调查和调试。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [线上问题排查系列-org.apache.thrift.transport.TTransportException:java.net.SocketTimeoutExceptio](https://blog.csdn.net/MichaelLi916/article/details/119458931)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [org.apache.thrift.transport.TTransportException: GSS initiate failed](https://blog.csdn.net/m0_37759590/article/details/131488324)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值