Detailed documentation for using docker to build a SpringCloud environment

Table of Contents

  • 1. Introduction
  • 2. Preparation
  • 3. Create a Docker Compose file
  • Fourth, create a Spring Cloud project
  • 5. Build and run Spring Cloud applications
  • Six, detailed explanation
    • 1. Docker Compose file
    • 2. Spring Cloud project configuration
    • 3. Build and run the application
    • 4. Precautions
  • 7. Summary

1. Introduction

Spring Cloud is a microservice framework built on Spring Boot. It provides multiple components, including service registration and discovery, configuration management, load balancing, circuit breakers, etc., to help developers build and manage microservice applications more conveniently. Docker is a lightweight containerization technology that can package applications and dependencies into an independent image and run it anywhere.

Using Docker to build a Spring Cloud environment can help us manage and deploy microservice applications more easily. In this article, we will introduce how to use Docker and Docker Compose to build a Spring Cloud environment including Eureka server, configuration server, Zuul gateway and two microservices.

2. Preparations

Before starting, you need to make sure that you have installed Docker and Docker Compose tools in your local environment. If it is not installed, you can refer to the official Docker documentation to install it.

3. Create Docker Compose file

Create a new folder locally to store Docker Compose files and Spring Cloud project code. Create a file called docker-compose.yml under that folder and copy the following content into the file:

version: '3'
services:
  eureka-server:
    image: springcloud/eureka-server
    ports:
      - "8761:8761"
  config-server:
    image: springcloud/config-server
    ports:
      - "8888:8888"
    environment:
      - SPRING_PROFILES_ACTIVE=native
    volumes:
      - ./config:/config
  zuul-server:
    image: springcloud/zuul-server
    ports:
      - "8080:8080"
    environment:
      - SPRING_PROFILES_ACTIVE=dev
    depends_on:
      -eureka-server
  user-service:
    image: springcloud/user-service
    ports:
      - "8081:8081"
    environment:
      - SPRING_PROFILES_ACTIVE=dev
    depends_on:
      -eureka-server
      -config-server
  order-service:
    image: springcloud/order-service
    ports:
      - "8082:8082"
    environment:
      - SPRING_PROFILES_ACTIVE=dev
    depends_on:
      -eureka-server
      -config-server

The above docker-compose.yml file defines 5 services:

  • eureka-server: Eureka server, used for service registration and discovery.
  • config-server: configuration server, used to manage application configuration files.
  • zuul-server: Gateway server for routing requests and load balancing.
  • user-service: User service, providing user-related APIs.
  • order-service: order service, providing order-related APIs.

In the definition of the above services, we use the official image provided by Spring Cloud, which allows us to deploy and manage services more conveniently. At the same time, we also specified the port number and dependencies used by each service to ensure correct communication between services.

4. Create a Spring Cloud project

Create a new Spring Cloud project using any IDE or text editor. In this example, we will create two services: user-service and order-service.

Add the following dependencies to your project:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

These dependencies will add support for Eureka client, config client and Zuul gateway.

Add the following to the application.properties file:

# Eureka configuration
spring.application.name=user-service
eureka.client.serviceUrl.defaultZone=http://eureka-server:8761/eureka/


# configure server configuration
spring.cloud.config.uri=http://config-server:8888
spring.cloud.config.profile=dev

# Zuul configuration
zuul.routes.user-service.url=http://user-service:8081
zuul.routes.order-service.url=http://order-service:8082

These configurations will connect to the Eureka server, config server, and Zuul gateway, and register services with the Eureka server, while pointing routes to the corresponding services.

5. Build and run Spring Cloud applications

First, we need to package the Spring Cloud application into a docker image. Run the following command in the project root directory:

docker build -t springcloud/user-service .
docker build -t springcloud/order-service .

This will build docker images for user-service and order-service respectively, with image names sprintcloud/user-service and sprintcloud/order-service.

Next, we can use Docker Compose to start the entire Spring Cloud environment. Run the following command in the project root directory:

docker-compose up

