{"id":34674947,"url":"https://github.com/pl1998/swoole_http","last_synced_at":"2025-12-24T20:21:44.826Z","repository":{"id":182955346,"uuid":"297513874","full_name":"pl1998/swoole_http","owner":"pl1998","description":"基于swoole的HTTP服务.提供简单的路由分发、控制器 以及mysql、redis连接池","archived":false,"fork":false,"pushed_at":"2021-04-15T09:45:47.000Z","size":1092,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-07-22T08:03:03.398Z","etag":null,"topics":["http-server","php","swoole"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pl1998.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-09-22T02:29:28.000Z","updated_at":"2023-07-22T08:03:08.339Z","dependencies_parsed_at":"2023-07-22T10:01:23.063Z","dependency_job_id":null,"html_url":"https://github.com/pl1998/swoole_http","commit_stats":null,"previous_names":["pl1998/swoole_http"],"tags_count":2,"template":null,"template_full_name":null,"purl":"pkg:github/pl1998/swoole_http","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pl1998%2Fswoole_http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pl1998%2Fswoole_http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pl1998%2Fswoole_http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pl1998%2Fswoole_http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pl1998","download_url":"https://codeload.github.com/pl1998/swoole_http/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pl1998%2Fswoole_http/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28007938,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-12-24T02:00:07.193Z","response_time":83,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http-server","php","swoole"],"created_at":"2025-12-24T20:21:43.913Z","updated_at":"2025-12-24T20:21:44.821Z","avatar_url":"https://github.com/pl1998.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"基于swoole搭建的一个高性能的HTTP服务器\n\n#### 目录结构\n```shell script\n├── README.md\n├── bin\n│   ├── Client.php\n│   ├── Command.php\n│   ├── HttpServer.php\n│   ├── Log.php\n│   ├── Route.php\n│   ├── ServerQueue.php\n│   ├── controller\n│   │   ├── Controller.php\n│   │   ├── IndexController.php\n│   │   └── Response.php\n│   ├── exception\n│   │   └── HttpException.php\n│   ├── pool\n│   │   ├── DB.php\n│   │   ├── DbPool.php\n│   │   ├── Pool.php\n│   │   ├── Mysqli.php\n│   │   ├── PDO.php\n│   │   └── RedisPool.php\n│   ├── queue\n│   │   └── Queue.php\n│   ├── static\n│   │   └── favicon.ico\n│   └── utility\n├── composer.json\n├── composer.lock\n├── config\n│   ├── database.php\n│   ├── email.php\n│   ├── log.php\n│   ├── route.php\n│   └── server.php\n├── docs\n├── helpers.php\n├── http.php\n├── tcp.php\n├── test\n├── tests.php\n└── view\n    ├── 404.html\n    ├── echarts.html\n    └── log_list.html\n```\n\n#### 功能简洁\n  * 使用大量单例 避免使用 `static`属性 PHP全局变量产生内存泄漏问题\n  * 支持简单的路由分发\\控制 简洁的控制器\n  * 支持redis、mysql连接池 实现了curd 以及事务操作\n\n#### 要求\n\n   * php版本\u003e=7.3\n   * swoole4\n   \n#### 安装使用\n```\nget clone https://github.com/pl1998/swoole_http.git\nphp http.php\n```   \n\n\u003e 全局辅助函数文件 `helpers.php 通过`composer.json`文件 中`autoload` 实现了自动加载\n\u003e 主要实现一些全局函数 读取视图函数 view()  读取配置函数 config()\n```json\n    ...\n   \"autoload\": {\n        \"psr-4\": {\n            \"app\\\\\": \"bin\" //主目录自动加载 映射命名空间\n        },\n        \"files\": [\n            \"helpers.php\"\n        ]\n    }\n    ...\n```\n\n#### 配置文件\n```shell script\n├── config\n│   ├── database.php //数据库配置\n│   ├── route.php    //路由文件\n│   └── server.php  //tcp http 配置以及端口\n```\n\n#### 新增路由 `router.php`\n\n```php\n/**\n * 路由配置\n */\nreturn [\n  'GET'  =\u003e [\n      '/favicon.ico'=\u003efalse,\n      //访问路径 =\u003e 实例控制器 方法名 \n      '/api/tests'=\u003e[\\app\\controller\\TestController::init(),'tests'],\n  ],\n  'POST' =\u003e [\n\n  ],\n  'PUT'  =\u003e [\n\n  ],\n  'DELETE' =\u003e [\n\n  ]\n];\n\n```\n#### 控制器\n```php\nnamespace app\\controller;\n\nuse app\\pool\\DbPool;\n\nclass TestController\n{\n\n    //每个控制器都应该有的单例\n    protected static $init;\n\n    public static function init()\n    {\n        if(is_null(static::$init)) {\n            echo \"加载控制器单例\".\"\\n\";\n            static::$init = new self();\n        }\n        return static::$init;\n    }\n    \n    //拿到请求和响应\n    public function test(object $request,object $response)\n    {\n           //业务逻辑\n                  $db = new DbPool();\n          \n          \n                  $id = $db-\u003etable('users')-\u003einsert([\n                      'name'=\u003e'latent',\n                      'password'=\u003e'',\n                      'avatar'=\u003e'',\n                      'email'=\u003e'pltruenine@163.com',\n                      'created_at'=\u003edate('Y-m-d H:i:s'),\n                      'updated_at'=\u003edate('Y-m-d H:i:s'),\n                      'uid'=\u003e'56895',\n                      'age'=\u003e22,\n                  ]);\n                 \n                  $response-\u003eheader('Content-Type','application/json');\n                  $response-\u003eend(success([\n                      'id'=\u003e$id\n                  ],'插入成功'));\n\n    }\n}\n```\n\n#### 启动服务器\n```shell script\nphp http.php\n---------------\n$ php http.php \n0.0.0.0:9501\n````\n\n\n#### 我们来跑hello world\n\n```php\n public function test(object $request,object $response)\n    {\n        $response-\u003eheader('Content-Type', 'text/html');\n        $response-\u003eend(\"hello world!\");\n  }\n```\n\n\n#### 这性能逼近gRPC(3万多)\n![file](./docs/WechatIMG257.png)\n\n\n#### 测试插入数据并启动重启服务器\n\n#### 浏览器访问[0.0.0.0:9501](http://0.0.0.0:9501/api/test) 尝试多请求几次\n\n![file](./docs/WechatIMG253.png)\n\n####命令行\n```shell script\n$ php http.php\n0.0.0.0:9501\n当前使用内存:1489312\n加载路由单例\n加载Index控制器单例\n加载Test控制器单例\n当前使用内存:1584304\n当前使用内存:1584304\n当前使用内存:1584304\n当前使用内存:1584304\n当前使用内存:1584304\n当前使用内存:1584304\n当前使用内存:1584304\n```\n#### 开始测试\n```shell script\nwrk -t4 -c50 -d30 http://127.0.0.1:9501/api/test\n```\n\n![file](./docs/WechatIMG254.png)\n\n![file](./docs/WechatIMG255.jpeg)\n\n#### 此时已经插入了3万多条数据了\n\n![file](./docs/WechatIMG256.png)\n\n#### 感觉不太行 可能是连接池的问题\n```php\nreturn [\n    'mysql' =\u003e[\n        'host'=\u003e'127.0.0.1',\n        'port'=\u003e3306,\n        'coding'=\u003e'utf8mb4',\n        'dbname'=\u003e'swoole',\n        'username'=\u003e'root',\n        'password'=\u003e'root',\n        'size'=\u003e15, //默认只给连接池15个连接\n    ]\n];\n```\n\n\n#### 连接池调大继续测试\n\n![file](./docs/WechatIMG258.png)\n#### 结论\n\u003e`Latency`参数明显高一点了 但`Requests/sec` 只提高了40个的样子 如果用协程来跑性能应该更高\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpl1998%2Fswoole_http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpl1998%2Fswoole_http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpl1998%2Fswoole_http/lists"}