Kerangka kerja microservice PHP - `Halo dunia` dari Swoft


Ikhtisar


Apa itu Swoft?


Swoft adalah kerangka kerja coroutine microservice kinerja tinggi PHP Ini telah diterbitkan selama bertahun-tahun dan telah menjadi pilihan terbaik untuk php. Ini bisa seperti Go, server web coroutine bawaan dan klien coroutine umum dan ada dalam memori, tidak tergantung pada PHP-FPM tradisional. Ada operasi Go bahasa yang serupa, mirip dengan anotasi fleksibel Spring Cloud framework.


Melalui tiga tahun akumulasi dan eksplorasi arah, Swoft telah menjadikan Swoft Spring Cloud di dunia PHP, yang merupakan pilihan terbaik untuk kerangka kerja berkinerja tinggi PHP dan manajemen layanan mikro.


Github


https://github.com/swoft-cloud/swoft


Artikel


Ini adalah serangkaian tutorial Swoft, yang akan terus diperbarui. Anda dipersilakan untuk berdiskusi dan belajar bersama.


Tutorial


Instal


Gunakan komposer untuk menginstal Swoft


swoft:/www# composer create-project swoft/swoft swoft Installing swoft/swoft (v2.0.5) - Installing swoft/swoft (v2.0.5): Loading from cache Created project in swoft > @php -r "file_exists('.env') || copy('.env.example', '.env');" Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 84 installs, 0 updates, 0 removals - Installing swoft/stdlib (v2.0.5): Loading from cache - Installing swoft/server (v2.0.5): Loading from cache - Installing nikic/php-parser (v4.2.4): Downloading (100%) ...... toolkit/cli-utils suggests installing inhere/php-validate (Very lightweight data validate tool) toolkit/cli-utils suggests installing inhere/console (a lightweight php console application library.) ...... Writing lock file Generating autoload files 

Mulai


Setelah menginstal, buka direktori root proyek Swoft dan mulai Swoft seperti di bawah ini.


 root@MyServer:/tmp/swoft# php bin/swoft http:start 2019/09/14-10:29:34 [INFO] Swoft\SwoftApplication:setSystemAlias(485) Set alias @base=/tmp/swoft 2019/09/14-10:29:34 [INFO] Swoft\SwoftApplication:setSystemAlias(486) Set alias @app=@base/app ...... 2019/09/14-10:29:35 [INFO] Swoft\Processor\ConsoleProcessor:handle(39) Console command route registered (group 14, command 42) Information Panel *********************************************************************** * HTTP | Listen: 0.0.0.0:18306, type: TCP, mode: Process, worker: 3 * RPC | Listen: 0.0.0.0:18307, type: TCP *********************************************************************** HTTP server start success ! 2019/09/14-10:29:35 [INFO] Swoft\Server\Server:startSwoole(492) Swoole\Runtime::enableCoroutine 2019/09/14-10:29:35 [INFO] ...... 

Mulai sukses, Anda dapat melihat port dari Http dan Rpc. Kemudian Anda mengakses alamat [ http://127.0.0.1:18306/ ] ( http://127.0.0.1:18306/ ) di browser. Halaman berikut akan muncul.



Halo dunia


Buat file HelloWorldController.php baru di bawah direktori kontroler Swoft (app / Http / Controller) seperti ini.


 <?php declare(strict_types=1); namespace App\Http\Controller; use Swoft\Http\Server\Annotation\Mapping\Controller; use Swoft\Http\Server\Annotation\Mapping\RequestMapping; /** * Class HelloWorldController * * @since 2.0 * * @Controller(prefix="hello-world") */ class HelloWorldController { /** * @RequestMapping() * * @return string */ public function index(): string { return 'Hello World !'; } } 

Pengontrol dan router yang digunakan di sini akan dijelaskan secara rinci dalam artikel berikutnya, restart layanan, akses browser [ http://127.0.0.1:18306/hello-world/index ] ( http://127.0.0.1:18306/ halo - Dunia / indeks), Anda akan mendapatkan halaman berikut.



Bench


Cukup uji Swoft di bawah alat Apache Bench dan hasilnya adalah sebagai berikut:



Github


https://github.com/swoft-cloud/swoft

Source: https://habr.com/ru/post/id467413/


All Articles