Thrift学习

来源:互联网 发布:网上配眼镜 知乎 编辑:程序博客网 时间:2024/06/02 04:20

简单测试用例。
1. 先下载thrift exe与thrift.jar
2. 编译.thrift文件

service  HelloWorldService {  string sayHello(1:string username)}
  1. thrift命令把.thrift文件编译成对应语言
thrift-0.8.0.exe -r -gen java ./demoHello.thrift
  1. 测试
    1) 接口实现
public class HelloWorldImpl implements HelloWorldService.Iface {    public HelloWorldImpl() {    }    public String sayHello(String username) throws TException {        return "Hi," + username + " welcome to my blog www.micmiu.com";    }}
2) 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;public class HelloClientDemo {    public static final String SERVER_IP = "localhost";    public static final int SERVER_PORT = 8090;    public static final int TIMEOUT = 30000;    public void startClient(String userName) {        TTransport transport = null;        try {            transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);            // 协议要和服务端一致            TProtocol protocol = new TBinaryProtocol(transport);            // TProtocol protocol = new TCompactProtocol(transport);            // TProtocol protocol = new TJSONProtocol(transport);HelloWorldService.Client client = new HelloWorldService.Client(                    protocol);            transport.open();            String result = client.sayHello(userName);            System.out.println("Thrify client result =: " + result);        } catch (TTransportException e) {            e.printStackTrace();        } catch (TException e) {            e.printStackTrace();        } finally {            if (null != transport) {                transport.close();            }        }    }    /**     * @param args     */    public static void main(String[] args) {        HelloClientDemo client = new HelloClientDemo();        client.startClient("Michael");    }}
3) 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;public class HelloServerDemo {    public static final int SERVER_PORT = 18090;    public void startServer() {        try {            System.out.println("HelloWorld TSimpleServer start ....");            TProcessor tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(                    new HelloWorldImpl());            // HelloWorldService.Processor&lt;HelloWorldService.Iface&gt; tprocessor =            // new HelloWorldService.Processor&lt;HelloWorldService.Iface&gt;(            // new HelloWorldImpl());            // 简单的单线程服务模型,一般用于测试            TServerSocket serverTransport = new TServerSocket(SERVER_PORT);            TServer.Args tArgs = new TServer.Args(serverTransport);            tArgs.processor(tprocessor);            tArgs.protocolFactory(new TBinaryProtocol.Factory());            // tArgs.protocolFactory(new TCompactProtocol.Factory());            // tArgs.protocolFactory(new TJSONProtocol.Factory());            TServer server = new TSimpleServer(tArgs);            server.serve();        } catch (Exception e) {            System.out.println("Server start error!!!");            e.printStackTrace();        }    }    /**     * @param args     */    public static void main(String[] args) {        HelloServerDemo server = new HelloServerDemo();        server.startServer();    }}
4) pom.xml
<?xml version="1.0"?>-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"><modelVersion>4.0.0</modelVersion><groupId>com.test</groupId><artifactId>com.test</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>com.test Maven Webapp</name><url>http://maven.apache.org</url>-<dependencies>-<dependency><groupId>org.apache.thrift</groupId><artifactId>libthrift</artifactId><version>0.10.0</version></dependency><!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->-<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.25</version><scope>test</scope></dependency></dependencies>-<build><finalName>com.test</finalName></build></project>

转载自:http://www.micmiu.com/soa/rpc/thrift-sample/