Home Backend Development PHP Tutorial Thrift的java和php数据交互

Thrift的java和php数据交互

Jun 23, 2016 pm 01:29 PM

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);}
Copy after login

使用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;	}}
Copy after login

实现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;	}}
Copy after login

服务器端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();	}}
Copy after login

客户端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();		}	}}
Copy after login

客户端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";}?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles