Claim service

Interface service

An RPC service can be provided by defining an interface and implementing an interface.

Directory definition

The directories given in the official application are as follows:

 app/
  Rpc/
    - Lib/          // 服务的公共接口定义目录,里面通常只有php接口类
    - Services/     // 具体的服务接口实现类,里面的类通常实现了 Lib 中定义的接口 

Of course, when used in multiple services, the lib library app/Rpc/Lib moved to a public git repository, and each service is used by composer.

Defining interface

The service provider defines the interface format and stores it in the public lib library. The service caller and the lib library can use the interface service. The interface definition is the same as the normal interface.

 **
 * Class UserInterface
 *
 * @since 2.0
 */
interface UserInterface
{
    /**
     * @param int   $id
     * @param mixed $type
     * @param int   $count
     *
     * @return array
     */
    public function getList(int $id, $type, int $count = 10): array;

    /**
     * @param int $id
     *
     * @return bool
     */
    public function delete(int $id): bool;

    /**
     * @return string
     */
    public function getBigContent(): string;
} 

Interface implementation

An interface, there will be a variety of different implementations, identified by a version number is that logic implementation.

annotation

@Service

  • Version defines the interface version, the default is 1.0

Instance

Implementation version 1

 /**
 * Class UserService
 *
 * @since 2.0
 *
 * @Service()
 */
class UserService implements UserInterface
{
    /**
     * @param int   $id
     * @param mixed $type
     * @param int   $count
     *
     * @return array
     */
    public function getList(int $id, $type, int $count = 10): array
    {
        return ['name' => ['list']];
    }

    /**
     * @param int $id
     *
     * @return bool
     */
    public function delete(int $id): bool
    {
        return false;
    }

    /**
     * @return string
     */
    public function getBigContent(): string
    {
        $content = Co::readFile(__DIR__ . '/big.data');
        return $content;
    }
} 

Implementation version 2

 /**
 * Class UserServiceV2
 *
 * @since 2.0
 *
 * @Service(version="1.2")
 */
class UserServiceV2 implements UserInterface
{
    /**
     * @param int   $id
     * @param mixed $type
     * @param int   $count
     *
     * @return array
     */
    public function getList(int $id, $type, int $count = 10): array
    {
        return [
            'name' => ['list'],
            'v'    => '1.2'
        ];
    }

    /**
     * @param int $id
     *
     * @return bool
     */
    public function delete(int $id): bool
    {
        return false;
    }

    /**
     * @return string
     */
    public function getBigContent(): string
    {
        $content = Co::readFile(__DIR__ . '/big.data');
        return $content;
    }
} 

Different implementations need to define different unique version numbers . If they are the same, the loaded service will overwrite the previous service.

/docs/2.x/en/rpc-server/statement.html
progress-bar