Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DDD and Spring Boot

zhanpon
December 15, 2018

DDD and Spring Boot

JJUG CCC 2018 Fallでの発表資料です。

zhanpon

December 15, 2018
Tweet

More Decks by zhanpon

Other Decks in Programming

Transcript

  1. υϝΠϯۦಈઃܭͱ
    Spring BootΛ׆༻ͨ͠
    ΞϓϦέʔγϣϯ։ൃ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  2. ૝ఆ͢ΔϦεφʔ
    • Spring BootΛ৮ͬͨ͜ͱ͕͋Δ
    • DDDʹ͍ͭͯฉ͍ͨ͜ͱ͕͋Δ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  3. ࣗݾ঺հ: @zhanpon
    • 2017೥10݄ɿϠϑʔʹ৽ଔೖࣾ
    • 2018೥4݄ɿJava, Spring BootΛ࢖͍͸͡ΊΔ
    • 2018೥5݄ɿJJUG CCC 2018 SpringʹॳࢀՃ
    ձࣾͰͷओͳ׆ಈ
    • ਪનγεςϜͷ։ൃɾӡ༻
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  4. DDDͱ͸Կ͔
    • υϝΠϯۦಈઃܭ (Domain-Driven Design) ͷུশ
    • Eric Evans ͷ "Domain-Driven Design: Tackling Complexity in the
    Heart of Software" ͱ͍͏ຊ͕ݩʹͳ͍ͬͯΔ
    • ෯޿͍࿩୊͕͋ͬͯҰݴͰ͸આ໌Ͱ͖ͳ͍
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  5. ʢࢲʹͱͬͯʣDDDͱ͸Կ͔
    • ΦϒδΣΫτࢦ޲ϓϩάϥϛϯάΛݱ࣮ͷෳࡶͳγεςϜʹద
    ༻͢ΔͨΊͷํ๏࿦
    • SOLIDͳઃܭΛ࣮ݱ͢ΔͨΊͷσβΠϯύλʔϯʢ୯Ұ੹೚ͷݪ
    ଇɺ։์/ด࠯ͷݪଇ……ʣ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  6. Ξ΢τϥΠϯ
    1. DDDΛ΍ΔϞνϕʔγϣϯ
    2. DDDΛ࣮ફͨ͠ܦݧ
    3. ٕज़తͳTIPS
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  7. Part I: DDDΛ΍ΔϞν
    ϕʔγϣϯ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  8. ࣦഊஊ
    • ۀ຿ΞϓϦέʔγϣϯΛSpring BootͷνϡʔτϦΞϧͷԆ௕Ͱ
    ࣮૷ͨ͠ΒͻͲ͍͜ͱʹͳͬͨ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  9. Α͋͘ΔSpring Bootͷ
    νϡʔτϦΞϧ
    1. ΤϯςΟςΟΛ࣮૷͢Δ
    2. ϦϙδτϦΛఆٛ͢Δ
    3. ίϯτϩʔϥʔΛ࣮૷͢Δ
    4. ׬੒ʂ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  10. ΤϯςΟςΟΛ࣮૷͢Δ
    @Entity
    public class User {
    @Id
    private String id;
    private String fullName;
    // getterͱsetter
    }
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  11. ϦϙδτϦΛఆٛ͢Δ
    public interface UserRepository extends CrudRepository {
    }
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  12. ίϯτϩʔϥʔΛ࣮૷͢Δ
    @RestController
    public class UserController {
    @Autowired
    UserRepository userRepository;
    @GetMapping("/users")
    public List allUsers() {
    return userRepository.findAll();
    }
    }
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  13. ׬੒ʂ
    • ͋ͱ͸ UserController ʹϩδοΫΛॻ͍͍͚ͯ͹OK (?)
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  14. ໰୊఺
    • UserΫϥεͷΧϓηϧԽ͸ʁ
    • UserController ͕ࡍݶͳ͘ෳࡶԽ͢Δ
    • ୯ମςετ͕ࠔ೉
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  15. υϝΠϯϞσϧශ݂঱
    • ୯ͳΔखଓ͖ܕઃܭʹͳͬͯΔ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  16. Part II: DDDΛ࣮ફͨ͠
    ࣮ྫ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  17. ࣾ಺ͷϓϩδΣΫτ
    • ػցֶशͷ੒Ռ෺Λຖ೔ࣗಈͰσϓϩΠ͍ͨ͠
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  18. φΠʔϒʹ࣮૷ͨ͠Β
    • ϦϙδτϦʹΞΫηε͢ΔΫϥε (DockerRegistryAccessor)
    ͱ࣮ߦ؀ڥʹσϓϩΠ͢ΔΫϥε (KubernetesDeployer) Λ༻
    ҙ͢Δ
    • DockerRegistryAccessor ͕࠷৽൛Λऔͬͯ͘Δ
    • KubernetesDeployer ͕ͦΕΛσϓϩΠ͢Δ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  19. φΠʔϒʹ࣮૷ͨ͠Β
    • ͱΓ࣮͋͑ͣ૷Ͱ͖ͦ͏ɻ͔͠͠΋ͬͱෳࡶʹͳͬͨΒʁ
    • UserControllerͷΑ͏ʹͲΜͲΜෳࡶʹͳΔ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  20. ૿͑Δཁ݅
    • ࣗಈσϓϩΠ͢Δ࣌ؒଳΛ੍ݶ͍ͨ͠
    • ٳ೔͸σϓϩΠΛېࢭ͍ͨ͠
    • ࠷৽൛͡Όͳͯ҆͘ఆ൛ΛσϓϩΠ͍ͨ͠
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  21. υϝΠϯϞσϧΛݟग़͢
    • ιϑτ΢ΣΞγεςϜͷ͏ͪɺࣗ෼͕ݩʑղ͖͔ͨͬͨ໰୊Λ
    දݱ͢Δίʔυͷ઎ΊΔྔ͸গͳ͍
    • ͦͷ෦෼͕WebΞϓϦέʔγϣϯϑϨʔϜϫʔΫͳͲͷٕज़త
    ৄࡉʹຒ΋Εͳ͍Α͏ʹ͍ͨ͠
    • υϝΠϯ૚͕ٕज़తৄࡉʹґଘ͠ͳ͍Α͏ʹ͢Δ (DIP)
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  22. ࠓճͷυϝΠϯϞσϧ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  23. Version
    • ఆظతʹੜ੒͞ΕΔػցֶशͷ੒Ռ෺ɻྫɿݘೣ෼ྨثͷ2018
    ೥12݄15೔9࣌όʔδϣϯ
    @Value
    public class Version {
    private final String modelName;
    private final Instant instant;
    }
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  24. VersionSet
    • Setͷϥούʔ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  25. Policy
    public enum Policy {
    NONE((versions, t) -> Optional.empty()),
    LATEST((versions, t) -> {
    return versions.latest();
    })
    private BiFunction> policy;
    Policy(BiFunction> policy) {
    this.policy = policy;
    }
    }
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  26. VersionCatalog
    public interface VersionCatalog {
    VersionSet list(String modelName);
    }
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  27. Deployer
    public interface Deployer {
    void deploy(Version version);
    }
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  28. ࢓༷มߋ
    • ػցֶशͷ੒Ռ෺ΛஷΊΔ৔ॴ͕Docker Registry͔ΒΦϒδΣ
    ΫτετϨʔδʹͳͬͨ
    !
    • VersionCatalog ͷ࣮૷ΫϥεΛม͑Δ͚ͩ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  29. DDDΛ΍ͬͯྑ͔ͬͨ͜ͱ
    • ࢓༷มߋָ͕
    • ςετ͕͠΍͍͢
    • ࣗ෼ͷ໰୊ʹର͢Δཧղ͕ίʔυͱͯ͠දݱ͞ΕΔ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  30. ࠷ޙʹ
    ͢΂ͯͷιϑτ΢ΣΞγεςϜ͸ɺεςʔΫϗϧμʔʹ2ͭͷҟͳ
    ΔՁ஋Λఏڙ͢ΔɻͦΕ͸ʮৼΔ෣͍ʯͱʮߏ଄ʯͰ͋Δɻ
    — Robert C. Martin
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  31. ͓·͚
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  32. Part III: ٕज़తͳTIPS
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide

  33. ϓϩδΣΫτߏ੒
    • domainͱapplicationͱ͍͏2ͭͷGradleϓϩδΣΫτΛ࡞ͬͨ
    • domain͸Javaͷඪ४ϥΠϒϥϦʹ͔͠ґଘ͍ͯ͠ͳ͍
    • application͸domainʹґଘ͍ͯ͠Δɻத਎͸;ͭ͏ͷSpring Boot
    ΞϓϦέʔγϣϯ
    Copyright © 2018 Yahoo Japan Corporation. All rights reserved.

    View Slide