Content Service

Introduction

in this article, we will build the Content Service. This service provides a simple static asset service with the sole responsibility of serving static HTML, CSS, JS, and Image content.

Requirements

Before you get started, you will need the following:
  • Java
  • Maven
  • Docker
  • Docker-Compose
Refer to the Development Toolbox article if you do not have these installed locally.

Building the Content Service

The third service we will build leverages the Spring frameworks spring-boot-starter-web. This dependency provides the service with the ability to serve content.

Maven POM.XML

loading...

Here we see many of the same dependencies and plugins we encountered in the previous two services. The key features here are:
  • spring-boot-starter-web - This dependency brings with it support to serve our static content.
  • spring-cloud-config-client- This dependency allows the service to notify the Discovery Service that it is running.

The Source

In our third service we see that we still are not writing much code to create our Content Service. This application looks nearly identical to the first two.

loading...

One important distinction is the inclusion of the @EnableDiscoveryClient annotation. This instructs the service to notify the Discovery Service of its existance.

Service Configuration

This Content Service is configured with both bootstrap.yml and application.yml files.

bootstrap.yml

loading...


In this bootstrap.yml configuration file, we declare the service's name, and we configure it to retrieve its remote configuration from the application's Configuration Service.

application.yml

loading...

Here we configure the service to:

  • listen on port 4040.
  • we turn off the default white label error page.
  • configure the Eureka Client.
  • set the web filter to DEBUG to log incoming requests.
  • configure multipart file support for file upload.

Content-Service Docker File

The following dockerfile create our ContentService container image.

loading...

Docker-Compose

Here we continue to extend our existing Docker-Compose file to include the Content Service configuration.

loading...

We also include depends_on: config-started to ensure that the configuration service is available.

Content Service in Action

We can copy the Docker-Compose file above to your local machine, and run it from the command prompt:

docker-compose -f ./dc-03-content.yml up -d  

Once all the services have started, Point a web browser to http://localhost:4040



Here you see the Content-Service in action.

Metrics and Monitoring

We will be not be monitoring any service-specific metrics in the Content-Service, but as with the Configuration-Service and Discovery-Service, we will be monitoring its JVM. To enable this, we will need to update our prometheus.yml configuration file to include the following scrape_configs: entry.




  ###################
  # content service #
  ###################
  - job_name: "content-service"
    metrics_path: "/actuator/prometheus"
    basic_auth:
      username: 'think'
      password: 'microservices'
    scrape_interval: 5s
    static_configs:
    - targets: ['content-service:4040']

	


Now open up Grafana in your browser. Navigate to the JVM (Micrometer) dashboard (or import it doesn't exist) we imported in the previous article,



Here we select Content-Service from the Application drop-down selector to view its JVM metrics.

Switching over to the HTTP Dashboard, we can select CONTENT-SERVICE and view its data.

Grafana HTTP Dashboard


Resources



Coming Up

In our next article, we will be building the ApiGatewayService to provide a front door for our future application clients. This service will be using the Spring Cloud Gateway project.