42
Javi Rodriguez @jarodllo Ingeniero Software Co-Organizador Grupo Java Valencia @vlc_jug

De zero a 100 con Spring Boot

Embed Size (px)

Citation preview

Page 1: De zero a 100 con Spring Boot

Javi Rodriguez

@jarodllo

• Ingeniero Software

• Co-Organizador Grupo Java Valencia

@vlc_jug

Page 2: De zero a 100 con Spring Boot
Page 3: De zero a 100 con Spring Boot

De zero a 100 con Spring Boot

Spring Cloud, JTwig y algo más….

Page 4: De zero a 100 con Spring Boot

¿Qué vamos a ver?1. Contexto de Spring Boot 2. Introducción a Spring Boot 3. Requisitos mínimos 4. Spring Boot 5. Ejemplos 6. Arquitectura MicroServicios 7. Spring Cloud

Page 5: De zero a 100 con Spring Boot

Esquema conceptual básico

Page 6: De zero a 100 con Spring Boot

PHIL WEBB How not to hate Spring in 2016

Page 7: De zero a 100 con Spring Boot

Contexto

Page 8: De zero a 100 con Spring Boot
Page 9: De zero a 100 con Spring Boot

• Spring IO Spring One 2013

Punto de encuentro desarrolladores

• Cohesionada y versionda para el desarrollo de aplicaciones modernas

• Spring IO es 100% opensource, liviano y modular

• Spring IO Foundation y Spring IO Execution IO Coordination

Page 10: De zero a 100 con Spring Boot

Introducción

Page 11: De zero a 100 con Spring Boot

• Presentado en SpringOne 2013

• 1.4.0

• 1.3.3 current

• Es “opinionated framework”

• Funcionalidad out-of-the-box

• Herramienta para empezar aplicaciones de forma rápida

• Forma parte del ecosistema SPRING

• Permite cambiar los valores por defecto rápidamente

Page 12: De zero a 100 con Spring Boot

• Spring Boot < > Spring Framework

• Diferentes objetivos

• Diferentes dependencias

• Diferente release schedule

• Nos ofrece herramientas de monitorización • health checking • metrics endpoints

• Evita configurar multiples XML

Page 13: De zero a 100 con Spring Boot

¿Cómo empezamos?

Page 14: De zero a 100 con Spring Boot

• Java >= “1.6”

• Maven >=3 || Gradle >=1.6

• Spring Initializr

• Spring Boot CLI

spring init -dweb,data-jpa,h2,thymeleaf --build maven valenciaJug

Page 15: De zero a 100 con Spring Boot

https://start.spring.io/ SPRING INITIALIZR

Page 16: De zero a 100 con Spring Boot

Starters• Conjunto de dependencias

• recomendadas, consistentes

• spring-boot-starter-* vs *-spring-boot-starter

• Disponible para web, batch, integration, data, mail, redis, social-facebook, …

• spring-boot-starter-actuator

• spring-boot-starter-remote-shell

Page 17: De zero a 100 con Spring Boot

Dependencias transitivas <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

<exclusions>

<exclusion>

<groupId>com.fasterxml.jackson.core</groupId>

</exclusion>

</exclusions> </dependency>

Page 18: De zero a 100 con Spring Boot

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.4.3</version>

</dependency>

Dependencias transitivas

Page 19: De zero a 100 con Spring Boot

Spring Boot Actuator Production-ready features

• Spring Boot incluye funcionalidad monitorizar y controlar tu application

• HTTP endpoints, JMX, remote (SSH or Telnet)

• /mappings, /health, /env, /beans

Page 20: De zero a 100 con Spring Boot

Externalizar Configuración

• Ficheros YAML que permite un formato jerárquico.

• SnakeYAML —> via spring-boot-starter.

Page 21: De zero a 100 con Spring Boot

#application.yml

valenciajug: greeting: hello world!

