Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Boot Microservices: Building a Microservices Application Using Spring Boot

DZone's Guide to

Spring Boot Microservices: Building a Microservices Application Using Spring Boot

Now that we've learned how to set up and run a Spring Boot app using Eclipse IDE and CLI, we'll see how to build a microservices application using Spring Boot.

· Microservices Zone ·
Free Resource

Containerized Microservices require new monitoring. Read the eBook that explores why a new APM approach is needed to even see containerized applications.

In the previous blog, you have learned how to setup and run Spring Boot using Eclipse IDE and CLI. Now in this Spring Boot Microservices blog, let me show how we can create Microservices Application for Top Sports Brands using Spring Boot and Netflix Eureka Server in detail. Before creating the application, let me tell you what are the challenges with Microservices Architecture.

Spring Boot enables building production-ready applications quickly and provides non-functional features:

  • Embedded servers which are easy to deploy with the containers.
  • It helps in monitoring the multiples components.
  • It helps in configuring the components externally.

So, let us see the challenges with microservices architecture.

Challenges With Microservice Architecture

While developing a number of smaller microservices might look easy, there is a number of inherent complexities that are associated with microservices architectures. Let's look at some of the challenges:

  • Automating the Components: It becomes difficult to automate everything because there are a number of smaller components instead of a monolith, i.e. builds, deployment, monitoring, etc.
  • Perceptibility: There is a number of small components to deploy and maintain which sometimes becomes difficult to monitor and identify problems. It requires great perceptibility around all the components.
  • Configuration Management: There is a great need to maintain the configurations for the components across the various environments.
  • Debugging: It becomes difficult to probe each and every service for an error. Centralized Logging and Dashboards are essential to make it easy to debug problems.
  • Consistency: You cannot have a wide range of tools solving the same problem. While it is important to foster innovation, it is also important to have some decentralized governance around the languages, platforms, technology and tools used for implementing/deploying/monitoring microservices.

Building Architecture for Top Sports Brands With Spring Boot

In this Spring Boot microservices example, we will be creating Top Sports Brands' application, which will have three services:

  1. Eureka Service- This service will register every microservice and then the client microservice will look up the Eureka server to get a dependent microservice to get the job done. This Eureka Server is owned by Netflix and in this, Spring Cloud offers a declarative way to register and invoke services by Java annotation.
  2. Item Catalog Service - This service will generate the list of sports brands which are popular in the market.
  3. Edge Service - It is similar to the standalone Item service created in Bootiful Development with Spring Boot and Angular. However, it will have fallback capabilities which prevent the client from receiving an HTTP error when the service is not available

Let us see which of the following tools required to create this Spring Boot microservices example application.

If you facing any difficulty in installing and running the above tools, please refer to this blog.

Creating a Eureka Service

To begin with, create a EurekaServer Spring Starter Project in Eclipse IDE. Click on Spring Starter Project and click on Next.

Name your Spring Starter Project as EurekaServer and other Information will be filled automatically.

Note: Make sure your Internet is connected otherwise it will show an error.

Now, modify EurekaServer/src/main/resources/application.properties file to add a port number and disable registration.

Open EurekaServer/src/main/java/com/example/EurekaServiceApplication.java and add @EnableEurekaServer above @SpringBootApplication.

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer @SpringBootApplication

This annotation will configure a registry that will allow other applications to communicate.

To start the Application: Right Click on the Project -> Run As -> Click on " Spring Boot App "

http://localhost:8761

Now open http://localhost:8761. Here Spring Eureka Server will open and will show no service will be running.

Spring Boot Microservices: Creating an Item Catalog Service

Again create a new project. Use Item-catalog-service for the artifact name and click on Next.

Add the following dependencies:

  • Actuator: features to help you monitor and manage your application
  • EurekaDiscovery: for service registration
  • JPA: to save/retrieve data
  • H2: an in-memory database
  • RestRepositories: to expose JPA repositories as REST endpoints
  • Web: Spring MVC and embedded Tomcat
  • DevTools: to auto-reload the application when files change
  • Lombok: to reduce boilerplate code

Click on Finish.

Now, create an entity, to ItemCatalogServiceApplication.java . The code below assumes you're putting all classes in the same file.

If you're using an editor that doesn't auto-import classes, here's the list of imports needed at the top of ItemCatalogServiceApplication.java.

Add an application name in item-catalog-service/src/main/resources/application.properties file to display in the Eureka service, and set the port to 8088.

Now, Create the Cloud Properties file.

Click on File -> New -> Other -> File and add the below code in this file and save it.

eureka.instance.hostname=${vcap.application.uris[0]:localhost}
eureka.instance.nonSecurePort=80
eureka.instance.metadataMap.instanceId=${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
eureka.instance.leaseRenewalIntervalInSeconds = 5

eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5
eureka.client.serviceUrl.defaultZone=${vcap.services.pwa-eureka-service.credentials.uri}/eureka/

Now, to start the Application:

Right Click on Project -> Run As -> Click on " Spring Boot App "

Note: In case of error try this step: Right Click on the Project -> Run As -> Click on "Maven Build."

Now open http://localhost:8761. Here you will see Item Catalog service will be running.

You will see the list of items from the catalog service.

Now let us move forward and create the Edge Service.

Creating an Edge Service

It is similar to the standalone Item service created in Bootiful Development with Spring Boot and Angular. However, it will have fallback capabilities which prevent the client from receiving an HTTP error when the service is not available.

Again create a new project. Use edge-service for the artifact name:

  • Eureka Discovery: for service registration
  • Feign: a declarative web service client
  • Zuul: provides intelligent routing
  • Rest Repositories: to expose JPA repositories as REST endpoints
  • Web: Spring MVC and embedded Tomcat
  • Hystrix: a circuit breaker to stop cascading failure and enable resilience
  • Lombok: to reduce boilerplate code

Click on Finish.

Since the item-catalog-service is running on port 8088, you'll need to configure this application to run on a different port. Modify edge-service/src/main/resources/application.properties to set the port to 8089 and set an application name.

Now, Create the Cloud Properties file.

Click on File -> New -> Other -> File and add below code in this file and save it.

eureka.instance.hostname=${vcap.application.uris[0]:localhost}
eureka.instance.nonSecurePort=80
eureka.instance.metadataMap.instanceId=${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}
eureka.instance.leaseRenewalIntervalInSeconds = 5

eureka.client.region = default
eureka.client.registryFetchIntervalSeconds = 5
eureka.client.serviceUrl.defaultZone=${vcap.services.pwa-eureka-service.credentials.uri}/eureka/

To enable Feign, Hystrix, and registration with the Eureka server, add the appropriate annotations to EdgeServiceApplication.java:

Create a Item DTO (Data Transfer Object) in this same file. Lombok's will generate a methods, getters, setters, and appropriate constructors.

Create a ItemClient interface that uses Feign to communicate to the Item-catalog-service.

Create a RestController below the ItemClient that will filter out less-than-top brands and shows a /top-brands endpoint.

Start the edge-service application with Maven or your IDE and verify it registers successfully with the Eureka server.

Now invoke localhost:8089/top-brands, you will see the list of top brands from the catalog service.

Note: If you shut down the item-catalog-service application, you'll get a 500 internal server error.

To fix this, you can use Hystrix to create a fallback method and tell the goodItems() method to use it.

Restart the edge-service and you should see an empty list returned.

Start the item-catalog-service again and this list should eventually return the full list of top brands names.

If you want the source code for this application, please leave your comment in comment section.

Discover how to automatically manage containers and microservices with better control and performance using Instana APM. Try it for yourself today.

Topics:
microservices ,spring boot ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}