{"id":4135,"date":"2023-11-04T23:14:05","date_gmt":"2023-11-04T23:14:05","guid":{"rendered":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/"},"modified":"2023-11-05T05:47:58","modified_gmt":"2023-11-05T05:47:58","slug":"creating-scalable-microservices-with-docker","status":"publish","type":"post","link":"http:\/\/localhost:10003\/creating-scalable-microservices-with-docker\/","title":{"rendered":"Creating Scalable Microservices with Docker"},"content":{"rendered":"
Scalability is a key characteristic in building modern applications. Microservices architecture is a popular approach in building scalable applications. It lets developers break down a monolithic application into small, loosely coupled services. Each service is responsible for a particular functionality and can be independently deployed, scaled and maintained.<\/p>\n
Docker is a containerization technology that simplifies the process of packaging, deploying and managing applications. Combining docker with microservices architecture can help developers build scalable applications that can be easily deployed, managed and scaled irrespective of the underlying infrastructure. In this tutorial, we will explore how to create scalable microservices with Docker.<\/p>\n
To follow along with this tutorial, you will need the following:
\n– Docker installed on your system.
\n– Basic knowledge of docker commands and microservices architecture.<\/p>\n
The first step in building a microservices application is to identify the services that make up the application. Each service should have a specific functionality. For this example, we will build a simple e-commerce application consisting of the following services:
\n– Product service: responsible for managing products.
\n– Order service: responsible for managing orders.
\n– Payment service: responsible for managing payments.<\/p>\n
Once we have identified the services, we can start building each service as a separate microservice. For each service, we need to write the code for the functionality it provides, define an interface (API) that other services can use to interact with it and package it as a docker image.<\/p>\n
Let’s take an example of building the product service. We can define the API for the product service using the OpenAPI standard.<\/p>\n
openapi: \"3.0.0\"\ninfo:\n title: Product API\n version: \"1.0.0\"\npaths:\n \/product:\n get:\n summary: Get all products\n responses:\n '200':\n description: A list of products.\n content:\n application\/json:\n schema:\n type: array\n items:\n $ref: '#\/components\/schemas\/Product'\n post:\n summary: Create a new product.\n responses:\n '201':\n description: The created product.\n content:\n application\/json:\n schema:\n $ref: '#\/components\/schemas\/Product'\ncomponents:\n schemas:\n Product:\n type: object\n required:\n - name\n - price\n properties:\n id:\n type: string\n format: uuid\n description: The unique identifier for the product.\n name:\n type: string\n description: The name of the product.\n description:\n type: string\n description: A description of the product.\n price:\n type: number\n format: double\n minimum: 0\n description: The price of the product.\n<\/code><\/pre>\nWe can use this API definition to generate the code for the product service using a code generator like Swagger codegen. Once we have the code, we can package it as a docker image. We can define a Dockerfile for the product service as follows:<\/p>\n
FROM openjdk:11-jre-slim\n\nWORKDIR \/app\n\n# Copy the service code into the container\nCOPY target\/product-service-1.0-SNAPSHOT.jar .\n\n# Set the service command\nCMD [\"java\", \"-jar\", \"product-service-1.0-SNAPSHOT.jar\"]\n<\/code><\/pre>\nWe can use the maven build tool to build the product service and package it as a docker image.<\/p>\n
mvn clean package docker:build -DskipTests\n<\/code><\/pre>\nWe can repeat these steps to build the order and payment services and package them as docker images.<\/p>\n
3. Define the infrastructure<\/h2>\n
To deploy and manage the microservices, we need to define the infrastructure for the application. There are several ways to do this. One way is to use docker-compose. Docker-compose is a tool for defining and running multi-container Docker applications.<\/p>\n
We can define the infrastructure for our e-commerce application in a docker-compose file. Here’s an example docker-compose file:<\/p>\n
version: '3.7'\nservices:\n product-service:\n image: <product-service-image>\n ports:\n - \"8080:8080\"\n environment:\n SPRING_DATASOURCE_URL: jdbc:mysql:\/\/mysql\/product?useSSL=false\n SPRING_DATASOURCE_USERNAME: root\n SPRING_DATASOURCE_PASSWORD: password\n depends_on:\n - mysql\n order-service:\n image: <order-service-image>\n ports:\n - \"8081:8081\"\n environment:\n PRODUCT_SERVICE_URL: http:\/\/product-service:8080\n SPRING_DATASOURCE_URL: jdbc:mysql:\/\/mysql\/order?useSSL=false\n SPRING_DATASOURCE_USERNAME: root\n SPRING_DATASOURCE_PASSWORD: password\n depends_on:\n - product-service\n - mysql\n payment-service:\n image: <payment-service-image>\n ports:\n - \"8082:8082\"\n environment:\n ORDER_SERVICE_URL: http:\/\/order-service:8081\n SPRING_DATASOURCE_URL: jdbc:mysql:\/\/mysql\/payment?useSSL=false\n SPRING_DATASOURCE_USERNAME: root\n SPRING_DATASOURCE_PASSWORD: password\n depends_on:\n - order-service\n - mysql\n mysql:\n image: mysql:5.7\n environment:\n MYSQL_DATABASE: product\n MYSQL_ROOT_PASSWORD: password\n volumes:\n - db-data:\/var\/lib\/mysql\nvolumes:\n db-data:\n<\/code><\/pre>\nIn the above docker-compose file, we have defined the services for the product, order and payment microservices. We have also defined the mysql service as a dependency for the other services. The environment variables for the services are also defined. The mysql service uses a named volume to persist the data.<\/p>\n
4. Deploy the services<\/h2>\n
Once we have defined the services and infrastructure, we can deploy the services using docker-compose.<\/p>\n
docker-compose up -d\n<\/code><\/pre>\nThis command will start all the services defined in the docker-compose file in detached mode. We can check the status of the services using the docker-compose ps command.<\/p>\n
docker-compose ps\n<\/code><\/pre>\n5. Scale the services<\/h2>\n
One of the benefits of using docker and microservices architecture is the ability to scale the services horizontally. We can scale the services by increasing the number of instances of a service.<\/p>\n
For example, if we want to scale the product service to three instances, we can use the docker-compose scale command like this:<\/p>\n
docker-compose scale product-service=3\n<\/code><\/pre>\n6. Monitor the services<\/h2>\n
Monitoring the microservices is essential to ensure the application is running smoothly. We can use several tools to monitor the services. One such tool is Prometheus.<\/p>\n
Prometheus is a monitoring and alerting system. It collects metrics from the services and stores them in a time-series database. It provides a query language to extract and aggregate the metrics. We can use Prometheus to monitor the services in our application.<\/p>\n
To use Prometheus, we need to add the Prometheus client library to each service. We can add the following dependency to each service:<\/p>\n
<dependency>\n <groupId>io.prometheus<\/groupId>\n <artifactId>simpleclient_spring_boot<\/artifactId>\n <version>0.10.0<\/version>\n<\/dependency>\n<\/code><\/pre>\nWe also need to add the following configuration to each service’s application.properties file:<\/p>\n
management.endpoint.metrics.enabled=true\nmanagement.metrics.export.prometheus.enabled=true\n<\/code><\/pre>\nOnce we have added the Prometheus client library and configuration, we can start the services and navigate to the \/actuator\/prometheus endpoint of each service to verify that Prometheus is collecting metrics.<\/p>\n
We can also set up a Prometheus server to collect and display the metrics. We can define a docker-compose file for the Prometheus server as follows:<\/p>\n
version: '3.7'\nservices:\n prometheus:\n image: prom\/prometheus:v2.28.1\n volumes:\n - .\/prometheus.yml:\/etc\/prometheus\/prometheus.yml\n ports:\n - \"9090:9090\"\n<\/code><\/pre>\nIn the above docker-compose file, we have defined the prometheus service and mounted a configuration file that specifies the targets to scrape. The Prometheus server will scrape the metrics from the services defined as targets in the configuration file.<\/p>\n
7. Conclusion<\/h2>\n
In this tutorial, we have explored how to build scalable microservices with Docker. We have defined the services, built the services as docker images, defined the infrastructure using docker-compose and deployed the services. We have also explored how to scale the services and monitor them using Prometheus.<\/p>\n
Using Docker and microservices architecture can help developers build scalable, easily deployable and manageable applications. With tools like docker-compose and Prometheus, managing the infrastructure and monitoring the services can be simplified.<\/p>\n","protected":false},"excerpt":{"rendered":"
Introduction Scalability is a key characteristic in building modern applications. Microservices architecture is a popular approach in building scalable applications. It lets developers break down a monolithic application into small, loosely coupled services. Each service is responsible for a particular functionality and can be independently deployed, scaled and maintained. Docker Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[621,199,1425,96,1433,569,1394,1431,1432,1430],"yoast_head":"\nCreating Scalable Microservices with Docker - Pantherax Blogs<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n