Upload
acogoluegnes
View
1.387
Download
8
Embed Size (px)
Citation preview
Microservices withNetflix OSS & Spring Cloud
Arnaud Cogoluègnes
Spring Framework
Spring Boot
Spring Cloud
Eureka, Hystrix, Zuul, Ribbon
Service discoveryCircuit breaker
Load balancing
API gateway
Configuration server
Netflix OSS and Spring Cloud are
application frameworksalso valid for traditional applications
open source
Docker Container Docker Container
Where does it fit?
Spring Boot Application(Netflix OSS & Spring Cloud)
Java Virtual Machine
Eureka Service Registry
Java Virtual Machine
Infrastructure(Mesos, vanilla datacenter, VM, Cloud Foundry, AWS, laptop)
The use case
Frontend
Backend(instance 1)
Backend(instance 2)
Service registry(Eureka)
Discovers
Registers
Registers
Balances
Wishlist
PackagingSecurity
Service discovery
Monitoring
Continuous deployment
Resiliency
Centralized logging
Centralized configuration
Netflix stack
Eureka (service registry)Hystrix (circuit breaker)
Ribbon (client load balancer)Zuul (proxy)
....
Spring Cloud
Built on top of Spring BootSpring-ifies some nifty libraries (e.g. Netflix)Provides goodies (e.g. configuration server)Pretty much all you need for microservices
Spring Boot
Spring Framework for the massesNo XML, no container (as you wish)
All the Spring stuff:Dependency injection, transaction
management, REST, ...
Eureka server with Spring Boot
@SpringBootApplication
@EnableEurekaServer // activates Eureka
public class EurekaServer {
public static void main(String[] args) {
SpringApplication.run(EurekaServer.class, args);
}
}
Eureka client with Spring Cloud
@SpringBootApplication
@EnableEurekaClient // application registers to Eureka
public class BackendServiceApplication {
public static void main(String[] args) {
SpringApplication.run(BackendServiceApplication.class, args);
}
}
Eureka client
Frontend
Backend(instance 1)
Backend(instance 2)
Service registry(Eureka)
Discovers
Registers
Registers
REST client call
@Repository
public class ContactRepository {
@Autowired RestTemplate restClient;
public ContactsResponse contacts() {
ContactsResponse response = restClient.getForObject(
"http://backend-service/contacts", // host = service name
ContactsResponse. class
);
response.setOk( true);
return response;
}
Client load balancer: Ribbon
Handles HTTP requestsBalances load and detects failures
Resolves services from Eureka
Client load balancing
Frontend
Backend(instance 1)
Backend(instance 2)
Service registry(Eureka)
Balances
Circuit breaker: Hystrix
Why? To prevent cascading failureHow? async, detect failures, open/close
Where? Around services calls
Hystrix with Spring Cloud
@Repository
public class ContactRepository {
@HystrixCommand(fallbackMethod = "contactsFailure")
public ContactsResponse contacts() {
// real call (protected by circuit breaker)
}
public ContactsResponse contactsFailure() {
// fallback, when real call fails
}
}
Hystrix activation
@SpringBootApplication
@EnableCircuitBreaker // protects targeted methods
@EnableEurekaClient
@EnableHystrixDashboard // enables dashboard
public class FrontApplication {
public static void main(String[] args) {
SpringApplication.run(FrontApplication.class,args);
}
}
JavaScript application
Browser
Static content
Frontend(app.js) Backend service
REST calls
CORS?Security?
API gateway (reverse proxy)
Browser
Static content
Frontend(app.js)
Backend service
REST calls
REST calls forwarding
API gateway with Spring Cloud
Browser
Static content
Frontend(app.js)
Backend service
REST calls
REST calls fowarding
Handled by Netflix’s Zuul in frontend
Eureka-aware API gateway
Browser
Static content
Frontend(app.js)
Backend service
REST calls
REST calls forwarding Eureka
Discovers
Registers
Zuul API gateway activation
@SpringBootApplication
@EnableCircuitBreaker
@EnableEurekaClient
@EnableHystrixDashboard
@EnableZuulProxy // activates Zuul API gateway
public class FrontApplication {
public static void main(String[] args) {
SpringApplication.run(FrontApplication.class,args);
}
}
Configuration server
The only local parameter of a serviceFor DB URL, password, thread pool
size, etc.
Configuration server in Spring Boot
@SpringBootApplication
@EnableConfigServer
public class ConfigurationServer {
public static void main(String[] args) {
SpringApplication.run(ConfigurationServer.class,args);
}
}
From files to HTTP endpoints
$ tree --charset ascii
.
|-- application.yml
|-- backend-service.yml
`-- front-application.yml
Packaging with Docker
mvn clean package
# Dockerfile
FROM java:openjdk-8u45-jre
MAINTAINER [email protected]
RUN mkdir /microservice
ADD ./target /microservice
WORKDIR /microservice
ENTRYPOINT ["java", "-jar", "/microservice/backend-service-1.0.jar"]
Wishlist
PackagingSecurity
Service discovery
Monitoring
Continuous deployment
Resiliency
Centralized logging
Centralized configuration
Summary
Mature, battle-tested librariesHelp to implement microservices architecture
Transparent for the developper