esd-cloud/saber-cloud-plugin

0.3 2019-06-12 06:52 UTC

This package is auto-updated.

Last update: 2024-04-12 16:58:30 UTC


README

使用saber进行微服务访问,声明试web服务客户端

通过接口描述web客户端行为,服务提供方和调用方共用接口,服务提供方实现接口。

接口描述

/**
 * @RequestMapping("test")
 * Interface IRestController
 */
interface IRestController
{
    /**
     * get请求
     * @GetMapping("/")
     * @return string
     */
    public function hello();

    /**
     * get请求
     * @GetMapping("test/{name}")
     * @PathVariable("name")
     * @RequestParam("id")
     * @param $name
     * @param $id
     * @return string
     */
    public function test($name, $id);

}

服务提供方AnnRestController

/**
 * @RestController()
 * Class TestController
 * @package ESD\Plugins\EasyRoute
 */
class AnnRestController extends EasyController implements IRestController
{

    /**
     * 找不到方法时调用
     * @param $methodName
     * @return mixed
     */
    protected function defaultMethod(?string $methodName)
    {
        // TODO: Implement defaultMethod() method.
    }

    /**
     * get请求
     * @GetMapping("/")
     * @return string
     */
    public function hello()
    {
        return "hello";
    }

    /**
     * get请求
     * @GetMapping("test/{name}")
     * @PathVariable("name")
     * @RequestParam("id")
     * @param $name
     * @param $id
     * @return string
     */
    public function test($name, $id)
    {
        return "hello"
    }
}

客户端RestClient

/**
 * @SaberClient(host="http://127.0.0.1:8082",fallback=RestClientFallback::class)
 * Interface RestClient
 * @package ESD\Plugins\SaberCloud\ExampleClass\Clients
 */
interface RestClient extends IRestController
{

}

客户端降级结果RestClientFallback

class RestClientFallback implements RestClient
{

    /**
     * get请求
     * @GetMapping("/")
     * @return string
     */
    public function hello()
    {
        return "hello";
    }

    /**
     * get请求
     * @GetMapping("test/{name}")
     * @PathVariable("name")
     * @RequestParam("id")
     * @param $name
     * @param $id
     * @return string
     */
    public function test($name, $id)
    {
        return "test";
    }
}

使用客户端

    /**
     * @Inject()
     * @var RestClient
     */
    private $restClient;
    
     /**
     * get请求
     * @GetMapping("/")
     * @return string
     */
    public function hello()
    {
        return $this->restClient->hello();
    }