thrift 编程

来源:互联网 发布:高仿买家退货淘宝介入 编辑:程序博客网 时间:2024/05/22 09:42

1)   Hello.thrift

a)        namespace java service.demo

 service Hello{

 string helloString(1:string para)

  i32helloInt(1:i32 para)

 bool helloBoolean(1:bool para)

 void helloVoid()

 string helloNull()

 }

b)        thrift --gen java Hello.thrift

     使用 Thrift工具编译Hello.thrift,就会生成相应的 Hello.java文件。

   该文件包含:

1)        Hello.thrift 文件中描述的服务 Hello的接口定义,即 Hello.Iface接口;

2)        服务调用的底层通信细节;

3)        客户端的调用逻辑 Hello.Client ;

4)        服务器端的处理逻辑 Hello.Processor.

 

c)         HelloServiceImpl.java

创建服务器端实现代码,将HelloServiceImpl作为具体的处理器传递给 Thrift服务器,代码如下:

import org.apache.thrift.TException;

 public class HelloServiceImpl implementsHello.Iface {

   @Override

   public boolean helloBoolean(boolean para) throws TException {

       return para;

   }

   @Override

   public int helloInt(int para) throws TException {

       try {

            Thread.sleep(20000);

       } catch (InterruptedException e) {

           e.printStackTrace();

       }

       return para;

   }

   @Override

   public String helloNull() throws TException {

       return null;

   }

   @Override

   public String helloString(String para) throws TException {

       return para;

   }

   @Override

   public void helloVoid() throws TException {

       System.out.println("Hello World");

   }

 }

 

d)          HelloServiceServer.java

创建服务器端实现代码,将 HelloServiceImpl 作为具体的处理器传递给 Thrift 服务器,代码如下:package service.server; 
 import org.apache.thrift.TProcessor; 
 import org.apache.thrift.protocol.TBinaryProtocol; 
 import org.apache.thrift.protocol.TBinaryProtocol.Factory; 
 import org.apache.thrift.server.TServer; 
 import org.apache.thrift.server.TThreadPoolServer; 
 import org.apache.thrift.transport.TServerSocket; 
 import org.apache.thrift.transport.TTransportException; 
 import service.demo.Hello; 
 import service.demo.HelloServiceImpl; 
 
 public class HelloServiceServer { 
    /** 
     * 启动 Thrift 服务器
     * @param args 
     */ 
    public static void main(String[] args) { 
        try { 
            // 设置服务端口为 7911 
            TServerSocket serverTransport = new TServerSocket(7911); 
            // 设置协议工厂为 TBinaryProtocol.Factory 
            Factory proFactory = new TBinaryProtocol.Factory(); 
            // 关联处理器与 Hello 服务的实现
            TProcessor processor = new Hello.Processor(new HelloServiceImpl()); 
            TServer server = new TThreadPoolServer(processor, serverTransport, 
                    proFactory); 
            System.out.println("Start server on port 7911..."); 
            server.serve(); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } 
    } 
 }

 

e)         HelloServiceClient.java

package service.client; 
 import org.apache.thrift.TException; 
 import org.apache.thrift.protocol.TBinaryProtocol; 
 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 service.demo.Hello; 
 
 public class HelloServiceClient { 
 /** 
     * 调用 Hello 服务
     * @param args 
     */ 
    public static void main(String[] args) { 
        try { 
            // 设置调用的服务地址为本地,端口为 7911 
            TTransport transport = new TSocket("localhost", 7911); 
            transport.open(); 
            // 设置传输协议为 TBinaryProtocol 
            TProtocol protocol = new TBinaryProtocol(transport); 
            Hello.Client client = new Hello.Client(protocol); 
            // 调用服务的 helloVoid 方法
            client.helloVoid(); 
            transport.close(); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } catch (TException e) { 
            e.printStackTrace(); 
        } 
    } 
 }

2)    架构图

   Thrift服务器包含用于绑定协议和传输层的基础架构,它提供阻塞、非阻塞、单线程和多线程的模式运行在服务器上,可以配合服务器 / 容器一起运行,可以和现有的 J2EE 服务器 /Web 容器无缝的结合。

3)    Server 端启动、服务时序图


4)    Client 端调用服务时序图


0 0
原创粉丝点击