Skip to the content.

tars-client document

Introduction

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

Instruction

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

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 172.16.0.161 -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 172.16.0.161, 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.

	$config->setModuleName("App.Server");
    $config->setCharsetName("UTF-8");
  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'] = "127.0.0.1";
    $route['iPort'] = 8080;
    $routeInfo[] = $route;
    $config = new \Tars\client\CommunicatorConfig();
    $config->setRouteInfo($routeInfo);

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.