scripts Store the scripts required by the business, such as tars2php.sh, which is responsible for generating the code required by the server based on the tars file
src The directory where the business logic is located mainly contains the following structure:
tars The tcp service depends on the example.tars file under this folder The tars.proto.php file is required to generate the code for the servant, which will be explained in the following guideline.
There are two ways to ensure that tcp-server uses the correct template: - Create a new tars.tarsphp.tcp template, add the following content:
<tars>
<application>
...
<client>
...
</client>
<server>
...
package_length_type=N
open_length_check=1
package_length_offset=0
package_body_offset=0
package_max_length=2000000
</server>
</application>
</tars>
Just add it in
package_length_type=N
open_length_check=1
package_length_offset=0
package_body_offset=0
package_max_length=2000000
Enter Operations Management on the platform => Deployment Services, fill in the corresponding application name and service name, note that this is the same as tars.proto.php in the tars folder below Need to be completely consistent.
Select the service type as tars_php
Select the template as the TCP service template just created, set is not enabled by default
Select an available port and fill in the server's intranet IP
The port type is TCP !!!! Protocol type TCP service must choose TARS !!!!!!
The number of threads corresponds to the number of SWOOLE processes
The maximum number of connections, the maximum queue length, and the queue timeout period are not effective for the PHP service.
Click Add and Submit, then please enter the development guidline
I wrote a tars file myself, let's name it example.tars
Create the corresponding directory structure, fixed to scripts, src and tars
Create a new tars2php.sh file under scripts with the content
cd ../tars/
php ../src/vendor/phptars/tars2php/src/tars2php.php ./tars.proto.php
<?php
return array(
'appName' => 'PHPTest',
'serverName' => 'PHPServer',
'objName' => 'obj',
'withServant' => true,//Decide whether it is server-side or client-side automatic generation
'tarsFiles' => array(
'./example.tars'
),
'dstPath' => '../src/servant',
'namespacePrefix' => 'Server\servant',
);
APPName, serverName, objName must be exactly the same as those applied on the tars platform. withServant must be true while specifying the path to tarsFiles.
dstPath is generally ../ src /?
, here is ../ src / servant
, so the generated code will go to this folder.
namespacePrefix is the namespace of the corresponding code, here is Server \ servant
, which also corresponds to the name of psr-4 in composer.json.
Executing tars2php.sh under scripts will generate a three-level folder under src/servant, Here is PHPTest/PHPServer/obj
Create a new impl folder under src Create a new PHPServerServantImpl.php file in this file. This file must implement all the corresponding methods in TestTafServiceServant.php.
Create a new composer.json file with the following content:
{
"name" : "tars-tcp-server-demo",
"description": "tars tcp server",
"require": {
"phptars/tars-server": "~0.2",
"phptars/tars-deploy": "~0.1",
"phptars/tars-log": "~0.1",
"phptars/tars2php": "~0.1",
"ext-zip" : ">=0.0.1"
},
"autoload": {
"psr-4": {
"Server\\" : "./"
}
},
"minimum-stability": "stable",
"scripts" : {
"deploy" : "\\Tars\\deploy\\Deploy::run"
}
}
Among them, psr-4 in name, description, and autoload can be modified to what you need. Here we take this as an example.
8. Create index.php under src with the following content:
<?php require_once(DIR."/vendor/autoload.php");
use \Tars\cmd\Command;
//php tarsCmd.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();
This file is responsible for startup and entry loading
9. Create a new services.php file with the following content:
<?php /**
// Load the code in namespace mode under the framework of psr4 return [
'obj' => [
'home-api' => '\Server\servant\PHPTest\PHPServer\obj\TestTafServiceServant',
'home-class' => '\Server\impl\PHPServerServantImpl',
'protocolName' => 'tars', //http, json, tars or other
'serverType' => 'tcp', //http(no_tars default), websocket, tcp(tars default), udp
],
];
Home-api refers to the namespace name and class name corresponding to the interface file
home-class refers to the namespace name and class name corresponding to the implementation file
10. composer install, load the corresponding dependencies
11. Create a new conf directory under src to store the configuration. The default is ENVConf.php
12. Implement business logic in PHPServerServantImpl
13. If you need to make other tars service calls, please refer to the documentation of the tars-client module
14. After completing the code development, execute `composer run-script deploy` in the src directory to automatically package the code
15. Upload the packaged code to the tars platform and publish it