RPC框架-Thrift的使用
Apache Thrift
Thrift 是一種介面描述語言,通過二進位制通訊協議為多種程式語言定義和建立服務。Thrift是一種可擴充套件的跨語言服務的RPC框架,由Facebook開發並且開源。
應用
安裝
MAC
brew install thrift
其他安裝方式
解壓後進入目錄執行:./configure && make
編寫IDL檔案
安裝好Thrift之後需要建立一個.thrift
結尾的檔案。該檔案定義了thrift中的型別和服務。這些被定義的服務將被實現且能夠被客戶端呼叫。
IDL即:Interface definition language 介面描述語言,它是Thrift自己的一套介面定義語法。官方語法定義如下:
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
# Thrift Tutorial
# Mark Slee (mcslee@facebook.com)
#
# This file aims to teach you how to use Thrift, in a .thrift file. Neato. The
# first thing to notice is that .thrift files support standard shell comments.
# This lets you make your thrift file executable and include your Thrift build
# step on the top line. And you can place comments like this anywhere you like.
#
# Before running this file, you will need to have installed the thrift compiler
# into /usr/local/bin.
/**
* The first thing to know about are types. The available types in Thrift are:
*
* bool Boolean, one byte
* i8 (byte) Signed 8-bit integer
* 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?
*/
// Just in case you were wondering... yes. We support simple C comments too.
/**
* Thrift files can reference other Thrift files to include common struct
* and service definitions. These are found using the current path, or by
* searching relative to any paths specified with the -I compiler flag.
*
* Included objects are accessed using the name of the .thrift file as a
* prefix. i.e. shared.SharedObject
*/
include "shared.thrift"
/**
* Thrift files can namespace, package, or prefix their output in various
* target languages.
*/
namespace cpp tutorial
namespace d tutorial
namespace dart tutorial
namespace java tutorial
namespace php tutorial
namespace perl tutorial
namespace haxe tutorial
/**
* Thrift lets you do typedefs to get pretty names for your types. Standard
* C style here.
*/
typedef i32 MyInteger
/**
* Thrift also lets you define constants for use across languages. Complex
* types and structs are specified using JSON notation.
*/
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}
/**
* You can define enums, which are just 32 bit integers. Values are optional
* and start at 1 if not supplied, C style again.
*/
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
/**
* Structs are the basic complex data structures. They are comprised of fields
* which each have an integer identifier, a type, a symbolic name, and an
* optional default value.
*
* Fields can be declared "optional", which ensures they will not be included
* in the serialized output if they aren't set. Note that this requires some
* manual management in some languages.
*/
struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: optional string comment,
}
/**
* Structs can also be exceptions, if they are nasty.
*/
exception InvalidOperation {
1: i32 whatOp,
2: string why
}
/**
* Ahh, now onto the cool part, defining a service. Services just need a name
* and can optionally inherit from another service using the extends keyword.
*/
service Calculator extends shared.SharedService {
/**
* A method definition looks like C code. It has a return type, arguments,
* and optionally a list of exceptions that it may throw. Note that argument
* lists and exception lists are specified using the exact same syntax as
* field lists in struct or exception definitions.
*/
void ping(),
i32 add(1:i32 num1, 2:i32 num2),
i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),
/**
* This method has a oneway modifier. That means the client only makes
* a request and does not listen for any response at all. Oneway methods
* must be void.
*/
oneway void zip()
}
/**
* That just about covers the basics. Take a look in the test/ folder for more
* detailed examples. After you run this file, your generated code shows up
* in folders with names gen-<language>. The generated code isn't too scary
* to look at. It even has pretty indentation.
*/
根據語法,編寫簡單的IDL檔案如下:
檔名: exampleService.thrift
介面定義如下:
namespace java example
namespace php example
/**
* 異常程式碼
*/
enum ErrorCode {
/**
* 成功
*/
SUCCESS = 0,
/**
* 失敗
*/
FAILED = -1,
}
/**
* 異常程式碼含義
*/
const map<i16,string> ERROR_CODE_MESSAGE = {
ErrorCode.SUCCESS: 'success',
ErrorCode.FAILED: 'failed',
}
/**
* 返回資料結構
*/
struct Data {
/**
* 加數
*/
1: i32 data_one,
/**
* 被加數
*/
2: i32 data_two,
/**
* 和
*/
3: i32 sum,
}
/**
* 服務返回結果
*/
struct Result {
1: i32 code;
2: string message;
3: optional Data data;
}
/**
* 服務
*/
service CalcService {
/**
* 計算兩個數的和
*/
Result sum(1:i32 data_one, 2:i32 data_two),
}
介面定義完成之後生成響應語言的程式碼,執行:thrift -r -gen php:server -v -strict -out /path/to/code/ /path/to/exampleService.thrift
服務實現
服務定義好之後,需要實現服務,實現服務的檔名稱:PhpService.php
,內容如下:
<?php
namespace example;
error_reporting(E_ALL);
require __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/CalcService.php';
require_once __DIR__ . '/Types.php';
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../vendor/apache/thrift/lib/php/lib');
$loader->register();
if (php_sapi_name() == 'cli') {
ini_set("display_errors", "stderr");
}
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
class CalculatorHandler implements CalcServiceIf
{
/**
* 計算兩個數的和
*
* @param int $data_one
* @param int $data_two
* @return \example\Result 服務返回結果
*
*/
public function sum($data_one, $data_two)
{
if (!is_numeric($data_one) || !is_numeric($data_two)) {
$error_code = ErrorCode::FAILED;
$messages = Constant::get('ERROR_CODE_MESSAGE');
$msg = $messages[$error_code];
$result = new Result();
$result->code = $error_code;
$result->message = $msg;
return $result;
}
$code = ErrorCode::SUCCESS;
$messages = Constant::get('ERROR_CODE_MESSAGE');
$msg = $messages[$code];
$result = new Result();
$result->code = $code;
$result->message = $msg;
$result_data = new Data();
$result_data->data_one = $data_one;
$result_data->data_two = $data_two;
$result_data->sum = $data_one + $data_two;
$result->data = $result_data;
return $result;
}
}
header('Content-Type', 'application/x-thrift');
if (php_sapi_name() == 'cli') {
echo "\r\n";
}
$handler = new CalculatorHandler();
$processor = new CalcServiceProcessor($handler);
$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TBinaryProtocol($transport, true, true);
$transport->open();
$processor->process($protocol, $protocol);
$transport->close();
啟動服務
在nginx配置中新增如下配置,監聽服務:
server {
listen 8080;
server_name localhost;
location ~* ^/php/PhpServer {
rewrite . /index.php last;
}
location = /index.php {
internal;
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME /path/to/PhpService.php;
include fastcgi_params;
}
}
其中的fastcgi_param
引數的值是PhpService.php
檔案的絕對路徑.
執行nginx -s reload
重啟nginx。
客戶端程式碼
客戶端檔名:PhpClient.php,內容如下:
<?php
namespace example;
error_reporting(E_ALL);
require __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/CalcService.php';
require_once __DIR__ . '/Types.php';
use Thrift\ClassLoader\ThriftClassLoader;
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/../thrift/lib/php/lib');
$loader->register();
use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;
try {
$socket = new THttpClient('localhost', 8080, '/php/PhpServer');
$transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($transport);
/**
* @var CalcServiceIf
*/
$client = new CalcServiceClient($protocol);
$transport->open();
$sum = $client->sum(1, 2);
var_dump($sum);
$transport->close();
echo '<br /> DONE <br />';
} catch (TException $tx) {
print 'TException: ' . $tx->getMessage() . "\n";
}
執行客戶端
執行客戶端程式碼,請求服務。
在PhpStorm中執行PhpClient.php檔案,會自動開啟預設瀏覽器,通過瀏覽器執行POST請求,將請求傳送到nginx。
列印輸出如下:
object(example\Result)#9 (3) {
["code"]=> int(0)
["message"]=> string(7) "success"
["data"]=> object(example\Data)#10 (3) {
["data_one"]=> int(1)
["data_two"]=> int(2)
["sum"]=> int(3)
}
}
可以看到返回的資料結構和我們的thrift檔案中定義的資料結構是一模一樣的。
遠端部署與訪問
上面的服務端程式碼和客戶端程式碼可以部署到不通的主機上,實現遠端呼叫。而且,客戶端程式碼可以有多種實現方式,比如JAVA,Python等。
問題
- 多個客戶端如何保證服務端解析程式碼的一致性呢?
- 服務端程式碼和客戶端程式碼該如何在應用中管理呢?
等等一致性問題都可以通過composer來管理。