Table of contents
Updated at 2020-03-20 16:00:22

tars-client document


Tars-client is the client that PHP calls tars service. Mainly include the master control routing network transceiver capability and reporting capability.

The tests directory provides a test case for the tars service named App.Server.Servant


Refer to the test cases provided.

Tars-client side calls the tars service. When instantiating, it needs to pass in the instance of \Tars\client\CommunicatorConfig and set the necessary configuration information. Mainly contains the following

  • Service address
    • Automatic routing
      • When there are multiple machines providing services, the service can be discovered through the master automatic routing method
    • Designated service address
      • This method is used for grayscale or when you need to obtain services from a specific address
  • Network transmission
    • Contains three modes: socket, swoole sync, swoole coroutine
  • Report service name
    • Specify the name of the report module. The main control addressing is tarsproxy by default, which can be filled in according to the business when specifying the service address
  • Encoding format

Service address

Combine test case testServant.php to introduce code specifications for different service addressing.

Set related configuration through \Tars\client\CommunicatorConfig class. Test case gives sample code for two addressing modes.

  1. Master routing

Once the locator is specified, tars can automatically grab the service address based on the service name. The format of the locator configuration is as follows

	$config = new \Tars\client\CommunicatorConfig();
    $config->setLocator("tars.tarsregistry.QueryObj@tcp -h -p 17890");

The tars route master is also a tars service, the service name is tars.tarsregistry.QueryObj, the transmission protocol is tcp, the service address is, and the port is 17890. In actual development, please fill in according to the actual situation of the service

After the above-mentioned main control service is determined, the reported module name and encoding format may be specified as required. The default reporting module name is tarsproxy. In order to facilitate business tracking, it is recommended to specify the reporting module name again.

  1. Specify IP

Specify the server's address. This method needs to specify the IP and port of the service, the code is as follows

	$route['sIp'] = "";
    $route['iPort'] = 8080;
    $routeInfo[] = $route;
    $config = new \Tars\client\CommunicatorConfig();

Other uses are the same as automatic routing.

Network transmission mode

  1. Socket synchronization mode

Rely on the sockets extension to send and receive TCP and UDP packets.

  1. Swoole synchronization mode

Use Swoole\Client to send and receive TCP and UDP packets.

  1. Swoole coroutine mode

Use Swoole\Couroutine\Client to send and receive TCP and UDP packets.