Spring Boot and Netflix OSS

44
Spring Cloud, Spring Boot and Netflix OSS Spencer Gibb twitter: @spencerbgibb email: [email protected] Dave Syer twitter: @david_syer email: [email protected] (Spring Boot and Netflix OSS or Spring Cloud Components) http://presos.dsyer.com/decks/cloud-boot-netflix.html 1 of 44 24/09/14 21:52

description

Speakers: Spencer Gibb, Dr. David Syer Web / JavaScript Track Learn basic concepts about Spring Boot and Netflix OSS software and how to integrate Netflix OSS technologies into Spring Boot including: distributed configuration using Spring and Archaius Eureka (service registration and discovery) Feign (easy rest clients) Ribbon (client side load balancing) Hystrix (circuit breaker/fault tolerance library) RxJava (Reactive Programming library) Zuul (routing) Turbine and Hystrix Dashboard (service health dashboard)

Transcript of Spring Boot and Netflix OSS

Page 1: Spring Boot and Netflix OSS

Spring Cloud, Spring Boot and NetflixOSS

Spencer Gibbtwitter: @spencerbgibbemail: [email protected]

Dave Syertwitter: @david_syeremail: [email protected]

(Spring Boot and Netflix OSSor Spring Cloud Components)

http://presos.dsyer.com/decks/cloud-boot-netflix.html

1 of 44 24/09/14 21:52

Page 2: Spring Boot and Netflix OSS

Outline

Define microservices

Outline some distributed system problems

Introduce Netflix OSS and its integration with Spring Boot

Spring Cloud demos

http://presos.dsyer.com/decks/cloud-boot-netflix.html

2 of 44 24/09/14 21:52

Page 3: Spring Boot and Netflix OSS

What are micro-services?

Not monolithic :-)

Smaller units of a larger system

Runs in its own process

Lightweight communication protocols

Single Responsibility Principle

The UNIX way

http://www.slideshare.net/ewolff/micro-services-small-is-beautiful http://martinfowler.com/articles/microservices.htmlhttp://davidmorgantini.blogspot.com/2013/08/micro-services-what-are-micro-services.html

http://presos.dsyer.com/decks/cloud-boot-netflix.html

3 of 44 24/09/14 21:52

Page 4: Spring Boot and Netflix OSS

Lightweight Services andREST

There is a strong trend in distributed systems withlightweight architectures

People have started to call them "microservices"

http://presos.dsyer.com/decks/cloud-boot-netflix.html

4 of 44 24/09/14 21:52

Page 5: Spring Boot and Netflix OSS

Spring Boot

It needs to be super easy to implement and update a service:

@RestControllerclass ThisWillActuallyRun { @RequestMapping("/") String home() { Hello World! }}

and you don't get much more "micro" than that.

http://presos.dsyer.com/decks/cloud-boot-netflix.html

5 of 44 24/09/14 21:52

Page 6: Spring Boot and Netflix OSS

Cloudfoundry

Deploying services needs to be simple and reproducible

$ cf push app.groovy

and you don't get much more convenient than that.

(Same argument for other PaaS solutions)

http://presos.dsyer.com/decks/cloud-boot-netflix.html

6 of 44 24/09/14 21:52

Page 7: Spring Boot and Netflix OSS

Continuous Delivery

Microservices lend themselves to continuous delivery.

You actually need continuous delivery to extract maximumvalue.

Book (Humble and Farley): http://continuousdelivery.comNetflix Blog: http://techblog.netflix.com/2013/08/deploying-netflix-api.html

http://presos.dsyer.com/decks/cloud-boot-netflix.html

7 of 44 24/09/14 21:52

Page 8: Spring Boot and Netflix OSS

Example DistributedSystem: Minified

http://presos.dsyer.com/decks/cloud-boot-netflix.html

8 of 44 24/09/14 21:52

Page 9: Spring Boot and Netflix OSS

No Man (Microservice) isan Island

It's excellent to be able to implement a microservice reallyeasily (Spring Boot), but building a system that way surfaces"non-functional" requirements that you otherwise didn'thave.

