2.thrift使用

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们使用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值