网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
if ($name == 'service') {
$this->service = $arguments[0];
return $this;
}
$json_data = json_encode([
'service' => $this->service,
'action' => $name,
'param' => $arguments[0],
]);
$client = new \Swoole\Client(SWOOLE_SOCK_TCP);
if (!$client->connect('127.0.0.1', 9501, -1)) {
exit("connect failed. Error: {$client->errCode}\n");
}
$client->send($json_data);
$result = $client->recv();
var_dump($result);
$client->close();
}
}
$client = new Client();
$client->service(‘CartService’)->getList(1);
Server.php
<?php require\_once 'vendor/autoload.php'; class Server { protected $server; public function \_\_construct() { $this->server = new \Swoole\Server("127.0.0.1", 9501); $this->onConnect(); $this->onReceive(); $this->onClose(); $this->start();; } public function onConnect() { $this->server->on('Connect', function ($serv, $fd) { echo "Client: Connect.\n"; }); } public function onReceive() { $this->server->on('Receive', function ($serv, $fd, $from\_id, $data) { $data = json\_decode($data, true); $service = $data['service']; $action = $data['action']; $page = $data['param']; $class = "rpc\\Service\\" . $service; $instance = new $class; $result = json\_encode($instance->$action($page)); $serv->send($fd, $result); }); } public function onClose() { $this->server->on('Close', function ($serv, $fd) { echo "Client: Close.\n"; }); } public function start() { $this->server->start(); } } $server = new Server(); ``` CartService.php ``` <?php namespace rpc\Service; class CartService { public function getList($page) { return [ [ 'name' => 'zhangsan', 'sex' => $page, ], [ 'name' => 'lisi', 'sex' => 0, ] ]; } } ``` composer.json(主要声明一下自动加载规则) ``` { "name": "gift/rpc", "authors": [ { "name": "gift", "email": "853183966@qq.com" } ], "require": {}, ![img](https://img-blog.csdnimg.cn/img_convert/5f5ad2bde722ce3658b06d2f8657dac8.png) ![img](https://img-blog.csdnimg.cn/img_convert/837ba6346b6dc97d7740b5789417e225.png) ![img](https://img-blog.csdnimg.cn/img_convert/d46d08d0111ebb20b58c034ef68ac03c.png) **既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!** **由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新** **[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)** 知识点,真正体系化!** **由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新** **[如果你需要这些资料,可以戳这里获取](https://bbs.csdn.net/topics/618658159)**