There are laws of physics that make some problemsunsolvable (consistency, latency), but brittleness andmanageability can be addressed with generic, boiler platepatterns.

http://presos.dsyer.com/decks/cloud-boot-netflix.html

9 of 44 24/09/14 21:52

Page 10: Spring Boot and Netflix OSS

Emergent features ofmicro-services systems

Coordination of distributed systems leads to boiler platepatterns

Distributed/versioned configuration

Service registration and discovery

Routing

Service-to-service calls

Load balancing

Circuit Breaker

Asynchronous

Distributed messaging

http://presos.dsyer.com/decks/cloud-boot-netflix.html

10 of 44 24/09/14 21:52

Page 11: Spring Boot and Netflix OSS

Spring IO Platformhttp://presos.dsyer.com/decks/cloud-boot-netflix.html

11 of 44 24/09/14 21:52

Page 12: Spring Boot and Netflix OSS

Example: CoordinationBoiler Plate

http://presos.dsyer.com/decks/cloud-boot-netflix.html

12 of 44 24/09/14 21:52

Page 13: Spring Boot and Netflix OSS

Bootification

How to bring the ease of Spring Boot to a micro-servicesarchitecture?

Netflix OSS

Consul

etcd

zookeeper

custom

doozerd

ha proxy

nginx

Typesafe Config

and many more... what to choose?

http://presos.dsyer.com/decks/cloud-boot-netflix.html

13 of 44 24/09/14 21:52

Page 14: Spring Boot and Netflix OSS

Netflix OSS

Eureka

Hystrix & Turbine

Ribbon

Feign

Zuul

Archaius

Curator

Asgaard

...

Mikey Cohen Netflix edge architecture, http://goo.gl/M159zi

http://presos.dsyer.com/decks/cloud-boot-netflix.html

14 of 44 24/09/14 21:52

Page 15: Spring Boot and Netflix OSS

Example: Spring Cloudand Netflix

http://presos.dsyer.com/decks/cloud-boot-netflix.html

15 of 44 24/09/14 21:52

Page 16: Spring Boot and Netflix OSS

Configuration Server

Pluggable source

Git implementation

Versioned

Rollback-able

Configuration client auto-configured via starter

http://presos.dsyer.com/decks/cloud-boot-netflix.html

16 of 44 24/09/14 21:52

Page 17: Spring Boot and Netflix OSS

Spring CloudConfiguration Server

Supports applications <appname>.properties

Supports environments <appname>-<envname>.yml

Default environment application.propertiesapplies to all applications and environments

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

17 of 44 24/09/14 21:52

Page 18: Spring Boot and Netflix OSS

Config Client

Consumers of config server can use client library as SpringBoot plugin

Features:

Bootstrap Environment from server

POST to /env to change Environment

@RefreshScope for atomic changes to beans via Springlifecycle

POST to /refresh

POST to /restart

http://presos.dsyer.com/decks/cloud-boot-netflix.html

18 of 44 24/09/14 21:52

Page 19: Spring Boot and Netflix OSS

Environment Endpoint

POST to /env

Re-binds @ConfigurationProperties

Resets loggers if any logging.level changes are detected

Sends EnvironmentChangeEvent with list of propertiesthat changed

http://presos.dsyer.com/decks/cloud-boot-netflix.html

19 of 44 24/09/14 21:52

Page 20: Spring Boot and Netflix OSS

Refresh Endpoint

POST to /refresh

Re-loads configuration including remote config server

Re-binds @ConfigurationProperties

Resets @RefreshScope cache

http://presos.dsyer.com/decks/cloud-boot-netflix.html

20 of 44 24/09/14 21:52

Page 21: Spring Boot and Netflix OSS

RefreshScope

Annotate @Beans

Atomic updates during /refresh

DEMO

@EnableConfigurationProperties(MyProps)public class Application {

@Autowired private MyProps props

@RefreshScope @Bean public Service service() { new Service(props.name) }}

http://presos.dsyer.com/decks/cloud-boot-netflix.html

21 of 44 24/09/14 21:52

Page 22: Spring Boot and Netflix OSS

Restart Endpoint

