TARS-HTTP-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 client based on the tars file
src The directory where the business logic is located mainly contains the following structure:
component: Stores the base class of the Controller, which is convenient for all Controllers
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;
controller: C layer in MVC model;
servant: The client code generated by tars2php, this directory name can be completely customized, and only needs to be distinguished when it is used;
composer.json: state the project's dependencies
index.php: The entry file for the entire service. The file name can be customized, but you must change the private template on the platform. Add the entry field under server.
services.php: declare the base namespaceName of the entire project
tars The tcp service depends on the example.tars file under this folder The tars.client.proto.php file is necessary 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 execution path of PHP !!!!!!! Secondly, there are two ways to ensure that the http-server uses the correct template:
Create a new tars.tarsphp.http by yourself, add the following content:
Just add it in :
The second way is to add this part to the private template:
Enter Operations Management => 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 http 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 HTTP service must choose non-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
Create the corresponding directory structure, fixed to scripts, src and tars
Create a new directory under src and copy the component and controller folders in the example
Create a new composer.json file with the following content:
The psr-4 in name, description, and autoload can be modified to what you need. Here we take this as an example.
Create index.php under src with the following content:
This file is responsible for startup and entry loading
Create a new services.php file with the following content:
namespaceName is the namespace name actually used by the business and must correspond to the configuration in composer.json monitorStoreConf Storage configuration for the main report information
className is the class name of the storage implementation class for the main report information. The default is \Tars\monitor\cache\SwooleTableStoreCache::class Use swoole_table storage, and tars-monitor also provides the redis storage method. Users can also customize new implementations, but must implement the \Tars\monitor\contract\StoreCacheInterface interface
config is the configuration information of the implementation class that stores the main report information. It is passed as a parameter when the implementation class is initialized. The default value corresponds to the size of swoole_table.
composer install, load the corresponding dependencies
Create a new conf directory to store the configuration under src. The default is ENVConf.php
Create a new tars.proto.php file under the tars folder, which needs to include a description of your service itself:
This name must correspond exactly to the name on the tars platform.
If you just try it, then you can skip to step 14, if you need to call the tars service, please continue
Put the hello.tars of tcp-server into the tars folder, and create a new tarsclient.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 false, and specify the path of tarsFiles. dstPath is generally
../ src /?
, here is../ src / servant
, so the generated code will go to this folder. namespacePrefix is the namespace of the code, here isHttpServer \ 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 - stores files generated by structs in tars
tars folder - stores tars files
TestTafServiceServant.php - the actual remote rpc call file
Add the corresponding rpc calling code in the controller. For details, please refer to the demo in the code or the instructions of tars-client.
After completing the code development, execute composer run-script deploy in the src directory to automatically package the code
Upload the packaged code to the tars platform and publish it
take a rest
Last updated