Spring Boot - Wird Spring jetzt wirklich einfach?

34
SPRING BOOT Wird Spring jetzt wirklich einfach? Jens Presser

Transcript of Spring Boot - Wird Spring jetzt wirklich einfach?

Page 1: Spring Boot - Wird Spring jetzt wirklich einfach?

SPRING BOOTWird Spring jetzt wirklich einfach?

Jens Presser

Page 2: Spring Boot - Wird Spring jetzt wirklich einfach?

INHALTE1. Hintergrund2. Was ist Spring Boot?3. Wie funktioniert es?4. Demo

Page 3: Spring Boot - Wird Spring jetzt wirklich einfach?

HINTERGRUND

Page 4: Spring Boot - Wird Spring jetzt wirklich einfach?
Page 5: Spring Boot - Wird Spring jetzt wirklich einfach?

Herausforderungen

DependenciesVersionenSpring KomponentenSpring Klassenhohe Lernkurve"selbstgemachte" Spring-Konfigurationen

yeah, sure, you just connect your TransactionAwareConnectionFactoryProxyto your UserCredentialsConnectionFactoryAdapter to your ..

Page 6: Spring Boot - Wird Spring jetzt wirklich einfach?

Folgen

BoilerplateGoogle/Stackoverflow Driven DevelopmentCopy&Paste aus anderen Projekten

Page 7: Spring Boot - Wird Spring jetzt wirklich einfach?

WAS IST SPRING BOOT?

Page 8: Spring Boot - Wird Spring jetzt wirklich einfach?

Spring Boot Mission Statement

Spring Boot makes it easy to create stand-alone, production-grade Spring based

Applications that you can "just run". We takean opinionated view of the Spring platform

and third-party libraries so you can getstarted with minimum fuss. Most Spring Boot

applications need very little Springconfiguration.

Page 9: Spring Boot - Wird Spring jetzt wirklich einfach?

Ziele

Vereinfachung von Spring ProjektenOut-of-the-box VerhaltenNichtfunktionale Features bspw. Embedded Servers, Healthchecks, Metrics, Externalized Configuration

Optimierungen in Richtung:MicroservicesCloud-basierte Applikationen12 factor applications

Page 10: Spring Boot - Wird Spring jetzt wirklich einfach?

Exkurs: 12 factor applications

one codebase, many deploysexplicit dependenciesconfiguration in environmentbacking services as attached resourcesseparate build, release and run stagesstateless processexport service via port bindingscale out as processesrobustness with fast startup times, graceful shutdownkeep dev and prod as similar as possiblelogs as event streams (stdout)admin/management tasks as one-off processes

Page 11: Spring Boot - Wird Spring jetzt wirklich einfach?

Features

Dependency Management Maven/GradleAutoConfigurationConvention over ConfigurationExternalisierte KonfigurationProfileAusführbare ArtefakteKeine Bytecode Manipulation oder Codegenerierung

Page 12: Spring Boot - Wird Spring jetzt wirklich einfach?
Page 13: Spring Boot - Wird Spring jetzt wirklich einfach?
Page 14: Spring Boot - Wird Spring jetzt wirklich einfach?

WIE FUNKTIONIERT ES?

Page 15: Spring Boot - Wird Spring jetzt wirklich einfach?

Spring Initializr

Generator für Spring Boot ProjekteSelektion von Spring Boot KomponentenParametriert durch:

Artefakt-Koordinaten (group, artifact)PackagingJava/Spring Boot VersionSprache (Java/Groovy)

http://start.spring.io/

Page 16: Spring Boot - Wird Spring jetzt wirklich einfach?
Page 17: Spring Boot - Wird Spring jetzt wirklich einfach?

Spring Boot Starters

"POM" Projekte für definierte Sets von DependenciesDepencency ManagementZugriff auf:

Spring Portfolio Module z.B. Web, Security, Data

3rd Party z.B. Activiti, Stormpath

Eigene Starters

Page 18: Spring Boot - Wird Spring jetzt wirklich einfach?

Spring Boot Plugins

Plugins für Build-Tools Maven und GradleDependecy (Version) ManagementSelf-executable JARs

enhalten abhängige JARseigener Classloader und Main Klasse

Ausführbarkeit aus dem Build-Tool:

java -jar target/my-spring-boot-demo.jar

mvn spring-boot:run

gradle bootRun

Page 19: Spring Boot - Wird Spring jetzt wirklich einfach?

Spring Booting...

