
上QQ阅读APP看书,第一时间看更新
Microservices internals
To build proper microservices, there are several components we would need to consider. To understand the five components, let's discuss the main tasks a microservice is expected to undertake:
- The microservice will need to be able to send and receive messages with other services and the outside world so that tasks can be carried out in harmony. The communication aspect of a microservice takes different forms. Restful APIs are very popular when interacting with the outside world, and message queues are very helpful when communicating with other services. There are other popular techniques that are also popular such as gRPC.
- The microservice will need a configuration layer; this could be via environmental variables, a file or database. This configuration layer will tell the microservice how to operate. For example, let's assume that our service needs to listen on a TCP address and a port number to be able to receive messages; the TCP address and the port number will be part of the configuration that gets fed to our service when it starts up.
- The microservice will need to log events that happen to it so that we can troubleshoot issues and understand behaviors. For example, if a communication issue occurs while sending a message to another service, we'll need the error to be logged somewhere in order for us to be able to identify the problem.
- The microservice will need to be able to persist data by storing it in a database or other forms of data stores; we'll also need to be able to retrieve data at a later time. For example, in case of the MyEvents application, our microservices will need to store and retrieve data related to the users, the bookings, and the events.
- Finally, there is the core, the most important piece of our microservice. The core is the code responsible for the task that our microservice is expected to do. For example, if our microservice is responsible for handling user bookings, then the microservice core is where we write the code that would perform the task of handling the user's bookings.
So, based on the previous five points, the building blocks of a microservice should look like this:

Building blocks of a microservice
Those building blocks provide a good foundation to build efficient microservices. The rules are not set in stone. You can make your microservice either simpler or more complex, depending on the application you are trying to build.