Thrift的java和php数据交互
Thrift是一个软件框架(远程过程调用框架),用来进行可扩展且跨语言的服务的开发,封装了数据传输格式(二进制、json)和网络通信的服务框架,提供多语言(C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml)的网络服务器端和客户端程序组件
适用于搭建大型数据交换及存储的通用工具,对于大型系统中的内部数据传输相对于JSON和xml无论在性能、传输大小上有明显的优势。
本文以注册服务接口和登录服务器接口为教程,
Thrift开发的几个概念:
Server 服务模型
Handler 数据处理接口
Processor 数据处理对象
Protocol 数据传输协议
Transport 数据传输方式
(1)支持的传输格式
TBinaryProtocol ? 二进制格式.
TCompactProtocol ? 压缩格式
TJSONProtocol ? JSON格式
TSimpleJSONProtocol ?提供JSON只写协议, 生成的文件很容易通过脚本语言解析。
TDebugProtocol ? 使用易懂的可读的文本格式,以便于debug
(2) 支持的通信方式(数据传输方式)(Transport)
TFileTransport:文件(日志)传输类,允许client将文件传给server,允许server将收到的数据写到文件中。
THttpTransport:采用Http传输协议进行数据传输
TSocket:采用TCP Socket进行数据传输
TZlibTransport:压缩后对数据进行传输,或者将收到的数据解压
下面几个类主要是对上面几个类地装饰(采用了装饰模式),以提高传输效率。
TBufferedTransport:对某个Transport对象操作的数据进行buffer,即从buffer中读取数据进行传输,或者将数据直接写入buffer
TFramedTransport:以frame为单位进行传输,非阻塞式服务中使用。同TBufferedTransport类似,也会对相关数据进行buffer,同时,它支持定长数据发送和接收。
TMemoryBuffer:从一个缓冲区中读写数据
(3)支持的服务模型
TSimpleServer ? 简单的单线程服务模型,常用于测试
TThreadedServer - 多线程服务模型,使用阻塞式IO,每个请求创建一个线程。
TThreadPoolServer ? 线程池服务模型,使用标准的阻塞式IO,预先创建一组线程处理请求。
TNonblockingServer ? 多线程服务模型,使用非阻塞式IO(需使用TFramedTransport数据传输方式)
处理大量更新的话,主要是在TThreadedServer和TNonblockingServer中进行选择。TNonblockingServer能够使用少量线程处理大量并发连接,但是延迟较高;TThreadedServer的延迟较低。实际中,TThreadedServer的吞吐量可能会比TNonblockingServer高,但是TThreadedServer的CPU占用要比TNonblockingServer高很多。
服务端编写的一般步骤:
1. 创建Handler
2. 基于Handler创建Processor
3. 创建Transport(通信方式)
4. 创建Protocol方式(设定传输格式)
5. 基于Processor, Transport和Protocol创建Server
6. 运行Server
客户端编写的一般步骤:
1. 创建Transport
2. 创建Protocol方式
3. 基于Transport和Protocol创建Client
4. 运行Client的方法
上边概述内容参考自:
下面开始正式代码教程
服务描述文件test.thrift,定义了login服务和register
/** * The first thing to know about are types. The available types in Thrift are: * * bool Boolean, one byte * byte Signed byte * i16 Signed 16-bit integer * i32 Signed 32-bit integer * i64 Signed 64-bit integer * double 64-bit floating point value * string String * binary Blob (byte array) * map<t1,t2> Map from one type to another * list<t1> Ordered list of one type * set<t1> Set of unique elements of one type * * Did you also notice that Thrift supports C style comments? */namespace java com.penngonamespace php com.penngostruct User { 1: i64 id, 2: string name, 3: string password}service LoginService{ User login(1:string name, 2:string psw);} service RegisterService{ User createUser(1:string name, 2:string psw);}
使用thrift生成对应平台语言代码
thrift -gen java test.thrift
thrift -gen php test.thrift
如果php需要生成服务器端,需求改为thrift -gen php:server test.thrift
java
实现LoginServiceImpl.java登录接口业务
import org.apache.thrift.TException;public class LoginServiceImpl implements LoginService.Iface{ public LoginServiceImpl(){ } public User login(String name, String psw) throws TException{ User user = null; if(name.equals("penngo") && psw.equals("123")){ user = new User(); user.setId(1); user.setName("penngo"); } return user; }}
实现RegisterServiceImpl.java注册接口业务
import org.apache.thrift.TException;public class RegisterServiceImpl implements RegisterService.Iface{ public RegisterServiceImpl(){ } public User createUser(String name, String psw) throws TException{ User user = new User(); user.setId(2); user.setName(name); user.setPassword(psw); return user; }}
服务器端java代码
package com.penngo.main;import org.apache.thrift.TMultiplexedProcessor;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TThreadPoolServer;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TTransportException;import com.penngo.LoginService;import com.penngo.LoginServiceImpl;import com.penngo.RegisterService;import com.penngo.RegisterServiceImpl;public class Server { private void start() { try { TServerSocket serverTransport = new TServerSocket(7911); // 用户登录 LoginService.Processor loginProcessor = new LoginService.Processor( new LoginServiceImpl()); // 用户注册 RegisterService.Processor registerProcessor = new RegisterService.Processor( new RegisterServiceImpl()); // Factory protFactory = new TBinaryProtocol.Factory(true, true); // TServer server = new TThreadPoolServer(new // TThreadPoolServer.Args(serverTransport) // .processor(loginProcessor)); TMultiplexedProcessor processor = new TMultiplexedProcessor(); processor.registerProcessor("LoginService", loginProcessor); processor.registerProcessor("RegisterService", registerProcessor); TServer server = new TThreadPoolServer(new TThreadPoolServer.Args( serverTransport).processor(processor)); System.out.println("Starting server on port 7911 ..."); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String args[]) { Server srv = new Server(); srv.start(); }}
客户端java
package com.penngo.main;import org.apache.thrift.*;import org.apache.thrift.protocol.*;import org.apache.thrift.transport.*;import com.penngo.LoginService;import com.penngo.RegisterService;import com.penngo.User;public class Client { public static void main(String[] args) { try { TTransport transport = new TSocket("localhost", 7911); TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "LoginService"); // TProtocol protocol = new TBinaryProtocol(transport); // LoginService.Client client = new LoginService.Client(protocol); LoginService.Client loginClient = new LoginService.Client(mp1); TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "RegisterService"); RegisterService.Client registerClient = new RegisterService.Client( mp2); transport.open(); User user = loginClient.login("penngo", "123"); if (user != null) { System.out.println("登录成功:" + user.getId() + " " + user.getName()); } else { System.out.println("登录失败"); } User user2 = registerClient.createUser("test", "123"); if (user2 != null) { System.out.println("创建用户成功:" + user2.getId() + " " + user2.getName()); } else { System.out.println("创建用户失败"); } transport.close(); } catch (TException x) { x.printStackTrace(); } }}
客户端php
<?phpnamespace com\penngo;require_once __DIR__.'/../../lib/Thrift/ClassLoader/ThriftClassLoader.php';//echo __DIR__.'/../../lib/Thrift/ClassLoader/ThriftClassLoader.php';use Thrift\ClassLoader\ThriftClassLoader;$GEN_DIR = realpath(dirname(__FILE__)).'/../../gen-php';$loader = new ThriftClassLoader();$loader->registerNamespace('Thrift', __DIR__ . '/../../lib');//$loader->registerDefinition('shared', $GEN_DIR);$loader->registerDefinition('com', $GEN_DIR);$loader->register();if (php_sapi_name() == 'cli') { ini_set("display_errors", "stderr");}use Thrift\Protocol\TBinaryProtocol;use Thrift\Protocol\TMultiplexedProtocol;use Thrift\Transport\TSocket;use Thrift\Transport\THttpClient;use Thrift\Transport\TBufferedTransport;use Thrift\Exception\TException;use com\penngo\RegisterServiceClient;use com\penngo\LoginServiceClient;try { if (array_search('--http', $argv)) { //$socket = new THttpClient('localhost', 8080, '/php/PhpServer.php'); } else { $socket = new TSocket('localhost', 7911); } $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $loginProtocol = new TMultiplexedProtocol($protocol, "LoginService"); $registerProtocol = new TMultiplexedProtocol($protocol, "RegisterService"); $loginClient = new LoginServiceClient($loginProtocol); $registerClient = new RegisterServiceClient($registerProtocol); $transport->open(); $user = $loginClient->login('penngo', '123'); print "user===={$user->id} {$user->name} \n"; $user = $registerClient->createUser('test', '123456'); print "user===={$user->id} {$user->name} \n"; $transport->close();} catch (TException $tx) { print 'TException: '.$tx->getMessage()."\n"; print 'TException: '.$tx->getTraceAsString()."\n";}?>

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

SOLID原則在PHP開發中的應用包括:1.單一職責原則(SRP):每個類只負責一個功能。 2.開閉原則(OCP):通過擴展而非修改實現變化。 3.里氏替換原則(LSP):子類可替換基類而不影響程序正確性。 4.接口隔離原則(ISP):使用細粒度接口避免依賴不使用的方法。 5.依賴倒置原則(DIP):高低層次模塊都依賴於抽象,通過依賴注入實現。

如何在系統重啟後自動設置unixsocket的權限每次系統重啟後,我們都需要執行以下命令來修改unixsocket的權限:sudo...

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...
