1. 程式人生 > >Intellij IDEA實現 一個簡單的thrift Demo

Intellij IDEA實現 一個簡單的thrift Demo

Thrift是一個由Facebook開發,跨語言的RPC框架,使用IDL(介面定義語言)描述。支援多種通訊協議:TCompactProtocol(壓縮)、TBinaryProtocol(二進位制)和TJSONProtocol(json)。
 

一,需使用介面定義語言描述介面,Test.thrift使用命令生成檔案,見下圖。由於本人的thrift版本為0.8.0,生成的java程式碼,有些程式碼在java1.7有問題,做此修改,將@override去掉,catch到某個異常,直接呼叫printSatck列印,而非throw new xxxException。

namespace java com.thrift.demo

service  HelloWorldService {
  string sayHello(1:string name)
}
$thrift -gen java Test.thrift

二、新建一個maven專案,新建包名com.thrift.demo,將生成的java檔案拷貝到此下。其中pom.xml檔案加入依賴如下

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>7</source>
          <target>7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.8.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.6.1</version>
    </dependency>

新建客戶端,helloClient,程式碼如下,程式碼都很簡單,都是thrift的模組格式

package com.thrift.demo;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class helloClient {

  public static final String SERVER_IP = "127.0.0.1";
  public static final int SERVER_PORT = 9090;
  public static final int TIMEOUT = 30000;

  /**
   *
   * @param userName
   */
  public void startClient(String userName) {
    TTransport transport = null;
    try {
      transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
      TProtocol protocol = new TBinaryProtocol(transport);
      HelloWorldService.Client client = new HelloWorldService.Client(protocol);
      transport.open();
      String result = client.sayHello(userName);
      System.out.println("thrift remote call : " + result);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (null != transport) {
        transport.close();
      }
    }
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    System.out.println("thrift client main executing...");
    helloClient client = new helloClient();
    client.startClient("THRIFT");

  }
}

將此客戶端打包成jar,打包方法見本人另一篇部落格
三、新建另一個maven工程,同理客戶端,拷貝thrift生成的介面檔案,新建包,再新建類服務端,程式碼如下:

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class helloServer {
  public static final int SERVER_PORT = 9090;

  public void startServer() {
    try {
      System.out.println("server start ....");
      TProcessor tprocessor = new HelloWorldService.Processor(new HelloWorldImpl());
      TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
      TServer.Args tArgs = new TServer.Args(serverTransport);
      tArgs.processor(tprocessor);
      tArgs.protocolFactory(new TBinaryProtocol.Factory());
      TServer server = new TSimpleServer(tArgs);
      server.serve();
    } catch (Exception e) {
      System.out.println("Server happened error!!!");
      e.printStackTrace();
    }
  }

  /**
   * @param args
   */
  public static void main(String[] args) {
    System.out.println("thrift server executing...");
    helloServer server = new helloServer();
    server.startServer();
  }
}

新建Iface的實現類程式碼

public class HelloWorldImpl implements HelloWorldService.Iface {

  @Override
  public String sayHello(String name) {
    System.out.println("client name:" + name);
    return name;
  }
}

將jar上傳到linux下,直接使用下述命令啟動客戶端和伺服器端。

$java -jar xxx.jar

參考文獻
https://www.cnblogs.com/xxxteam/archive/2013/04/25/3042760.html
https://blog.csdn.net/mn960mn/article/details/50476759