Microservice Architecture with Reusable Components at JSFest

Microservice Architecture with Reusable Components at JSFest

A2443b68836f2d166eae52b940e99bf6?s=128

Nikita Galkin

March 30, 2018
Tweet

Transcript

  1. Microservice Architecture with Reusable Components Mar 30, 2018

  2. Slides JSFest bot

  3. Nikita Galkin Love and Know: ▰ How to make developers

    and business happy ▰ Technical and process debt elimination Believe that: ▰ Any problem must be solved at the right level ▰ Software is easy. People are hard ▰ A problem should be highlighted, an idea should be "sold", a solution should be demonstrated Links: Site GitHub Twitter Facebook 3
  4. 4

  5. 5 “Wanna do something good, do it yourself” Jean-Baptiste Emanuel

    Zorg (Gary Oldman)
  6. None
  7. 7 “Almost all the successful microservice stories have started with

    a monolith that got too big and was broken up” from MonolithFirst Martin Fowler
  8. 8

  9. 9 “I picked Java because I felt the most people

    would be able to understand the code examples if they were written in Java. That was the case in 1997, but how about in 2017? … But I went for the alternative: JavaScript.” 27 March 2018
  10. None
  11. Components

  12. Best Practices

  13. Reusable Components

  14. 14 Why?

  15. 15 Don’t Repeat Yourself

  16. 16 What?

  17. 17 Scoped packages

  18. Scoped packages 18 ▶ npm install @zorg/zf + @zorg/zf@1.0.5 added

    1 package in 1.727s ▶ cat ~/.npmrc @zorg:registry=http://npm.zorg-weapon.com
  19. 19

  20. 20

  21. First package 21 ▰ @your-scope/code-style ▰ Code Style and Violence

    by Anton Nemtsev
  22. 22 What is not reusable component?

  23. 23

  24. “Small, autonomous services that work together. 24

  25. Evolution

  26. 26

  27. 27 How?

  28. 28 Robustness Engineered for engineers Useful Simple Examples included

  29. npm i -g how-to-npm how-to-npm Or Free 1,5 hour-long video:

    How to Write an Open Source JavaScript Library
  30. Git flow 30 How to convince the team?

  31. 31 How to convince the manager?

  32. “Wanna do something good, do it yourself” Zorg

  33. “Wanna do something good, do it yourself” Zorg

  34. “Don't use require(../../utils/logger), use npm packages” © Galkin Awesome Pair!

    But how reuse code... Microservices
  35. 35 How to develop reusable components?

  36. ▰ npx scripts ▻ eslint helper ▰ plop ▰ yeoman.io

    ▰ raml2html Code, spec, docs generators 36
  37. Generators 37

  38. Test Driven Development 38 FAIL PASS TDD REFACTORING “Repetition of

    a very short development cycle: requirements are turned into very specific test cases, then the software is improved to pass the new tests, only.” Kent Beck
  39. Types or Documentation Driven Development 39 tsc Add JSDoc TDD

    ▰ choose typescript or JSDoc ▰ autocomplete for engineers ▰ always actual code documentation ▰ puppeteer source-code is awesome JSDoc example REFACTORING
  40. /** * @typedef {Object} CoverageEntry * @property {string} url *

    @property {string} text * @property {!Array<!{start: number, end: number}>} ranges */
  41. Theft Driven Development 41 STEAL ADOPT TDD SHARE

  42. Reusable Components

  43. I'll show you my package.json during speaker corner

  44. 44 Where?

  45. Source code storage CI for build and test Artifact storage

  46. Git flow 46 Git flow

  47. Git flow 47 Git flow

  48. Github Flow

  49. Monorepo with Lerna

  50. Or one repo for one component?

  51. Your DevOps Engineer: @#_$!

  52. Your DevOps Engineer: @#_$!

  53. 12factor.net

  54. “II. Dependencies Explicitly declare and isolate dependencies 54

  55. “III. Config Store config in the environment 55

  56. “V. Build, release, run Strictly separate build and run stages

    56
  57. None
  58. “VI. Processes Execute the app as one or more stateless

    processes 58
  59. “XI. Logs Treat logs as event streams 59

  60. stdout stderr

  61. 61 How to develop microservices?

  62. MonolithFirst

  63. How your system works?

  64. ECB is a variant of Model/View/Controller (MVC). It was published

    by Ivar Jacobson (Ericsson Co.) in the 80's with a focus on separating responsibilities of elements in Object Oriented Systems.
  65. Boundary – objects that interface with system actors (e.g. a

    user or external service). Side effected. Boundary component should be reusable. EBC Pattern – Boundary 65
  66. Boundary place for validation

  67. ▰ Wrapper for Enterprise Bus ▰ File Storage wrapper (S3/Local

    FileSystem) ▰ JSON RPC with different transports Possible Boundaries Components 67
  68. Control – objects that implement business logic. This code has

    the main value for business. This part of every microservice is unique. EBC Pattern – Control 68
  69. Control – just control.

  70. Entity – objects representing system data, often from the domain

    model. In our application we use an entity also for transforming data from one format to another. For example, from XML to JSON. Entity can be shared between applications as interfaces or components. EBC Pattern – Entity 70
  71. Entity for data transform

  72. microservices base application with: ▰ Base boundary class ▰ App

    class with start stop ▰ Default logger ▰ Hooks Basement 72
  73. 1. Only Boundary provide services for the "other world". 2.

    Boundary can call only to Control. 3. Control can call anybody. 4. Entity can't call anybody (!), only be called. EBC Pattern 73
  74. 74 Why does it matter?

  75. Bootstrapping and Graceful shutdown

  76. Interface Microservice { void start(); void stop(); } Microservice interface

    76
  77. Bootstrapping

  78. 1. Attach uncaught exception hooks. 2. Log start of bootstrapping.

    3. Verify configuration. 4. Init boundaries and controls. 5. Attach shutdown hooks. 6. Log finish of bootstrapping. Bootstrapping 78
  79. Graceful Shutdown

  80. For graceful shutdown application should: 1. Log start of graceful

    shutdown. 2. Close incoming boundaries. For example, stop accepting new tasks for processing from queue, stop handling new incoming http requests, etc. Graceful shutdown 80
  81. 3. Set a forced timeout for completion current tasks or

    save them for future processing. Recommended value for this timeout is not more then 5 second. 4. Notify consumers about shutdown. For example, send heartbeat-shutdown. Graceful shutdown 81
  82. 5. Correctly disconnect from all connections. For example, database, RabbitMQ,

    Redis, etc. 6. Log finish of graceful shutdown and stop the process. Graceful shutdown 82
  83. 83 Let’s summarize!

  84. ▰ Reusable Components ▰ Monolith First ▰ The Twelve Factors

    ▰ EСB (Entity, Boundary, Control) Pattern ▰ Bootstrapping and Graceful shutdown Microservice Best Practises 84
  85. 85 Robustness Engineered for engineers Useful Simple Examples included

  86. None
  87. None
  88. None
  89. 89 Thank you for attention! Be consistent in your microservices!!!

    You can find me on Twitter as @galk_in Slides are available at speakerdeck.com/galkin or on my site galk.in
  90. Slides JSFest bot

  91. ▰ koa ▰ bunyan ▰ dotenv-safe ▰ gatsbyjs ▰ rxjs

    My favorite packages 91 ▰ pre-commit ▰ eslint ▰ jest ▰ raml-cli ▰ dredd