@ConfigurationProperties(prefix = "valenciajug")public class Greeting { private String greeting; public String getGreeting() { return greeting; } public void setGreeting(String greeting) { this.greeting = greeting; }

@Configuration @EnableConfigurationProperties(Greeting.class) public class MyConfiguration {}

@Autowiredprivate Greeting greeting;

Page 22: De zero a 100 con Spring Boot
Page 23: De zero a 100 con Spring Boot
Page 24: De zero a 100 con Spring Boot

@RestController class ThisWillActuallyRun {

@RequestMapping("/") String hello() {

"Hello ValenciaJug!”; }

}

$ spring run app.groovy

Page 25: De zero a 100 con Spring Boot

@RestController class ThisWillActuallyRun {

@RequestMapping("/") String hello() {

"Hello ValenciaJug!”; }

public static void main(String[] args) { SpringApplication.run(ThisWillActuallyRun.class, args);

} }

$ spring run app.groovy

@Grab(“org.springframework.boot:spring-boot-web-starter:0.5.0") @EnableAutoConfiguration

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.*;

Page 26: De zero a 100 con Spring Boot

Estructura básica package com.example.myproject;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

// @Configuration @EnableAutoConfiguration @ComponentScan

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

}

Page 27: De zero a 100 con Spring Boot

JTwig (Modern Java Template Engine)

Page 28: De zero a 100 con Spring Boot

• Flexible

• Fácil de aprender

• Rápido

• Integración con Spring

• Herencia

Modern Java Template Engine

Page 29: De zero a 100 con Spring Boot

{% if variable %} <title> Home Page </title>

{% else %} <title> {{ title }} </title>

{% endif %}

{% for feature in features %}

<li> {{ feature.name }}

</li>

{% endfor %}

Page 30: De zero a 100 con Spring Boot

<!DOCTYPE html> <html> <head> {% block head %} <link rel="stylesheet" href="style.css" /> {% endblock %}

{% block title %}<title>TITULO<title>{% endblock %}

</head> <body> <div id="content">{% block content %}{% endblock %}</div> <div id="footer"> {% block footer %} {% endblock %} </div> </body> </html>

#base.twig

Page 31: De zero a 100 con Spring Boot

{% extends "base.twig" %}

{% block title %}<title>INDEX<title>{% endblock %}

{% block content %} <h1>Index</h1> <p> Hellow World! </p> {% endblock %}

Page 32: De zero a 100 con Spring Boot

Ejemplos

Page 33: De zero a 100 con Spring Boot

ValenciaJUG• Spring Boot MVC • Tomcat 8003 • Jtwig • Mysql

LoginController (/)

UserController (/user)

(/user/{id})UserService User

Page 34: De zero a 100 con Spring Boot

MicroService & Spring Cloud

Page 35: De zero a 100 con Spring Boot
Page 36: De zero a 100 con Spring Boot

• Servicios pequeños

• Escalado

• Despliegues

MicroServiciosBeneficios

Page 37: De zero a 100 con Spring Boot

• Cómo localizo los servicios?

• Que pasa si alguno falla?

• Cómo los configuro?

MicroServiciosDesafíos

Page 38: De zero a 100 con Spring Boot

MicroServicios• Integración con Netflix OSS

• Discovery (Eureka)

• Circuit Breaker (Hystrix)

• Intelligent Routing (Zuul)

• Client Side Load Balancing (Ribbon)

Page 39: De zero a 100 con Spring Boot

Eureka Server

Greeting-Service ValenciaJUG

• Server (Eureka) • Localhost: 8001 • @EnableEurekaServer

• Service Discovery (Eureka) • Localhost: 8002 • @EnableEurekaClient

• Service Discovery (Eureka) • Localhost: 8003 • @EnableEurekaClient

Page 40: De zero a 100 con Spring Boot

Ejemplos

40

Page 41: De zero a 100 con Spring Boot

Conclusiones

• Herramienta potente y productiva

• No implica “nuevos” conocimientos

• Constante evolución

• Estamos muy acoplados al framework

• Documentación

Page 42: De zero a 100 con Spring Boot

GRACIAS!!