Microservices with Spring Profiles(Step by Step Tutorial)

Microservices With Spring Profiles

In this step by step tutorial, I will teach you how to build microservices using Spring Profiles.

Spring Profiles allow you to split a single application into microservices each running with a different profile(run configuration). Also in this tutorial, we would set up and start service discovery using Eureka Server.

 

The Microservices we’ll Build

We would build microservices that represents a school system. I would have four services:

  • Teachers service
  • Students service
  • Courses service
  • Departments service

The architecture for the system we are building is given below:

Microservices With Spring Profiles

Microservices With Spring Profiles

Follow the Steps Below

For these steps, the actual details are provided in the Video here.

Step 1: Setup and start the Eureka Server

You simply create a spring boot starter project and add the Eureka Server dependency.

For some reason, I don’t see spring-boot-starter web dependency. In that case, you need to add the dependency to the pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

Also add the following to the application.properties file

server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

These lines tell the server not to search for other servers (we just have one server). It also tell the server to run on port 8761 instead of default port 8080.

Also remember to add the @EnableEurekaServer annotation to the main application class.

 

Step 2: Build a Eureka Client monolith application

Create a spring boot starter project and add the Eureka Client dependency. Also add the spring-boot-starter-web dependency to to pom.xml.

You can fire up the Eureka Server and visit http://localhost:8761 to make sure it’s up and running. The window is as shown below

Note: In your case, you’ll not see any services under the Application column. That would appear later.

 

Step 3: Create the four packages

Create the following four packages: students, courses, teachers and departments

 

Step 4: Add the controller class

For each of the package, add the controller class with the following content:

@GetMapping("/students")
public String Home() {
	return "Welcome to Students Services";
}

Do the same for all the other packages/controllers.

 

Step 5: Add the @EnableEurekaClient annotation to the the main application class

 

Step 6: Add the @Profile annotation to all the Controller classes. For example, in the student controller, just below the @RestController annotation, add the annotation

@Profile("students")

Repeat the same for the other services changing the name of the profile accordingly.

 

Step 7: Set the server.port=0 in the application.properties file.

This setting tells spring boot to automatically assign a port (not the default 8080 port) to any of the service that starts up.

 

Step 8: Set the application name in the application.properties file

We need to set the application name to correspond to the profile name. So at startup, the active profile name is used to generated the application name. In this way, we ensure the four profiles run as four different application.

To do this, add the following code to theĀ  application.properties file

spring.application.name = ${spring.profiles.active}

 

Step 9: Set up the run configuration.

This is a very important part. To do that, click on run in the menu and select ‘Run Configuration’. The Run Configurations window opens up as shown below:

Spring Boot Run Configuration Window

Spring Boot Run Configuration Window

Step 10: Select Spring Boot App in the list and click on the Launch New Configuration button (circled in red)

Step 11: Give the configuration a name and click on Apply. In this case students-config

Step 12: Set the spring boot run configuration as shown in the figure below:

Step 13: Click on the Arguments tab. In the VM arguments field, enter the following:

-Dspring.profiles.active=students

This is for the students service. The window is shown below:

Run Configuration VM arguments

Run Configuration VM arguments

Step 14: Repeat steps 10 to 13 for the other 3 profiles.

At this point, we have completed setting up the services. So we now need to test them

Step 15: Fire up the students service.Go to Eureka Server page. Then refresh it to make sure the services shows up under Applications.

Step 16: Check the port for the student service. Then visit http://localhost:<port>/students.

You will see that the welcome message is displayed.

Step 17: Repeat step 15 and 16 for the other three services.

 

I’ll like to say thumbs up to you if you successfully come this far!