cloud native microservices with spring cloud

55
CLOUD NATIVE MICROSERVICES WITH SPRING CLOUD CONOR SVENSSON

Upload: conor-svensson

Post on 09-Jan-2017

552 views

Category:

Technology


7 download

TRANSCRIPT

Page 1: Cloud Native Microservices with Spring Cloud

CLOUD NATIVE MICROSERVICES WITH SPRING CLOUD

CONOR SVENSSON

Page 2: Cloud Native Microservices with Spring Cloud
Page 3: Cloud Native Microservices with Spring Cloud
Page 4: Cloud Native Microservices with Spring Cloud

SPRING BOOT

SPRING BOOT

▸ Stand-alone Spring-based applications

▸ Tomcat embedded container (supports Jetty & JBoss Undertow too)

▸ Starter POMs

▸ Annotation driven

▸ Java Configuration Beans

Page 5: Cloud Native Microservices with Spring Cloud

SPRING BOOT

Page 6: Cloud Native Microservices with Spring Cloud

SPRING BOOT

CONFIGURATION BEANS

Page 7: Cloud Native Microservices with Spring Cloud

SPRING BOOT

DEPLOYMENT

▸ Self contained jar

▸ Web application archive

▸ Build targets

▸ $ mvn spring-boot:run

▸ $ gradle bootRun

Page 8: Cloud Native Microservices with Spring Cloud

SPRING BOOT

TESTING

▸ spring-boot-starter-test starter POM provides:

▸ Spring Test

▸ Unit

▸ Hamcrest + Assert4J (v1.4)

▸ Mockito

▸ MockMvc

▸ @WebIntegrationTest

Page 9: Cloud Native Microservices with Spring Cloud
Page 10: Cloud Native Microservices with Spring Cloud

ACME HOME LOANS

Page 11: Cloud Native Microservices with Spring Cloud

ACME HOME LOANS

LOAN APPLICATION

EXTERNAL CREDIT

SERVICE

WEBSITE SERVICE

SUBMISSION SERVICE

1

2 3

Page 12: Cloud Native Microservices with Spring Cloud
Page 13: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

SPRING CLOUD

▸ Microservice friendly components

▸ Distributed & versioned configuration

▸ Service discovery

▸ Dynamic routing

▸ Circuit breakers

▸ Distributed messaging

▸ Getting started:

Page 14: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

SPRING CLOUD

▸ Config

▸ Netflix

▸ Bus

▸ Cloud Foundry

▸ Cluster

▸ Consul

▸ Security

▸ Sleuth

▸ Data Flow

▸ Stream

▸ Modules

▸ Task

▸ Zookeeper

▸ AWS

▸ Connectors

▸ CLI

Page 15: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

CONFIG SERVER

▸ Git hosted configuration repository

▸ SVN & filesystem also supported (see implementations of org.springframework.cloud.config.server.EnvironmentRepository)

▸ Multiple security options w/Spring Security (HTTP Basic -> OAuth bearer tokens)

▸ Push updates via Spring Cloud Bus

Page 16: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

CONFIG SERVER

Page 17: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

CONFIG SERVER CONFIGURATION

application.yml:

Page 18: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

CLIENT CONFIGURATION FILE

website.yml:

Page 19: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

RESOURCE FORMAT

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

(label: master by default)

Page 20: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

RESOLVED CONFIGURATION

Page 21: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

SHARED RESOURCES

▸ Place in application.yml in root of configuration repo

▸ Profile specific configuration always takes precedence over shared

▸ E.g. Eureka server (more on this shortly)

Page 22: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

CLIENT CONFIGURATION

bootstrap.yml:

Page 23: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

CLIENT PROFILES

▸ Annotate classes to associate with a profile

▸ @Profile(“…”)

▸ Configuration (bootstrap/application properties)

▸ spring.profiles.active = …

▸ Command line

▸ -Dspring.profiles.active=…

▸ Environment variable

▸ SPRING_PROFILES_ACTIVE=…

Page 24: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

RESOLVED CLIENT CONFIGURATION

Page 25: Cloud Native Microservices with Spring Cloud

SPRING CLOUD

CONFIG SERVER GOTCHAS

▸ Client’s don’t fail on Config Server failure - boot with defaults (e.g. port 8080)

▸ To enable use spring.cloud.config.failFast=true

▸ Enable retries:

▸ Add spring-retry and spring-boot-starter-aop

▸ spring.cloud.config.retry.

Page 26: Cloud Native Microservices with Spring Cloud
Page 27: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

SPRING CLOUD NETFLIX

▸ Service discovery (Eureka)

▸ Client side load balancing (Ribbon)

▸ Dynamic routing (Zuul)

▸ Circuit breaker (Hystrix)

▸ + a few others…

Page 28: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

EUREKA

▸ Service discovery client & server

▸ Maintains registry of clients with metadata

▸ Host/port

▸ Health indicator URL

▸ Client heartbeats (30 sec default - changing not encouraged)

▸ Lease renewed with server

▸ Service available when client & server(s) metadata cache all in sync

▸ Can take up to 3 heart beats

Page 29: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

EUREKA SERVER

Page 30: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

EUREKA SERVER DASHBOARD

Page 31: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

