thrift入门

一:thrift 框架引入java

上一篇文章咱们了解了基于java的rpc实现方式。很是的核心,可是也很是的简单。代码不够通用,不支持异步等详细的功能。若是须要的话,咱们得手动写代码去作封装和功能的完善。咱们常说不要重复造轮子,有没有现成的rpc框架能让我直接用呢?确定有,并且不止一种。好比 Dubbo,gRPC,Thrift等。apache

二:thrift的使用框架

(1)下载 thrifteclipse

去官网(http://thrift.apache.org/download)下载 thrift-x.x.x.exe 文件异步

(2)配置环境变量(为了在任意路径都能使用thrift命令)socket

在path中加一下thrift-x.x.x.exe文件的路径。好比我把这个文件放到了 C:\soft\developSoft\thrift-0.12.0 目录下maven

(3)编写Hello.thrift文件,声明rpc方法和类名等信息ide

namespace java com.cs.thrift

service Hello{

	string helloString(1:string para)
	i32 helloInt(1:string para)
	bool helloBoolean(1:bool para)
	void helloVoid()
	string helloNull()

}

(4)经过thrift生成对应的java类文件测试

 

(5)新建一个maven项目,把生成的类copy到myeclipse中(拷贝gen-java目录下的全部文件便可)ui

(6)编写服务的实现类

新建 HelloServiceImpl,实现 Hello.Iface。并实现里面的方法。

public class HelloServiceImpl implements Hello.Iface{

    @Override
    public String helloString(String para) throws TException {
        // TODO Auto-generated method stub
        return "这是个人一个thrift hello java——————" + para;
    }

    @Override
    public int helloInt(String para) throws TException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean helloBoolean(boolean para) throws TException {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void helloVoid() throws TException {
        // TODO Auto-generated method stub
        
    }

    @Override
    public String helloNull() throws TException {
        // TODO Auto-generated method stub
        return null;
    }

}

 

(7)编写server

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

/**
 * 编写 Thrift服务端,并启动
 * @author cuishuai01
 */
public class HelloServiceServer {

    public static void main(String[] args) throws TTransportException {
        
        //设置服务监听端口为 8888
        TServerSocket tServerSocket = new TServerSocket(8888);
        //建立本身具体的processor 并 关联Hello服务的实现
        TProcessor tProcessor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
        //关联processor
        TServer.Args tArgs = new TServer.Args(tServerSocket);
        tArgs.processor(tProcessor);
        
        //设置协议工厂为 TBinaryProtocol.Factory
        tArgs.protocolFactory(new TBinaryProtocol.Factory());
        
        TServer tServer = new TSimpleServer(tArgs);
        
     System.out.println("下面开始监听客户端发来的服务请求!"); tServer.serve(); } }

(8)编写client

/**
 * 编写客户端,并启动
 * @author cuishuai01
 */
public class HelloClient {

    public static void main(String[] args) throws Exception {
        //设置服务端的信息
        TTransport tTransport = new TSocket("127.0.0.1", 8888, 3000);
        
        //协议要和服务端一致
        TProtocol tProtocol = new TBinaryProtocol(tTransport);
//        TProtocol tProtocol = new TCompactProtocol(tTransport);
//        TProtocol tProtocol = new TJSONProtocol(tTransport);
        Hello.Client client = new Hello.Client(tProtocol);
        
        tTransport.open();//开启socket
        
        String result = client.helloString("thrift java client");//客户端调用服务端方法
        System.out.println(result);//打印服务端方法的返回结果
        
    }
    
}

(9)测试

启动 HelloServiceServer, 再执行 HelloClient 来调用服务端的方法,效果以下。

相关文章
相关标签/搜索