<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                > JSON RPC 是一種基于 JSON 格式的輕量級的 RPC 協議標準,易于使用和閱讀。在 Hyperf 里由 hyperf/json-rpc 組件來實現,可自定義基于 HTTP 協議來傳輸,或直接基于 TCP 協議來傳輸。 > 服務有兩種角色,一種是 服務提供者(ServiceProvider),即為其它服務提供服務的服務,另一種是 服務消費者(ServiceConsumer),即依賴其它服務的服務,一個服務既可能是 服務提供者(ServiceProvider),同時又是 服務消費者(ServiceConsumer)。而兩者直接可以通過 服務契約 來定義和約束接口的調用,在 Hyperf 里,可直接理解為就是一個 接口類(Interface),通常來說這個接口類會同時出現在提供者和消費者下。 [TOC] ## 安裝 ~~~ composer require hyperf/json-rpc ~~~ > 該組件只是 JSON RPC 的協議處理的組件,通常來說,您仍需配合 hyperf/rpc-server 或 hyperf/rpc-client 來滿足 服務端 和 客戶端的場景,如同時使用則都需要安裝: > 要使用 JSON RPC 服務端: ~~~ composer require hyperf/rpc-server ~~~ > 要使用 JSON RPC 客戶端: ~~~ composer require hyperf/rpc-client ~~~ ## 提供者 ### 定義服務提供者 > 此代碼的 CalculatorServiceInterface不做展示,自行抽取定義 ~~~ namespace App\JsonRpc; use Hyperf\RpcServer\Annotation\RpcService; /** * 注意,如希望通過服務中心(consul)來管理服務,需在注解內增加 publishTo 屬性 * @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http") * @RpcService2222(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http", publishTo="consul") */ class CalculatorService implements CalculatorServiceInterface { // 實現一個加法方法,這里簡單的認為參數都是 int 類型 public function add(int $a, int $b): int { // 這里是服務方法的具體實現 return $a + $b; } } ~~~ ### 配置JSON RPC服務端 > 配置文件:/config/server.php > 本人僅展示jsonrpc-http方式,還支持jsonrpc、jsonrpc-tcp-length-check協議 ~~~ use Hyperf\Server\Server; use Hyperf\Server\SwooleEvent; return [ // 這里省略了該文件的其它配置 'servers' => [ [ 'name' => 'jsonrpc-http', 'type' => Server::SERVER_HTTP, 'host' => '0.0.0.0', 'port' => 9502, 'sock_type' => SWOOLE_SOCK_TCP, 'callbacks' => [ SwooleEvent::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'], ], ], ], ]; ~~~ ## 消費者 > 一個 服務消費者(ServiceConsumer) 可以理解為就是一個客戶端類,但在 Hyperf 里您無需處理連接和請求相關的事情,只需要進行一些鑒定配置即可。 ### 創建消費者類 > 此代碼的 CalculatorServiceInterface不做展示,自行從服務端代碼處拷貝過來 ~~~ namespace App\JsonRpc; use Hyperf\RpcClient\AbstractServiceClient; class CalculatorServiceConsumer extends AbstractServiceClient implements CalculatorServiceInterface { /** * 定義對應服務提供者的服務名稱 * @var string */ protected $serviceName = 'CalculatorService'; /** * 定義對應服務提供者的服務協議 * @var string */ protected $protocol = 'jsonrpc-http'; public function add(int $a, int $b): int { return $this->__request(__FUNCTION__, compact('a', 'b')); } } ~~~ ### 服務節點配置 > 需要在配置文件定義一個配置標記要從何服務中心獲取節點信息,位于 /config/autoload/services.php ~~~ return [ 'consumers' => [ [ // 對應消費者類的 $serviceName 'name' => 'CalculatorService', // 這個消費者要從哪個服務中心獲取節點信息,如不配置則不會從服務中心獲取節點信息 //'registry' => [ // 'protocol' => 'consul', // 'address' => 'http://127.0.0.1:8500', //], // 如果沒有指定上面的 registry 配置,即為直接對指定的節點進行消費,通過下面的 nodes 參數來配置服務提供者的節點信息 'nodes' => [ ['host' => '127.0.0.1', 'port' => 9504], ], ] ], ]; ~~~ ### 配置接口契約對應關系 > 通過注入 CalculatorServiceInterface 接口來使用服務端提供的方法,需在 /config/autoload/dependencies.php 內定義 CalculatorServiceInterface 和 CalculatorServiceConsumer 的關系 ~~~ return [ App\JsonRpc\CalculatorServiceInterface::class => App\JsonRpc\CalculatorServiceConsumer::class, ]; ~~~ ### Controller中進行調用 ~~~ declare(strict_types=1); namespace App\Controller; use App\JsonRpc\CalculatorServiceInterface; use Hyperf\Di\Annotation\Inject; class IndexController extends AbstractController { /** * @Inject() * @var CalculatorServiceInterface */ public $calculatorService; public function index() { $number = $this->calculatorService->add(3, 6); return [ 'number' => $number, ]; } } ~~~ ## 發布到服務中心 ### 安裝Consul服務端 > 參考consul篇 ### 服務端(提供者):composer安裝相關組件 ~~~ composer require hyperf/consul composer require hyperf/service-governance ~~~ ### 服務端(提供者):配置consul支持 > 配置文件: /config/autoload/consul.php > 配置完成后,在啟動服務時,Hyperf 會自動地將 @RpcService 定義了 publishTo 屬性為 consul 的服務注冊到服務中心去 ~~~ return [ 'uri' => 'http://127.0.0.1:8500', ]; ~~~ ### 客戶端(消費者):composer安裝相關組件 ~~~ composer require hyperf/consul ~~~ ### 客戶端(消費者):配置使用consul查找 > 需要在配置文件定義一個配置標記要從何服務中心獲取節點信息,位于 /config/autoload/services.php ~~~ return [ 'consumers' => [ [ // 對應消費者類的 $serviceName 'name' => 'CalculatorService', // 這個消費者要從哪個服務中心獲取節點信息 'registry' => [ 'protocol' => 'consul', 'address' => 'http://127.0.0.1:8500', ] ] ], ]; ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看