Thrift示例
http://blog.zhengdong.me/2012/05/10/hello-world-by-thrift-using-java/
demo.thrift 檔案內容如下
namespace java com.vv.test
struct Item {
1: i64 id,
2: string content,
}
service CrawlingService {
void write(1:list<Item> items),
}
使用命令自動生成檔案
F:\>thrift-0.10.0.exe --gen java demo.thrift
然後拷貝到專案
然後編寫樣例程式碼
- import java.util.ArrayList;
- import java.util.List;
- import org.apache.thrift.TException;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.protocol.TProtocol;
- import org.apache.thrift.server.TServer;
- import org.apache.thrift.server.TThreadPoolServer;
- import org.apache.thrift.transport.TServerSocket;
- import org.apache.thrift.transport.TSocket;
- import org.apache.thrift.transport.TTransport;
- import org.apache.thrift.transport.TTransportException;
- import com.vv.test.CrawlingService;
- import com.vv.test.Item;
- public class T {
- public static void main(String[] args) throws InterruptedException {
- new Thread(new Server()).start();
- Thread.sleep(1000);
- Client client = new Client();
- List<Item> list = new ArrayList<Item>();
- for (int i = 1; i <= 10; i++) {
- Item item = new Item();
- item.setId(i);
- item.setContent("hello world " + i);
- list.add(item);
- }
- client.write(list);
- }
- }
- class Server implements Runnable {
- @Override
- public void run() {
- try {
- // Set port
- TServerSocket serverTransport = new TServerSocket(9090);
- // Set CrawlingHandler we defined before
- // to processor, which handles RPC calls
- // Remember, one service per server
- CrawlingHandler handler = new CrawlingHandler();
- CrawlingService.Processor<CrawlingService.Iface> processor = new CrawlingService.Processor<CrawlingService.Iface>(
- handler);
- TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
- System.out.println("Starting server on port 9090 ...");
- server.serve();
- } catch (TTransportException e) {
- e.printStackTrace();
- }
- }
- }
- class Client {
- public void write(List<Item> items) {
- TTransport transport;
- try {
- transport = new TSocket("localhost", 9090);
- transport.open();
- TProtocol protocol = new TBinaryProtocol(transport);
- CrawlingService.Client client = new CrawlingService.Client(protocol);
- client.write(items);
- transport.close();
- } catch (TTransportException e) {
- e.printStackTrace();
- } catch (TException e) {
- e.printStackTrace();
- }
- }
- }
- class CrawlingHandler implements CrawlingService.Iface {
- @Override
- public void write(List<Item> items) throws TException {
- for (Item item : items) {
- System.out.println(item);
- }
- };
- }
執行結果如下:
但是輸出的最後一行顯示"Received 1" ,這個輸出是從哪裡來的?以後慢慢再看吧.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-2135577/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Thrift RPC 系列教程(1)——Thrift語言RPC
- ImportError: No module named thrift.Thrift問題解決ImportError
- thrift安裝
- Thrift c#C#
- Thrift使用入門(1) - Thrift概述及其安裝
- Mac 安裝thriftMac
- Thrift 入門教程
- thrift with Go (0.11.0)Go
- ubuntu下安裝thriftUbuntu
- Thrift RPC Client 流程RPCclient
- Thrift-java例項Java
- Thrift架構~目錄架構
- Thrift IDL 快速入門
- Thrift RPC 通訊搭建RPC
- Thrift的網路堆疊
- Thrift RPC新增access logRPC
- golang 網路框架之 thriftGolang框架
- Thrift之Protocol原始碼分析Protocol原始碼
- 如何使用thrift 服務引擎元件元件
- Thrift原理分析(一)基本概念
- JMeter 測試 thrift RPC 介面JMeterRPC
- windows下編譯安裝thriftWindows編譯
- Thrift協議的服務模型協議模型
- Thrift-java學習小結Java
- ubuntu thrift 0.9.3編譯安裝Ubuntu編譯
- From CORBA technology To Thrift technologyORB
- 對Thrift的一點點理解
- Thrift使用入門(2) - 用Thrift實現一個簡單的Server/Client應用程式Serverclient
- Thrift RPC 系列教程(3)——模組化RPC
- thrift原始碼分析-架構設計原始碼架構
- Spring Boot 中使用 thrift 入門Spring Boot
- RPC框架實踐之:Apache ThriftRPC框架Apache
- Storm-原始碼分析-Thrift的使用ORM原始碼
- 初探Thrift客戶端非同步模式客戶端非同步模式
- Mac OS X 下搭建thrift環境Mac
- thrift中的超時(timeout)坑
- Thrift server端的幾種工作模式分析Server模式
- python通過thrift操作hbase例項Python