1. 程式人生 > >Thrift 入門之helloWorld

Thrift 入門之helloWorld

post location 分享 snap open() 成了 rgs void return

不多說,先看項目結構

技術分享圖片

首先先編寫一個hello.thrift的文件

hello.thrift

namespace java sawshaw

service HelloService {
     string hello(1:string method, 2:string param)
}

 註意了,這個namespace是 thrif 根目錄下tutorial目錄的gen-java目錄下的,如果沒有這個目錄,先cmd到tutorial目錄,執行thrift -r --gen java tutorial.thrif。就會看到一個gen-java目錄了,而這個sawshaw是我自定義的目錄,把這個hello.thrift文件放到和tutorial目錄同級,cmd到該目錄後執行thrift -r --gen hello.thrift

可以看到生成了一個HelloService的java類

技術分享圖片

把這個類復制到項目下面改下包名就可以了

再寫個實現類HelloImpl對客戶端的請求作響應

HelloImpl

package com.sawshaw.thrift;

import org.apache.thrift.TException;

import com.sawshaw.thrift.HelloService.Iface;

public class HelloImpl implements Iface{

	public String hello(String method, String param) throws TException {
		return "method:"+method+",param:"+param;
	}

}

編寫服務端代碼以啟動server

HelloServer

package com.sawshaw.thrift;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

import com.sawshaw.thrift.HelloService.Iface;

public class HelloServer {
	public static void main(String[] args) throws TTransportException {
        HelloService.Iface impl=new HelloImpl();
        HelloService.Processor<Iface> processor =   
        		new HelloService.Processor<Iface>(impl);
        TServerTransport serverTransport = new TServerSocket(8080);  
        TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);  
        System.out.println("server has start...");
        tArgs.processor(processor);  
        tArgs.protocolFactory(new TBinaryProtocol.Factory());  
        TServer server = new TThreadPoolServer(tArgs);  
        server.serve();  
	}
}  

編寫客戶端HelloClient調用服務端代碼

HelloClient

package com.sawshaw.thrift;

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 HelloClient {
	public static void main(String[] args) {
		TTransport transport;
		transport = new TSocket("localhost", 8080);
        try {
			transport.open();
		} catch (TTransportException e1) {
			e1.printStackTrace();
		}
        TProtocol protocol = new TBinaryProtocol(transport);
        HelloService.Iface client = new HelloService.Client(protocol);
        // 調用服務的 hello 方法
        String resp = null;
		try {
			resp = client.hello("method","param");
		} catch (TException e) {
			e.printStackTrace();
		}
        System.out.println("resp:"+resp);
        transport.close();
	}

}

 當然pom.xml要引用thrift的包,會自動加載thrift的依賴包

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sawshaw</groupId>
  <artifactId>thrift</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>thrift</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
  <groupId>org.apache.thrift</groupId>
  <artifactId>libthrift</artifactId>
  <version>0.11.0</version>
</dependency>
  </dependencies>
</project> 

服務端啟動後,客戶端運行結果:

resp:method:method,param:param  

Thrift 入門之helloWorld