package com.example.myproject;

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

@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }

... that's all!

Page 20: Spring Boot - Wird Spring jetzt wirklich einfach?

AutoConfiguration

Vorgefertigte Spring @ConfigurationsCondition und @Conditional@ConditionalOnClass@ConditionalOnMissingBean@ConditionalOnWebApplication

Page 21: Spring Boot - Wird Spring jetzt wirklich einfach?

@Configuration @ConditionalOnClass(ObjectMapper.class) @ConditionalOnBean(ObjectMapper.class) protected class MappingJackson2HttpMessageConverterConfiguration { @Bean @ConditionalOnMissingBean public MappingJackson2HttpMessageConverter mj2hmc(ObjectMapper objectMapper) { // pseude code, der Name ist zu lang ;-) MappingJackson2HttpMessageConverter converter = new MJ2HMC(objectMapper); if (this.properties.isJsonPrettyPrint() != null) { converter.setPrettyPrint(this.properties.isJsonPrettyPrint()); } return converter; } }

Page 22: Spring Boot - Wird Spring jetzt wirklich einfach?

Externalized Configuration

Spring Boot DefaultsPropertySources in @Configurationapplication.properties in JAR / externapplication-{profile}.properties in JAR / externUmgebungsvariablen

System-Properties

SPRING_APPLICATION_JSON UmgebungsvariableCLI Argumente

SERVER_PORT=8080

-Dserver.port=8080

--server.port=8080

Page 23: Spring Boot - Wird Spring jetzt wirklich einfach?

YAML Format für Konfigurationsdateien (application.yml)Property Placeholder

profilspezifische Konfigurationsdateienapplication.propertiesapplication-default.propertiesapplication-[profile].properties

Typsicherheit, Casting, Namespacing via@ConfigurationProperties

prop.message=Hello ${prop.name}!

spring.profiles.active=prod spring.profiles.include=proddb,prodmq

Page 24: Spring Boot - Wird Spring jetzt wirklich einfach?

@ConfigurationProperties

@Component @ConfigurationProperties(prefix="connection") public class ConnectionSettings { private String username; private InetAddress remoteAddress; // ... getters and setters }

@Autowired private ConnectionSettings connection;

connection.username=demo connection.remoteAddress=192.168.0.1

Page 25: Spring Boot - Wird Spring jetzt wirklich einfach?

Logging

Vordefinierte DefaultsFormate, Loglevel, Appender etc. konfiguriertDefault: Nur CONSOLE AusgabeERROR, WARN, INFO⇒ Keine Log-Konfigurationsdatei nötig

Page 26: Spring Boot - Wird Spring jetzt wirklich einfach?

Log-Konfiguration

Logdatei kann aktiviert werden

Debug über Flag aktivierbar

Log-Level

Log Pattern

logging.file=demo.log logging.path=/data/demo-app/

--debug -Ddebug=true

logging.level.my.package=DEBUG

logging.pattern.console=... logging.pattern.file=...

Page 27: Spring Boot - Wird Spring jetzt wirklich einfach?

DevTools

Default-Properties für Development z.B. spring.thymeleaf.cache=false

Automatic RestartLiveReload Browser SupportRemote Debug Tunneling (via HTTP[S])Remote Update and Restart

Page 28: Spring Boot - Wird Spring jetzt wirklich einfach?

Actuators

production ready featuresHealthChecks z.B. Connections zu DB, MQ oder Diskspace …

Metrics und Metric Export z.B. Uptime, Requests, Memory, Pools, Cache, GC, Heap …

Weitere Endpoints: info, env, dump, shutdown …

JMX (over HTTP)Remote ShellAuditingPID und Port Dateien

Page 29: Spring Boot - Wird Spring jetzt wirklich einfach?

Caveat

KonventionenAutoConfigurationUmgebungsvariablenDefault: Single ConnectionFramework für ein Framework

Page 30: Spring Boot - Wird Spring jetzt wirklich einfach?

DEMO TIME

Page 31: Spring Boot - Wird Spring jetzt wirklich einfach?

Fazit

Page 32: Spring Boot - Wird Spring jetzt wirklich einfach?

IMHO

Schnelles AufsetzenSinnige Defaults80:20 RegelPrototyping?Kenntnis der Konventionen/Automatismen

Page 33: Spring Boot - Wird Spring jetzt wirklich einfach?

VIELEN DANK FÜR EURE AUFMERKSAMKEIT!Fragen oder Anmerkungen?