Skip to the content.

TARS-TCP-SERVER Description

Directory Structure

  1. 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

  2. src The directory where the business logic is located mainly contains the following structure:
    • conf: configuration required by the business, here is just a demo, if the configuration is issued from the platform, it will also be written to this folder by default;
    • impl: The actual implementation code of the business is the corresponding implementation of the interface. The file path of the specific implementation needs to be written to services.php;
    • servant: server-side code generated by tars2php, this directory name is completely customizable, you only need to distinguish it when using it;
    • cservant: call the code of other clients, the name can be customized, refer to the code generated by tars2php;
    • composer.json: state the project’s dependencies
    • index.php: The entry file for the entire service can be customized, but the private template on the platform must be changed, and the entry field is added under the server
    • services.php: Declare the address of the interface and the address of the actual implementation.These two addresses will be used for instantiation calls and annotation resolution, respectively.
  3. 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.

Service Deployment Guideline

  1. Enter Operations Management => Template Management The platform will provide a new template for PHP, named tars.tarsphp.default, !!!!!!! You must first modify the PHP execution path !!!!!!!

   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
  1. 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.

  2. Select the service type as tars_php

  3. Select the template as the TCP service template just created, set is not enabled by default

  4. Select an available port and fill in the server’s intranet IP

  5. The port type is TCP !!!! Protocol type TCP service must choose TARS !!!!!!

  6. The number of threads corresponds to the number of SWOOLE processes

  7. The maximum number of connections, the maximum queue length, and the queue timeout period are not effective for the PHP service.

  8. Click Add and Submit, then please enter the development guidline

Development guideline

  1. I wrote a tars file myself, let’s name it example.tars

  2. Create the corresponding directory structure, fixed to scripts, src and tars

  3. Create a new tars2php.sh file under scripts with the content

cd ../tars/

php ../src/vendor/phptars/tars2php/src/tars2php.php ./tars.proto.php
  1. Put example.tars in the tars folder and create a new tars.proto.php file under the tars folder:
<?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.

  1. Executing tars2php.sh under scripts will generate a three-level folder under src/servant, Here is PHPTest/PHPServer/obj
    • classes folder - Holds files generated by structs in tars
    • tars folder - stores tars files
    • TestTafServiceServant.php - interface file
  2. 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.

  3. Create a new composer.json file with the following content: ```json { “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
<?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

  1. Create a new services.php file with the following content:
<?php
/**
 * Created by PhpStorm.
 * User: liangchen
 * Date: 2018/2/24
 * Time: 2:51 
 */

// 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

  1. composer install, load the corresponding dependencies

  2. Create a new conf directory under src to store the configuration. The default is ENVConf.php

  3. Implement business logic in PHPServerServantImpl

  4. If you need to make other tars service calls, please refer to the documentation of the tars-client module

  5. After completing the code development, execute composer run-script deploy in the src directory to automatically package the code

  6. Upload the packaged code to the tars platform and publish it