2. Agenda
● Introduction
● Comparison with monolith architecture
● Advantages of microservices
● Challenges with microservices
● Eureka server
● Creating a spring boot microservice
● Communicating with other microservices
● Load balancing
3. Introduction
Microservices is an approach to developing a single application as a suite of small
services, each running in its own process and communicating with lightweight
mechanisms, often an HTTP resource API.
Microservices allow large systems to be built up from a number of collaborating
components. It does at the process level what Spring has always done at the
component level: loosely coupled processes instead of loosely coupled
components.
4. In comparison with Monolith application
Monolith applications are typically huge - more 100,000 line of code. In
some instances even more than million lines of code. Monoliths are
characterized by -
● Large Application Size
● Long Release Cycles
● Large Teams
5. Typical challenges in Monolith application
● Scalability challenges
● New technology adoption
● Difficult to perform automation tests
● Difficult to adapt to modern development practices
● Adapting to device explosion
Microservice architectures evolved as a solution to the scalability and
innovation challenges with monolithic architectures.
6. What Does Microservice Architecture Look Like?
This is how the same application would look like when developed using
microservices architecture.
8. Advantages of Microservices
● New technology and process adaptation becomes easier. You can try
new technologies with the newer microservices that we create.
● Faster release cycles.
● Scaling with the cloud.
9. ● Quick setup needed: You cannot spend a month setting up each microservice.
You should be able to create microservices quickly.
● Automation: Because there are a number of smaller components instead of a
monolith, you need to automate everything - Builds, Deployment,
Monitoring, etc.
● Visibility: You now have a number of smaller components to deploy and
maintain, maybe 100 or maybe 1000 components. You should be able to
monitor and identify problems automatically. You need great visibility around
all the components.
● Configuration Management: You need to maintain configurations for
hundreds of components across environments. You would need a
Configuration Management solution
Challenges With Microservice Architectures
10. Spring Boot
Spring Boot enables building production-ready applications quickly and provides non-functional
features:
● Embedded servers (easy deployment with containers)
● Metrics (monitoring)
● Health checks (monitoring)
● Externalized configuration
Spring Cloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed
systems. Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud
developers can quickly stand up services and applications that implement those patterns.
Solutions to Challenges with Microservice Architectures
11. Important Spring Cloud Modules
● Dynamically scale up and down. using a combination of
○ Naming Server (Eureka)
○ Ribbon (Client Side Load Balancing)
○ Feign (Easier REST Clients)
● Visibility and monitoring with
○ Zipkin Distributed Tracing
○ Netflix API Gateway
● Configuration Management with Spring Cloud Config Server
● Fault Tolerance with Hystrix
12. Eureka Server
When you have multiple processes working together they need to find each
other. The developers at Netflix had this problem when building their systems
and created a registration server called Eureka (“I have found it” in Greek).
Fortunately for us, they made their discovery server open-source and Spring
has incorporated into Spring Cloud, making it even easier to run up a Eureka
server.
13. @SpringBootApplication
@EnableEurekaServer
public class ServiceRegistrationServer {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistrationServer.class, args);
}
}
Configurations in application.properties
spring.application.name=netflix-eureka-naming-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
The configuration specifies that I am not a client and stops the server process trying to register with itself.
Eureka Server Application
14. Creating a Microservice
A microservice is a stand-alone process that handles a well-defined requirement.
When configuring applications with Spring we emphasize Loose Coupling and Tight Cohesion, These are
not new concepts (Larry Constantine is credited with first defining these in the late 1960s - reference) but
now we are applying them, not to interacting components (Spring Beans), but to interacting processes.
15. @SpringBootApplication
@EnableEurekaClient
public class AccountsService {
public static void main(String[] args) {
SpringApplication.run(AccountsService.class, args);
}
}
What makes this a microservice is the registration with the eureka-server via @EnableEurekaClient
In application.properties-
spring.application.name=account-service
server.port=8000
eureka.client.service-url.default-zone=http://localhost:8761/eureka
Note that this file -
● Sets the application name as accounts-service. This service registers under this name.
● Specifies a custom port to listen on (8000).
● The URL of the Eureka Service process - from the previous section.
16. Communicating with other microservices
Using RestTemplate -
To consume a RESTful service, Spring provides the RestTemplate class. This allows you to send HTTP
requests to a RESTful server and fetch data in a number of formats - such as JSON and XML.
public Account getByNumber(String accountNumber) {
Account account = new RestTemplate().getForObject(serviceUrl
+ "/accounts/{number}", Account.class, accountNumber);
if (account == null)
throw new AccountNotFoundException(accountNumber);
else
return account;
}
17. Using Feign (Spring cloud module)
@FeignClient(name="account-service" url="localhost:8000")
public interface AccountNumberServiceProxy {
@GetMapping("/accounts/{number}")
public Account retrieveAccount(@PathVariable("number") String number);
}
Enabling Feign clients -
Before we are able to use Feign, we need to enable it by using @EnableFeignClients annotation on the appropriate package where
the client proxies are defined.
@SpringBootApplication
@EnableFeignClients("com.springboot.microservice.example.account")
@EnableDiscoveryClient
public class AccountNumberServiceApplication {
public static void main(String[] args) {
SpringApplication.run(AccountNumberServiceApplication.class, args);
}
}
18. Load Balancing Using Ribbon
If you have multiple instances of a service available, ribbon picks one for you. (Eureka on their own
doesn’t perform load-balancing so we use Ribbon to do it instead).
Add dependency in build.gradle-
compile('org.springframework.cloud:spring-cloud-starter-ribbon')
@FeignClient(name="account-service")
@RibbonClient(name="account-service")
public interface AccountNumberServiceProxy {
@GetMapping("/accounts/{number}")
public Account retrieveAccount(@PathVariable("number") String number);
}