POST to /restart closes application context andrefreshes it

Probably more useful in development than production(leaks?)

Disabled by default

http://presos.dsyer.com/decks/cloud-boot-netflix.html

22 of 44 24/09/14 21:52

Page 23: Spring Boot and Netflix OSS

Encrypted Properties

Authenticated clients have access to unencrypted data.

Only encrypted data is stored in git.

Support for server side or client side decryption

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

23 of 44 24/09/14 21:52

Page 24: Spring Boot and Netflix OSS

Discovery: Eureka

Service Registration Server

Highly Available

In AWS terms, multi Availability Zone and Region aware

http://presos.dsyer.com/decks/cloud-boot-netflix.html

24 of 44 24/09/14 21:52

Page 25: Spring Boot and Netflix OSS

Eureka Client

Register service instances with Eureka Server

@EnableEurekaClient auto registers instance inserver

Eureka Server

Eureka Client

@EnableEurekaClientpublic class Application {}

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

25 of 44 24/09/14 21:52

Page 26: Spring Boot and Netflix OSS

Circuit Breaker: Hystrix

latency and fault tolerance

isolates access to other services

stops cascading failures

enables resilience

circuit breaker pattern

dashboard

Release It!: https://pragprog.com/book/mnee/release-it

http://presos.dsyer.com/decks/cloud-boot-netflix.html

26 of 44 24/09/14 21:52

Page 27: Spring Boot and Netflix OSS

Declarative Hystrix

Programmatic access is cumbersome

@HystrixCommand to the rescue

@EnableHystrix via starter pom

Wires up spring aop aspect

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

27 of 44 24/09/14 21:52

Page 28: Spring Boot and Netflix OSS

Hystrix Synchronous

private String getDefaultMessage() { return "Hello World Default";}

@HystrixCommand(fallbackMethod="getDefaultMessage")public String getMessage() { return restTemplate.getForObject(/*...*/);}

http://presos.dsyer.com/decks/cloud-boot-netflix.html

28 of 44 24/09/14 21:52

Page 29: Spring Boot and Netflix OSS

Hystrix Future

@HystrixCommand(fallbackMethod="getDefaultMessage")public Future<String> getMessageFuture() { return new AsyncResult<String>() { public String invoke() { return restTemplate.getForObject(/*...*/); } };}

//somewhere elseservice.getMessageFuture().get();

http://presos.dsyer.com/decks/cloud-boot-netflix.html

29 of 44 24/09/14 21:52

Page 30: Spring Boot and Netflix OSS

Hystrix Observable

@HystrixCommand(fallbackMethod="getDefaultMessage")public Observable<String> getMessageRx() { return new ObservableResult<String>() { public String invoke() { return restTemplate.getForObject(/*...*/); } };}

//somewhere elsehelloService.getMessageRx().subscribe(newObserver<String>() { @Override public void onCompleted() {} @Override public void onError(Throwable e) {} @Override public void onNext(String s) {}});

http://presos.dsyer.com/decks/cloud-boot-netflix.html

30 of 44 24/09/14 21:52

Page 31: Spring Boot and Netflix OSS

Circuit Breaker Metrics

Via actuator /metrics

Server side event stream /hystrix.stream

Dashboard app via @EnableHystrixDashboard

More coming...

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

31 of 44 24/09/14 21:52

Page 32: Spring Boot and Netflix OSS

Metric Aggregation:Turbine

Aggregator for Hystrix data

Pluggable locator

Static list

Eureka

http://presos.dsyer.com/decks/cloud-boot-netflix.html

32 of 44 24/09/14 21:52

Page 33: Spring Boot and Netflix OSS

Ribbon

Client side load balancer

Pluggable transport

Protocols: http, tcp, udp

Pluggable load balancing algorithms

Round robin, “best available”, random, response timebased

Pluggable source for server list

Static list, Eureka!

http://presos.dsyer.com/decks/cloud-boot-netflix.html

33 of 44 24/09/14 21:52

Page 34: Spring Boot and Netflix OSS

Feign

Declarative web service client definition

Annotate an interface

Highly customizable

Encoders/decoders

