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

[#JSUG] SmartNews における container friendly な Spr...

[#JSUG] SmartNews における container friendly な Spring Boot アプリケーション開発

SmartNews に新たに登場した「タイムセール」のバックエンドシステムの開発の裏側をお話しました。

KOMIYA Atsushi

May 31, 2019
Tweet

More Decks by KOMIYA Atsushi

Other Decks in Programming

Transcript

  1. SmartNews ʹ͓͚Δ container friendly ͳ Spring Boot ΞϓϦέʔγϣϯ։ൃ (container friendly

    ݴ͏΄Ͳͷൃද಺༰͡Όͳ͍ͳ ) (Spring Boot ͕ओ໾ͱ͍͏ײ͡Ͱ΋ͳ͍ͳ… )
  2. ૡ͍ఠΜͰ࿩Λ͢Δͱ… • AWS + AmazonLinux • ALB/CLB + ASG +

    EC2 • nginx + Spring Boot w/ embedded Tomcat • Java 8 (Java, Scala, Kotlin) • Terraform, CircleCI, CodeDeploy, Nexus, Datadog
  3. ։ൃ౰ॳͷঢ়گ • 2019 Q1 தʹ։࢝ͯ͠ Q1 தʹϦϦʔε༧ఆ • ࣮ࡍʹϦϦʔεͰ͖ͨͷ͸ Q2

    ಄ • ϦϦʔε·Ͱͷ࣌ؒత༛༧͕গͳ͍ • ׳Ε਌͠Έɺ։ൃޮ཰Α͍ϑϨʔϜϫʔΫ / ݴޠΛ બ୒ • Spring Boot + Kotlin
  4. ։ൃ౰ॳͷঢ়گ • 2019 Q1 ࣌఺ͷࣾ಺֎ͷঢ়گ • Java 11 ͕ϦϦʔε͞Εͯ 1Q

    Ҏ্ܦա • ࣾ಺తʹ͸·ͩ Java 8 ͕ݱ໾ͷγεςϜ͕େ൒ Λ઎Ί͍ͯͨ • ৽ن։ൃ͢ΔαʔϏεͳͷͰɺ͍·͞Β Java 8 ͸ બ୒ͨ͘͠ͳ͍
  5. ։ൃ౰ॳͷঢ়گ • ࣾ಺ͷඪ४తͳٕज़ελοΫ͸ Java 8 લఏ • طଘγεςϜͷ Java 11

    ҠߦΛݕ౼ • ඪ४తͳٕज़ελοΫͷԸܙ͸ڗड͍ͨ͠ • Spring Boot ΞϓϦέʔγϣϯ & JDK Λ
 ߹ΘͤͯίϯςφԽͤ͞Δ͜ͱʹͨ͠
  6. ։ൃ؀ڥͷ੔උ • ։ൃ؀ڥͷݸਓࠩΛ࠷খԽ͍ͨ͠ • MySQL αʔόΛखݩͷ؀ڥʹ༻ҙ͢Δ • ଞαʔϏεͰ΋ MySQL Λར༻͍ͯ͠Δ͕ɺ


    όʔδϣϯ͕ҟͳΓಘΔͨΊڞ༻͠೉͍ • Datadog agent (ͷ DogStatsD) ͕ඞཁʹͳΔ • ϝτϦΫεͷ؂ࢹʹ Datadog Λར༻͍ͯ͠Δ
  7. # docker-compose.yml version: '3.1' services: db: image: mysql:5.7.25 environment: MYSQL_ROOT_PASSWORD:

    password MYSQL_DATABASE: main_db volumes: - ./tmp/mysql:/var/lib/mysql ports: - "13307:3306" dogstatsd: image: datadog/docker-dd-agent:12.7.5321-dogstatsd environment: API_KEY: ${DATADOG_API_KEY} ports: - “8125:8125/udp"
  8. # application.yml spring: profiles: local-dev datasource: url: jdbc:mysql://127.0.0.1:3306/main_db username: root

    password: password management.metrics.export.statsd.host: 127.0.0.1 --- spring: profiles: docker-dev datasource: url: jdbc:mysql://host.docker.internal:3306/main_db username: root password: password management.metrics.export.statsd.host: host.docker.internal
  9. Ұൠతͳ Spring Boot app ͷϏϧυ • Spring Boot Gradle plugin

    Λಋೖ͢Δ • ./gradlew build ͢Δ • fat jar ͕࡞ΒΕΔ
  10. Docker Πϝʔδͱͯ͠Ϗϧυ͢Δ • Docker Gradle plugin (Palantir) Λಋೖ͢Δ • ./gradlew

    build docker Ͱ Docker
 ΠϝʔδΛϏϧυ͢Δ • ./gradlew dockerPush Ͱ Docker
 ΠϝʔδΛϨδετϦʹ push ͢Δ
  11. # build.gradle plugins { id 'org.springframework.boot' id 'com.palantir.docker' id 'net.researchgate.release'

    } // ... docker { name “$dockerNamespace/spring-boot-app:$version” copySpec.from(“build/libs/spring-boot-app—${version}.jar”) .into('build/libs') .rename { “spring-boot-app.jar” } } docker.dependsOn(build) // ... afterReleaseBuild.dependsOn(dockerPush)
  12. ϕʔεΠϝʔδɺԿΛબͿʁ • ༷ʑͳ૊৫͕͍ΖΜͳΠϝʔδΛఏڙ͍ͯͯ͠ɺ ͱͯ΋ ΧΦε ଟ༷ੑʹ෋ΜͰ͍Δ • OS: Debian, Ubuntu,

    Alpine Linux, Oracle Linux, Amazon Linux, … • ૊৫: Docker official, Oracle, AdoptOpenJDK, Azul systems, … • ͦͷଞ: LTS ͷ༗ແ, JRE/JDK, …
  13. ϕʔεΠϝʔδɺԿΛબͿʁ • AdoptOpenJDK ΛબΜͩ • “LTS ͕͋ͬͯ JDK ͷΠϝʔδͰ͋Ε͹ԿͰ΋Α͔ͬ ͨɻࠓʹͯ͠ΈΕ͹

    Amazon Corretto ΛબΜͰ΋Α ͔͔ͬͨ΋͠Εͳ͍ͱ൓ল͍ͯ͠Δ” • OS ͸ (Alpine Ͱ͸ͳ͘) Ubuntu Λબ୒, slim ͸બ͹ͣ • slim Πϝʔδͩͱ jstack ͳͲ͕ೖ͍ͬͯͳ͍
  14. ϨΠϠΛ͋͑ͯ෼͚Δ FROM adoptopenjdk/openjdk11:jdk-11.0.3_7 ARG DEPENDENCY=build/dependency COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib COPY ${DEPENDENCY}/META-INF

    /app/META-INF COPY ${DEPENDENCY}/BOOT-INF/classes /app CMD ["java", "-cp", "app:app/lib/*", "foo.bar.EntryPoint"]
  15. Spring Boot app ͷίϯςφΛಈ͔͢ $ docker run -d —name spring-boot-app

    \ -p 8080:8080 \ -e SPRING_PROFILES_ACTIVE=production \ -v /path/to/host/log:/path/to/container/log \ spring-boot-app:X.Y.Z
  16. Spring Boot app ͷίϯςφΛಈ͔͢ $ docker run -d —name spring-boot-app

    \ -p 8080:8080 \ -e SPRING_PROFILES_ACTIVE=production \ -v /path/to/host/log:/path/to/container/log \ spring-boot-app:X.Y.Z
  17. # application.yml spring.profiles: production spring: profiles: include: common, aws, prd-db

    --- spring.profiles: staging spring: profiles: include: common, aws, stg-db --- spring.profiles: local spring: profiles: include: common, local-db
  18. ϗετͷϑΝΠϧγεςϜʹϩάग़ྗ͢Δ $ docker run -d —name spring-boot-app \ -p 8080:8080

    \ -e SPRING_PROFILES_ACTIVE=production \ -v /path/to/host/log:/path/to/container/log \ spring-boot-app:X.Y.Z
  19. # build.gradle dependencies { // ... implementation ‘org.springframework.boot:spring-boot-starter-actuator’ implementation ‘io.micrometer:micrometer-registry-statsd’

    } # application.yml management: metrics: export: statsd: flavor: datadog host: host.docker.internal # 18.03 ΑΓલͷ docker Λ࢖͏৔߹͸ --network=host ͷΦϓγϣϯ෇͚ͯ # docker run ͱ͢Δɻ·ͨ host ͷࢦఆ͸ҎԼͷΑ͏ʹ 127.0.0.1 ͱ͢Δ host: 127.0.0.1
  20. ·ͱΊ • Java 11 + Spring Boot + Kotlin ͱ͍͏ߏ੒ͷ


    ৽نαʔϏεͷ։ൃͰ Docker Λ׆༻ͨ͠ • ։ൃ؀ڥͷ੔උ • Docker Πϝʔδͷߏஙํ๏ • σϓϩΠɾΞϓϦέʔγϣϯ࣮ߦํ๏