{"id":1055,"date":"2021-12-21T14:44:04","date_gmt":"2021-12-21T14:44:04","guid":{"rendered":"https:\/\/egany.com\/blogs\/?p=1055"},"modified":"2021-12-24T03:10:23","modified_gmt":"2021-12-24T03:10:23","slug":"microservices-so-3-service-communication-3-lan-thay-doi","status":"publish","type":"post","link":"https:\/\/egany.com\/blogs\/microservices-so-3-service-communication-3-lan-thay-doi\/","title":{"rendered":"Microsevices #3 \u2013 Service Communication &#8211; 3 l\u1ea7n thay \u0111\u1ed5i"},"content":{"rendered":"<p>Xin ch\u00e0o! Ch\u00fang ta l\u1ea1i quay tr\u1edf l\u1ea1i v\u1edbi lo\u1ea1t b\u00e0i v\u1ec1 tr\u1ea3i nghi\u1ec7m c\u1ee7a m\u00ecnh trong vi\u1ec7c x\u00e2y d\u1ef1ng h\u1ec7 th\u1ed1ng microservices t\u1ea1i EGANY, trong s\u1ed1 tr\u01b0\u1edbc \u2013 <a href=\"https:\/\/egany.com\/blogs\/microsevices-2-trien-khai-microservices-tinh-gon-de-bat-dau-de-dang-hon\/\">Microsevices #2 \u2013 Tri\u1ec3n khai microservices \u201ctinh g\u1ecdn\u201d \u0111\u1ec3 b\u1eaft \u0111\u1ea7u d\u1ec5 d\u00e0ng h\u01a1n<\/a> \u0111\u00e3 c\u00f3 \u0111\u1ec1 c\u1eadp t\u1edbi m\u1ed9t s\u1ed1 y\u1ebfu t\u1ed1 c\u1ea7n \u0111\u1ec3 x\u00e2y d\u1ef1ng microservices &quot;tinh gon&quot;, h\u00f4m nay m\u00ecnh s\u1ebd chia s\u1ebb v\u1edbi m\u1ecdi ng\u01b0\u1eddi v\u1ec1 v\u1ea5n \u0111\u1ec1 trong b\u00e0i vi\u1ebft tr\u00ean c\u00f3 \u0111\u1ec1 c\u1eadp \u0111\u00f3 l\u00e0 service communication. B\u00e0i vi\u1ebft n\u00e0y s\u1ebd n\u00f3i v\u1ec1 qu\u00e1 tr\u00ecnh m\u00ecnh b\u1eaft \u0111\u1ea7u tri\u1ec3n khai, thay \u0111\u1ed5i khi t\u00ecm hi\u1ec3u v\u1ec1 c\u00e1i d\u00e2n t\u00ecnh x\u00f4n xao l\u00e0 gRPC (\u1edf \u0111\u00e2y m\u00ecnh d\u00f9ng gRPC danh cho nodejs) v\u00e0 nh\u1eefng v\u1ea5n \u0111\u1ec1 ph\u00e1t sinh. V\u1eady nh\u1eefng v\u1ea5n \u0111\u1ec1 n\u00e0y l\u00e0 g\u00ec? Gi\u1ea3i quy\u1ebft nh\u01b0 th\u1ebf n\u00e0o? H\u00e3y c\u00f9ng t\u00ecm hi\u1ec3u trong b\u00e0i vi\u1ebft h\u00f4m n\u00e0y nh\u00e9.<\/p>\n<h2>Kh\u1edfi \u0111\u1ea7u<\/h2>\n<p>S\u1ef1 kh\u1edfi \u0111\u1ea7u lu\u00f4n l\u00e0 m\u1ed9t th\u1eed th\u00e1ch, sau khi t\u00ecm hi\u1ec3u m\u1ed9t s\u1ed1 t\u00e0i li\u1ec7u v\u1ec1 v\u1ea5n \u0111\u1ec1 giao ti\u1ebfp gi\u1eefa c\u00e1c service nh\u01b0 h\u00ecnh d\u01b0\u1edbi \u0111\u00e2y<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/architecture\/microservices\/architect-microservice-container-applications\/media\/communication-in-microservice-architecture\/sync-vs-async-patterns-across-microservices.png\" alt=\"Figure 4-15. Anti-patterns and patterns in communication between microservices\" \/><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/architecture\/microservices\/architect-microservice-container-applications\/communication-in-microservice-architecture\" rel=\"nofollow noopener\" target=\"_blank\">Xem chi ti\u1ebft<\/a><\/p>\n<p>R\u1ed3i h\u00ecnh n\u00e0y<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/microservices.io\/i\/Microservice_Architecture.png\" alt=\"Microservice_Architecture.png\" \/><\/p>\n<p><a href=\"https:\/\/microservices.io\/patterns\/microservices.html\" rel=\"nofollow noopener\" target=\"_blank\">Xem chi ti\u1ebft<\/a><\/p>\n<p>Ti\u1ebfp n\u00e8<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/microservices.io\/i\/PatternsRelatedToMicroservices.jpg\" alt=\"PatternsRelatedToMicroservices.jpg\" \/><\/p>\n<p><a href=\"https:\/\/microservices.io\/patterns\/microservices.html\" rel=\"nofollow noopener\" target=\"_blank\">Xem chi ti\u1ebft<\/a><\/p>\n<blockquote>\n<p>Tr\u1eddi m\u00ecnh l\u00e0 th\u1eb1ng sinh vi\u00ean m\u1edbi ra tr\u01b0\u1eddng m\u00e0? C\u00e1i g\u00ec th\u1ebf n\u00e0y? M\u00ecnh n\u00ean l\u1ef1a ch\u1ecdn c\u00e0i g\u00ec \u0111\u00e2y? M\u00ecnh n\u00ean theo &quot;\u00f4ng&quot; n\u00e0o \u0111\u00e2y? Th\u00f4i ki\u1ebfm g\u00ec xem ti\u1ebfp coi c\u00f3 g\u00ec n\u1eefa kh\u00f4ng.<\/p>\n<\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/www.nginx.com\/wp-content\/uploads\/2016\/04\/Richardson-microservices-part1-2_microservices-architecture.png\" alt=\"Richardson-microservices-part1-2_microservices-architecture.png\" \/><\/p>\n<p><a href=\"https:\/\/www.nginx.com\/blog\/introduction-to-microservices\/\" rel=\"nofollow noopener\" target=\"_blank\">Xem chi ti\u1ebft<\/a><\/p>\n<p>R\u1ed3i! C\u00f3 v\u1ebb nh\u01b0 d\u00f9ng REST API \u0111\u01b0\u1ee3c nhi\u1ec1u n\u01a1i \u0111\u1ec1 c\u1eadp t\u1edbi, h\u01a1n n\u1eefa trong tr\u01b0\u1eddng m\u00ecnh c\u0169ng \u0111\u01b0\u1ee3c h\u1ecdc v\u00e0 l\u00e0m quen c\u00f2n m\u1ea5y c\u00e1i kia nh\u00ecn n\u00f3 m\u00f4ng lung qu\u00e1, tri\u1ec3n khai th\u00ec gi\u1edd ch\u01b0a c\u00f3 kinh nghi\u1ec7m, s\u1ea3n ph\u1ea9m th\u00ec c\u1ea7n ho\u00e0n th\u00e0nh s\u1edbm nh\u1ea5t c\u00f3 th\u1ec3, gi\u1edd m\u00e0 lao \u0111\u1ea7u v\u00e0o m\u1ea5y c\u00e1i cao si\u00eau th\u00ec ch\u1eafc m\u00f9a thu sang n\u0103m m\u1edbi xong. V\u00ec th\u1ebf m\u00ecnh quy\u1ebft \u0111\u1ecbnh ch\u1ecdn REST API \u0111\u1ec3 tri\u1ec3n khai tr\u01b0\u1edbc, r\u1ed3i sau n\u00e0y n\u00e2ng c\u1ea5p sau. M\u00ecnh b\u1eaft \u0111\u1ea7u tri\u1ec3n khai nh\u1eefng services \u0111\u1ea7u ti\u00ean v\u00e0 m\u1ecdi th\u1ee9 kh\u00e1 \u1ed5n tr\u00ean m\u00f4i tr\u01b0\u1eddng production.<\/p>\n<h2>S\u1ef1 xu\u1ea5t hi\u1ec7n c\u1ee7a gRPC<\/h2>\n<p>Trong th\u1eddi gian n\u00e0y m\u00ecnh c\u00f3 t\u00ecm hi\u1ec3u th\u00eam v\u1ec1 ch\u1ee7 \u0111\u1ec1 n\u00e0y v\u00e0 r\u1ed3i m\u1ed9t c\u00f4ng ngh\u1ec7 \u0111ang \u0111\u01b0\u1ee3c chia s\u1ebb v\u00e0 th\u1ea3o lu\u1eadn \u0111\u00f3 l\u00e0 <a href=\"https:\/\/grpc.io\/\" rel=\"nofollow noopener\" target=\"_blank\">gRPC<\/a>, t\u1ea1i th\u1eddi \u0111i\u1ec3m m\u00ecnh t\u00ecm hi\u1ec3u th\u01b0\u1eddng nh\u1eefng so s\u00e1nh v\u1ec1 t\u1ed1c \u0111\u1ed9 gi\u1eefa gRPC v\u1edbi REST API (HTTP) sau khi xem x\u00e9t v\u00e0 c\u00e2n nh\u1eafc m\u1ed9t s\u1ed1 \u0111\u1eb7c \u0111i\u1ec3m sau:<\/p>\n<ul>\n<li>T\u1ed1c \u0111\u1ed9 gRPC l\u00e0 nhanh h\u01a1n<\/li>\n<li>Xu\u1ea5t ph\u00e1t t\u1eeb c\u00f4ng ngh\u1ec7 c\u1ee7a Google<\/li>\n<li>\u0110\u01b0\u1ee3c nhi\u1ec1u ng\u01b0\u1eddi \u0111\u00e1nh gi\u00e1 t\u00edch c\u1ef1c<\/li>\n<li>Ph\u00f9 h\u1ee3p v\u1edbi giao ti\u1ebfp n\u1ed9i b\u1ed9 gi\u1eefa c\u00e1c service<\/li>\n<li>V\u00e0 l\u00e0 d\u00e2n c\u00f4ng ngh\u1ec7 th\u00ec m\u00ecnh c\u0169ng mu\u1ed1n \u1ee9ng d\u1ee5ng nh\u1eefng c\u00f4ng ngh\u1ec7 t\u1ed1t cho nh\u1eefng s\u1ea3n ph\u1ea9m c\u1ee7a c\u00f4ng ty v\u00e0 h\u1ecdc th\u00eam c\u00e1i m\u1edbi.<\/li>\n<\/ul>\n<p>M\u00ecnh b\u1eaft \u0111\u1ea7u th\u1eed nghi\u1ec7m v\u00e0 th\u1ef1c hi\u1ec7n demo v\u1edbi th\u01b0 vi\u1ec7n <a href=\"https:\/\/www.npmjs.com\/package\/grpc\" rel=\"nofollow noopener\" target=\"_blank\">grpc d\u00e0nh cho nodejs<\/a>, xong xui m\u00ecnh tr\u00ecnh b\u00e0y v\u1edbi s\u1ebfp v\u00e0 anh s\u1ebfp r\u1ea5t hoan ngh\u00eanh, nh\u01b0ng v\u1eabn nh\u1eafc nh\u1edf v\u1ec1 nh\u1eefng v\u1ea5n \u0111\u1ec1 c\u00f3 th\u1ec3 g\u1eb7p ph\u1ea3i v\u00e0 khuy\u00ean l\u00e0 n\u00ean c\u00e2n nh\u1eafc k\u0129 tr\u01b0\u1edbc khi tri\u1ec3n khai.<\/p>\n<blockquote>\n<p>D\u1ea1, \u0111\u1ec3 em xem x\u00e9t nh\u01b0ng m\u00e0 c\u00e1i n\u00e0y em c\u0169ng test r\u1ed3i, n\u00f3 ch\u1ea1y ngon nh\u01b0 demo anh th\u1ea5y \u0111\u1ea5y<\/p>\n<\/blockquote>\n<p>\u00c0, l\u00fac n\u00e0y m\u00ecnh c\u0169ng c\u00f3 l\u00e0m vi\u1ec7c v\u1edbi m\u1ed9t b\u1ea1n dev kh\u00f4ng cao nh\u01b0ng m\u00e0 th\u00e2n thi\u1ec7n v\u00e0 vui v\u1ebb ^^. Sau khi tr\u00ecnh b\u00e0y v\u1edbi s\u1ebfp xong, m\u00ecnh c\u00f3 trao \u0111\u1ed5i th\u00eam v\u1edbi b\u1ea1n n\u00e0y, 2 ng\u01b0\u1eddi th\u1ed1ng nh\u1ea5t v\u00e0 lao v\u00e0o <strong>thay th\u1ebf to\u00e0n b\u1ed9 REST API th\u00e0nh gRPC<\/strong> (Ch\u1ed7 n\u00e0y in \u0111\u1eadm n\u00e8, cu\u1ed1i b\u00e0i s\u1ebd hi\u1ec3u), d\u00f9ng th\u01b0 vi\u1ec7n <a href=\"https:\/\/www.npmjs.com\/package\/grpc\" rel=\"nofollow noopener\" target=\"_blank\">grpc d\u00e0nh cho nodejs<\/a>.<\/p>\n<h3>K\u1ebft qu\u1ea3<\/h3>\n<p>Sau h\u01a1n 1 tu\u1ea7n tri\u1ec3n khai th\u00ec m\u1ecdi th\u1ee9 \u0111\u00e3 xong, kh\u00e1 nhanh ph\u1ea3i kh\u00f4ng m\u1ed9t ph\u1ea7n l\u00e0 v\u00ec m\u00ecnh c\u00f3 t\u1ef1 tri\u1ec3n khai m\u1ed9t package (npm) \u0111\u1ec3 h\u1ed7 tr\u1ee3 v\u1ec1 vi\u1ec7c khai b\u00e1o c\u00e1c file <code>proto<\/code> \u0111\u00f3 l\u00e0 <a href=\"https:\/\/www.npmjs.com\/package\/@zerocore\/grpc-helper\" rel=\"nofollow noopener\" target=\"_blank\">@zerocore\/grpc-helper<\/a>. N\u1ebfu c\u00e1c b\u1ea1n c\u00f3 t\u00ecm hi\u1ec3u ho\u1eb7c \u0111\u00e3 d\u00f9ng th\u00ec \u0111\u1ed1i v\u1edbi \u1ee9ng d\u1ee5ng c\u00f3 nhi\u1ec1u model hay entity ph\u1ee9c t\u1ea1p th\u00ec vi\u1ec7c vi\u1ebft file <code>proto<\/code> kh\u00e1 l\u00e0 m\u1ea5t th\u1eddi gian v\u00e0 &quot;r\u01b0\u1eddng r\u00e0&quot; m\u00ecnh ngh\u0129 l\u00e0 v\u1eady, do \u0111\u00f3 th\u01b0 vi\u1ec7n tr\u00ean gi\u00fap t\u1ed1i gi\u1ea3n hay \u0111\u01a1n gi\u1ea3n vi\u1ec7c khai b\u00e1o, c\u1ee5 th\u1ec3 b\u1ea1n t\u00ecm hi\u1ec3u th\u00eam nh\u00e9.<\/p>\n<p>Gi\u1edd quay l\u1ea1i ch\u1ee7 \u0111\u1ec1 ch\u00ednh, sau khi tri\u1ec3n khai tr\u00ean m\u00f4i tr\u01b0\u1eddng production, m\u1ecdi th\u1ee9 ho\u1ea1t \u0111\u1ed9ng kh\u00e1 \u1ed5n.<\/p>\n<blockquote>\n<p>G\u00e1c ch\u00e2n l\u00ean b\u00e0n, \u0103n mi\u1ebfng b\u00e1nh, u\u1ed1ng mi\u1ebfng tr\u00e0 t\u1eadn h\u01b0\u1edfng cu\u1ed9c s\u1ed1ng.<\/p>\n<\/blockquote>\n<h3>V\u1ea5n \u0111\u1ec1 ph\u00e1t sinh<\/h3>\n<p>B\u1ed5ng m\u1ed9t ng\u00e0y, m\u1ed9t th\u00e0nh vi\u00ean trong \u0111\u1ed9i h\u1ed7 tr\u1ee3 kh\u00e1ch h\u00e0ng ph\u1ea3n h\u1ed3i r\u1eb1ng kh\u00e1ch h\u00e0ng kh\u00f4ng \u0111\u0103ng nh\u1eadp \u0111\u01b0\u1ee3c.<\/p>\n<blockquote>\n<p>Qu\u1ea7y, qu\u1ea7y chuy\u1ec7n g\u00ec \u0111\u00e2y?<\/p>\n<\/blockquote>\n<p>\u0110\u1eddi kh\u00f4ng nh\u01b0 l\u00e0 m\u01a1, sau m\u1ed9t th\u1eddi gian ho\u1ea1t \u0111\u1ed9ng, \u0111\u00e2u \u0111\u00f3 kho\u1ea3ng g\u1ea7n 1 th\u00e1ng th\u00ec m\u1ed9t s\u1ed1 v\u1ea5n \u0111\u1ec1 b\u1eaft \u0111\u1ea7u ph\u00e1t sinh, sau khi nh\u1eadn \u0111\u01b0\u1ee3c ph\u1ea3n h\u1ed3i v\u00e0 b\u1eaft \u0111\u1ea7u \u0111i\u1ec1u tra th\u00ec ph\u00e1t hi\u1ec7n h\u1ec7 th\u1ed1ng g\u1eb7p l\u1ed7i <code>14 UNAVAILABLE: read ECONNRESET<\/code> do th\u01b0 vi\u1ec7n grpc ghi nh\u1eadn.<\/p>\n<blockquote>\n<p>Chuy\u1ec7n g\u00ec v\u1eady? C\u00f3 th\u1ec3 n\u00f3 ch\u1ec9 x\u1ea3y ra c\u1ee5c b\u1ed9 trong 1 services m\u00e0 th\u00f4i.<\/p>\n<\/blockquote>\n<p>Ch\u01b0a d\u1ee9t suy ngh\u0129, m\u1ed9t \u0111\u1ed3ng \u0111\u1ed9i l\u00ean ti\u1ebfng, anh \u01a1i sao m\u1ea5y c\u00e1i service kh\u00e1c n\u00f3 c\u0169ng b\u00e1o l\u1ed7i \u00fd v\u1eady n\u00e8 r\u1ed3i 1,2,5,7&#8230; t\u1ea5t c\u1ea3 c\u00e1c service \u0111\u1ec1u g\u1eb7p l\u1ed7i t\u01b0\u01a1ng t\u1ef1. Trong t\u00ecnh th\u1ebf c\u1ea5p b\u00e1ch c\u1ea7n kh\u00f4i ph\u1ee5c h\u1ec7 th\u1ed1ng c\u00e0ng nhanh c\u00e0ng t\u1ed1t, quy\u1ebft \u0111\u1ecbnh \u0111\u01b0\u1ee3c \u0111\u01b0a ra ngay l\u1eadp t\u1ee9c b\u1eb1ng m\u1ed9t ph\u01b0\u01a1ng ph\u00e1p m\u00e0 ai c\u0169ng bi\u1ebft l\u00e0 ph\u01b0\u01a1ng ph\u00e1p g\u00ec v\u1edbi hi\u1ec3u qu\u1ea3 \u0111\u1ea1t 96,69%&#8230; C\u00e1c b\u1ea1n \u0111o\u00e1n \u0111\u00fang r\u1ed3i \u0111\u1ea5y, \u0111\u00f3 l\u00e0 t\u1eaft m\u00e1y kh\u1edfi \u0111\u1ed9ng l\u1ea1i, c\u0169ng may l\u00e0 h\u1ec7 th\u1ed1ng \u0111\u01b0\u1ee3c tri\u1ec3n khai v\u1edbi <a href=\"https:\/\/docs.docker.com\/engine\/swarm\/\" rel=\"nofollow noopener\" target=\"_blank\">docker swarm<\/a> do \u0111\u00f3 vi\u1ec7c kh\u1edfi \u0111\u1ed9ng\/restart kh\u00e1 \u0111\u01a1n gi\u1ea3n v\u00e0 nhanh ch\u00f3ng, c\u00e0ng d\u1ec5 d\u00e0ng h\u01a1n khi s\u1eed d\u1ee5ng <a href=\"https:\/\/www.portainer.io\/\" rel=\"nofollow noopener\" target=\"_blank\">portainer<\/a>. V\u00e0 k\u1ebft qu\u1ea3 \u0111\u00fang nh\u01b0 mong \u0111\u1ee3i m\u1ecdi th\u1ee9 \u0111\u00e3 ho\u1ea1t \u0111\u1ed9ng tr\u1edf l\u1ea1i b\u00ecnh th\u01b0\u1eddng.<\/p>\n<p>Sau \u0111\u1ea5y m\u00ecnh v\u1eabn ti\u1ebfp t\u1ee5c theo d\u00f5i h\u1ec7 th\u1ed1ng, l\u1ed7i tr\u00ean v\u1eabn x\u1ea3y ra, sau t\u00ecm hi\u1ec3u th\u00eam, \u0111\u00e3 \u0111\u01b0a ra m\u1ed9t s\u1ed1 gi\u1ea3 thi\u1ebft cho nguy\u00ean nh\u00e2n g\u00e2y ra l\u1ed7i \u0111\u01b0\u1ee3c li\u1ec7t k\u00ea b\u00ean d\u01b0\u1edbi, nh\u01b0ng tr\u01b0\u1edbc h\u1ebft c\u1ea7n l\u01b0\u1edbt qua m\u1ed9t s\u1ed1 th\u00f4ng tin kh\u00e1c v\u1ec1 b\u1ed1i c\u1ea3nh l\u00fac n\u00e0y.<\/p>\n<p><strong>T\u00ecnh tr\u1ea1ng<\/strong><\/p>\n<ul>\n<li>L\u00fac n\u00e0y h\u1ec7 th\u1ed1ng \u0111ang \u0111\u01b0\u1ee3c tri\u1ec3n khai tr\u00ean <a href=\"https:\/\/www.digitalocean.com\/\" rel=\"nofollow noopener\" target=\"_blank\">Digital Ocean<\/a><\/li>\n<li>Do kinh ph\u00ed h\u1ea1n h\u1eb9p n\u00ean s\u1eed d\u1ee5ng t\u00e0i nguy\u00ean kh\u00e1 ti\u1ebft ki\u1ec7m<\/li>\n<li>Digital Ocean l\u00e2u l\u00e2u c\u0169ng hay c\u00f3 th\u00f4ng b\u00e1o v\u1ec1 l\u1ed7i network<\/li>\n<li>Th\u01b0 vi\u1ec7n grpc danh cho nodejs c\u0169ng c\u00f2n kh\u00e1 m\u1edbi m\u1eb7c d\u00f9 c\u00f4ng ngh\u1ec7 gRPC do Google gi\u1edbi thi\u1ec7u c\u0169ng kh\u00e1 l\u00e2u r\u1ed3i.<\/li>\n<\/ul>\n<p><strong>Gi\u1ea3 thi\u1ebft<\/strong><\/p>\n<ul>\n<li>V\u00ec thi\u1ebfu t\u00e0i nguy\u00ean, \u1edf \u0111\u00e2y c\u00f3 th\u1ec3 l\u00e0 do thi\u1ebfu RAM n\u00ean \u1ea3nh h\u01b0\u1edfng t\u1edbi network d\u1eabn t\u1edbi l\u1ed7i tr\u00ean.<\/li>\n<li>C\u0169ng c\u00f3 th\u1ec3 do l\u1ed7i c\u1ee7a nh\u00e0 cung c\u1ea5p v\u1ec1 network.<\/li>\n<li>L\u1ed7i th\u01b0 vi\u1ec7n.<\/li>\n<li>L\u1ed7i code c\u1ee7a developer.<\/li>\n<\/ul>\n<p>Trong th\u1eddi gian n\u00e0y m\u00ecnh c\u0169ng t\u1ef1 c\u1ea3i thi\u1ec7n t\u00ecnh h\u00ecnh b\u1eb1ng c\u00e1ch t\u1ef1 tri\u1ec3n khai code x\u1eed l\u00fd retry. R\u1ed3i m\u1ed9t ng\u00e0y \u0111\u1eb9p tr\u1eddi khi ki\u1ec3m tra th\u01b0 vi\u1ec7n grpc tr\u00ean th\u00ec nh\u1eadn \u0111\u01b0\u1ee3c th\u00f4ng tin nh\u01b0 c\u00e1c b\u1ea1n th\u1ea5y \u1edf h\u00ecnh ph\u00eda d\u01b0\u1edbi<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/egany.cdn.vccloud.vn\/blogs\/wp-content\/uploads\/2021\/12\/recomend.png\" alt=\"\" \/><\/p>\n<p>Trong phi\u00ean b\u1ea3n m\u1edbi c\u00f3 c\u00e1c t\u00ednh n\u0103ng \u0111\u00e1ng ch\u00fa \u00fd nh\u01b0 c\u00e1c b\u1ea1n c\u00f3 th\u1ec3 th\u1ea5y \u1edf h\u00ecnh d\u01b0\u1edbi<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/egany.cdn.vccloud.vn\/blogs\/wp-content\/uploads\/2021\/12\/photo_2020-04-27_16-59-04.jpg\" alt=\"\" \/><\/p>\n<p><a href=\"https:\/\/www.npmjs.com\/package\/@grpc\/grpc-js\" rel=\"nofollow noopener\" target=\"_blank\">npm th\u01b0 vi\u1ec7n m\u1edbi \u1edf \u0111\u00e2y<\/a><\/p>\n<p>M\u00ecnh c\u0169ng \u0111\u00e3 chuy\u1ec3n \u0111\u1ed5i sang th\u01b0 vi\u1ec7n m\u1edbi nh\u01b0ng t\u00ecnh h\u00ecnh v\u1eabn kh\u00f4ng \u0111\u01b0\u1ee3c c\u1ea3i thi\u1ec7n. M\u1ed9t th\u00e1ng sau m\u00ecnh quy\u1ebft \u0111\u1ecbnh quay tr\u1edf l\u1ea1i ph\u01b0\u01a1ng th\u1ee9c giao ti\u1ebfp c\u0169 (REST API), c\u1ed5 \u0111i\u1ec3n nh\u01b0ng l\u1ea1i \u1ed5n \u0111\u1ecbnh.<\/p>\n<h2>Quay l\u1ea1i v\u1edbi REST API<\/h2>\n<p>V\u00ec tr\u01b0\u1edbc kia, khi chuy\u1ec3n sang d\u00f9ng th\u01b0 vi\u1ec7n grpc d\u00e0nh cho nodejs m\u00ecnh kh\u00f4ng c\u00f3 d\u1ef1 ph\u00f2ng vi\u1ec7c s\u1ebd quay l\u1ea1i REST API, v\u00ec v\u1eady vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i n\u00e0y kh\u00e1 m\u1ea5t th\u1eddi gian, m\u1ed9t ph\u1ea7n v\u00ec c\u00e1c t\u00ednh n\u0103ng c\u0169 \u0111ang ch\u1ea1y, t\u00ednh n\u0103ng m\u1edbi \u0111\u01b0\u1ee3c ph\u00e1t tri\u1ec3n s\u1eafp \u0111\u01b0\u1ee3c ra m\u1eaft. Nh\u01b0ng v\u1edbi s\u1ef1 n\u1ed7 l\u1ef1c th\u00ec c\u0169ng \u0111\u00e3 ho\u00e0n th\u00e0nh vi\u1ec7c chuy\u1ec3n \u0111\u1ed5i. V\u1edbi REST API h\u1ec7 th\u1ed1ng ho\u1ea1t \u0111\u1ed9ng \u1ed5n \u0111\u1ecbnh cho t\u1edbi th\u1eddi \u0111i\u1ec3m hi\u1ec7n t\u1ea1i.<\/p>\n<p>Tuy nhi\u00ean v\u1edbi REST API m\u00e0 d\u00f9ng giao ti\u1ebfp n\u1ed9i b\u1ed9 th\u00ec vi\u1ec7c \u0111\u1ecbnh ngh\u0129a c\u00e1c endpoint, r\u1ed3i vi\u1ebft t\u00e0i li\u1ec7u, m\u1ed7i khi c\u00f3 thay \u0111\u1ed5i th\u00ec c\u1eadp nh\u1eadt \u1edf c\u00e1c service li\u00ean quan kh\u00e1 l\u00e0 t\u1ed1n th\u1eddi gian v\u00e0 c\u00f4ng s\u1ee9c, v\u00ec v\u1eady m\u00ecnh lu\u00f4n t\u00ecm ki\u1ebfm th\u00eam c\u00e1c c\u00e1ch th\u1ee9c kh\u00e1c t\u1ed1t h\u01a1n. \u00c0, \u1edf \u0111\u00e2y m\u00ecnh kh\u00f4ng \u0111\u1ec1 c\u1eadp t\u1edbi thi\u1ebft k\u1ebf theo h\u01b0\u1edbng s\u1ef1 ki\u1ec7n nh\u01b0 pub\/sub nha ^^, c\u00e1i n\u00e0y c\u00f3 th\u1ec3 m\u00ecnh s\u1ebd n\u00f3i \u1edf c\u00e1c b\u00e0i ti\u1ebfp theo. R\u1ed3i m\u1ed9t ng\u00e0y s\u1ebfp g\u1ecdi t\u00ean, \u00ea t\u1edbi ch\u1ec9 cho c\u00e1i n\u00e0y hay l\u1eafm n\u00e8.<\/p>\n<h2>Moleculer &#8211; Transporter<\/h2>\n<p><a href=\"https:\/\/moleculer.services\/\" rel=\"nofollow noopener\" target=\"_blank\">Moleculer Service<\/a>, anh s\u1ebfp m\u1eb7c d\u00f9 b\u1eadn nhi\u1ec1u vi\u1ec7c nh\u01b0ng v\u1eabn hay t\u00ecm \u0111\u01b0\u1ee3c m\u1ea5y th\u1ee9 kh\u00e1 l\u00e0 m\u1edbi v\u00e0 hay h\u1ecd &#8211; n\u1ec3 th\u1eadt s\u1ef1, v\u1ec1 framework tr\u00ean ai c\u00f3 h\u1ee9ng th\u00fa th\u00ec click v\u00e0o link \u1edf \u0111\u1ea7u \u0111o\u1ea1n n\u00e0y \u0111\u1ec3 xem nh\u00e9. R\u1ed3i quay l\u1ea1i c\u00e2u chuy\u1ec7n, t\u1ea1i th\u1eddi \u0111i\u1ec3m n\u00e0y moleculer v\u1eabn ch\u01b0a c\u00f3 b\u1ea3n ph\u00e1t h\u00e0nh ch\u00ednh th\u1ee9c v\u00e0 khi xem qua th\u00ec c\u0169ng ch\u01b0a h\u1ed7 tr\u1ee3 t\u1ed1t Typescript, m\u00e0 Typescript l\u00e0 m\u1ee5c ti\u00eau h\u01b0\u1edbng t\u1edbi ti\u1ebfp theo c\u1ee7a m\u00ecnh, n\u00ean l\u00fac \u0111\u1ea5y c\u0169ng ch\u01b0a c\u00f3 quy\u1ebft \u0111\u1ecbnh d\u00f9ng.<\/p>\n<p>Quay l\u1ea1i th\u1eddi \u0111i\u1ec3m hi\u1ec7n t\u1ea1i, cu\u1ed1i n\u0103m c\u00f4ng vi\u1ec7c c\u0169ng b\u1eadn nh\u01b0ng ch\u1ee7 y\u1ebfu l\u00e0 ho\u00e0n th\u00e0nh nh\u1eefng g\u00ec ch\u01b0a l\u00e0m xong ch\u1ee9 c\u0169ng kh\u00f4ng ph\u00e1t tri\u1ec3n m\u1edbi n\u1eefa, v\u00ec v\u1eady m\u00ecnh m\u1edbi c\u00f3 th\u1eddi gian ngh\u0129 v\u1ec1 vi\u1ec7c c\u1ea3i thi\u1ec7n h\u1ec7 th\u1ed1ng, n\u00ean c\u00f3 xem l\u1ea1i moleculer v\u00e0 th\u1ea5y n\u00f3 \u0111\u00e3 h\u1ed7 tr\u1ee3 Typescript c\u0169ng kh\u00e1 \u1ed5n v\u00e0 h\u01a1n n\u1eefa moleculer c\u00f3 tri\u1ec3n khai transporter kh\u00e1 linh ho\u1ea1t, xem h\u00ecnh d\u01b0\u1edbi \u0111\u1ec3 d\u1ec5 h\u00ecnh dung.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/moleculer.services\/docs\/0.14\/assets\/networking.svg\" alt=\"moleculer_transporter\" \/><\/p>\n<p><a href=\"https:\/\/moleculer.services\/docs\/0.14\/networking.html#Transporters\" rel=\"nofollow noopener\" target=\"_blank\">Xem chi ti\u1ebft<\/a><\/p>\n<p>Ngo\u00e0i ra c\u00f2n c\u00f3 c\u00e1c c\u01a1 ch\u1ebf li\u00ean quan t\u1edbi event<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/moleculer.services\/docs\/0.14\/assets\/balanced-events.gif\" alt=\"moleculer_event_1\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/moleculer.services\/docs\/0.14\/assets\/broadcast-events.gif\" alt=\"moleculer_event_2\" \/><\/p>\n<p>Nh\u1eefng \u01b0u \u0111i\u1ec3m d\u1ec5 nh\u1eadn th\u1ea5y g\u1ed3m<\/p>\n<ul>\n<li>\u0110\u01b0\u1ee3c h\u1ed7 tr\u1ee3 s\u1eb5n\/built-in.<\/li>\n<li>D\u1ec5 t\u00edch h\u1ee3p v\u1edbi nhi\u1ec1u transporter kh\u00e1c nhau nh\u01b0 redis, kafka, rabbitmq&#8230;<\/li>\n<li>C\u00e1c node s\u1ebd t\u1ef1 \u0111\u1ed9ng register khi ch\u1ea1y.<\/li>\n<li>Giao ti\u1ebfp gi\u1eefa c\u00e1c service kh\u00e1 \u0111\u01a1n gi\u1ea3n khi code.<\/li>\n<li>V\u00e0 c\u00e1c \u01b0u \u0111i\u1ec3m kh\u00e1c.<\/li>\n<\/ul>\n<p>Hi\u1ec7n t\u1ea1i m\u00ecnh \u0111ang refactor c\u00e1c d\u1ef1 \u00e1n c\u0169 sang moleculer gi\u00fap h\u1ec7 th\u1ed1ng ho\u1ea1t \u0111\u1ed9ng \u0111\u01b0\u1ee3c t\u1ed1t h\u01a1n, ph\u00e1t tri\u1ec3n trong nh\u1eefng n\u0103m ti\u1ebfp theo \u0111\u01b0\u1ee3c d\u1ec5 d\u00e0ng h\u01a1n, nhanh h\u01a1n hy v\u1ecdng m\u1ecdi vi\u1ec7c \u0111\u00fang nh\u01b0 k\u1ef3 v\u1ecdng ^^.<\/p>\n<h2>M\u1ed9t s\u1ed1 b\u00e0i h\u1ecdc r\u00fat ra<\/h2>\n<p>Qua nh\u1eefng g\u00ec \u0111\u00e3 tr\u1ea3i qua, m\u00ecnh r\u00fat ra \u0111\u01b0\u1ee3c m\u1ed9t s\u1ed1 b\u00e0i h\u1ecdc sau:<\/p>\n<ul>\n<li>Nh\u1eefng g\u00ec m\u1edbi, \u0111\u01b0\u1ee3c nhi\u1ec1u \u0111\u00e1nh gi\u00e1 t\u1ed1t th\u00ec ch\u01b0a ch\u1eafc l\u00e0 ph\u00f9 h\u1ee3p v\u1edbi m\u00ecnh.<\/li>\n<li>Thay \u0111\u1ed5i g\u00ec \u0111\u1ea5y \u0111ang ch\u1ea1y th\u00ec hay n\u00ean thay \u0111\u1ed5i t\u1eeb t\u1eeb v\u00e0 ch\u1ea1y th\u1eed trong th\u1eddi gian \u0111\u1ee7 \u0111\u1ec3 \u0111\u00e1nh gi\u00e1. V\u00ec c\u00f3 th\u1ec3 ch\u1ea1y trong 1, 2 th\u00e1ng \u0111\u1ea7u kh\u00f4ng sao nh\u01b0ng n\u00f3 l\u1ea1i c\u00f3 v\u1ea5n \u0111\u1ec1 \u1edf th\u00e1ng th\u1ee9 3 ^^. \u00c0 m\u00e0 c\u00f3 nh\u1edb ch\u1ed7 in \u0111\u1eadm \u1edf tr\u00ean kh\u00f4ng nh\u1ec9? ^^.<\/li>\n<li>H\u00e3y c\u00f3 k\u1ebf ho\u1ea1ch cho tr\u01b0\u1eddng h\u1ee3p rollback.<\/li>\n<li>Nh\u1eefng c\u00e1i c\u0169 hay l\u00e2u \u0111\u1eddi th\u01b0\u1eddng s\u1ebd \u1ed5n \u0111\u1ecbnh.<\/li>\n<li>N\u1ebfu nh\u1eefng g\u00ec \u1edf hi\u1ec7n t\u1ea1i g\u00e2y kh\u00f3 hay c\u1ea3m th\u1ea5y ch\u01b0a h\u00e0i l\u00f2ng th\u00ec \u0111\u1eebng ng\u1ea7n ng\u1ea1i t\u00ecm ki\u1ebfm c\u00e1i m\u1edbi.<\/li>\n<\/ul>\n<p>B\u00e0i vi\u1ebft t\u1edbi \u0111\u00e2y l\u00e0 h\u1ebft r\u1ed3i, c\u1ea3m \u01a1n c\u00e1c b\u1ea1n \u0111\u00e3 theo d\u00f5i, ch\u00fac m\u1ed9t ng\u00e0y t\u1ed1t l\u00e0nh v\u00e0 h\u1eb9n g\u1eb7p l\u1ea1i trong c\u00e1c b\u00e0i ti\u1ebfp theo. Ngo\u00e0i ra b\u1ea1n c\u00f3 th\u1ec3 \u0111\u1ecdc th\u00eam c\u00e1c b\u00e0i vi\u1ebft trong lo\u1ea1t b\u00e0i v\u1ec1 tr\u1ea3i nghi\u1ec7m ph\u00e1t tri\u1ec3n h\u1ec7 th\u1ed1ng microservices t\u1ea1i EGANY \u1edf d\u01b0\u1edbi \u0111\u00e2y:<\/p>\n<ul>\n<li><a href=\"https:\/\/egany.com\/blogs\/microsevices-1-su-lua-chon\/\">Microsevices #1 \u2013 S\u1ef1 l\u1ef1a ch\u1ecdn<\/a><\/li>\n<li><a href=\"https:\/\/egany.com\/blogs\/microsevices-2-trien-khai-microservices-tinh-gon-de-bat-dau-de-dang-hon\/\">Microsevices #2 \u2013 Tri\u1ec3n khai microservices \u201ctinh g\u1ecdn\u201d \u0111\u1ec3 b\u1eaft \u0111\u1ea7u d\u1ec5 d\u00e0ng h\u01a1n<\/a><\/li>\n<\/ul>\n<h2>Tham kh\u1ea3o<\/h2>\n<p>N\u1ed9i dung, h\u00ecnh \u1ea3nh s\u1eed d\u1ee5ng trong b\u00e0i vi\u1ec7t \u0111\u01b0\u1ee3c tham kh\u1ea3o v\u00e0 l\u1ea5y t\u1eeb c\u00e1c ngu\u1ed3n d\u01b0\u1edbi \u0111\u00e2y:<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/architecture\/microservices\/architect-microservice-container-applications\/media\/communication-in-microservice-architecture\/sync-vs-async-patterns-across-microservices.png\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/architecture\/microservices\/architect-microservice-container-applications\/media\/communication-in-microservice-architecture\/sync-vs-async-patterns-across-microservices.png<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/architecture\/microservices\/architect-microservice-container-applications\/communication-in-microservice-architecture\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.microsoft.com\/en-us\/dotnet\/architecture\/microservices\/architect-microservice-container-applications\/communication-in-microservice-architecture<\/a><\/li>\n<li><a href=\"https:\/\/microservices.io\/i\/Microservice_Architecture.png\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/microservices.io\/i\/Microservice_Architecture.png<\/a><\/li>\n<li><a href=\"https:\/\/microservices.io\/patterns\/microservices.html\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/microservices.io\/patterns\/microservices.html<\/a><\/li>\n<li><a href=\"https:\/\/microservices.io\/i\/PatternsRelatedToMicroservices.jpg\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/microservices.io\/i\/PatternsRelatedToMicroservices.jpg<\/a><\/li>\n<li><a href=\"https:\/\/microservices.io\/patterns\/microservices.html\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/microservices.io\/patterns\/microservices.html<\/a><\/li>\n<li><a href=\"https:\/\/www.nginx.com\/wp-content\/uploads\/2016\/04\/Richardson-microservices-part1-2_microservices-architecture.png\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.nginx.com\/wp-content\/uploads\/2016\/04\/Richardson-microservices-part1-2_microservices-architecture.png<\/a><\/li>\n<li><a href=\"https:\/\/www.nginx.com\/blog\/introduction-to-microservices\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.nginx.com\/blog\/introduction-to-microservices\/<\/a><\/li>\n<li><a href=\"https:\/\/grpc.io\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/grpc.io\/<\/a><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/grpc\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.npmjs.com\/package\/grpc<\/a><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/@zerocore\/grpc-helper\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.npmjs.com\/package\/@zerocore\/grpc-helper<\/a><\/li>\n<li><a href=\"https:\/\/docs.docker.com\/engine\/swarm\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/docs.docker.com\/engine\/swarm\/<\/a><\/li>\n<li><a href=\"https:\/\/www.portainer.io\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.portainer.io\/<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.digitalocean.com\/<\/a><\/li>\n<li><a href=\"https:\/\/www.npmjs.com\/package\/@grpc\/grpc-js\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/www.npmjs.com\/package\/@grpc\/grpc-js<\/a><\/li>\n<li><a href=\"https:\/\/moleculer.services\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/moleculer.services\/<\/a><\/li>\n<li><a href=\"https:\/\/moleculer.services\/docs\/0.14\/assets\/networking.svg\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/moleculer.services\/docs\/0.14\/assets\/networking.svg<\/a><\/li>\n<li><a href=\"https:\/\/moleculer.services\/docs\/0.14\/networking.html#Transporters\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/moleculer.services\/docs\/0.14\/networking.html#Transporters<\/a><\/li>\n<li><a href=\"https:\/\/moleculer.services\/docs\/0.14\/assets\/balanced-events.gif\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/moleculer.services\/docs\/0.14\/assets\/balanced-events.gif<\/a><\/li>\n<li><a href=\"https:\/\/moleculer.services\/docs\/0.14\/assets\/broadcast-events.gif\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/moleculer.services\/docs\/0.14\/assets\/broadcast-events.gif<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Xin ch\u00e0o! Ch\u00fang ta l\u1ea1i quay tr\u1edf l\u1ea1i v\u1edbi lo\u1ea1t b\u00e0i v\u1ec1 tr\u1ea3i nghi\u1ec7m c\u1ee7a m\u00ecnh trong vi\u1ec7c x\u00e2y d\u1ef1ng h\u1ec7 th\u1ed1ng microservices t\u1ea1i EGANY, trong s\u1ed1 tr\u01b0\u1edbc \u2013 Microsevices #2 \u2013 Tri\u1ec3n khai microservices \u201ctinh g\u1ecdn\u201d \u0111\u1ec3 b\u1eaft \u0111\u1ea7u d\u1ec5 d\u00e0ng h\u01a1n \u0111\u00e3 c\u00f3 \u0111\u1ec1 c\u1eadp t\u1edbi m\u1ed9t s\u1ed1 y\u1ebfu t\u1ed1 c\u1ea7n \u0111\u1ec3 x\u00e2y d\u1ef1ng microservices &quot;tinh gon&quot;, h\u00f4m nay m\u00ecnh s\u1ebd chia s\u1ebb v\u1edbi m\u1ecdi ng\u01b0\u1eddi v\u1ec1 v\u1ea5n \u0111\u1ec1 trong b\u00e0i vi\u1ebft tr\u00ean c\u00f3 \u0111\u1ec1 c\u1eadp \u0111\u00f3 l\u00e0 service communication. B\u00e0i vi\u1ebft n\u00e0y s\u1ebd n\u00f3i v\u1ec1 qu\u00e1 tr\u00ecnh m\u00ecnh b\u1eaft \u0111\u1ea7u tri\u1ec3n khai, thay \u0111\u1ed5i khi t\u00ecm hi\u1ec3u<\/p>\n","protected":false},"author":4,"featured_media":1064,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27,54],"tags":[53,49,50,73,79,43,83],"_links":{"self":[{"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/posts\/1055"}],"collection":[{"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/comments?post=1055"}],"version-history":[{"count":0,"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/posts\/1055\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/media\/1064"}],"wp:attachment":[{"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/media?parent=1055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/categories?post=1055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egany.com\/blogs\/wp-json\/wp\/v2\/tags?post=1055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}