背景:RPC是一种常用的技术,比如游戏服务器,算法服务等都会用到。有时我们想使用一个简单的RPC框架;或者客户端服务端程序是由不用语言编写,但是要使用RPC技术,这时thrift是一种很好的选择。thrift是一种跨语言的RCP框架(不跨语言也可以),下面讲解如何用thrift编写一个简单的客户端服务端均为Java语言的RPC框架。

定义接口,生成代码

创建一个helloworld.thrift文件,内容如下
service Hello {
string helloString(1:string word)
}

用命令:thrift —gen java helloworld.thrift 生成一个名为Hello的Java文件,把此文件放入客户端和服务端项目的thrift包里。
image.png
依赖如下:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.thrift</groupId>
  4. <artifactId>libthrift</artifactId>
  5. <version>0.13.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.slf4j</groupId>
  9. <artifactId>slf4j-log4j12</artifactId>
  10. <version>1.5.8</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.13</version>
  16. </dependency>
  17. </dependencies>

客户端代码

  1. package client;
  2. import org.apache.thrift.TException;
  3. import org.apache.thrift.protocol.TBinaryProtocol;
  4. import org.apache.thrift.protocol.TProtocol;
  5. import org.apache.thrift.transport.TSocket;
  6. import org.apache.thrift.transport.TTransport;
  7. import thrift.Hello;
  8. import java.io.IOException;
  9. public class SimilarityThriftClient {
  10. public static void main(String[] args) {
  11. try {
  12. TTransport transport;
  13. transport = new TSocket("localhost", 9000);
  14. transport.open();
  15. TProtocol protocol = new TBinaryProtocol(transport);
  16. Hello.Client client = new Hello.Client(protocol);
  17. perform(client);
  18. transport.close();
  19. } catch (TException e) {
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. private static void perform(Hello.Client client) throws TException, IOException {
  26. String str = "001.jpg,002.jpg,003.jpg,004.jpg,005.jpg,demo.jpg";
  27. String[] paths = str.split(",");
  28. for(String path:paths){
  29. //ratio是客户端吧path传过去后服务端的返回信息,即服务端代码里的1
  30. String ratio = client.helloString(path);
  31. System.out.println(ratio);
  32. }
  33. }
  34. }

服务端代码

  1. package server;
  2. import org.apache.thrift.protocol.TBinaryProtocol.Factory;
  3. import org.apache.thrift.server.TServer;
  4. import org.apache.thrift.server.TThreadPoolServer;
  5. import org.apache.thrift.server.TThreadPoolServer.Args;
  6. import org.apache.thrift.transport.TServerSocket;
  7. import org.apache.thrift.transport.TTransportException;
  8. import thrift.Hello;
  9. import thrift.Hello.Processor;
  10. /**
  11. * 启动服务
  12. *
  13. */
  14. public class ThriftServer {
  15. /**
  16. * 启动Thrift服务器
  17. */
  18. public void startServer() {
  19. try {
  20. // 定义传输的socket,设置服务端口为6789
  21. TServerSocket serverTransport = new TServerSocket(9000);
  22. // 设置协议工厂为 TBinaryProtocol.Factory
  23. Factory proFactory = new Factory(true, true);
  24. // 关联处理器与 Hello服务的实现
  25. Hello.Processor processor = new Processor(new HelloServiceImpl());
  26. // 定义服务端的参数值
  27. Args args = new Args(serverTransport);
  28. args.processor(processor);
  29. args.protocolFactory(proFactory);
  30. TServer server = new TThreadPoolServer(args);
  31. // 服务端开启服务s
  32. server.serve();
  33. } catch (TTransportException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. public static void main(String[] args) {
  38. System.out.println("ServerStart!");
  39. ThriftServer server = new ThriftServer();
  40. server.startServer();
  41. }
  42. }
  1. package server;
  2. import org.apache.thrift.TException;
  3. import thrift.Hello.Iface;
  4. /**
  5. * 服务端实现类
  6. *
  7. * @author liutao
  8. */
  9. public class HelloServiceImpl implements Iface {
  10. public String helloString(String word) throws TException {
  11. //输出word是在服务端输出
  12. System.out.println(word);
  13. //return是给客户端返回信息
  14. return "1";
  15. }
  16. }