201505 Tokyo Meetup

download 201505 Tokyo Meetup

of 44

description

Cloud Foundry

Transcript of 201505 Tokyo Meetup

  • Unless otherwise indicated, these slides are 2013-2014Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    From Zero to Hero with Spring Boot

    Stphane Nicoll Pivotal

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Hello!

    2 @snicoll

    https://github.com/snicoll

    @snicoll

    [email protected]

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/3 @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/4 @snicoll

    Spring Boot lets you pair-program with the Spring team. Josh Long, @starbuxman

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Introduction to Spring Boot

    ! Single point of focus (as opposed to large collection of spring-* projects) ! A tool for getting started very quickly with Spring

    ! Common non-functional requirements for a "real" application

    ! Exposes a lot of useful features by default

    ! Gets out of the way quickly if you want to change defaults

    ! An opportunity for Spring to be opinionated

    5 @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    _ Spring Boot is NOT

    ! A prototyping tool

    ! Only for embedded container apps

    ! Sub-par Spring experience

    ! For Spring beginners only

    6 @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Installation

    ! Requirements: JDK6+ Maven 3.2+ / Gradle 1.12+

    ! Tools: Spring Tool Suite (STS) - IntelliJ IDEA - Netbeans Spring CLI (from https://start.spring.io or gvm)

    7 @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Lets get started!

    8 @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Getting Started Really Quickly

    9 @snicoll

    @RestControllerpublic class HomeController {

    @Value("${conference.name:jsug}") private String conference;

    @RequestMapping("/") public String home() { return "Hello " + conference; }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    First integration test

    10 @snicoll

    @RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = DemoApplication.class)@WebIntegrationTest(randomPort = true)public class HomeControllerIntegrationTest {

    @Value("${local.server.port}") private int port;

    @Test public void runAndInvokeHome() { String url = "http://localhost:" + port + "/"; String body = new RestTemplate() .getForObject(url, String.class); assertThat(body, is("Hello jsug")); }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add Spring Data JPA

    11 @snicoll

    org.springframework.boot spring-boot-starter-data-jpa

    com.h2database h2 runtime

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add a Speaker entity

    12 @snicoll

    @Entitypublic class Speaker { @GeneratedValue @Id private Long id; private String firstName; private String lastName; private String twitter; @Column(columnDefinition = "TEXT") private String bio;

    public Speaker(String firstName, String lastName, String twitter) { // initialize attributes }

    // getters and setters}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add a Speaker Repository

    13 @snicoll

    public interface SpeakerRepository extends CrudRepository {

    Speaker findByTwitter(String twitter);

    Collection findByLastName(String lastName);}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Test that repository

    14 @snicoll

    @RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = DemoApplication.class)public class SpeakerRepositoryTest {

    @Autowired private SpeakerRepository speakerRepository;

    @Test public void testFindByTwitter() throws Exception { Speaker stephane = speakerRepository.save( new Speaker("Stephane", "Nicoll", "snicoll")); assertThat(speakerRepository.findByTwitter("snicoll").getId(), is(stephane.getId())); }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add Spring Data REST

    15 @snicoll

    org.springframework.boot spring-boot-starter-data-rest

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    RESTful repository

    16 @snicoll

    public interface SpeakerRepository extends CrudRepository {

    @RestResource(path = "by-twitter") Speaker findByTwitter(@Param("id") String twitter);

    Collection findByLastName(@Param("name") String lastName);}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add Spring Security

    17 @snicoll

    org.springframework.boot spring-boot-starter-security

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Spring Security config

    18 @snicoll

    @Configurationpublic class SecurityConfig extends GlobalAuthenticationConfigurerAdapter {

    @Override public void init(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("hero").password("hero").roles("HERO", "USER").and() .withUser("user").password("user").roles("USER"); }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Fix our test

    19 @snicoll

    @RunWith(SpringJUnit4ClassRunner.class)@SpringApplicationConfiguration(classes = DemoApplication.class)@WebIntegrationTest(randomPort = true)public class HomeControllerIntegrationTest {

    @Value("${local.server.port}") private int port;

    @Test public void runAndInvokeHome() { String url = "http://localhost:" + port + "/"; String body = new TestRestTemplate("user", "user") .getForObject(url, String.class); assertThat(body, is("Hello jsug")); }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add Spring Boot actuator

    20 @snicoll

    org.springframework.boot spring-boot-starter-actuator

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add a health indicator

    21 @snicoll

    @Beanpublic HealthIndicator jsugHealthIndicator() { return () -> { if (new Random().nextBoolean()) { return Health.up().build(); } else { return Health.down().withDetail("Boooo",42).build(); } };}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add remote shell

    22 @snicoll

    org.springframework.boot spring-boot-remote-shell

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add a speaker controller

    23 @snicoll

    @Controllerpublic class SpeakerController {

    private final SpeakerRepository speakerRepository;

    @Autowired public SpeakerController(SpeakerRepository speakerRepository) { this.speakerRepository = speakerRepository; }

    @RequestMapping("/ui/speakers/{id}") public String show(@PathVariable Long id, ModelMap model) { Speaker speaker = speakerRepository.findOne(id); if (speaker == null) { throw new SpeakerNotFoundException(); } model.put("speaker", speaker); return "speakers/show"; }

    @ResponseStatus(HttpStatus.NOT_FOUND) public static class SpeakerNotFoundException extends RuntimeException { }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Create speaker show view

    24 @snicoll

    View speaker

    Stephane Nicoll Sample Biography. @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Best experience with PaaS

    ! Spring Boot features get a lot done for 12factor.net

    ! PaaS friendly: fast startup, devops oriented

    25 @snicoll

    $ mvn package$ cf push jsug -p target/jsug-0.0.1-SNAPSHOT.jar$ cf scale jsug -i 4

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Starter POMs

    ! standard POM / gradle files: define dependencies

    ! many available: batch, integration, web, ampq

    ! starter data-jpa = spring-data-jpa + hibernate

    26 @snicoll

    org.springframework.boot spring-boot-starter-web

    compile("org.springframework.boot:spring-boot-starter-web")

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Writing your own starter

    ! Add support for X in Boot with a PR!

    ! Distribute a client lib in your company

    ! Standardize usage of component X in a platform

    ! [your use case here]

    27 @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Lets write our own starter!

    28 @snicoll

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    New auto-config project

    ! Create a new hello-service-auto-configuration project

    ! Only one mandatory dependency

    ! Should contain specific dependencies and auto-configuration classes

    29 @snicoll

    org.springframework.boot spring-boot-autoconfigure

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add custom service interface

    ! This is the part that were trying to auto-configure

    ! In a typical use case, this interface comes from a 3rd party lib

    30 @snicoll

    public interface HelloService {

    String sayHello();

    }

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Create a default implementation

    ! This default implementation will be shipped with the auto-config project but should not be used if the application provides one.

    31 @snicoll

    public class ConsoleHelloService implements HelloService {

    public String sayHello() { System.out.println("Hello Console"); }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Auto-configuration example

    32 @snicoll

    @Configuration@ConditionalOnClass(HelloService.class)public class HelloServiceAutoConfiguration {

    @ConditionalOnMissingBean @Bean public HelloService helloService() { return new ConsoleHelloService(); }

    }

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Declare the auto-configuration

    33 @snicoll

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\demo.hello.HelloServiceAutoConfiguration

    // one can order AutoConfigurations// with those annotations@AutoConfigureBefore@AutoConfigureAfter

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add a dependency to our auto-configuration

    ! Add our auto-config project as a dependency in our main project

    34 @snicoll

    org.test.jsug hello-service-auto-configuration ...

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Invoke our service when the application starts

    ! A Hello Service implementation is used on startup

    ! Running this will use the default implementation

    35 @snicoll

    @Componentpublic class Startup implements CommandLineRunner { @Autowired private HelloService helloService;

    @Override public void run(String... args) throws Exception { helloService.sayHello(); }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Override default (auto-configured) implementation

    ! Add a @Bean definition in our DemoApplication class

    ! We provide our own implementation, so the default one wont be created

    36 @snicoll

    @Beanpublic HelloService helloService() {

    return () -> LoggerFactory.getLogger(DemoApplication.class) .info("Hello from logs"); }

    }

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Add type-safe properties

    37 @snicoll

    @ConfigurationProperties("hello")public class HelloProperties {

    private String prefix = "Hello ";

    private String target;

    // getters and setters}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Enable properties support

    38 @snicoll

    @EnableConfigurationProperties(HelloProperties.class)@Configuration@ConditionalOnClass(HelloService.class)public class HelloServiceAutoConfiguration {

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Use type-safe properties

    39 @snicoll

    public class ConsoleHelloService implements HelloService {

    @Autowired private HelloProperties properties;

    @Override public void run(String... strings) throws Exception { System.out.println(properties.getPrefix() + " " + properties.getTarget()); }}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Generate properties meta-data

    ! annotation processor that generates meta-data

    ! uses Javadoc to build keys descriptions

    ! default values detected

    ! manual declaration allowed

    40 @snicoll

    org.springframework.boot spring-boot-configuration-processor true

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Document properties

    41 @snicoll

    @ConfigurationProperties("hello")public class HelloProperties {

    /** * Prefix of welcome message. */ private String prefix = "Hello "; /** * Target of welcome message. */ private String target;

    // getters and setters}

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    https://spring.io powered by

    42 @snicoll

    github.com/spring-io/sagan

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

    Links

    ! https://projects.spring.io/spring-boot/

    ! https://github.com/spring-projects/spring-boot

    ! https://spring.io/guides

    ! https://spring.io/blog

    ! https://spring.io/questions

    ! https://www.youtube.com/user/SpringSourceDev

    43 @snicoll

    @springboot @springcentral

  • Unless otherwise indicated, these slides are 2013-2015Pivotal Software, Inc. and licensedunder a

    Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/44

    Learn More. Stay Connected.

    ! Start your project at start.spring.io

    ! Getting started guides

    ! Follow @springboot

    ! StackOverflow - #spring-boot

    Twitter: twitter.com/springcentral

    YouTube: spring.io/video

    LinkedIn: spring.io/linkedin

    Google Plus: spring.io/gplus

    @snicoll