Thirft简单使用

安装Thrift

到thrift官网下载thrift.exe

http://thrift.apache.org/download

 

thrift-0.10.0.exe复制到C:\Program Files\thrift 并改名为thrift.exe

 

 

配置环境变量

 

 

打开dos窗口输入thrift -version 查看thrift安装版本,出现下图,则成功

 

使用Thrift

编写thrift接口文件并生成java文件

编写thrift接口文件HelloWorld.thrift,并放在Daaa文件夹下

  

namespace java com.thrift.demo

service HelloWorldService {
    string sayHello(1:string username)
}

 

运行命令thrift -o D:\aaaa -gen java D:\aaa\HelloWorld.thrift

说明:使用-o参数指定输出路径

 

 

aaaa下就会生成所需要的java文件

 

 

使用由Thrift文件生成的java文件

将接口文件拷贝到自己的工程中,并导入相关jar

Thriftjar包下载地址如下,可以修改版本号来下载与exe版本对应的jar

http://repo1.maven.org/maven2/org/apache/thrift/libthrift/0.10.0/

 

 

编写接口实现代码

 

package com.thrift.demo;

import org.apache.thrift.TException;

import com.thrift.demo.HelloWorldService.Iface;

public class HelloWorldServiceImpl implements Iface{

     private static int count = 0;  
    
    @Override
    public String sayHello(String username) throws TException {
        count += 1;  
        System.out.println("get " + username + " " +count);   
        return "hello " + username + " " + count;  
    }

}


编写
server代码

 Thrift相关jar包下载:https://github.com/xiaorenwu-dashijie/Thrift.git

package com.thrift.demo;

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.server.TThreadPoolServer.Args;  
import org.apache.thrift.transport.TServerSocket;  
import org.apache.thrift.transport.TTransportException; 

import com.thrift.demo.HelloWorldService;
import com.thrift.demo.HelloWorldService.Processor;

public class Server {
    public void startServer() {  
        try {  
            System.out.println("thrift server open port 1234");
            TServerSocket serverTransport = new TServerSocket(1234);  
            HelloWorldService.Processor process = new Processor(new HelloWorldServiceImpl());  
            Factory portFactory = new TBinaryProtocol.Factory(true, true);  
            Args args = new Args(serverTransport);  
            args.processor(process);  
            args.protocolFactory(portFactory);  
            TServer server = new TThreadPoolServer(args);  
            server.serve();  
        } catch (TTransportException e) {  
            e.printStackTrace();  
        }  
    }  
      
    public static void main(String[] args) {  
        System.out.println("thrift server init");
        Server server = new Server();  
        System.out.println("thrift server start");
        server.startServer();  
        System.out.println("thrift server end");
    }  
}

 

编写client 代码

 

package com.thrift.demo;

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 com.thrift.demo.HelloWorldService;

public class Client {
    public void startClient() {  
        TTransport transport;  
        try {  
            System.out.println("thrift client connext server at 1234 port ");
            transport = new TSocket("localhost", 1234);  
            TProtocol protocol = new TBinaryProtocol(transport);  
            HelloWorldService.Client client = new HelloWorldService.Client(protocol);  
            transport.open();  
            System.out.println(client.sayHello("panguso"));  
            transport.close();  
            System.out.println("thrift client close connextion");
        } catch (TTransportException e) {  
            e.printStackTrace();  
        } catch (TException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public static void main(String[] args) {  
        System.out.println("thrift client init ");
        Client client = new Client();  
        System.out.println("thrift client start ");
        client.startClient();  
        System.out.println("thrift client end ");
    }  
}


运行
serverclient代码 

启动server

 

 

启动client

 

 

Server端输出

 

 

posted @ 2017-11-06 14:00  劈天造陆  阅读(510)  评论(0编辑  收藏  举报