Thirft简单使用

时间:2023-03-09 01:44:13
Thirft简单使用

安装Thrift

到thrift官网下载thrift.exe

http://thrift.apache.org/download

Thirft简单使用

Thirft简单使用

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

Thirft简单使用

配置环境变量

Thirft简单使用

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

Thirft简单使用

使用Thrift

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

编写thrift接口文件HelloWorld.thrift,并放在D盘aaa文件夹下

namespace java com.thrift.demo

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

Thirft简单使用

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

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

Thirft简单使用

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

Thirft简单使用

Thirft简单使用

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

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

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

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

Thirft简单使用

编写接口实现代码

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 ");
}
}

运行server和client代码

启动server端

Thirft简单使用

启动client端

Thirft简单使用

Server端输出

Thirft简单使用