SwooleをつかったフレームワークSwoftをたのしむ

 SwooleをつかったフレームワークSwoftをたのしむ

uzullaさんのPHPerKaigi2018「大統一PHP」(https://speakerdeck.com/uzulla/da-tong-php)で紹介されていたSwooleという非同期かつ、Coroutineの仕組みをもったライブラリがあります
Swooleを用いたフレームワークの1つにSwoftがあります。
SwoftはビルトインでHTTPのサーバとして動作し、PSR-7、PSR-14、PSR-15、PSR-16を準拠している
モダンでハイパフォーマンスなAOPとCoroutineをもったPHPフレームワークです
そんないけてるフレームワークSwoftを紹介します

#phpconsen

Ec2fcdc4ea7905b289967a2c4c43e154?s=128

CyberAgent SGE Engineer

January 26, 2019
Tweet

Transcript

  1. 4XPPMFΛ͔ͭͬͨϑϨʔϜϫʔΫ 4XPGUΛͨͷ͠Ή !HPPEPP  QIQDPOTFO

  2. • גࣜձࣾαΠόʔΤʔδΣϯτ • 4(&౷ׅຊ෦ٕज़౷ׅࣨࣨ௕ • ࢠձࣾࣾ$50 • גࣜձࣾ$SBGU&HH • גࣜձࣾδʔΫϨετ

    • גࣜձࣾαϜβοϓ • നҪɹӳ • ΤϯδχΞ • #MPH IUUQBNFCMPKQHPPEPP ͓લɺ୭Α • ઋ୆ࢢຽ • 5XJUUFS !HPPEPP • %2 ͙ͪ͢Ή '# 
 ϓΫϦϙ ཱྀܳਓ
  3. "HFOEB w4XPPMFͱ͸ w4XPPMFΛ༻͍ͨϑϨʔϜϫʔΫ w4XPGUͱ͸ wαϯϓϧΞϓϦ w·ͱΊ

  4. 4XPPMFͱ͸

  5. 1)1ͰΠϕϯτۦಈͷඇಉظ ˍίϧʔνϯϕʔεͷฒߦॲཧ

  6. 1)1FS,JBHJͷV[VMMB͞ΜͷൃදͰ஌Γ·ͨ͠ https://speakerdeck.com/uzulla/da-tong-php

  7. 1)1ͷFYUFOTJPOͱͯ͠ ಋೖ͠·͢ # pecl install swoole

  8. 4XPPMFΛ༻͍ͨ ϑϨʔϜϫʔΫ

  9. 4XPPMFͷ(JU)VCͰ঺հ͞Ε͍ͯΔ ϑϨʔϜϫʔΫɹ̏ͭ https://github.com/swoole/swoole-src

  10. 4XPGU ͜Ε͔Β঺հ͢ΔϑϨʔϜϫʔΫ ϞμϯͰɺߴੑೳͰ "01ͱ$PSPVUJOFΛඋ͑ͨϑϨʔϜϫʔΫ

  11. &BTZTXPPMF ؆୯ʹ4XPPMFΛࢼ͢ͷͳΒɺͪ͜Β https://speakerdeck.com/sgeengineer/isucon8deyu-xuan-luo-tisitafalsede-swoole-easyswoole-deshu-kihuan-ete-gan-xiang-zhan-debentizou-rasetemitahua 1)1$POGFSFODFͷ -5Ͱ࿩͠·ͨ͠ɾɾɾ

  12. ͪͳΈʹɾɾ 1)1$POGFSFODF-5ͷ݁࿦

  13. ࢀߟ4MJN̏ͷॳظείΞ໿ 

  14. 4XPPMFΛ༻͍ͨ ϑϨʔϜϫʔΫͷ͸ͳ͠ʹ ΋Ͳͬͯ

  15. 4BCFS 'BUF޷͖͕ͭͬͨͬ͘Ά͍

  16. 4XPGUͱ͸

  17. https://www.swoft.org/ ެࣜ )1

  18. தࠃޠ͕ಡΊͳ͍ͷͰ ӳޠʹ຋༁ͯ͠ ಡΈ·͢

  19. https://www.swoft.org/

  20. ͳΜͱͳ͘ಡΊΔ

  21. https://www.swoft.org/ 1)1ͷϑϧελοΫ DPSPVUJOFϑϨʔϜϫʔΫ

  22. ಛ௃

  23. •Base on Swoole extension •Built-in HTTP, TCP, WebSocket Coroutine Server

    •Powerful AOP (Aspect Oriented Programming) •Flexible and comprehensive annotations framework •Global dependency injection container •PSR-7 based HTTP message implementation •PSR-14 based event manager •PSR-15 based middleware •PSR-16 based cache design •Scalable high performance RPC •Holistic service governance, fallback, load balance, service registration and discovery •Database ORM •Universal connection pools •Mysql, Redis, RPC, HTTP Coroutine Clients •Coroutine driver client and blocking driver client seamlessly switch automatically •Coroutine and asynchronous task delivery •Custom user processes •RESTful supported •Internationalization (i18n) supported •High performance router •Fast and flexible parameter validator •Alias mechanism •Powerful log component •Cross-platform application auto- reload mechanism https://github.com/swoft-cloud/swoft ͷREADMEͷӳ༁ Ҿ༻ɿ
  24. ΊͬͪΌ ػೳઆ໌͋Δ

  25. ࠓճઆ໌͢Δͷ͸ ࣍ͷҰ෦ͷػೳ αϯϓϧΞϓϦͰ࢖༻ͨ͠ػೳ

  26. •Built-in HTTP, TCP, WebSocket Coroutine Server •Powerful AOP (Aspect Oriented

    Programming) •Flexible and comprehensive annotations framework •Global dependency injection container •PSR-7 based HTTP message implementation •Database ORM •Universal connection pools
  27. ࣮ࡍʹ αϯϓϧΞϓϦ ͭͬͯ͘Έͨ

  28. αϯϓϧΞϓϦ

  29. #PPLTIFMGΞϓϦ

  30. (PPHMF͕༻ҙͨ͠νϡʔτϦΞϧ༻ΞϓϦ https://github.com/GoogleCloudPlatform/getting-started-php w .Z42-΁3FBE8SJUF w ը૾ϑΝΠϧͷΞοϓϩʔυ w ੩తϑΝΠϧͷ഑৴ w (PPHMFೝূ

    ͕੝Γࠐ·Ε͍ͯΔ
  31. 4XPGUͰ ͭͬͯ͘ΈΔ

  32. 3FRVFTUMJGFDZDMF https://doc.swoft.org/master/zh-CN/core/framework.html

  33. 4XPGUͷϑΥϧμߏ੒ ࠷খݶ Project ├ app         │ ├ Controllers │ │ └ BooksController.php │ └

    Models │   └ Entity │    └ Books.php ├ config │ ├ beans │ │ └ base.php │ └ properties │   ├ app.php │   └ db.php ├ public ├ runtime │ ├ logs │ ├ uploadfiles │ └ sessions ├ resources │ └ views │   └ books │     └ view.php └ bin   ├ bootstrap.php   └ swoft
  34. 4XPGUͷϑΥϧμߏ੒ ࠷খݶ Project ├ app         │ ├ Controllers │ │ └ BooksController.php │ └

    Models │   └ Entity │    └ Books.php ├ config │ ├ beans │ │ └ base.php │ └ properties │   ├ app.php │   └ db.php ├ public ├ runtime │ ├ logs │ ├ uploadfiles │ └ sessions ├ resources │ └ views │   └ books │     └ view.php └ bin   ├ bootstrap.php   └ swoft ίϯτϩʔϥʔ Ϟσϧ
  35. 4XPGUͷϑΥϧμߏ੒ ࠷খݶ Project ├ app         │ ├ Controllers │ │ └ BooksController.php │ └

    Models │   └ Entity │    └ Books.php ├ config │ ├ beans │ │ └ base.php │ └ properties │   ├ app.php │   └ db.php ├ public ├ runtime │ ├ logs │ ├ uploadfiles │ └ sessions ├ resources │ └ views │   └ books │     └ view.php └ bin   ├ bootstrap.php   └ swoft ౎౓ಡΈࠐ·ΕΔ ઃఆϑΝΠϧ ˞FOW͕༏ઌ CFBOੜ੒࣌ʹಡΈࠐ·ΕΔ ϑΝΠϧ
  36. 4XPGUͷϑΥϧμߏ੒ ࠷খݶ Project ├ app         │ ├ Controllers │ │ └ BooksController.php │ └

    Models │   └ Entity │    └ Books.php ├ config │ ├ beans │ │ └ base.php │ └ properties │   ├ app.php │   └ db.php ├ public ├ runtime │ ├ logs │ ├ uploadfiles │ └ sessions ├ resources │ └ views │   └ books │     └ view.php └ bin   ├ bootstrap.php   └ swoft ੩తϑΝΠϧஔ͖৔ DTTɺKT
  37. 4XPGUͷϑΥϧμߏ੒ ࠷খݶ Project ├ app         │ ├ Controllers │ │ └ BooksController.php │ └

    Models │   └ Entity │    └ Books.php ├ config │ ├ beans │ │ └ base.php │ └ properties │   ├ app.php │   └ db.php ├ public ├ runtime │ ├ logs │ ├ uploadfiles │ └ sessions ├ resources │ └ views │   └ books │     └ view.php └ bin   ├ bootstrap.php   └ swoft Ұ࣌ϑΝΠϧஔ͖৔
  38. 4XPGUͷϑΥϧμߏ੒ ࠷খݶ Project ├ app         │ ├ Controllers │ │ └ BooksController.php │ └

    Models │   └ Entity │    └ Books.php ├ config │ ├ beans │ │ └ base.php │ └ properties │   ├ app.php │   └ db.php ├ public ├ runtime │ ├ logs │ ├ uploadfiles │ └ sessions ├ resources │ └ views │   └ books │     └ view.php └ bin   ├ bootstrap.php   └ swoft UFNQMBUFϑΝΠϧ
  39. 4XPGUͷϑΥϧμߏ੒ ࠷খݶ Project ├ app         │ ├ Controllers │ │ └ BooksController.php │ └

    Models │   └ Entity │    └ Books.php ├ config │ ├ beans │ │ └ base.php │ └ properties │   ├ app.php │   └ db.php ├ public ├ runtime │ ├ logs │ ├ uploadfiles │ └ sessions ├ resources │ └ views │   └ books │     └ view.php └ bin   ├ bootstrap.php   └ swoft ىಈ༻ϑΝΠϧ
  40. ίϯτϩʔϥʔ

  41. ίϯτϩʔϥʔ w 63-ͷϚοϐϯάΛBOOPUBUJPOͰ ઃఆ w ςϯϓϨʔτɺϨΠΞ΢τϑΝΠϧΛ BOOPUBUJPOͰઃఆ

  42. ίϯτϩʔϥʔ 143४ڌ

  43. ίϯτϩʔϥʔ ʙதུʙ %FQFOEFODZ *OKFDUJPO

  44. Ϟσϧ &OUJUZ

  45. Ϟσϧ &OUJUZ $ php bin/swoft entity:create -d bookshelf ࣍ͷίϚϯυͰ%#͔Βࣗಈੜ੒ ϦόʔεΤϯδχΞϦϯά

  46. Ϟσϧ03.

  47. Ϟσϧ03. 03. #PPLTςʔϒϧσʔλΛऔಘ

  48. $POpH%#

  49. $POpH%# DPOOFDUJPOQPPMͷઃఆ

  50. ڵຯΛ΋ͬͨํ͸GPSLͯ͠ ͥͻ৮ͬͯΈ͍ͯͩ͘͞ https://github.com/shirai-suguru/Google-Bookshelf-swoft

  51. ·ͱΊ

  52. w4XPGUͰ(PPHMFͷ#PPLTIFMG ΞϓϦΛͭͬͯ͘Έͨ w"OOPUBUJPOʹΑΔϧʔςΟϯάɺ %*͕͓खܰ w143΋Ζ΋Ζ४ڌ     

    w03.΋׬උ wҙࣝ͠ͳͯ͘΋ίωΫγϣϯϓʔ Ϧϯά͞ΕΔ
  53. Ϟμϯʹ͔͚Δ ʴ Ճ͑ͯɺ$PSPVUJOF Ͱฒྻॲཧָ͕ʑʂ ࠓճ͸આ໌Ͱ͖ͣ 

  54. Έͳ͞Μ΋ TXPPMFΛ͔ͭͬͨ ϑϨʔϜϫʔΫʹ νϟϨϯδ͠·͠ΐ͏

  55. ͓·͚

  56. *46$0/ ϕϯνϚʔΫ݁Ռ ൺֱ

  57. લճͱಉ͡؀ڥ͸ ༻ҙͰ͖ͳ͔ͬͨͷͰ ($1্Ͱଌͬͨ ݁ՌͰ͢ n1-highcpu-2ʢvCPU x 2ɺϝϞϦ 1.8 GBʣ

  58. %FGBVMU 4MJN &BTZ4XPPMF W 4XPGU ճ໨    ̎ճ໨

       ճ໨    ճ໨    ̑ճ໨    ฏۉ    ˞ϩδοΫมߋͳ͠ͷঢ়ଶ 1)1 4XPPMF
  59. %FGBVMU 4MJN &BTZ4XPPMF W 4XPGU ճ໨    ̎ճ໨

       ճ໨    ճ໨    ̑ճ໨    ฏۉ    ˞ϩδοΫมߋͳ͠ͷঢ়ଶ 1)1 4XPPMF ϑϨʔϜϫʔΫͷ ҧ͍͸ޡࠩͰ͢ʂ https://github.com/shirai-suguru/isucon8-swoft https://github.com/shirai-suguru/isucon8-easyswoole &BTZ4XPPMFͷ࣮૷ɿ 4XPGUͷ࣮૷ɿ
  60. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