EUREKA CLIENT SETUP

@EnableEurekaClient annotation

application.yml in Config Server repo

Page 32: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

RIBBON

▸ Client side loan balancer

▸ Can delegate to Eureka for server lists

▸ Or list servers

▸ stores.ribbon.listOfServers=… + ribbon.eureka.enabled=false

Page 33: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

RIBBON USAGE

▸ Via RestTemplate

▸ No different to normal usage - Spring Cloud Commons abstraction

▸ Qualifier’s required if using regular & Ribbon enabled RestTemplate

Page 34: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

ZUUL

▸ JVM based router & load balancer

▸ Provides single point of entry to services

▸ Including single point of authentication

▸ By default creates route for every service in Eureka

▸ Refer to http://localhost/credit-service routes to http://credit-service

▸ Filters provide limited entry points to system

Page 35: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

ZUUL SERVER CREATION

‣ Include dependency spring-cloud-starter-zuul

‣ @EnableZuulProxy application annotation

‣ E.g. Allow access only to credit-service

Page 36: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

SIDECAR

▸ Non-JVM access to components via Zuul proxy

▸ Setup Spring Boot application with @EnableSidecar

▸ Configure for your service:

▸ sidecar.port=… + sidecar.health-ui=…

▸ Access all services by Zuul URL (Sidecar running on port 80)

▸ http://localhost/config-server

Page 37: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

HYSTRIX

▸ Circuit breaker

▸ Threshold breached (20 failures in 5 seconds) => breaker kicks in

▸ Default timeout threshold 1 second

▸ Per dependency thread pools

▸ Async command support (not Spring @Async)

▸ Sync or async fallback

Page 38: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

HYSTRIX CLIENT

▸ @EnableCircuitBreaker application annotation

▸ @HystrixCommand on applicable methods

Page 39: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

Page 40: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

HYSTRIX STREAM - PRE-REQUESTS

Page 41: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

HYSTRIX STREAM - POST-REQUESTS

Page 42: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

HYSTRIX DASHBOARD

Page 43: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

HYSTRIX STREAM

Page 44: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

HYSTRIX DASHBOARD

Page 45: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

TURBINE - AGGREGATE MULTIPLE HYSTRIX CLIENTS

Page 46: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

MONITORING

▸ Add dependency spring-boot-actuator

▸ Makes available various application metrics via /metrics endpoint

▸ Integration also available with DropWizard metrics (add dependency)

▸ Spring Cloud

▸ Spectator (supersedes Servo) - metrics collection

▸ Atlas - metrics backend

Page 47: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

Page 48: Cloud Native Microservices with Spring Cloud
Page 49: Cloud Native Microservices with Spring Cloud

CLOUD FOUNDRY

CLOUD FOUNDRY

▸ Cloud Foundry

▸ PAAS

▸ Supports multiple languages & frameworks

▸ Manages VM containers for deployments within a “space”

▸ Multiple spaces for different environments

▸ Command-line tool (cf)

Page 50: Cloud Native Microservices with Spring Cloud

CLOUD FOUNDRY

SPRING CLOUD FOUNDRY + CONNECTORS

▸ Spring Cloud Foundry

▸ Binding of CF single sign on to your services

▸ Integration with CF Service Discovery

▸ Spring Cloud Connectors

▸ Connectors for Spring Cloud on CF

▸ Config Server

▸ Eureka (Service Discovery)

▸ Hystrix (Circuit Breaker)

Page 51: Cloud Native Microservices with Spring Cloud

CLOUD FOUNDRY

CLOUD FOUNDRY DEPLOYMENT

▸ Signup to preferred CF platform

Page 52: Cloud Native Microservices with Spring Cloud

CLOUD FOUNDRY

CLOUD FOUNDRY DEPLOYMENT

▸ Create Spring Cloud components in CF provider

▸ Config Server

▸ Eureka (Service Discovery)

▸ Hystrix (Circuit Breaker)

▸ Update client applications

▸ CF provider client libraries (see https://docs.pivotal.io/spring-cloud-services/index.html)

▸ Deploy

▸ cf push AcmeWebsite  -p website/target/website-0-SNAPSHOT.jar

Page 53: Cloud Native Microservices with Spring Cloud

ACME HOME LOANS

Page 54: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

THANKS

[email protected]

Page 55: Cloud Native Microservices with Spring Cloud

NETFLIX OSS + SPRING

RESOURCES

▸ Spring Cloud documentation - http://projects.spring.io/spring-cloud/spring-cloud.html

▸ Spring Boot sample projects - https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples

▸ Spring Cloud sample projects - https://github.com/spring-cloud-samples

▸ Hystrix annotations - https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica

▸ Useful demonstration of Spring Cloud usage - http://callistaenterprise.se/blogg/teknik/2015/05/20/blog-series-building-microservices/

▸ Creation of Spring Cloud Components on Pivotal Web Services https://docs.pivotal.io/spring-cloud-services/index.html

▸ Code to accompany this talk - https://github.com/conor10/homeloans

▸ https://www.huffle.com.au

▸ Ether Mining 101, SydEthereum Meetup @Tyro Fintech Hub, Thursday 30th June - http://bit.ly/28KdbgW