Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

• • • • 技術ブログもあるよ! 出前館のオーダーブースト広告を支える技術 と私 導入によるバッチ処理の改善

Slide 3

Slide 3 text

• • • • • • •

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

• • •

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

• •

Slide 9

Slide 9 text

①店舗広告を 設定 ②店舗広告を 参照 ③広告経由の 注文を連携 ①〜③ 広告データをやりとり

Slide 10

Slide 10 text

①店舗広告を 設定 ②店舗広告を 参照 ③広告経由の 注文を連携 ①〜③ 広告データをやりとり

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

• • • • •

Slide 16

Slide 16 text

• • • • •

Slide 17

Slide 17 text

• •

Slide 18

Slide 18 text

• • • • •

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

build.gradle.ktsイメージ plugins {...} allprojects { project } subprojects { project } project(":app-api") {...} project(":app-batch") {...} project(":app-subscriber") {...} project(":usecase") {...} project(":infra") {...} project(":domain-model") {...} project(":test-common") {...} project(":test-common-spring") {...} project(":test-gatling") {...}

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

• •

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

@Data data class Shop( val shopId: ShopId, val shopCode: String, val shopName: String, val shopImageUrl: String, val shopCategory: String, ) { init { if (shopName.isBlank() || shopImageUrl.isBlank() || shopCategory.isBlank()) { throw DomainModelException(“ShopCopy items must not be Empty.”) } } }

Slide 37

Slide 37 text

data class Shop( val shopId: ShopId, val shopName: String, val shopImageUrl: String, val shopCategory: String, ) { companion object { fun fromApiResponse( shopId: ShopId, shopName: String, shopImageUrl: String, shopCategory: String, ): Shop { return Shop( shopId = shopId, shopName = shopName, shopImageUrl = shopImageUrl, shopCategory = shopCategory, ) } } }

Slide 38

Slide 38 text

data class Shop( val shopId: ShopId, val shopCode: String, val shopName: String, val shopImageUrl: String, val shopCategory: String, ) { init { if (shopName.isBlank() || shopImageUrl.isBlank() || shopCategory.isBlank()) { throw DomainModelException(“ShopCopy items must not be Empty.”) } } }

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

