This document provides an overview of microservices and how to develop them using Spring. It discusses the challenges of distributed systems and how Spring Boot and Spring Cloud Netflix address areas like configuration, service registration, load balancing, fault tolerance, and monitoring. Examples are provided for building microservices with Spring Boot, integrating configuration with Spring Cloud Config, registering services with Eureka, load balancing with Ribbon and Feign, handling faults with Hystrix, and monitoring with Hystrix Dashboard. Reactive programming with RxJava is also introduced as an approach for concurrent API integration.
Accelerate your Kubernetes clusters with Varnish Caching
Developing Microservices using Spring - Beginner's Guide
1. Developing
Microservices
using Spring
-
Beginners Guide
* Disclaimer: There are various topics covered in this session, each topic is worth a 2 day workshop session in itself, so this will cover only the very basics and try
to provide a birds eye view of the various concepts.
2. Agenda
● Microservices
● What ?
● Why ?
● Challenges of a distributed system
● Building Microservices using Spring
● Spring Boot
● Spring Cloud & Netflix OSS
● Conclusion
3. All Roads Lead to Microservices
Cloud
aPaaS
Continuous Delivery
Dev Ops
Agile
Microservices
4. Microservices – What ?
The term "Microservice Architecture" has sprung up over the
last few years to describe a particular way of designing
software applications as suites of independently deployable
services.
-Martin Fowler
http://martinfowler.com/articles/microservices.html
Microservices are loosely coupled service oriented
architecture with bounded contexts.
- Adrian Cockroft
5. Monolith Application
● Imaginary E-Commerce App
● Very efficient inter process calls between
components
● Good architecture, until it needs to scale
during busy shopping season.
6. Monolith Application
● You want to scale only 'Shopping
Cart' and 'Checkout' Modules,
but instead have to scale the
entire App
● Other Challenges with multiple
database and Session
management
7. Monolith Application – Good Parts
● Contains Everything
● Single Deployment
● Minimum viable product can
be reached quickly
● Easy (or Familiar) to understand
8. Monolith Application – Bad Parts
● Complex Architecture
● Difficult to Scale
(cannot scale only selected
modules)
● Long term commitment
to one technical stack
9. Microservices
● Distillation of SOA
● Single Context
● Smart Services / Dumb Pipes
● RESTful Communications
● Smaller codebase – easy to
reason about
● Easy to scale
11. Challenges of distributed system
● Configuration Management
● Service Registration & Discovery
● Load Balancing
● Fault Tolerance
● Monitoring
● Concurrent API Integration & Transformation
*(This is not a complete list...)
12. Spring Boot
● Built on top of Spring framework
● Opinionated convention over configuration
● Creates stand-alone “Production” ready app
● Embedded Tomcat / Jetty
● Various Starters POMs to simplify configuration
● https://start.spring.io/ - Spring Initializr template project (We can
replace our kick start scripts with something like this)
13. Spring Boot
● It can get pretty small..
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
Exercise 1 : Spring Boot Hello World
14. Spring Boot
● Retrieve data from database – Spring Data REST
● Entity Class
@Entity
@Table(name="users")
public class User implements Serializable{... }
● Repository Class
@Repository
public interface UserRepository extends JpaRepository<User, Integer> { }
● Main Class
@SpringBootApplication
public class SampleDataJpaApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleDataJpaApplication.class, args);
}
}
● The same project runs in local and Cloud - PWS
Exercise 2 : Spring Data REST
15. Spring Cloud
● Writing a single service with Spring Boot is easy.
● But things gets complex with managing multiple services
● http://cloud.spring.io/spring-cloud-netflix/ , to the rescue.
● Similar to Spring Data umbrella project.
17. Spring Cloud Config
● Distributed configuration management.
● Provides server and client-side support for externalized configuration.
● Store Configuration properties (YAML content) in remote repository (Git
supported by default)
● Curl -X POST http://localhost:8888/bus/refresh, to get the latest values from
the config server.
Exercise 3 : Config Server
Cloud Bus (AMQP / RabbitMQ)
Git (default)
properties
Config Server
/refresh
/refresh
18. Service Registry & Discovery - Eureka
● Eureka is Netflix OSS Project for Service Discovery Server & Client.
● Eureka Server – can be configured to be highly available, supports server
replication for resiliency.
● Clients registers with Eureka and sends heartbeat messages to Server.
● Eureka Server
@EnableEurekaServer
class Eureka {
}
● Eureka Client
@EnableDiscoveryClient
public class Application {... }
● Producer Consumer example
Exercise 4 : Eureka – Producer / Consumer
19. Load balancing - Ribbon
● Ribbon is a Netflix OSS Project, it provides software load balancers with
cluster of servers.
● Provides basic functionality like supply the public DNS name of servers
to client, rotate among the list of servers according to certain logic.
● Load balancer components
Rule – logic to determine which server to return from the serverlist.
Ping – to ensure server liveness
ServerList – can be static or dynamic
● Spring RestTemplate has been added with little bit of magic and made
Ribbon enabled.
● FeignClient is alternative to Spring RestTemplate, Feign is declarative
web service client, makes writing web service clients easier.
Exercise 5 : Producer / Consumer example with load balancer / Feign
20. Fault Tolerance - Hystrix
● Hystrix – Circuit Breaker Pattern
● Its a state machine, with 3 states – Closed, Open and Half-Open.
● Failures does not cascade up to the top of the call stack.
Exercise 6 : Hystrix in action
21. Monitoring
● Hystrix Dashboard
Import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard
@EnableHystrixDashboard
class HystrixDashboard { … }
Exercise 7 : Hystrix Dashboard in action
22. Concurrent API Integration - RxJava
● RxJava is Java implementation of MS
Reactive Extensions.
● A library for composing asynchronous
and event-based programs by using
Observable sequences.
● Netflix created RxJava to simplify
server side concurrency, a single
“heavy” client request that is executed
in parallel on the server.
● The service layer API method return an Observable<T> getData(), this can be
asynch or synch, and its transparent to the caller of this service.
● Can be achieved using Java Futures, but it supports one value, not for a
sequences. Also calling Future.get() will be a blocking.
● Callbacks can be used for asynchronous execution and works well for single level
of execution, but things gets complicated with nested Callbacks (callback hell !).
23. Conclusion
● Microservices Architectural Style needs certain infrastructure competencies
like, Rapid Provisioning, Continuous Delivery, Basic Monitoring, etc.,
● There are many new design patterns to learn when implementing a
distributed system.
● Spring Cloud Netflix implements certain patterns and Spring Boot makes it
easy to use.