The PHP ability to call the tars service is provided in the tars-client, including:
Tars-server provides the underlying server framework and supports the following features:
The ability module to pull configuration files from the configuration service of the tars platform.
The module that packs the business code of tars-server.
The PHP extension code that tars relies on
Modules of remote log writing by tars
The function modules of main dispatching report and characteristic report of tars
The function module of tars for master addressing
The module of keeping alive service reporting by tars
Modules of configuration file parsing by tars
The tool of automatic code generation can automatically generate server and client code.
The platform will provide a template for PHP named tars.tarsphp.default
You must first modify the execution path of PHP in it
App = match tars.proto.php in the tar folder below
Server Name = match tars.proto.php in the tar folder below
Server Type = tars_php
Template = tars.tarsphp.default
obj = match tars.proto.php in the tar folder below
NodeName = tarsnode node ip
Port = server port
Port Type = TCP
Protocol = NOT TARS
Notice:
Basic steps:
src/composer.json
{
"name": "tars/helloworld/phphttp",
"description": "tars php http hello world",
"require": {
"phptars/tars-server": "~0.2",
"phptars/tars-deploy": "~0.1",
"phptars/tars2php": "~0.1",
"phptars/tars-log": "~0.1",
"ext-zip": ">=0.0.1"
},
"autoload": {
"psr-4": {
"PHPHttp\\": "./"
}
},
"minimum-stability": "stable",
"scripts": {
"deploy": "\\Tars\\deploy\\Deploy::run"
}
}
src/index.php
<?php
/**
* Platform entrypoint
*/
require_once __DIR__.'/vendor/autoload.php';
use \Tars\cmd\Command;
//php index.php conf restart
$config_path = $argv[1];
$pos = strpos($config_path, '--config=');
$config_path = substr($config_path, $pos + 9);
$cmd = strtolower($argv[2]);
$class = new Command($cmd, $config_path);
$class->run();
src/services.php
<?php
return [
// Hello is the object name
'Hello' => [
'namespaceName' => 'PHPHttp\\', // psr4 root namespace
'saveTarsConfigFileDir' => 'src/conf/', // config directory
'saveTarsConfigFileName' => ['',], // Config files which need to be pulled from Tars framework.
'monitorStoreConf' => [
//'className' => Tars\monitor\cache\RedisStoreCache::class,
//'config' => [
// 'host' => '127.0.0.1',
// 'port' => 6379,
// 'password' => ':'
//],
'className' => Tars\monitor\cache\SwooleTableStoreCache::class,
'config' => [
'size' => 40960
]
],
'registryStoreConf' => [
'className' => Tars\registry\RouteTable::class,
'config' => [
'size' => 200
]
],
'protocolName' => 'http', //http, json, tars or other
'serverType' => 'http', //http(no_tars default), websocket, tcp(tars default), udp
],
];
namespaceName should match the psr4 settings in composer.json.
monitorStoreConf is the stats report storage configuration
\Tars\monitor\cache\SwooleTableStoreCache::class
for _swooletable as default. tars-monitor allow you to use redis redis instead. You can also build your own storage class by implement \Tars\monitor\contract\StoreCacheInterface
.composer install
tars/tars.proto.php
to define your servant<?php
return [
'appName' => 'HelloWorld',
'serverName' => 'PHPHttp',
'objName' => 'Hello',
];
src/component/Controller.php
<?php
namespace PHPHttp\component;
use Tars\core\Request;
use Tars\core\Response;
class Controller
{
protected $request;
protected $response;
public function __construct(Request $request, Response $response)
{
$this->request = $request;
$this->response = $response;
}
public function getResponse()
{
return $this->response;
}
public function getRequest()
{
return $this->request;
}
public function cookie($key, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httponly = false)
{
$this->response->cookie($key, $value, $expire, $path, $domain, $secure, $httponly);
}
// send raw data to client
public function sendRaw($result)
{
$this->response->send($result);
}
public function header($key, $value)
{
$this->response->header($key, $value);
}
public function status($http_status_code)
{
$this->response->status($http_status_code);
}
}
src/controller/IndexController.php
<?php
namespace PHPHttp\controller;
use PHPHttp\component\Controller;
use Tars\client\CommunicatorConfig;
use Tars\App;
class IndexController extends Controller
{
public function actionIndex()
{
$this->sendRaw('Hello Tars!');
}
}
http://{machine_ip}:9000/index/index
composer run-script deploy
in src
directory to build deployment package.src/PHPHttp_xxx.tar.gz
and deploy/data/app/tars/app_log/HelloWorld/PHPHttp
if the service if failed to start.AppName = HelloWorld
ServerName = PHPTars
ObjName = Hello
scripts/tars2php.sh
#!/bin/bash
cd ../tars/
php ../src/vendor/phptars/tars2php/src/tars2php.php ./tarsclient.proto.php
src/composer.json
{
"name": "tars/helloworld/phptars",
"description": "tars php hello world",
"require": {
"phptars/tars-server": "~0.2",
"phptars/tars-deploy": "~0.1",
"phptars/tars2php": "~0.1",
"phptars/tars-log": "~0.1",
"ext-zip": ">=0.0.1"
},
"autoload": {
"psr-4": {
"PHPTars\\": "./"
}
},
"minimum-stability": "stable",
"scripts": {
"deploy": "\\Tars\\deploy\\Deploy::run"
},
"repositories": {
"packagist": {
"type": "composer",
"url": "https://mirrors.aliyun.com/composer/"
}
}
}
composer install
after composer.json created.src/index.php
<?php
/**
* platform entrypoint
*/
require_once __DIR__.'/vendor/autoload.php';
use \Tars\cmd\Command;
//php index.php conf restart
$config_path = $argv[1];
$pos = strpos($config_path, '--config=');
$config_path = substr($config_path, $pos + 9);
$cmd = strtolower($argv[2]);
$class = new Command($cmd, $config_path);
$class->run();
src/services.php
<?php
return [
// Hello is the ObjName in service deploy form
'Hello' => [
'home-api' => '\PHPTars\servant\HelloWorld\PHPTars\Hello\SayHelloTafServiceServant',
'home-class' => '\PHPTars\impl\SayHello',
'protocolName' => 'tars', //http, json, tars or other
'serverType' => 'tcp', //http(no_tars default), websocket, tcp(tars default), udp
],
];
tars/tars.proto.php
<?php
return [
'appName' => 'HelloWorld',
'serverName' => 'PHPTars',
'objName' => 'Hello',
];
tars/tarsclient.proto.php
<?php
return array(
'appName' => 'HelloWorld',
'serverName' => 'PHPTars',
'objName' => 'Hello',
'withServant' => true, // The tars2php.sh script will generate server code for true, client code for false.
'tarsFiles' => array(
'./SayHello.tars', // tars file location based on tars directory. Only support one file for tars server.
),
'dstPath' => '../src/servant', // The target direcoty for code generating. Location is based on scripts directory
'namespacePrefix' => 'PHPTars\servant',
);
tars/SayHello.tars
module SayHelloTafServiceServant
{
interface SayHelloTafService
{
int greeting(string name, out string greeting); // out stands for output parameter.
};
};
cd scripts && ./php2tars.sh
to generate tars codesrc/impl/SayHello.php
to implement tars API<?php
namespace PHPTars\impl;
use PHPTars\servant\HelloWorld\PHPTars\Hello\SayHelloTafServiceServant;
class SayHello implements SayHelloTafServiceServant
{
public function greeting($name, &$greeting)
{
$greeting = "PHPTars says hello to $name";
return 0;
}
}
home-class
. see src/services.php
composer run-script deploy
in src directory to build a deployment package/data/app/tars/app_log/HelloWorld/PHPTars
on node machine if service is failed to start.SayHello.tars
to tars/SayHello.tars
tars/tarsclient.proto.php
<?php
return array(
'appName' => 'HelloWorld',
'serverName' => 'PHPHttp',
'objName' => 'Hello',
'withServant' => false, //false for client code generating
'tarsFiles' => array(
'./SayHello.tars',
),
'dstPath' => '../src/servant',
'namespacePrefix' => 'PHPHttp\servant',
);
scripts/php2tars.php
to generate client code.src/controller/IndexController.php
public function actionTestGreeting()
{
$config = new \Tars\client\CommunicatorConfig();
$config->setLocator(\Tars\App::$tarsConfig['tars']['application']['client']['locator']);
$userService = new \PHPHttp\servant\HelloWorld\PHPTars\Hello\SayHelloTafServiceServant($config);
$greeting = '';
$return = $userService->greeting('Frank Lee', $greeting);
$this->sendRaw(json_encode(compact('return', 'greeting')));
}
PHPHttp\servant\HelloWorld\PHPTars\Hello\SayHelloTafServiceServant
is generated by tars2php.sh\Tars\App::$tarsConfig
will be initialized by tars configs after service started.\Tars\App::$tarsConfig['tars']['application']['client']['locator']
is the location of registry object which provide route service for tars nodes.http://{machine_ip}/index/TestGreeting