浏览代码

Merge branch 'master' of http://git.bjzxtw.org.cn:3000/zxt/admin_consumer

rkljw 3 天之前
父节点
当前提交
d418f493e7

+ 55 - 0
app/Controller/ClientController.php

@@ -0,0 +1,55 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Controller;
+
+use App\JsonRpc\ChatServiceInterface;
+use App\JsonRpc\UserServiceInterface;
+use App\JsonRpc\ClientServiceInterface;
+
+use App\Tools\PublicData;
+use App\Tools\Result;
+use function Hyperf\Support\env;
+use Hyperf\Context\Context;
+use Hyperf\Di\Annotation\Inject;
+use Hyperf\Validation\Contract\ValidatorFactoryInterface;
+// use App\Controller\WebSocketController as WebSocket;
+use Hyperf\Context\ApplicationContext as ApplicationContext;
+use App\Controller\WebSocketController;
+use Swoole\WebSocket\Frame;
+
+/**
+ *
+ * Class ClientController
+ * @package App\Controller
+ */
+//#[AutoController]
+class ClientController extends AbstractController
+{
+    #[Inject]
+    protected ValidatorFactoryInterface $validationFactory;
+    #[Inject]
+    private  ChatServiceInterface $chatServiceClient;
+    #[Inject]
+    private UserServiceInterface $userServiceClient;
+    #[Inject]
+    private ClientServiceInterface $clientServiceClient;
+    public function __construct(
+        ClientServiceInterface $clientServiceClient,
+        ValidatorFactoryInterface $validationFactory,
+        ChatServiceInterface $chatServiceClient,
+        UserServiceInterface $userServiceClient
+    ) {
+        $this->clientServiceClient = $clientServiceClient;
+        $this->validationFactory = $validationFactory;
+        $this->chatServiceClient = $chatServiceClient;
+        $this->userServiceClient = $userServiceClient;
+    }
+    public function test()
+    {
+        // var_dump($this->validationFactory, '----------2-------');
+        $test = $this->clientServiceClient->test(['name' => 'test']);
+        return Result::success($test);
+    }
+}

+ 39 - 0
app/JsonRpc/ClientService.php

@@ -0,0 +1,39 @@
+<?php
+
+namespace App\JsonRpc;
+
+use Hyperf\RpcClient\AbstractServiceClient;
+use Hyperf\Logger\LoggerFactory;
+use Psr\Log\LoggerInterface;
+
+class ClientService extends AbstractServiceClient implements ClientServiceInterface
+{
+    /**
+     * 定义对应服务提供者的服务名称
+     * @var string
+     */
+    protected string $serviceName = 'ClientService';
+    /**
+     * 定义对应服务提供者的服务协议
+     * @var string
+     */
+    protected string $protocol = 'jsonrpc-http';
+    protected $logger;
+
+    public function __construct(LoggerFactory $loggerFactory)
+    {
+        $this->logger = $loggerFactory->get('default');
+    }
+
+    /**
+     * test
+     * @param array $data
+     * @return array
+     */
+    public function test(array $data): array
+    {
+        var_dump($data, $this->serviceName, $this->protocol);
+        $this->logger->info('请求数据: ' . json_encode($data));
+        return $this->__request(__FUNCTION__, $data);
+    }
+}

+ 11 - 0
app/JsonRpc/ClientServiceInterface.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\JsonRpc;
+
+interface ClientServiceInterface
+{
+    /**
+     * @param array $data
+     */
+    public function test(array $data);
+}

+ 15 - 0
config/api/client.php

@@ -0,0 +1,15 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Controller\clientController;
+use Hyperf\HttpServer\Router\Router;
+use App\Middleware\Auth\WebMiddleware;
+
+Router::addGroup(
+    '/client',
+    function () {
+        Router::post('/test', [clientController::class, 'test']);
+    },
+    // ['middleware' => [WebMiddleware::class]]
+);

+ 10 - 0
config/autoload/services.php

@@ -123,5 +123,15 @@ return [
                 ['host' => '127.0.0.1', 'port' => 9502],
             ],
         ],
+        [
+            //客户端服务
+            'name' => 'ClientService',
+            'service' => \App\JsonRpc\ClientServiceInterface::class,
+            // 直接对指定的节点进行消费,通过下面的 nodes 参数来配置服务提供者的节点信息
+            'nodes' => [
+                ['host' => '127.0.0.1', 'port' => 9510],
+            ],
+        ],
     ],
+
 ];

+ 3 - 2
config/routes.php

@@ -1,6 +1,6 @@
 <?php
 
-declare (strict_types = 1);
+declare(strict_types=1);
 /**
  * This file is part of Hyperf.
  *
@@ -9,6 +9,7 @@ declare (strict_types = 1);
  * @contact  group@hyperf.io
  * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
  */
+
 use Hyperf\HttpServer\Router\Router;
 
 Router::addRoute(['GET', 'POST', 'HEAD'], '/index', 'App\Controller\IndexController@index');
@@ -37,4 +38,4 @@ require __DIR__ . '/api/web.php';
 require __DIR__ . '/api/order.php';
 require __DIR__ . '/api/collector.php';
 require __DIR__ . '/api/footer.php';
-
+require __DIR__ . '/api/client.php';