This will start all services defined in the docker-compose.yml file. After the application starts, you can view the console of the Eureka server by visiting http://localhost:8761, and you can see that user-service and order-service have been registered on the Eureka server.

Now you can access the API of user-service and order-service by visiting http://localhost:8080/user-service or http://localhost:8080/order-service. All requests will be routed through the Zuul gateway, and load balancing will be done automatically in the background.

6. Detailed explanation

1. Docker Compose file

In the Docker Compose file, we define 5 services: eureka-server, config-server, zuul-server, user-service and order-service. For each service, we define the following:

  • image: The name of the Docker image used, here we use the official image provided by Spring Cloud.
  • ports: the mapped port number, here we map the port number inside the container to the port number of the host so that the service can be accessed from the outside.
  • environment: the environment variable of the service, here we specify some Spring
    Cloud-related configuration, such as which configuration file to use (native means to use the local file system), which configuration environment to use (dev means the development environment), etc.
  • volumes: Specifies the mounted volume. Here we mount the configuration folder to the /config directory inside the container so that the service can read the configuration file.

2. Spring Cloud project configuration

In the Spring Cloud project, we added support for Eureka client, configuration client, and Zuul gateway, and specified service configuration information in the application.properties file. Specifically:

  • spring.application.name: Specify the application name, here we specify user-service and order-service-
    eureka.client.serviceUrl.defaultZone: Specify the address of the Eureka server, here we specify http://eureka-server:8761/eureka/. In this way, the application can be automatically registered to the Eureka server after it is started, and other services can also be discovered from the Eureka server.
  • spring.cloud.config.uri: Specifies the address of the configuration server, here we specify http://config-server:8888. In this way, the application can read its own configuration information from the configuration server after it starts.
  • spring.cloud.config.profile: Specifies the configuration environment of the configuration server. Here we specify dev to indicate the development environment.
  • zuul.routes: Specifies the routing rules of the Zuul gateway. Here we route user-service and order-service to http://user-service:8081 and http://order-service:8082 respectively. This way, all API requests will be routed and load balanced through the Zuul gateway.

3. Build and run the application

After completing the above configuration, we need to package the application into a docker image and use Docker Compose to start the entire Spring Cloud environment.

First, run the following command in the project root directory to build the docker images of user-service and order-service:

docker build -t springcloud/user-service .
docker build -t springcloud/order-service .

This will build docker images for user-service and order-service respectively, with image names sprintcloud/user-service and sprintcloud/order-service.

Next, we can use Docker Compose to start the entire Spring Cloud environment. Run the following command in the project root directory:

docker-compose up

This will start all services defined in the docker-compose.yml file. After the application starts, you can view the console of the Eureka server by visiting http://localhost:8761, and you can see that user-service and order-service have been registered on the Eureka server.

Now you can access the API of user-service and order-service by visiting http://localhost:8080/user-service or http://localhost:8080/order-service. All requests will be routed through the Zuul gateway, and load balancing will be done automatically in the background.

4. Precautions

When using Docker to build a Spring Cloud environment, you need to pay attention to the following points:

  • In the Docker Compose file, you need to define the dependencies of each service to ensure that the services can communicate correctly.
  • In the Spring Cloud project, the addresses and port numbers of the Eureka server, configuration server, and Zuul gateway need to be correctly specified.
  • When building a Docker image, you need to ensure that the image name is consistent with the service name defined in the Docker Compose file.
  • When starting the application, you need to make sure that the port number defined in the Docker Compose file is not in use.

7. Summary

Using Docker to build a Spring Cloud environment can help us manage and deploy microservice applications more easily. In this article, we introduced how to use Docker and Docker Compose to build a Spring Cloud environment including Eureka server, configuration server, Zuul gateway and two microservices. Through the introduction of this article, I believe you have learned how to use Docker and Docker Compose to build a Spring Cloud environment, and how to package a Spring Cloud application into a docker image and run it. Of course, in actual use, some customization and adjustments may be required according to specific needs and scenarios.

If you have any questions or questions feel free to ask me anytime.