Dalam beberapa tahun kebelakangan ini, seni bina perkhidmatan mikro telah menjadi pilihan popular untuk membina sistem berskala, fleksibel dan boleh diselenggara. Dengan membahagikan aplikasi kepada perkhidmatan yang lebih kecil dan bebas, adalah mungkin untuk mengekalkan, menguji dan menaik taraf setiap perkhidmatan secara autonomi, memudahkan skalabiliti dan kemasukan teknologi baharu.
Dalam artikel ini, kami akan meneroka penciptaan seni bina perkhidmatan mikro menggunakan Go dan NodeJS, dua bahasa yang digunakan secara meluas dan, dalam konteks ini, dengan ciri pelengkap. Selain itu, kami akan menggunakan prinsip Seni Bina Bersih, pendekatan reka bentuk yang bertujuan untuk memastikan kod bersih, modular dan mudah diselenggara serta diuji, memastikan logik perniagaan diasingkan daripada kebimbangan infrastruktur dan/atau kebergantungan.
Objektif projek ini adalah untuk mempraktikkan Go, bahasa yang saya pelajari baru-baru ini, dan menyemak semula konsep asas perkhidmatan mikro. Pada masa yang sama, kami akan menggunakan TypeScript dalam pembangunan perkhidmatan, menggunakan prinsip Seni Bina Bersih untuk mengukuhkan amalan reka bentuk perisian yang baik.
Berdasarkan premis ini, kami akan berpeluang meneroka kedua-dua perkara positif dan cabaran pendekatan ini. Lagipun, bukan setiap perniagaan memerlukan struktur yang begitu kompleks, dan projek praktikal adalah cara terbaik untuk memahami keperluan dan implikasinya yang sebenar.
Seni bina Microservices membahagikan aplikasi kepada perkhidmatan yang lebih kecil dan bebas, masing-masing bertanggungjawab untuk bahagian tertentu fungsi. Perkhidmatan ini berkomunikasi melalui API yang jelas, yang memudahkan penyelenggaraan, skalabilitas dan penggunaan teknologi baharu.
Kebaikan:
Modulariti: Memudahkan penyelenggaraan dan pembangunan bebas setiap perkhidmatan.
Kebolehskalaan: Membolehkan kebolehskalaan individu bagi setiap perkhidmatan mengikut permintaan.
Ketahanan: Mengasingkan kegagalan dan mengurangkan kesan masalah dalam satu perkhidmatan kepada yang lain.
Perbandingan dengan Monolith:
Monoliths: Aplikasi disepadukan ke dalam satu pangkalan kod. Walaupun mudah pada mulanya, ia boleh menjadi sukar untuk dikekalkan dan diskalakan dari semasa ke semasa.
Perkhidmatan mikro: Mereka menawarkan fleksibiliti dan skalabiliti yang lebih besar, tetapi boleh mewujudkan kerumitan tambahan dalam pengurusan dan komunikasi antara perkhidmatan.
Dalam seni bina perkhidmatan mikro, komunikasi antara perkhidmatan boleh dilakukan dalam dua cara utama: komunikasi tak segerak, menggunakan baris gilir mesej untuk contoh dan komunikasi segerak, melalui API REST. Perlu diketengahkan bahawa terdapat bentuk komunikasi lain selain beratur dan rehat.
Baris gilir mesej digunakan untuk membolehkan komunikasi tak segerak antara perkhidmatan mikro. Mereka membenarkan perkhidmatan menghantar dan menerima mesej tanpa memerlukan respons segera, yang membantu meningkatkan daya tahan dan kebolehskalaan sistem.
Peranan Barisan Mesej:
Komunikasi Asynchronous: Memudahkan pertukaran maklumat antara perkhidmatan tanpa memerlukan respons segera.
Ketahanan: Menguruskan lonjakan beban dan kegagalan sementara, memastikan mesej akhirnya diproses.
Perlaksanaan:
Alat: RabbitMQ dan Kafka ialah pilihan popular untuk menguruskan baris gilir mesej.
Penyepaduan: Laksanakan baris gilir mesej untuk komunikasi antara perkhidmatan yang ditulis dalam Go dan NodeJS, memastikan pertukaran data yang cekap dan berskala.
API RESTful digunakan untuk komunikasi segerak antara perkhidmatan. Ia adalah berdasarkan prinsip HTTP dan membenarkan perkhidmatan berinteraksi dengan cara yang standard dan cekap.
Clean Architecture ialah pendekatan reka bentuk yang bertujuan untuk mencipta sistem dengan asas kod yang teratur yang mudah diselenggara dan/atau diuji. Ia menekankan pengasingan kebimbangan dan kebebasan lapisan.
Prinsip Seni Bina Bersih:
Layer Separation: Divide code into distinct layers (domain, application, infrastructure) to isolate business logic from technical concerns.
Independence from Frameworks and Libraries: Ensure that business logic is not dependent on specific frameworks or technologies.
Application in Microservices:
Code Organization: Structure each microservice following Clean Architecture principles to ensure modular, testable and easy-to-maintain code.
Maintenance and Evolution: Facilitate the addition of new features and the modification of existing ones without compromising the integrity of the system.
In the microservices ecosystem, an HTTP endpoint plays a crucial role in orchestrating the document workflow, in this context, it is where it all begins. This endpoint is responsible for receiving and processing the request to create a new document. Upon receiving a request, it queues the document to the Go worker, which will take care of generating and processing the document. Additionally, the endpoint issues a notification to the document service via a Message Queue, informing that a new resource, that is, a document, has entered the queue for processing. This approach ensures efficient integration between system components, allowing the endpoint to manage the creation and tracking of documents in a coordinated and asynchronous manner, while the Go worker takes care of the actual creation of documents and the document service is updated on new items in the queue.
In addition to the HTTP endpoint, the system has two workers with different roles. The first, implemented in Go, is responsible for generating documents. It consumes tasks from a message queue, processes the data, and upon completion of processing, notifies a specific endpoint of completion. Go's efficiency ensures fast and robust processing. The second worker, developed in NodeJS, handles the creation of the initial state of the documents, defining them as "unprocessed" when inserted into the system. The agility of NodeJS allows for quick and efficient management of document states from the beginning of the workflow.
In summary, the system described demonstrates a well-coordinated flow for document management. The HTTP endpoint, together with Go and NodeJS workers, provides an integrated and efficient solution, guaranteeing document processing from creation to completion. The interaction between workers and REST is reflected in the architecture image below, which illustrates how the architecture promotes scalability and modularity, ensuring a robust and coordinated workflow. This approach not only improves operational efficiency, but can also be adapted for different usage scenarios, offering flexibility and future growth.
The final drawing:
The project repository: https://github.com/williamMDsilva/microservice-poc
Implementing a microservices architecture with Clean Architecture can present several challenges. One of the main challenges is a deep understanding of the business to create code that is truly scalable and maintains the integrity of the business logic. Clean Architecture requires that code be structured in a way that clearly separates concerns, which requires detailed knowledge of the domain for abstractions and separations to be effective.
In addition, knowledge of SOLID principles is crucial. These principles help create more cohesive, less coupled code, and a solid understanding of them can save significant time researching and troubleshooting. Applying SOLID principles not only improves code quality but also facilitates system maintenance and scalability.
In the specific case of Go, a solid foundation in the language can improve code readability and maintainability. Go offers tools and practices that help keep code clean and efficient, and deeper knowledge can make a difference when implementing complex services.
Finally, using a good boilerplate can be extremely beneficial. Well-designed Boilerplates for Clean Architecture not only speed up the start of development, but also ensure that new features are added within the initially proposed standard. They provide a framework that helps maintain code consistency and quality throughout the project.
To advance and improve the described architecture, some next steps are recommended:
Include Monitoring and Observability Resources: Implementing monitoring and observability tools is essential to ensuring system health and performance. The inclusion of metrics, logs and tracing helps identify problems and analyze system behavior in production.
Add Treatments for Unavailability: It is crucial to include mechanisms to deal with failures and unavailability, such as retries, circuit breakers and fallback strategies, to increase the resilience of the system and ensure continuity of services.
Perform Unit and Integration Tests: Tests are essential to ensure the quality of the code and the correct integration of components. Unit tests verify the functioning of isolated parts of the code, while integration tests ensure that the different components of the system work correctly together.
Refactor Services and Modules: Reviewing and refactoring existing services and modules to ensure that the code remains clean, readable and aligned with Clean Architecture principles is an ongoing task that improves the maintainability and scalability of the system.
Proof of Concept with Kafka: Considering a proof of concept to replace RabbitMQ with Kafka can offer insight into how different tools impact the project. Impact analysis on service design and overall architecture can provide valuable insights for future technology decisions.
This project demonstrated the effectiveness of well-planned architecture and the importance of careful implementation of Clean Architecture principles. The use of Go and NodeJS, combined with practices such as SOLID and the use of message queues and REST APIs, contributed to a robust and scalable system. However, developing and maintaining a microservices architecture presents challenges that require in-depth knowledge of the business and technology. Addressing these challenges with adequate planning and the adoption of good practices helps ensure the construction of efficient and sustainable systems. The path forward involves incorporating continuous improvements, such as advanced monitoring and new proofs of concept, to keep the architecture aligned with business needs and evolution.
Martin, R. C. (2008). Clean Code: Practical Agile Software Skills. Alta Books.
Martin, R. C. (2017). Clean Architecture: Frameworks and Principles for Software Design. Alta Books.
RabbitMQ. (n.d.). Tutorial Two - JavaScript. Retrieved from https://www.rabbitmq.com/tutorials/tutorial-two-javascript
RabbitMQ. (n.d.). Tutorial Two - Go. Retrieved from https://www.rabbitmq.com/tutorials/tutorial-two-go
Due to the academic and proof-of-concept nature of this project, some features were not implemented and remain as technical debt for future studies. Areas such as automated testing, error handling, resource streaming, service authentication and observability are topics that still need to be explored. All constructive criticism is welcome and encouraged, as it contributes to continuous improvement and deepening knowledge of these important areas.
Atas ialah kandungan terperinci [MICROSERVICES] Barisan Mesej dan REHAT – Pendekatan dengan Go, NodeJS dan Seni Bina Bersih. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!