Spring boot Micro services Architecture with Spring Cloud 1.0

Abhay Ahire
5 min readApr 25, 2022

In this article, we are going to see all fine details to create micro services in spring boot. This will be in series and in first article we are going to see all theoretical stuff and config server set up that you guys need to understand before jumping into implementing micro services with spring boot and spring cloud. Let’s first understand what is micro service architecture and what are the advantages of it.

What are microservices?

Micro services is a architecture(service-oriented architecture) (SOA). In this architecture, applications are broken down into various services. Motivation behind this is separation and modularity.

Unlike monolithic architecture, microservices are more beneficial.
We don’t need to specify all business logic into a single software module which leads to complexity and time consuming while debugging.

Few Advantages of microservices are

  • Increases scalability
  • More flexibility
  • Modular architecture
  • Ease of introducing new features
  • More reliable and robust structure

Monolithic Architecture

Why Spring boot+ Spring cloud for microservices?

With Spring Boot, your microservices can start small and iterate fast. That’s why it has become the de facto standard for Java microservices. Quickstart your project with Spring Initializr and then package as a JAR. With Spring Boot’s embedded server model, you’re ready to go in minutes.
Now we have understood , what is micro service architecture and how spring boot helps us implement same. Let’s understand the all components involve in this architecture.

Spring boot and Spring cloud Components

  • Spring Cloud Config Server
  • Manage multiple Environments using config server
  • Need of Spring Cloud Bus
  • Microservices inter communication using RestTemplate
  • Microservices inter communication using Feign Client and OpenFeign
  • Load Balancer (Ribbon)
  • Need of Naming Server (Eureka)
  • Service Registry and Service Discovery
  • Intro to API Gateway (Zuul) and Spring Cloud Gateway
  • Intro to Distributed Tracing (Zipkin)
  • Fault Tolerance(Hystrix)
  • Spring Cloud Bus

Let’s see all of them one by one. For this article, we’ll consider creating a Appointment MicroService and Spring Cloud Config Server.
Again, we are not going to write down any business logic or implementation for any of the service, we’re just considering this services to understand flow theoretically.

Spring Cloud Config Server -> Why ?

Let’s understand need of config server. In microservice architecture we need to maintain configuration for each service. This configuration may include database credentials for that service or any environment variables you may declare. Spring comes with application.properties file for storing configurations for your application. But it’s a hectic job for Devops person to update those configurations looking into individual service in case of any updates. So spring cloud comes up with a way to manage all configurations for each service at central place. We can do it with help of Spring cloud Config Server

Spring Cloud Config Server -> How ?

To create config server, you need to create spring app with Config Client dependency with following dependencies. Dev Tools is used for live reloading and config server for Spring Cloud Config Server.

Inside your main class, Add @EnableConfigServer annotation. Almost done,
But wait, where we’ll store configurations.

Spring Cloud Config Server -> Git Repo Role ?

You need to create a git repo and link it to your spring config application. You can create a local git repo or add it to origin. Inside your git repo, you can add multiple configuration files for your services like provider-service.properties, appointment-service.properties etc. If you want to manage multiple environments for a service then you can do that as well like appointment-service-dev, appointment-service-qa.properties. You’ll be able to read those configurations in config client. You need to make sure name of properties inside git repo must match with name of application inside your microservice application properties file. Also make sure to commit changes otherwise they’ll not be reflected

Spring Cloud Config Server -> Connect Git repo to microservice

For that you need to add
spring.cloud.config.server.git.uri: file://${user.home}/config-repo
inside your config server application.properties file.
Now navigate to browser and hit `http://localhost:9000/webtech4u-appointment-service/default` and you’ll be able to list of configurations we added inside webtech4u-appointment-service.properties file.

Spring Cloud Config Server -> Create Micro service and Read configurations from Application.properties

Let’s create Appointment Service app with following dependencies.

You can create config class to read configurations from application.properties.
Let’s create AppointmentConfig class and annotate it with @Component and @ConfigurationProperties(“Application name here”) and you can declare properties inside your application file inside this class with same name and getters, setters and spring will do the magic of mapping and you’ll be able to read those values inside config class.
You can now autowire your config class into your controller to test flow

Spring Cloud Config Server -> Connect Micro service and Spring Cloud Config Server

To connect your microservice with config server, you need to add spring.cloud.config.uri: http://configServerUrl.com in your service and you’ll be able to access every configuration inside your git repo by the file name e.g. In Appointment Service, you’ll be able to read configuration from appointment-service.properties inside git repo. The properties declare inside git repo properties file will override the local properties in your service.

Spring Cloud Config Server -> Implementation

Facing difficulties in understanding above flow, no worries! We have everything ready and implemented for you. Just visit https://github.com/abhay-321/webtech4u-spring-microservices, clone the repos and run Appointment Service and Config Server with the help of commands mvn clean install && mvn spring-boot:run and visit `http://localhost:9000/webtech4u-appointment-service/default to see config server output and `http://localhost:8100/configs to read configs in appointment service.

We’ll continue this series and understand rest of components involved in microservice architecture, see you guys in next part of this series!!!.

--

--

Abhay Ahire

Full stack Dev with Angular, Java , Node and Devops Engg AWS