Annotation processors (Feign, JAX-RS)

Logging

Supports Ribbon and therefore Eureka

http://presos.dsyer.com/decks/cloud-boot-netflix.html

34 of 44 24/09/14 21:52

Page 35: Spring Boot and Netflix OSS

Feign cont.

Auto-configuration

Support for Spring MVC annotations

Uses Spring MessageConverter’s for decoder/encoder

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

35 of 44 24/09/14 21:52

Page 36: Spring Boot and Netflix OSS

Feign cont.

public interface HelloClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Message hello();

@RequestMapping(method = RequestMethod.POST, value = "/hello", consumes = "application/json") Message hello(Message message);}

http://presos.dsyer.com/decks/cloud-boot-netflix.html

36 of 44 24/09/14 21:52

Page 37: Spring Boot and Netflix OSS

Routing: Zuul

JVM based router and filter

Similar routing role as httpd, nginx, or CF go router

Fully programmable rules and filters

Groovy

Java

any JVM language

http://presos.dsyer.com/decks/cloud-boot-netflix.html

37 of 44 24/09/14 21:52

Page 38: Spring Boot and Netflix OSS

How Netflix uses Zuul

Authentication

Insights

Stress Testing

Canary Testing

Dynamic Routing

Service Migration

Load Shedding

Security

Static Response handling

Active/Active traffic management

http://presos.dsyer.com/decks/cloud-boot-netflix.html

38 of 44 24/09/14 21:52

Page 39: Spring Boot and Netflix OSS

Spring Cloud Zuul Proxy

Store routing rules in config serverzuul.proxy.route.customers: /customers

uses Hystrix->Ribbon->Eureka to forward requeststo appropriate service

@EnableZuulProxy@Controllerclass Application { @RequestMapping("/") String home() { return 'redirect:/index.html#/customers' }}

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

39 of 44 24/09/14 21:52

Page 40: Spring Boot and Netflix OSS

Configuration: Archaius

Client side configuration library

extends apache commons config

extendible sources

Polling or push updates

DynamicStringProperty myprop =DynamicPropertyFactory.getInstance() .getStringProperty("my.prop");someMethod(myprop.get());

http://presos.dsyer.com/decks/cloud-boot-netflix.html

40 of 44 24/09/14 21:52

Page 41: Spring Boot and Netflix OSS

Archaius: SpringEnvironment Bridge

Auto-configured

Allows Archaius Dynamic*Properties to find valuesvia Spring Environment

Existing Netflix libraries configured via application.{properties,yml} and/or Spring Cloud Config Server

http://presos.dsyer.com/decks/cloud-boot-netflix.html

41 of 44 24/09/14 21:52

Page 42: Spring Boot and Netflix OSS

Spring Cloud Bus

Lightweight messaging bus using spring integrationabstractions

spring-amqp, rabbitmq and httpother implementations possible

Send messages to all services or...

To just one applications nodes (ie just service x)?destination=x

Post to /bus/env sends environment updates

Post to /bus/refresh sends a refresh command

DEMO

http://presos.dsyer.com/decks/cloud-boot-netflix.html

42 of 44 24/09/14 21:52

Page 43: Spring Boot and Netflix OSS

Spring Cloud Starters

spring-cloud-starter spring-cloud-starter-hystrix

spring-cloud-starter-bus-amqp

spring-cloud-starter-hystrix-dashboard

spring-cloud-starter-cloudfoundry

spring-cloud-starter-turbine

spring-cloud-starter-eureka spring-cloud-starter-zuul

spring-cloud-starter-eureka-server

http://presos.dsyer.com/decks/cloud-boot-netflix.html

43 of 44 24/09/14 21:52

Page 44: Spring Boot and Netflix OSS

Links

http://github.com/spring-cloud

http://github.com/spring-cloud-samples

http://blog.spring.io

http://presos.dsyer.com/decks/cloud-boot-netflix.html

Twitter: @spencerbgibb, @david_syer

Email: [email protected], [email protected]

http://presos.dsyer.com/decks/cloud-boot-netflix.html

44 of 44 24/09/14 21:52