microservices with netflix oss and spring cloud - dev day orange

41
Microservices with Netflix OSS & Spring Cloud Arnaud Cogoluègnes Lyon, October 6th, 2015

Upload: acogoluegnes

Post on 15-Apr-2017

1.387 views

Category:

Technology


8 download

TRANSCRIPT

Microservices withNetflix OSS & Spring Cloud

Arnaud CogoluègnesLyon, October 6th, 2015

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

Speaker: Arnaud Cogoluègnes

Netflix OSS and Spring Cloud aren’t

limited to the cloudinfrastructure or container solutions

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 server

Frontend

Backend(instance 1)

Backend(instance 2)

Service registry(Eureka)

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

}

}

Circuit breaker

Frontend

Backend(instance 1)

Backend(instance 2)

Service registry(Eureka)

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);

}

}

Hystrix dashboard

Source: https://github.com/Netflix/Hystrix

Hystrix dashboard explained

Source: https://github.com/Netflix/Hystrix

Hystrix dashboard aggregation

Source: https://github.com/Netflix/Hystrix

Source: https://github.com/Netflix/Hystrix

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

HTTP

Service 1

Service 2

Service 3

Configuration serverGit repository

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

Configuration on the client side

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

Questions?

Thank you!