@Component class AdShopReportGetUseCase( private val hogeMysqlRepository: hogeMysqlRepository, private val fugaExternalApiRepository: FugaExternalApiRepository, ) { fun execute(shopIds: List): List { val shops = hogeMysqlRepository.getShops( shopIds = shopIds, isExcludeDeleted = true, ) ?: run { throw InvalidInputParameterException( message = "invalid input parameter: shopIds=$shopIds", ) } val reports = fugaExternalApiRepository.getReport( shopIds = shopIds, ) // add more buisiness logic if needed val adShopReports = AdShopReport.listOf( shops = shops, reports = reports, ) return adShopReports } }

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

• •

Slide 43

Slide 43 text

• • • • mybatisGenerate // build.gradle.kts val mybatisConfig: Configuration by configurations.creating dependencies { mybatisConfig(“org.mybatis.generator:mybatis-generator-core:some-version") mybatisConfig("mysql:mysql-connector-java:some-version") } tasks.register("mybatisGenerate") { classpath = mybatisConfig mainClass.set("org.mybatis.generator.api.ShellRunner") args = listOf( "-configfile", "path/to/config.xml", "-overwrite" ) }

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

// define HTTP Interface interface HogeExternalApiClient { @GetExchange("/ad-shops/{ad_shop_id}") fun getAdShops( @RequestHeader("Authorization") token: String, @PathVariable("ad_shop_id") adShopId: String, ): ResponseEntity } // call HTTP Interface @Repository class HogeExternalApiRepository( private val hogeExternalApiClient: HogeExternalApiClient, ) { fun getAdShops(token: String, adShopId: String): List { val response = hogeExternalApiClient.getAdShops(token, adShopId) } }

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

@Bean fun hogeExternalApiClient(): HogeExternalApiClient { val httpClient = HttpClients .custom() .setConnectionManager(PoolingHttpClientConnectionManagerBuilder .create() // setting for Apache connection manager .build()) // setting for Apache Http client .build() val restClient = RestClient.builder() .requestFactory(HttpComponentsClientHttpRequestFactory(httpClient)) .baseUrl("http://localhost:8080") // setting for RestClient .build() return HttpServiceProxyFactory .builderFor(RestClientAdapter.create(restClient)) .build() .createClient(HogeApiClient::class.java) }

Slide 48

Slide 48 text

• •

Slide 49

Slide 49 text

• • • • • • • • • • • • • openApiGenerate

Slide 50

Slide 50 text

@Component @ConditionalOnProperty(prefix = "batch", name = ["name"], havingValue = BatchName.HOGE) class HogeBatch( private val hogeUseCase: HogeUseCase, ) : ApplicationRunner { override fun run(args: ApplicationArguments) { try { logger.info { "${this.javaClass.simpleName} started." } val start = System.currentTimeMillis() hogeUseCase.execute() val elapsed = System.currentTimeMillis() - start logger.info { "${this.javaClass.simpleName} ended. (${elapsed}ms)" } } catch (ex: Exception) { logger.error(ex) { "${this.javaClass.simpleName} failed." } throw ex } } }

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Slide 53

Slide 53 text

// test class CalculatorTest : StringSpec({ "addition should return correct result" { Calculator().add(2, 3) shouldBe 5 } "subtraction should return correct result" { Calculator().subtract(5, 3) shouldBe 2 } }) // test target class Calculator { fun add(a: Int, b: Int): Int = a + b fun subtract(a: Int, b: Int): Int = a - b }

Slide 54

Slide 54 text

// test using Should clause class CalculatorTest : ShouldSpec({ should("return the correct result when adding two numbers") { Calculator().add(2, 3) shouldBe 5 } should("return the correct result when subtracting two numbers") { Calculator().subtract(5, 3) shouldBe 2 } }) // test target class Calculator { fun add(a: Int, b: Int): Int = a + b fun subtract(a: Int, b: Int): Int = a - b }

Slide 55

Slide 55 text

// test like Given-When-Then class CalculatorTest : BehaviorSpec({ given("a calculator") { `when`("adding two numbers") { then("it should return the correct result") { Calculator().add(2, 3) shouldBe 5 } } `when`("subtracting two numbers") { then("it should return the correct result") { Calculator().subtract(5, 3) shouldBe 2 } } } }) // test target class Calculator { fun add(a: Int, b: Int): Int = a + b fun subtract(a: Int, b: Int): Int = a - b }

Slide 56

Slide 56 text

class CalculatorTest : FunSpec({ data class TestPattern( val a: Int, val b: Int, val expectedAddition: Int, val expectedSubtraction: Int ) context("Calculator operations") { withData( TestPattern(a = 2, b = 3, expectedAddition = 5, expectedSubtraction = -1), TestPattern(a = 10, b = 5, expectedAddition = 15, expectedSubtraction = 5), TestPattern(a = 0, b = 0, expectedAddition = 0, expectedSubtraction = 0) ) { testCase -> val calculator = Calculator() calculator.add(testCase.a, testCase.b) shouldBe testCase.expectedAddition calculator.subtract(testCase.a, testCase.b) shouldBe testCase.expectedSubtraction } } })

Slide 57

Slide 57 text

# docker-compose.yml sample-mysql: image: mysql:8.0.28-oracle ports: - “3306:3306” environment: MYSQL_DATABASE: sample MYSQL_USER: user001 volumes: - ./sample-mysql:/docker-entrypoint-initdb.d sample-wiremock: image: wiremock/wiremock:3.1.0 ports: - "18180:18180" volumes: - ./sample-wiremock:/home/wiremock command: - "--port=18180" class HogeTest : StringSpec({ val composeContainer = DockerComposeContainer(File(“../tool/docker-compose.yml”)) .withExposedService(“sample-mysql”, 3306, Wait.forListeningPort()) .withExposedService(“sample-wiremock”, 18180, Wait.forListeningPort()) .apply { start() } “sample test" { 1 + 1 shouldBe 2 } })

Slide 58

Slide 58 text

// libs.versions.toml [versions] gatling = "3.13.5” [plugins] gatling = { id = "io.gatling.gradle", version.ref = "gatling" } [libraries] gatling-charts-highcharts = { module = "io.gatling.highcharts:gatling-charts-highcharts", version.ref = "gatling" } gatling-app = { module = "io.gatling:gatling-app", version.ref = "gatling" } // build.gradle.kts project(":test-gatling") { apply(plugin = rootProject.libs.plugins.gatling.get().pluginId) dependencies { gatling(rootProject.libs.gatling.charts.highcharts) gatling(rootProject.libs.gatling.app) } gatling { System.getenv().forEach { environment.put(it.key, it.value) // pass environment variables to gatling app } } }

Slide 59

Slide 59 text

class HogeApiSimulations : Simulation() { val scenario1 = scenario(“scenario1”) .exec(http(“hoge-api-request”) .post(“/v1/hoges”) .body(StringBody(“”“{”key1“: ”XXX“, “key2”: 999}“”“.trimMargin())) .check(status().shouldBe(200)) ) val httpProtocol = http.baseUrl(”https://hoge.sample.com“) .header(”content-type“, ”application/json“) .header(”X-Api-Key“, System.getenv(”X_API_KEY“)) init { setUp( scenario1.injectOpen(rampUsersPerSec(10.0).to(100.0).during(60)) .andThen(scenario1.injectOpen(constantUsersPerSec(100.0).during(300))) ).protocols(httpProtocol) } }

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

# gradle/libs.versions.toml [versions] javaLanguageVersion = “21” springBoot = “3.4.0” springRetry = "2.0.11" [plugins] spring-boot = { id = "org.springframework.boot", version.ref = "springBoot" } [libraries] spring-retry = { module = "org.springframework.retry:spring-retry", version.ref = "springRetry" } spring-boot-dependencies = { module = "org.springframework.boot:spring-boot-dependencies", version.ref = "springBoot" } spring-boot-starter = { module = "org.springframework.boot:spring-boot-starter" } spring-boot-starter-web = { module = "org.springframework.boot:spring-boot-starter-web" } [bundles] spring-boot-setting = [ "spring-boot-starter", "spring-boot-starter-web”, "spring-retry" ]

Slide 64

Slide 64 text

# build.gradle.kts project(":app-api") { kotlin { jvmToolchain { this.languageVersion.set(JavaLanguageVersion.of(rootProject.libs.versions.javaLanguageVersion.get())) } apply(plugin = rootProject.libs.plugins.spring.boot.get().pluginId) dependencies { implementation(rootProject.libs.bundles.spring.boot.setting) }

Slide 65

Slide 65 text

// ControllerExtensions.kt fun HogesPostRequest.toDomainModel(): Hoge { return HogeModel( hogeId = HogeId(this.hogeId), fuga = Fuga.of(this.fuga), ) } // HogeController.kt @RestController class HogeController(private val hogeUseCase: HogeUseCase) : HogesApi { override fun hogesPost(hogesPostRequest: HogesPostRequest): ResponseEntity { hogeUseCase.create(hogesPostRequest.toDomainModel()) return ResponseEntity.ok(HogesPostResponse(code = "OK")) } }

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

// build.gradle.kts tasks.register("dockerComposeUp") { commandLine("docker-compose", "-f", "./docker-compose-test.yml", "up", "-d") } tasks.register("dockerComposeDown") { commandLine("docker-compose", "-f", "./docker-compose-test.yml", "down", "--rmi", "—volumes") } tasks.named("test") { dependsOn("dockerComposeUp") finalizedBy("dockerComposeDown") }

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

• • • •

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

// libs.versions.toml [libraries] opentelemetry-api = { module = "io.opentelemetry:opentelemetry-api" } // build.gradle.kts project(":sample") { dependencies { implementation(rootProject.libs.opentelemetry.api) } } //OpenTelemetryConfig.kt @Configuration class OpenTelemetryConfig { @Bean fun openTelemetry(): OpenTelemetry { return GlobalOpenTelemetry.get() } } // execute command java -javaagent:/path/to/the/opentelemetry-javaagent.jar –jar sample.jar

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

• • • • • • • • • • • • •

Slide 79

Slide 79 text

No content