TARS-TCP-SERVER Description
Directory Structure
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:
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.
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
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:
Just add it in :
The second method is to add this part to the private template, but this method is not recommended.
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
Development guideline
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
Put example.tars in the tars folder and create a new tars.proto.php file under the tars folder:
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
classes folder - Holds files generated by structs in tars
tars folder - stores tars files
TestTafServiceServant.php - interface file
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:
```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" } }
This file is responsible for startup and entry loading
Create a new services.php file with the following content:
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
composer install, load the corresponding dependencies
Create a new conf directory under src to store the configuration. The default is ENVConf.php
Implement business logic in PHPServerServantImpl
If you need to make other tars service calls, please refer to the documentation of the tars-client module
After completing the code development, execute
composer run-script deploy
in the src directory to automatically package the codeUpload the packaged code to the tars platform and publish it
Last updated