@k2works/claude-code-booster 3.5.0 → 3.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +42 -42
- package/bin/claude-code-booster +90 -90
- package/lib/assets/.claude/README.md +239 -239
- package/lib/assets/.claude/scripts/generate-inception-deck.mjs +911 -911
- package/lib/assets/.claude/settings.json +11 -11
- package/lib/assets/.claude/skills/ai-agent-guidelines/SKILL.md +111 -111
- package/lib/assets/.claude/skills/analyzing-architecture/SKILL.md +83 -83
- package/lib/assets/.claude/skills/analyzing-business/SKILL.md +95 -95
- package/lib/assets/.claude/skills/analyzing-data-model/SKILL.md +77 -77
- package/lib/assets/.claude/skills/analyzing-domain-model/SKILL.md +117 -88
- package/lib/assets/.claude/skills/analyzing-inception-deck/SKILL.md +84 -84
- package/lib/assets/.claude/skills/analyzing-non-functional/SKILL.md +95 -95
- package/lib/assets/.claude/skills/analyzing-operation/SKILL.md +95 -95
- package/lib/assets/.claude/skills/analyzing-requirements/SKILL.md +91 -91
- package/lib/assets/.claude/skills/analyzing-tech-stack/SKILL.md +101 -101
- package/lib/assets/.claude/skills/analyzing-test-strategy/SKILL.md +89 -89
- package/lib/assets/.claude/skills/analyzing-ui-design/SKILL.md +80 -80
- package/lib/assets/.claude/skills/analyzing-usecases/SKILL.md +72 -72
- package/lib/assets/.claude/skills/creating-adr/SKILL.md +113 -113
- package/lib/assets/.claude/skills/developing-backend/SKILL.md +100 -100
- package/lib/assets/.claude/skills/developing-frontend/SKILL.md +93 -93
- package/lib/assets/.claude/skills/developing-release/SKILL.md +120 -120
- package/lib/assets/.claude/skills/generating-slides/SKILL.md +94 -94
- package/lib/assets/.claude/skills/git-commit/SKILL.md +81 -81
- package/lib/assets/.claude/skills/killing-processes/SKILL.md +44 -44
- package/lib/assets/.claude/skills/operating-backup/SKILL.md +59 -59
- package/lib/assets/.claude/skills/operating-cicd/SKILL.md +54 -54
- package/lib/assets/.claude/skills/operating-deploy/SKILL.md +67 -67
- package/lib/assets/.claude/skills/operating-docs/SKILL.md +219 -219
- package/lib/assets/.claude/skills/operating-provision/SKILL.md +77 -77
- package/lib/assets/.claude/skills/operating-setup/SKILL.md +63 -63
- package/lib/assets/.claude/skills/orchestrating-analysis/SKILL.md +104 -104
- package/lib/assets/.claude/skills/orchestrating-development/SKILL.md +162 -161
- package/lib/assets/.claude/skills/orchestrating-operation/SKILL.md +158 -158
- package/lib/assets/.claude/skills/orchestrating-project/SKILL.md +144 -144
- package/lib/assets/.claude/skills/planning-releases/SKILL.md +119 -119
- package/lib/assets/.claude/skills/syncing-github-project/SKILL.md +151 -151
- package/lib/assets/.claude/skills/tracking-progress/SKILL.md +91 -91
- package/lib/assets/.claude/skills/validating-iteration-plan/SKILL.md +215 -215
- package/lib/assets/.devcontainer/devcontainer.json +34 -34
- package/lib/assets/.env.example +17 -17
- package/lib/assets/.gitattributes +4 -4
- package/lib/assets/.github/workflows/docker-publish.yml +77 -77
- package/lib/assets/.github/workflows/mkdocs.yml +39 -39
- package/lib/assets/AGENTS.md +94 -94
- package/lib/assets/CLAUDE.md +183 -183
- package/lib/assets/README.md +254 -254
- package/lib/assets/docker-compose.yml +33 -33
- package/lib/assets/docs/adr/index.md +10 -10
- package/lib/assets/docs/article/functional-desgin-ppp/all/01-immutability-and-data-transformation.md +475 -475
- package/lib/assets/docs/article/functional-desgin-ppp/all/02-function-composition.md +519 -519
- package/lib/assets/docs/article/functional-desgin-ppp/all/03-polymorphism.md +537 -537
- package/lib/assets/docs/article/functional-desgin-ppp/all/04-data-validation.md +300 -300
- package/lib/assets/docs/article/functional-desgin-ppp/all/05-property-based-testing.md +320 -320
- package/lib/assets/docs/article/functional-desgin-ppp/all/06-tdd-and-functional.md +498 -498
- package/lib/assets/docs/article/functional-desgin-ppp/all/07-composite-pattern.md +298 -298
- package/lib/assets/docs/article/functional-desgin-ppp/all/08-decorator-pattern.md +291 -291
- package/lib/assets/docs/article/functional-desgin-ppp/all/09-adapter-pattern.md +336 -336
- package/lib/assets/docs/article/functional-desgin-ppp/all/10-strategy-pattern.md +303 -303
- package/lib/assets/docs/article/functional-desgin-ppp/all/11-command-pattern.md +286 -286
- package/lib/assets/docs/article/functional-desgin-ppp/all/12-visitor-pattern.md +322 -322
- package/lib/assets/docs/article/functional-desgin-ppp/all/13-abstract-factory-pattern.md +319 -319
- package/lib/assets/docs/article/functional-desgin-ppp/all/14-abstract-server-pattern.md +365 -365
- package/lib/assets/docs/article/functional-desgin-ppp/all/15-gossiping-bus-drivers.md +156 -156
- package/lib/assets/docs/article/functional-desgin-ppp/all/16-payroll-system.md +178 -178
- package/lib/assets/docs/article/functional-desgin-ppp/all/17-video-rental-system.md +312 -312
- package/lib/assets/docs/article/functional-desgin-ppp/all/18-concurrency-system.md +287 -287
- package/lib/assets/docs/article/functional-desgin-ppp/all/19-wa-tor-simulation.md +286 -286
- package/lib/assets/docs/article/functional-desgin-ppp/all/20-pattern-interactions.md +274 -274
- package/lib/assets/docs/article/functional-desgin-ppp/all/21-best-practices.md +294 -294
- package/lib/assets/docs/article/functional-desgin-ppp/all/22-oo-to-fp-migration.md +337 -337
- package/lib/assets/docs/article/functional-desgin-ppp/all/index.md +388 -388
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/01-immutability-and-data-transformation.md +273 -273
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/02-function-composition.md +380 -380
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/03-polymorphism.md +384 -384
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/04-clojure-spec.md +350 -350
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/05-property-based-testing.md +352 -352
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/06-tdd-in-functional.md +383 -383
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/07-composite-pattern.md +529 -529
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/08-decorator-pattern.md +395 -395
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/09-adapter-pattern.md +399 -399
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/10-strategy-pattern.md +485 -485
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/11-command-pattern.md +566 -566
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/12-visitor-pattern.md +567 -567
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/13-abstract-factory-pattern.md +475 -475
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/14-abstract-server-pattern.md +462 -462
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/15-gossiping-bus-drivers.md +325 -325
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/16-payroll-system.md +401 -401
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/17-video-rental-system.md +450 -450
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/18-concurrency-system.md +475 -475
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/19-wator-simulation.md +739 -739
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/20-pattern-interactions.md +567 -567
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/21-best-practices.md +518 -518
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/22-oo-to-fp-migration.md +532 -532
- package/lib/assets/docs/article/functional-desgin-ppp/clojure/index.md +241 -241
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/01-immutability-and-data-transformation.md +383 -383
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/02-function-composition.md +374 -374
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/03-polymorphism.md +375 -375
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/04-data-validation.md +195 -195
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/05-property-based-testing.md +268 -268
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/06-tdd-and-fp.md +294 -294
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/07-effects-and-pure-functions.md +164 -164
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/08-error-handling-strategies.md +168 -168
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/09-io-and-external-systems.md +254 -254
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/10-concurrency-patterns.md +269 -269
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/11-command-pattern.md +148 -148
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/12-visitor-pattern.md +176 -176
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/13-abstract-factory-pattern.md +604 -604
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/14-abstract-server-pattern.md +729 -729
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/15-gossiping-bus-drivers.md +291 -291
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/16-payroll-system.md +420 -420
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/17-video-rental-system.md +319 -319
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/18-concurrency-system.md +466 -466
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/19-wator-simulation.md +523 -523
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/20-pattern-interactions.md +287 -287
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/21-best-practices.md +340 -340
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/22-oo-to-fp-migration.md +395 -395
- package/lib/assets/docs/article/functional-desgin-ppp/elixir/index.md +248 -248
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/01-immutability-and-data-transformation.md +384 -384
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/02-function-composition.md +452 -452
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/03-polymorphism.md +495 -495
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/04-data-validation.md +416 -416
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/05-property-based-testing.md +382 -382
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/06-tdd-functional.md +687 -687
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/07-composite-pattern.md +442 -442
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/08-decorator-pattern.md +479 -479
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/09-adapter-pattern.md +479 -479
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/10-strategy-pattern.md +427 -427
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/11-command-pattern.md +428 -428
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/12-visitor-pattern.md +339 -339
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/13-abstract-factory-pattern.md +309 -309
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/14-abstract-server-pattern.md +596 -596
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/15-gossiping-bus-drivers.md +355 -355
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/16-payroll-system.md +350 -350
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/17-video-rental-system.md +414 -414
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/18-concurrency-system.md +367 -367
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/19-wator-simulation.md +403 -403
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/20-pattern-interactions.md +291 -291
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/21-best-practices.md +324 -324
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/22-oo-to-fp-migration.md +332 -332
- package/lib/assets/docs/article/functional-desgin-ppp/fsharp/index.md +274 -274
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/01-immutability-and-data-transformation.md +298 -298
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/02-function-composition.md +304 -304
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/03-polymorphism.md +362 -362
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/04-data-validation.md +257 -257
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/05-property-based-testing.md +254 -254
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/06-tdd-functional.md +283 -283
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/07-composite-pattern.md +395 -395
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/08-decorator-pattern.md +319 -319
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/09-adapter-pattern.md +382 -382
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/10-strategy-pattern.md +287 -287
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/11-command-pattern.md +303 -303
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/12-visitor-pattern.md +326 -326
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/13-abstract-factory-pattern.md +332 -332
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/14-abstract-server-pattern.md +379 -379
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/15-gossiping-bus-drivers.md +177 -177
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/16-payroll-system.md +219 -219
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/17-video-rental-system.md +244 -244
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/18-concurrency-system.md +363 -363
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/19-wator-simulation.md +438 -438
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/20-pattern-interactions.md +325 -325
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/21-best-practices.md +403 -403
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/22-oo-to-fp-migration.md +469 -469
- package/lib/assets/docs/article/functional-desgin-ppp/haskell/index.md +174 -174
- package/lib/assets/docs/article/functional-desgin-ppp/index.md +90 -90
- package/lib/assets/docs/article/functional-desgin-ppp/rust/01-immutability-and-data-transformation.md +450 -450
- package/lib/assets/docs/article/functional-desgin-ppp/rust/02-function-composition.md +463 -463
- package/lib/assets/docs/article/functional-desgin-ppp/rust/03-polymorphism.md +425 -425
- package/lib/assets/docs/article/functional-desgin-ppp/rust/04-data-validation.md +273 -273
- package/lib/assets/docs/article/functional-desgin-ppp/rust/05-property-based-testing.md +247 -247
- package/lib/assets/docs/article/functional-desgin-ppp/rust/06-tdd-and-functional.md +841 -841
- package/lib/assets/docs/article/functional-desgin-ppp/rust/07-composite-pattern.md +384 -384
- package/lib/assets/docs/article/functional-desgin-ppp/rust/08-decorator-pattern.md +383 -383
- package/lib/assets/docs/article/functional-desgin-ppp/rust/09-adapter-pattern.md +339 -339
- package/lib/assets/docs/article/functional-desgin-ppp/rust/10-strategy-pattern.md +331 -331
- package/lib/assets/docs/article/functional-desgin-ppp/rust/11-command-pattern.md +356 -356
- package/lib/assets/docs/article/functional-desgin-ppp/rust/12-visitor-pattern.md +379 -379
- package/lib/assets/docs/article/functional-desgin-ppp/rust/13-abstract-factory-pattern.md +361 -361
- package/lib/assets/docs/article/functional-desgin-ppp/rust/14-abstract-server-pattern.md +392 -392
- package/lib/assets/docs/article/functional-desgin-ppp/rust/15-gossiping-bus-drivers.md +300 -300
- package/lib/assets/docs/article/functional-desgin-ppp/rust/16-payroll-system.md +297 -297
- package/lib/assets/docs/article/functional-desgin-ppp/rust/17-video-rental-system.md +304 -304
- package/lib/assets/docs/article/functional-desgin-ppp/rust/18-concurrency-system.md +315 -315
- package/lib/assets/docs/article/functional-desgin-ppp/rust/19-wator-simulation.md +311 -311
- package/lib/assets/docs/article/functional-desgin-ppp/rust/20-pattern-interactions.md +304 -304
- package/lib/assets/docs/article/functional-desgin-ppp/rust/21-best-practices.md +336 -336
- package/lib/assets/docs/article/functional-desgin-ppp/rust/22-oo-to-fp-migration.md +349 -349
- package/lib/assets/docs/article/functional-desgin-ppp/rust/index.md +243 -243
- package/lib/assets/docs/article/functional-desgin-ppp/scala/01-immutability-and-data-transformation.md +328 -328
- package/lib/assets/docs/article/functional-desgin-ppp/scala/02-function-composition.md +348 -348
- package/lib/assets/docs/article/functional-desgin-ppp/scala/03-polymorphism.md +357 -357
- package/lib/assets/docs/article/functional-desgin-ppp/scala/04-data-validation.md +364 -364
- package/lib/assets/docs/article/functional-desgin-ppp/scala/05-property-based-testing.md +515 -515
- package/lib/assets/docs/article/functional-desgin-ppp/scala/06-tdd-functional.md +557 -557
- package/lib/assets/docs/article/functional-desgin-ppp/scala/07-composite-pattern.md +363 -363
- package/lib/assets/docs/article/functional-desgin-ppp/scala/08-decorator-pattern.md +327 -327
- package/lib/assets/docs/article/functional-desgin-ppp/scala/09-adapter-pattern.md +517 -517
- package/lib/assets/docs/article/functional-desgin-ppp/scala/10-strategy-pattern.md +441 -441
- package/lib/assets/docs/article/functional-desgin-ppp/scala/11-command-pattern.md +407 -407
- package/lib/assets/docs/article/functional-desgin-ppp/scala/12-visitor-pattern.md +379 -379
- package/lib/assets/docs/article/functional-desgin-ppp/scala/13-abstract-factory-pattern.md +398 -398
- package/lib/assets/docs/article/functional-desgin-ppp/scala/14-abstract-server-pattern.md +476 -476
- package/lib/assets/docs/article/functional-desgin-ppp/scala/15-gossiping-bus-drivers.md +391 -391
- package/lib/assets/docs/article/functional-desgin-ppp/scala/16-payroll-system.md +342 -342
- package/lib/assets/docs/article/functional-desgin-ppp/scala/17-video-rental-system.md +324 -324
- package/lib/assets/docs/article/functional-desgin-ppp/scala/18-concurrency-system.md +730 -730
- package/lib/assets/docs/article/functional-desgin-ppp/scala/19-wator-simulation.md +624 -624
- package/lib/assets/docs/article/functional-desgin-ppp/scala/20-pattern-interactions.md +512 -512
- package/lib/assets/docs/article/functional-desgin-ppp/scala/21-best-practices.md +433 -433
- package/lib/assets/docs/article/functional-desgin-ppp/scala/22-oo-to-fp-migration.md +688 -688
- package/lib/assets/docs/article/functional-desgin-ppp/scala/index.md +243 -243
- package/lib/assets/docs/article/getting-start-tdd/clojure/01-todo-list-and-first-test.md +166 -166
- package/lib/assets/docs/article/getting-start-tdd/clojure/02-fake-it-and-triangulation.md +162 -162
- package/lib/assets/docs/article/getting-start-tdd/clojure/03-obvious-implementation-and-refactoring.md +135 -135
- package/lib/assets/docs/article/getting-start-tdd/clojure/04-version-control-and-conventional-commits.md +88 -88
- package/lib/assets/docs/article/getting-start-tdd/clojure/05-package-management-and-static-analysis.md +299 -299
- package/lib/assets/docs/article/getting-start-tdd/clojure/06-task-runner-and-ci-cd.md +241 -241
- package/lib/assets/docs/article/getting-start-tdd/clojure/07-protocols-and-records.md +131 -131
- package/lib/assets/docs/article/getting-start-tdd/clojure/08-multimethods-and-design-patterns.md +130 -130
- package/lib/assets/docs/article/getting-start-tdd/clojure/09-namespaces-and-module-design.md +127 -127
- package/lib/assets/docs/article/getting-start-tdd/clojure/10-higher-order-functions-and-composition.md +114 -114
- package/lib/assets/docs/article/getting-start-tdd/clojure/11-persistent-data-and-pipeline.md +138 -138
- package/lib/assets/docs/article/getting-start-tdd/clojure/12-error-handling-and-spec.md +161 -161
- package/lib/assets/docs/article/getting-start-tdd/clojure/index.md +65 -65
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter01.md +232 -232
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter02.md +244 -244
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter03.md +202 -202
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter04.md +92 -92
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter05.md +256 -256
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter06.md +195 -195
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter07.md +214 -214
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter08.md +249 -249
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter09.md +174 -174
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter10.md +166 -166
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter11.md +192 -192
- package/lib/assets/docs/article/getting-start-tdd/csharp/chapter12.md +211 -211
- package/lib/assets/docs/article/getting-start-tdd/csharp/index.md +83 -83
- package/lib/assets/docs/article/getting-start-tdd/elixir/01-todo-list-and-first-test.md +87 -87
- package/lib/assets/docs/article/getting-start-tdd/elixir/02-fake-it-and-triangulation.md +95 -95
- package/lib/assets/docs/article/getting-start-tdd/elixir/03-obvious-implementation-and-refactoring.md +109 -109
- package/lib/assets/docs/article/getting-start-tdd/elixir/04-version-control-and-conventional-commits.md +96 -96
- package/lib/assets/docs/article/getting-start-tdd/elixir/05-package-management-and-static-analysis.md +88 -88
- package/lib/assets/docs/article/getting-start-tdd/elixir/06-task-runner-and-ci-cd.md +71 -71
- package/lib/assets/docs/article/getting-start-tdd/elixir/07-structs-and-protocols.md +110 -110
- package/lib/assets/docs/article/getting-start-tdd/elixir/08-pattern-matching-and-guards.md +108 -108
- package/lib/assets/docs/article/getting-start-tdd/elixir/09-module-design-and-behaviours.md +104 -104
- package/lib/assets/docs/article/getting-start-tdd/elixir/10-higher-order-functions-and-pipeline.md +178 -178
- package/lib/assets/docs/article/getting-start-tdd/elixir/11-stream-and-lazy-evaluation.md +142 -142
- package/lib/assets/docs/article/getting-start-tdd/elixir/12-error-handling-and-with.md +145 -145
- package/lib/assets/docs/article/getting-start-tdd/elixir/index.md +35 -35
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter01.md +202 -202
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter02.md +246 -246
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter03.md +218 -218
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter04.md +179 -179
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter05.md +267 -267
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter06.md +190 -190
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter07.md +161 -161
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter08.md +175 -175
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter09.md +222 -222
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter10.md +189 -189
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter11.md +212 -212
- package/lib/assets/docs/article/getting-start-tdd/fsharp/chapter12.md +215 -215
- package/lib/assets/docs/article/getting-start-tdd/fsharp/index.md +71 -71
- package/lib/assets/docs/article/getting-start-tdd/go/01-todo-list-and-first-test.md +213 -213
- package/lib/assets/docs/article/getting-start-tdd/go/02-fake-it-and-triangulation.md +302 -302
- package/lib/assets/docs/article/getting-start-tdd/go/03-obvious-implementation-and-refactoring.md +339 -339
- package/lib/assets/docs/article/getting-start-tdd/go/04-version-control-and-conventional-commits.md +112 -112
- package/lib/assets/docs/article/getting-start-tdd/go/05-package-management-and-static-analysis.md +272 -272
- package/lib/assets/docs/article/getting-start-tdd/go/06-task-runner-and-ci-cd.md +233 -233
- package/lib/assets/docs/article/getting-start-tdd/go/07-encapsulation-and-polymorphism.md +394 -394
- package/lib/assets/docs/article/getting-start-tdd/go/08-design-patterns.md +422 -422
- package/lib/assets/docs/article/getting-start-tdd/go/09-solid-principles-and-module-design.md +400 -400
- package/lib/assets/docs/article/getting-start-tdd/go/10-higher-order-functions-and-composition.md +226 -226
- package/lib/assets/docs/article/getting-start-tdd/go/11-immutable-data-and-pipeline.md +296 -296
- package/lib/assets/docs/article/getting-start-tdd/go/12-error-handling-and-type-safety.md +411 -411
- package/lib/assets/docs/article/getting-start-tdd/go/index.md +83 -83
- package/lib/assets/docs/article/getting-start-tdd/haskell/01-todo-list-and-first-test.md +279 -279
- package/lib/assets/docs/article/getting-start-tdd/haskell/02-fake-it-and-triangulation.md +337 -337
- package/lib/assets/docs/article/getting-start-tdd/haskell/03-obvious-implementation-and-refactoring.md +257 -257
- package/lib/assets/docs/article/getting-start-tdd/haskell/04-version-control-and-conventional-commits.md +182 -182
- package/lib/assets/docs/article/getting-start-tdd/haskell/05-package-management-and-static-analysis.md +313 -313
- package/lib/assets/docs/article/getting-start-tdd/haskell/06-task-runner-and-ci-cd.md +309 -309
- package/lib/assets/docs/article/getting-start-tdd/haskell/07-algebraic-data-types-and-type-classes.md +412 -412
- package/lib/assets/docs/article/getting-start-tdd/haskell/08-pattern-matching-and-guards.md +390 -390
- package/lib/assets/docs/article/getting-start-tdd/haskell/09-module-design-and-smart-constructors.md +461 -461
- package/lib/assets/docs/article/getting-start-tdd/haskell/10-higher-order-functions-and-currying.md +434 -434
- package/lib/assets/docs/article/getting-start-tdd/haskell/11-function-composition-and-point-free.md +392 -392
- package/lib/assets/docs/article/getting-start-tdd/haskell/12-monad-and-error-handling.md +631 -631
- package/lib/assets/docs/article/getting-start-tdd/haskell/index.md +49 -49
- package/lib/assets/docs/article/getting-start-tdd/index.md +93 -93
- package/lib/assets/docs/article/getting-start-tdd/integration/01-language-overview.md +375 -375
- package/lib/assets/docs/article/getting-start-tdd/integration/02-test-framework-comparison.md +349 -349
- package/lib/assets/docs/article/getting-start-tdd/integration/03-tdd-pattern-comparison.md +445 -445
- package/lib/assets/docs/article/getting-start-tdd/integration/04-type-system-comparison.md +409 -409
- package/lib/assets/docs/article/getting-start-tdd/integration/05-dev-environment-comparison.md +330 -330
- package/lib/assets/docs/article/getting-start-tdd/integration/06-learning-roadmap.md +290 -290
- package/lib/assets/docs/article/getting-start-tdd/integration/index.md +69 -69
- package/lib/assets/docs/article/getting-start-tdd/java/01-todo-list-and-first-test.md +234 -234
- package/lib/assets/docs/article/getting-start-tdd/java/02-fake-it-and-triangulation.md +261 -261
- package/lib/assets/docs/article/getting-start-tdd/java/03-obvious-implementation-and-refactoring.md +185 -185
- package/lib/assets/docs/article/getting-start-tdd/java/04-version-control-and-conventional-commits.md +115 -115
- package/lib/assets/docs/article/getting-start-tdd/java/05-package-management-and-static-analysis.md +382 -382
- package/lib/assets/docs/article/getting-start-tdd/java/06-task-runner-and-ci-cd.md +272 -272
- package/lib/assets/docs/article/getting-start-tdd/java/07-encapsulation-and-polymorphism.md +626 -626
- package/lib/assets/docs/article/getting-start-tdd/java/08-design-patterns.md +393 -393
- package/lib/assets/docs/article/getting-start-tdd/java/09-solid-principles-and-module-design.md +310 -310
- package/lib/assets/docs/article/getting-start-tdd/java/10-higher-order-functions-and-composition.md +188 -188
- package/lib/assets/docs/article/getting-start-tdd/java/11-immutable-data-and-pipeline.md +167 -167
- package/lib/assets/docs/article/getting-start-tdd/java/12-error-handling-and-type-safety.md +205 -205
- package/lib/assets/docs/article/getting-start-tdd/java/index.md +61 -61
- package/lib/assets/docs/article/getting-start-tdd/node/01-todo-list-and-first-test.md +244 -244
- package/lib/assets/docs/article/getting-start-tdd/node/02-fake-it-and-triangulation.md +262 -262
- package/lib/assets/docs/article/getting-start-tdd/node/03-obvious-implementation-and-refactoring.md +169 -169
- package/lib/assets/docs/article/getting-start-tdd/node/04-version-control-and-conventional-commits.md +112 -112
- package/lib/assets/docs/article/getting-start-tdd/node/05-package-management-and-static-analysis.md +314 -314
- package/lib/assets/docs/article/getting-start-tdd/node/06-task-runner-and-ci-cd.md +235 -235
- package/lib/assets/docs/article/getting-start-tdd/node/07-encapsulation-and-polymorphism.md +327 -327
- package/lib/assets/docs/article/getting-start-tdd/node/08-design-patterns.md +322 -322
- package/lib/assets/docs/article/getting-start-tdd/node/09-solid-principles-and-module-design.md +285 -285
- package/lib/assets/docs/article/getting-start-tdd/node/10-higher-order-functions-and-composition.md +199 -199
- package/lib/assets/docs/article/getting-start-tdd/node/11-immutable-data-and-pipeline.md +207 -207
- package/lib/assets/docs/article/getting-start-tdd/node/12-error-handling-and-type-safety.md +295 -295
- package/lib/assets/docs/article/getting-start-tdd/node/index.md +56 -56
- package/lib/assets/docs/article/getting-start-tdd/php/01-todo-list-and-first-test.md +259 -259
- package/lib/assets/docs/article/getting-start-tdd/php/02-fake-it-and-triangulation.md +200 -200
- package/lib/assets/docs/article/getting-start-tdd/php/03-obvious-implementation-and-refactoring.md +248 -248
- package/lib/assets/docs/article/getting-start-tdd/php/04-version-control-and-conventional-commits.md +141 -141
- package/lib/assets/docs/article/getting-start-tdd/php/05-package-management-and-static-analysis.md +410 -410
- package/lib/assets/docs/article/getting-start-tdd/php/06-task-runner-and-ci-cd.md +321 -321
- package/lib/assets/docs/article/getting-start-tdd/php/07-encapsulation-and-polymorphism.md +372 -372
- package/lib/assets/docs/article/getting-start-tdd/php/08-design-patterns.md +453 -453
- package/lib/assets/docs/article/getting-start-tdd/php/09-solid-principles-and-module-design.md +460 -460
- package/lib/assets/docs/article/getting-start-tdd/php/10-higher-order-functions-and-composition.md +182 -182
- package/lib/assets/docs/article/getting-start-tdd/php/11-immutable-data-and-pipeline.md +266 -266
- package/lib/assets/docs/article/getting-start-tdd/php/12-error-handling-and-type-safety.md +308 -308
- package/lib/assets/docs/article/getting-start-tdd/php/index.md +84 -84
- package/lib/assets/docs/article/getting-start-tdd/python/01-todo-list-and-first-test.md +201 -201
- package/lib/assets/docs/article/getting-start-tdd/python/02-fake-it-and-triangulation.md +247 -247
- package/lib/assets/docs/article/getting-start-tdd/python/03-obvious-implementation-and-refactoring.md +199 -199
- package/lib/assets/docs/article/getting-start-tdd/python/04-version-control-and-conventional-commits.md +87 -87
- package/lib/assets/docs/article/getting-start-tdd/python/05-package-management-and-static-analysis.md +274 -274
- package/lib/assets/docs/article/getting-start-tdd/python/06-task-runner-and-ci-cd.md +190 -190
- package/lib/assets/docs/article/getting-start-tdd/python/07-encapsulation-and-polymorphism.md +208 -208
- package/lib/assets/docs/article/getting-start-tdd/python/08-design-patterns.md +172 -172
- package/lib/assets/docs/article/getting-start-tdd/python/09-solid-principles-and-module-design.md +130 -130
- package/lib/assets/docs/article/getting-start-tdd/python/10-higher-order-functions-and-composition.md +122 -122
- package/lib/assets/docs/article/getting-start-tdd/python/11-immutable-data-and-pipeline.md +116 -116
- package/lib/assets/docs/article/getting-start-tdd/python/12-error-handling-and-type-safety.md +126 -126
- package/lib/assets/docs/article/getting-start-tdd/python/index.md +55 -55
- package/lib/assets/docs/article/getting-start-tdd/ruby/01-todo-list-and-first-test.md +231 -231
- package/lib/assets/docs/article/getting-start-tdd/ruby/02-fake-it-and-triangulation.md +238 -238
- package/lib/assets/docs/article/getting-start-tdd/ruby/03-obvious-implementation-and-refactoring.md +228 -228
- package/lib/assets/docs/article/getting-start-tdd/ruby/04-version-control-and-conventional-commits.md +112 -112
- package/lib/assets/docs/article/getting-start-tdd/ruby/05-package-management-and-static-analysis.md +287 -287
- package/lib/assets/docs/article/getting-start-tdd/ruby/06-task-runner-and-ci-cd.md +248 -248
- package/lib/assets/docs/article/getting-start-tdd/ruby/07-encapsulation-and-polymorphism.md +279 -279
- package/lib/assets/docs/article/getting-start-tdd/ruby/08-design-patterns.md +329 -329
- package/lib/assets/docs/article/getting-start-tdd/ruby/09-solid-principles-and-module-design.md +196 -196
- package/lib/assets/docs/article/getting-start-tdd/ruby/10-higher-order-functions-and-composition.md +175 -175
- package/lib/assets/docs/article/getting-start-tdd/ruby/11-immutable-data-and-pipeline.md +237 -237
- package/lib/assets/docs/article/getting-start-tdd/ruby/12-error-handling-and-type-safety.md +398 -398
- package/lib/assets/docs/article/getting-start-tdd/ruby/index.md +83 -83
- package/lib/assets/docs/article/getting-start-tdd/rust/01-todo-list-and-first-test.md +211 -211
- package/lib/assets/docs/article/getting-start-tdd/rust/02-fake-it-and-triangulation.md +264 -264
- package/lib/assets/docs/article/getting-start-tdd/rust/03-obvious-implementation-and-refactoring.md +233 -233
- package/lib/assets/docs/article/getting-start-tdd/rust/04-version-control-and-conventional-commits.md +92 -92
- package/lib/assets/docs/article/getting-start-tdd/rust/05-package-management-and-static-analysis.md +212 -212
- package/lib/assets/docs/article/getting-start-tdd/rust/06-task-runner-and-ci-cd.md +164 -164
- package/lib/assets/docs/article/getting-start-tdd/rust/07-encapsulation-and-polymorphism.md +142 -142
- package/lib/assets/docs/article/getting-start-tdd/rust/08-design-patterns.md +145 -145
- package/lib/assets/docs/article/getting-start-tdd/rust/09-solid-principles-and-module-design.md +110 -110
- package/lib/assets/docs/article/getting-start-tdd/rust/10-higher-order-functions-and-composition.md +94 -94
- package/lib/assets/docs/article/getting-start-tdd/rust/11-immutable-data-and-pipeline.md +105 -105
- package/lib/assets/docs/article/getting-start-tdd/rust/12-error-handling-and-type-safety.md +112 -112
- package/lib/assets/docs/article/getting-start-tdd/rust/index.md +83 -83
- package/lib/assets/docs/article/getting-start-tdd/scala/01-todo-list-and-first-test.md +111 -111
- package/lib/assets/docs/article/getting-start-tdd/scala/02-fake-it-and-triangulation.md +107 -107
- package/lib/assets/docs/article/getting-start-tdd/scala/03-obvious-implementation-and-refactoring.md +99 -99
- package/lib/assets/docs/article/getting-start-tdd/scala/04-version-control-and-conventional-commits.md +123 -123
- package/lib/assets/docs/article/getting-start-tdd/scala/05-package-management-and-static-analysis.md +196 -196
- package/lib/assets/docs/article/getting-start-tdd/scala/06-task-runner-and-ci-cd.md +186 -186
- package/lib/assets/docs/article/getting-start-tdd/scala/07-case-classes-and-traits.md +139 -139
- package/lib/assets/docs/article/getting-start-tdd/scala/08-pattern-matching-and-sealed-traits.md +106 -106
- package/lib/assets/docs/article/getting-start-tdd/scala/09-packages-and-module-design.md +75 -75
- package/lib/assets/docs/article/getting-start-tdd/scala/10-higher-order-functions-and-composition.md +104 -104
- package/lib/assets/docs/article/getting-start-tdd/scala/11-collections-and-lazy-evaluation.md +94 -94
- package/lib/assets/docs/article/getting-start-tdd/scala/12-error-handling-and-type-safety.md +92 -92
- package/lib/assets/docs/article/getting-start-tdd/scala/index.md +65 -65
- package/lib/assets/docs/article/grokking-concurrency/all/index.md +404 -404
- package/lib/assets/docs/article/grokking-concurrency/all/part-1-ch02-sequential.md +554 -554
- package/lib/assets/docs/article/grokking-concurrency/all/part-2-ch04-05-threads.md +469 -469
- package/lib/assets/docs/article/grokking-concurrency/all/part-3-ch06-multitasking.md +520 -520
- package/lib/assets/docs/article/grokking-concurrency/all/part-4-ch07-parallel-patterns.md +420 -420
- package/lib/assets/docs/article/grokking-concurrency/all/part-5-ch08-09-synchronization.md +510 -510
- package/lib/assets/docs/article/grokking-concurrency/all/part-6-ch10-11-nonblocking-io.md +435 -435
- package/lib/assets/docs/article/grokking-concurrency/all/part-7-ch12-async.md +465 -465
- package/lib/assets/docs/article/grokking-concurrency/all/part-8-ch13-mapreduce.md +377 -377
- package/lib/assets/docs/article/grokking-concurrency/clojure/index.md +116 -116
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-1.md +108 -108
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-2.md +101 -101
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-3.md +122 -122
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-4.md +123 -123
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-5.md +118 -118
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-6.md +89 -89
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-7.md +100 -100
- package/lib/assets/docs/article/grokking-concurrency/clojure/part-8.md +120 -120
- package/lib/assets/docs/article/grokking-concurrency/csharp/index.md +101 -101
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-1.md +97 -97
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-2.md +123 -123
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-3.md +101 -101
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-4.md +112 -112
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-5.md +99 -99
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-6.md +61 -61
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-7.md +84 -84
- package/lib/assets/docs/article/grokking-concurrency/csharp/part-8.md +92 -92
- package/lib/assets/docs/article/grokking-concurrency/fsharp/index.md +65 -65
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-1.md +80 -80
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-2.md +103 -103
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-3.md +94 -94
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-4.md +110 -110
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-5.md +104 -104
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-6.md +93 -93
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-7.md +121 -121
- package/lib/assets/docs/article/grokking-concurrency/fsharp/part-8.md +107 -107
- package/lib/assets/docs/article/grokking-concurrency/haskell/index.md +248 -248
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-1.md +96 -96
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-2.md +96 -96
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-3.md +91 -91
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-4.md +106 -106
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-5.md +99 -99
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-6.md +95 -95
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-7.md +111 -111
- package/lib/assets/docs/article/grokking-concurrency/haskell/part-8.md +118 -118
- package/lib/assets/docs/article/grokking-concurrency/index.md +66 -66
- package/lib/assets/docs/article/grokking-concurrency/java/index.md +102 -102
- package/lib/assets/docs/article/grokking-concurrency/java/part-1.md +308 -308
- package/lib/assets/docs/article/grokking-concurrency/java/part-2.md +334 -334
- package/lib/assets/docs/article/grokking-concurrency/java/part-3.md +221 -221
- package/lib/assets/docs/article/grokking-concurrency/java/part-4.md +213 -213
- package/lib/assets/docs/article/grokking-concurrency/java/part-5.md +112 -112
- package/lib/assets/docs/article/grokking-concurrency/java/part-6.md +69 -69
- package/lib/assets/docs/article/grokking-concurrency/java/part-7.md +101 -101
- package/lib/assets/docs/article/grokking-concurrency/java/part-8.md +101 -101
- package/lib/assets/docs/article/grokking-concurrency/python/index.md +313 -313
- package/lib/assets/docs/article/grokking-concurrency/python/part-1.md +239 -239
- package/lib/assets/docs/article/grokking-concurrency/python/part-2.md +418 -418
- package/lib/assets/docs/article/grokking-concurrency/python/part-3.md +227 -227
- package/lib/assets/docs/article/grokking-concurrency/python/part-4.md +299 -299
- package/lib/assets/docs/article/grokking-concurrency/python/part-5.md +315 -315
- package/lib/assets/docs/article/grokking-concurrency/python/part-6.md +297 -297
- package/lib/assets/docs/article/grokking-concurrency/python/part-7.md +314 -314
- package/lib/assets/docs/article/grokking-concurrency/python/part-8.md +360 -360
- package/lib/assets/docs/article/grokking-concurrency/rust/index.md +270 -270
- package/lib/assets/docs/article/grokking-concurrency/rust/part-1.md +108 -108
- package/lib/assets/docs/article/grokking-concurrency/rust/part-2.md +120 -120
- package/lib/assets/docs/article/grokking-concurrency/rust/part-3.md +126 -126
- package/lib/assets/docs/article/grokking-concurrency/rust/part-4.md +175 -175
- package/lib/assets/docs/article/grokking-concurrency/rust/part-5.md +158 -158
- package/lib/assets/docs/article/grokking-concurrency/rust/part-6.md +94 -94
- package/lib/assets/docs/article/grokking-concurrency/rust/part-7.md +133 -133
- package/lib/assets/docs/article/grokking-concurrency/rust/part-8.md +155 -155
- package/lib/assets/docs/article/grokking-concurrency/scala/index.md +69 -69
- package/lib/assets/docs/article/grokking-concurrency/scala/part-1.md +78 -78
- package/lib/assets/docs/article/grokking-concurrency/scala/part-2.md +112 -112
- package/lib/assets/docs/article/grokking-concurrency/scala/part-3.md +93 -93
- package/lib/assets/docs/article/grokking-concurrency/scala/part-4.md +110 -110
- package/lib/assets/docs/article/grokking-concurrency/scala/part-5.md +119 -119
- package/lib/assets/docs/article/grokking-concurrency/scala/part-6.md +83 -83
- package/lib/assets/docs/article/grokking-concurrency/scala/part-7.md +131 -131
- package/lib/assets/docs/article/grokking-concurrency/scala/part-8.md +129 -129
- package/lib/assets/docs/article/grokkingfp/all/index.md +368 -368
- package/lib/assets/docs/article/grokkingfp/all/part-1-ch01-fp-introduction.md +530 -530
- package/lib/assets/docs/article/grokkingfp/all/part-1-ch02-pure-functions.md +923 -923
- package/lib/assets/docs/article/grokkingfp/all/part-2-ch03-immutable-data.md +1128 -1128
- package/lib/assets/docs/article/grokkingfp/all/part-2-ch04-higher-order-functions.md +1104 -1104
- package/lib/assets/docs/article/grokkingfp/all/part-2-ch05-flatmap.md +1026 -1026
- package/lib/assets/docs/article/grokkingfp/all/part-3-ch06-option.md +785 -785
- package/lib/assets/docs/article/grokkingfp/all/part-3-ch07-either-adt.md +871 -871
- package/lib/assets/docs/article/grokkingfp/all/part-4-ch08-io-monad.md +972 -972
- package/lib/assets/docs/article/grokkingfp/all/part-4-ch09-streams.md +926 -926
- package/lib/assets/docs/article/grokkingfp/all/part-5-ch10-concurrency.md +870 -870
- package/lib/assets/docs/article/grokkingfp/all/part-6-ch11-application.md +715 -715
- package/lib/assets/docs/article/grokkingfp/all/part-6-ch12-testing.md +626 -626
- package/lib/assets/docs/article/grokkingfp/all/writing-plan.md +712 -712
- package/lib/assets/docs/article/grokkingfp/clojure/index.md +276 -276
- package/lib/assets/docs/article/grokkingfp/clojure/part-1.md +667 -667
- package/lib/assets/docs/article/grokkingfp/clojure/part-2.md +643 -643
- package/lib/assets/docs/article/grokkingfp/clojure/part-3.md +620 -620
- package/lib/assets/docs/article/grokkingfp/clojure/part-4.md +697 -697
- package/lib/assets/docs/article/grokkingfp/clojure/part-5.md +751 -751
- package/lib/assets/docs/article/grokkingfp/clojure/part-6.md +721 -721
- package/lib/assets/docs/article/grokkingfp/csharp/index.md +246 -246
- package/lib/assets/docs/article/grokkingfp/csharp/part-1.md +811 -811
- package/lib/assets/docs/article/grokkingfp/csharp/part-2.md +971 -971
- package/lib/assets/docs/article/grokkingfp/csharp/part-3.md +981 -981
- package/lib/assets/docs/article/grokkingfp/csharp/part-4.md +949 -949
- package/lib/assets/docs/article/grokkingfp/csharp/part-5.md +947 -947
- package/lib/assets/docs/article/grokkingfp/csharp/part-6.md +739 -739
- package/lib/assets/docs/article/grokkingfp/elixir/index.md +203 -203
- package/lib/assets/docs/article/grokkingfp/elixir/part-1.md +712 -712
- package/lib/assets/docs/article/grokkingfp/elixir/part-2.md +838 -838
- package/lib/assets/docs/article/grokkingfp/elixir/part-3.md +985 -985
- package/lib/assets/docs/article/grokkingfp/elixir/part-4.md +974 -974
- package/lib/assets/docs/article/grokkingfp/elixir/part-5.md +1286 -1286
- package/lib/assets/docs/article/grokkingfp/elixir/part-6.md +1049 -1049
- package/lib/assets/docs/article/grokkingfp/fsharp/index.md +210 -210
- package/lib/assets/docs/article/grokkingfp/fsharp/part-1.md +714 -714
- package/lib/assets/docs/article/grokkingfp/fsharp/part-2.md +961 -961
- package/lib/assets/docs/article/grokkingfp/fsharp/part-3.md +972 -972
- package/lib/assets/docs/article/grokkingfp/fsharp/part-4.md +832 -832
- package/lib/assets/docs/article/grokkingfp/fsharp/part-5.md +911 -911
- package/lib/assets/docs/article/grokkingfp/fsharp/part-6.md +922 -922
- package/lib/assets/docs/article/grokkingfp/haskell/index.md +234 -234
- package/lib/assets/docs/article/grokkingfp/haskell/part-1.md +591 -591
- package/lib/assets/docs/article/grokkingfp/haskell/part-2.md +866 -866
- package/lib/assets/docs/article/grokkingfp/haskell/part-3.md +915 -915
- package/lib/assets/docs/article/grokkingfp/haskell/part-4.md +878 -878
- package/lib/assets/docs/article/grokkingfp/haskell/part-5.md +845 -845
- package/lib/assets/docs/article/grokkingfp/haskell/part-6.md +844 -844
- package/lib/assets/docs/article/grokkingfp/index.md +143 -143
- package/lib/assets/docs/article/grokkingfp/java/index.md +211 -211
- package/lib/assets/docs/article/grokkingfp/java/part-1.md +648 -648
- package/lib/assets/docs/article/grokkingfp/java/part-2.md +675 -675
- package/lib/assets/docs/article/grokkingfp/java/part-3.md +672 -672
- package/lib/assets/docs/article/grokkingfp/java/part-4.md +771 -771
- package/lib/assets/docs/article/grokkingfp/java/part-5.md +959 -959
- package/lib/assets/docs/article/grokkingfp/java/part-6.md +1328 -1328
- package/lib/assets/docs/article/grokkingfp/python/index.md +258 -258
- package/lib/assets/docs/article/grokkingfp/python/part-1.md +443 -443
- package/lib/assets/docs/article/grokkingfp/python/part-2.md +958 -958
- package/lib/assets/docs/article/grokkingfp/python/part-3.md +1004 -1004
- package/lib/assets/docs/article/grokkingfp/python/part-4.md +765 -765
- package/lib/assets/docs/article/grokkingfp/python/part-5.md +747 -747
- package/lib/assets/docs/article/grokkingfp/python/part-6.md +861 -861
- package/lib/assets/docs/article/grokkingfp/ruby/index.md +330 -330
- package/lib/assets/docs/article/grokkingfp/ruby/part-1.md +755 -755
- package/lib/assets/docs/article/grokkingfp/ruby/part-2.md +938 -938
- package/lib/assets/docs/article/grokkingfp/ruby/part-3.md +946 -946
- package/lib/assets/docs/article/grokkingfp/ruby/part-4.md +921 -921
- package/lib/assets/docs/article/grokkingfp/ruby/part-5.md +908 -908
- package/lib/assets/docs/article/grokkingfp/ruby/part-6.md +1412 -1412
- package/lib/assets/docs/article/grokkingfp/rust/index.md +242 -242
- package/lib/assets/docs/article/grokkingfp/rust/part-1.md +634 -634
- package/lib/assets/docs/article/grokkingfp/rust/part-2.md +1060 -1060
- package/lib/assets/docs/article/grokkingfp/rust/part-3.md +994 -994
- package/lib/assets/docs/article/grokkingfp/rust/part-4.md +573 -573
- package/lib/assets/docs/article/grokkingfp/rust/part-5.md +705 -705
- package/lib/assets/docs/article/grokkingfp/rust/part-6.md +508 -508
- package/lib/assets/docs/article/grokkingfp/scala/index.md +171 -171
- package/lib/assets/docs/article/grokkingfp/scala/part-1.md +543 -543
- package/lib/assets/docs/article/grokkingfp/scala/part-2.md +946 -946
- package/lib/assets/docs/article/grokkingfp/scala/part-3.md +919 -919
- package/lib/assets/docs/article/grokkingfp/scala/part-4.md +742 -742
- package/lib/assets/docs/article/grokkingfp/scala/part-5.md +722 -722
- package/lib/assets/docs/article/grokkingfp/scala/part-6.md +867 -867
- package/lib/assets/docs/article/grokkingfp/typescript/index.md +273 -273
- package/lib/assets/docs/article/grokkingfp/typescript/part-1.md +561 -561
- package/lib/assets/docs/article/grokkingfp/typescript/part-2.md +1129 -1129
- package/lib/assets/docs/article/grokkingfp/typescript/part-3.md +842 -842
- package/lib/assets/docs/article/grokkingfp/typescript/part-4.md +1087 -1087
- package/lib/assets/docs/article/grokkingfp/typescript/part-5.md +717 -717
- package/lib/assets/docs/article/grokkingfp/typescript/part-6.md +982 -982
- package/lib/assets/docs/article/practical-database-design/index.md +121 -121
- package/lib/assets/docs/article/practical-database-design/part1/chapter01.md +288 -288
- package/lib/assets/docs/article/practical-database-design/part1/chapter02.md +518 -518
- package/lib/assets/docs/article/practical-database-design/part1/chapter03.md +557 -557
- package/lib/assets/docs/article/practical-database-design/part2/chapter04.md +924 -924
- package/lib/assets/docs/article/practical-database-design/part2/chapter05.md +1627 -1627
- package/lib/assets/docs/article/practical-database-design/part2/chapter06.md +2716 -2716
- package/lib/assets/docs/article/practical-database-design/part2/chapter07.md +2082 -2082
- package/lib/assets/docs/article/practical-database-design/part2/chapter08.md +2105 -2105
- package/lib/assets/docs/article/practical-database-design/part2/chapter09.md +2031 -2031
- package/lib/assets/docs/article/practical-database-design/part2/chapter10.md +1387 -1387
- package/lib/assets/docs/article/practical-database-design/part2/chapter11.md +1677 -1677
- package/lib/assets/docs/article/practical-database-design/part2/chapter12.md +1417 -1417
- package/lib/assets/docs/article/practical-database-design/part2/chapter13.md +1434 -1434
- package/lib/assets/docs/article/practical-database-design/part3/chapter14.md +667 -667
- package/lib/assets/docs/article/practical-database-design/part3/chapter15.md +1625 -1625
- package/lib/assets/docs/article/practical-database-design/part3/chapter16.md +1915 -1915
- package/lib/assets/docs/article/practical-database-design/part3/chapter17.md +1708 -1708
- package/lib/assets/docs/article/practical-database-design/part3/chapter18.md +2095 -2095
- package/lib/assets/docs/article/practical-database-design/part3/chapter19.md +1123 -1123
- package/lib/assets/docs/article/practical-database-design/part3/chapter20.md +1031 -1031
- package/lib/assets/docs/article/practical-database-design/part3/chapter21.md +1382 -1382
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter14-orm.md +991 -991
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter15-orm.md +1300 -1300
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter16-orm.md +1166 -1166
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter17-orm.md +1584 -1584
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter18-orm.md +1183 -1183
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter19-orm.md +1016 -1016
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter20-orm.md +1753 -1753
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter21-orm.md +1447 -1447
- package/lib/assets/docs/article/practical-database-design/part3-orm/chapter22-orm.md +1878 -1878
- package/lib/assets/docs/article/practical-database-design/part4/chapter22.md +965 -965
- package/lib/assets/docs/article/practical-database-design/part4/chapter23.md +2069 -2069
- package/lib/assets/docs/article/practical-database-design/part4/chapter24.md +2439 -2439
- package/lib/assets/docs/article/practical-database-design/part4/chapter25.md +3661 -3661
- package/lib/assets/docs/article/practical-database-design/part4/chapter26.md +2916 -2916
- package/lib/assets/docs/article/practical-database-design/part4/chapter27.md +3105 -3105
- package/lib/assets/docs/article/practical-database-design/part4/chapter28.md +2697 -2697
- package/lib/assets/docs/article/practical-database-design/part4/chapter29.md +2544 -2544
- package/lib/assets/docs/article/practical-database-design/part4/chapter30.md +2180 -2180
- package/lib/assets/docs/article/practical-database-design/part4/chapter31.md +1192 -1192
- package/lib/assets/docs/article/practical-database-design/part4/chapter32.md +2101 -2101
- package/lib/assets/docs/article/practical-database-design/part5/chapter33.md +1032 -1032
- package/lib/assets/docs/article/practical-database-design/part5/chapter34.md +1609 -1609
- package/lib/assets/docs/article/practical-database-design/part5/chapter35.md +1453 -1453
- package/lib/assets/docs/article/practical-database-design/part5/chapter36.md +1292 -1292
- package/lib/assets/docs/article/practical-database-design/part5/chapter37.md +1470 -1470
- package/lib/assets/docs/article/practical-database-design/part5/chapter38.md +1698 -1698
- package/lib/assets/docs/article/practical-database-design/part5/chapter39.md +2334 -2334
- package/lib/assets/docs/article/practical-database-design/study/study2-1.md +1693 -1693
- package/lib/assets/docs/article/practical-database-design/study/study2-2.md +1347 -1347
- package/lib/assets/docs/article/practical-database-design/study/study2-3.md +2044 -2044
- package/lib/assets/docs/article/practical-database-design/study/study2-4.md +2229 -2229
- package/lib/assets/docs/article/practical-database-design/study/study2-5.md +2418 -2418
- package/lib/assets/docs/article/practical-database-design/study/study3-1.md +2205 -2205
- package/lib/assets/docs/article/practical-database-design/study/study3-2.md +2221 -2221
- package/lib/assets/docs/article/practical-database-design/study/study3-3.md +2253 -2253
- package/lib/assets/docs/article/practical-database-design/study/study3-4.md +2106 -2106
- package/lib/assets/docs/article/practical-database-design/study/study3-5.md +2507 -2507
- package/lib/assets/docs/article/practical-database-design/study/study4-1.md +2587 -2587
- package/lib/assets/docs/article/practical-database-design/study/study4-2.md +2075 -2075
- package/lib/assets/docs/article/practical-database-design/study/study4-3.md +1805 -1805
- package/lib/assets/docs/article/practical-database-design/study/study4-4.md +1895 -1895
- package/lib/assets/docs/article/practical-database-design/study/study4-5.md +2878 -2878
- package/lib/assets/docs/assets/css/extra.css +29 -29
- package/lib/assets/docs/assets/js/extra.js +44 -44
- package/lib/assets/docs/development/index.md +39 -39
- package/lib/assets/docs/operation/index.md +11 -11
- package/lib/assets/docs/reference/CodexCLIMCP/343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/351/226/213/347/231/272/343/203/225/343/203/255/343/203/274.md +532 -532
- package/lib/assets/docs/reference/CodexCLIMCP/343/202/265/343/203/274/343/203/220/343/203/274/350/250/255/345/256/232/346/211/213/351/240/206.md +341 -341
- package/lib/assets/docs/reference/Java/343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/347/222/260/345/242/203/346/247/213/347/257/211/343/202/254/343/202/244/343/203/211.md +581 -580
- package/lib/assets/docs/reference/SonarQube/343/203/255/343/203/274/343/202/253/343/203/253/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +642 -642
- package/lib/assets/docs/reference/TypeScript/343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/347/222/260/345/242/203/346/247/213/347/257/211/343/202/254/343/202/244/343/203/211.md +465 -465
- package/lib/assets/docs/reference/UI/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +450 -450
- package/lib/assets/docs/reference/images/Ansoff.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/BrandBasicStrategy.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/BrandCategorization.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/BrandRecurutementStrategy.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/BrandValue.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/BusinessActivitiy.svg +3 -3
- package/lib/assets/docs/reference/images/HRM.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/MarketingStructure.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/OrganizationElemnts.svg +3 -3
- package/lib/assets/docs/reference/images/PPM.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/PositioningMap.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/ProductLayer.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/ProductMix.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/SWOT.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/TargetMarket.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/ThreeGenericStrategies.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/VRIO.drawio.svg +3 -3
- package/lib/assets/docs/reference/images/ValueChain.drawio.svg +3 -3
- package/lib/assets/docs/reference/index.md +52 -52
- package/lib/assets/docs/reference//343/202/210/343/201/204/343/202/275/343/203/225/343/203/210/343/202/246/343/202/247/343/202/242/343/201/250/343/201/257.md +250 -242
- package/lib/assets/docs/reference//343/202/242/343/203/274/343/202/255/343/203/206/343/202/257/343/203/201/343/203/243/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +2216 -2216
- package/lib/assets/docs/reference//343/202/244/343/203/263/343/203/225/343/203/251/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +1878 -1878
- package/lib/assets/docs/reference//343/202/250/343/202/257/343/202/271/343/203/210/343/203/252/343/203/274/343/203/240/343/203/227/343/203/255/343/202/260/343/203/251/343/203/237/343/203/263/343/202/260.md +550 -544
- package/lib/assets/docs/reference//343/202/263/343/203/274/343/203/207/343/202/243/343/203/263/343/202/260/343/201/250/343/203/206/343/202/271/343/203/210/343/202/254/343/202/244/343/203/211.md +705 -705
- package/lib/assets/docs/reference//343/203/206/343/202/271/343/203/210/346/210/246/347/225/245/343/202/254/343/202/244/343/203/211.md +1313 -1313
- package/lib/assets/docs/reference//343/203/207/343/203/274/343/202/277/343/203/242/343/203/207/343/203/253/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +311 -311
- package/lib/assets/docs/reference//343/203/211/343/203/241/343/202/244/343/203/263/343/203/242/343/203/207/343/203/253/350/250/255/350/250/210/343/202/254/343/202/244/343/203/211.md +599 -599
- package/lib/assets/docs/reference//343/203/223/343/202/270/343/203/215/343/202/271/343/202/242/343/203/274/343/202/255/343/203/206/343/202/257/343/203/201/343/203/243/345/210/206/346/236/220/343/202/254/343/202/244/343/203/211.md +528 -528
- package/lib/assets/docs/reference//343/203/246/343/203/274/343/202/271/343/202/261/343/203/274/343/202/271/344/275/234/346/210/220/343/202/254/343/202/244/343/203/211.md +689 -682
- package/lib/assets/docs/reference//343/203/252/343/203/252/343/203/274/343/202/271/343/202/254/343/202/244/343/203/211.md +461 -461
- package/lib/assets/docs/reference//343/203/252/343/203/252/343/203/274/343/202/271/343/203/273/343/202/244/343/203/206/343/203/254/343/203/274/343/202/267/343/203/247/343/203/263/350/250/210/347/224/273/343/202/254/343/202/244/343/203/211.md +580 -560
- package/lib/assets/docs/reference//343/203/255/343/202/270/343/202/253/343/203/253/343/202/267/343/203/263/343/202/255/343/203/263/343/202/260.md +1367 -1367
- package/lib/assets/docs/reference//344/274/201/346/245/255/347/265/214/345/226/266/350/253/226.md +2637 -2636
- package/lib/assets/docs/reference//347/222/260/345/242/203/345/244/211/346/225/260/347/256/241/347/220/206/343/202/254/343/202/244/343/203/211.md +665 -663
- package/lib/assets/docs/reference//350/246/201/344/273/266/345/256/232/347/276/251/343/202/254/343/202/244/343/203/211.md +1248 -1248
- package/lib/assets/docs/reference//350/250/200/350/252/236/345/210/245/351/226/213/347/231/272/343/202/254/343/202/244/343/203/211.md +28 -0
- package/lib/assets/docs/reference//351/201/213/345/226/266/347/256/241/347/220/206.md +1482 -1482
- package/lib/assets/docs/reference//351/201/213/347/224/250/343/202/271/343/202/257/343/203/252/343/203/227/343/203/210/344/275/234/346/210/220/343/202/254/343/202/244/343/203/211.md +421 -421
- package/lib/assets/docs/reference//351/201/213/347/224/250/350/246/201/344/273/266/345/256/232/347/276/251/343/202/254/343/202/244/343/203/211.md +392 -392
- package/lib/assets/docs/reference//351/226/213/347/231/272/343/202/254/343/202/244/343/203/211.md +299 -299
- package/lib/assets/docs/reference//351/235/236/346/251/237/350/203/275/350/246/201/344/273/266/345/256/232/347/276/251/343/202/254/343/202/244/343/203/211.md +1236 -1236
- package/lib/assets/docs/review/index.md +5 -5
- package/lib/assets/docs/strategy/index.md +1 -1
- package/lib/assets/docs/template/ADR.md +30 -30
- package/lib/assets/docs/template/AWS/343/202/271/343/203/206/343/203/274/343/202/270/343/203/263/343/202/260/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +1366 -1366
- package/lib/assets/docs/template/AWS/343/203/227/343/203/255/343/203/200/343/202/257/343/202/267/343/203/247/343/203/263/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +634 -634
- package/lib/assets/docs/template/README.md +50 -50
- package/lib/assets/docs/template/index.md +23 -23
- package/lib/assets/docs/template//343/201/276/343/201/232/343/201/223/343/202/214/343/202/222/350/252/255/343/202/202/343/201/206/343/203/252/343/202/271/343/203/210.md +12 -12
- package/lib/assets/docs/template//343/202/242/343/203/227/343/203/252/343/202/261/343/203/274/343/202/267/343/203/247/343/203/263/351/226/213/347/231/272/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +547 -547
- package/lib/assets/docs/template//343/202/244/343/203/206/343/203/254/343/203/274/343/202/267/343/203/247/343/203/263/345/256/214/344/272/206/345/240/261/345/221/212/346/233/270.md +58 -58
- package/lib/assets/docs/template//343/202/244/343/203/263/343/202/273/343/203/227/343/202/267/343/203/247/343/203/263/343/203/207/343/203/203/343/202/255.md +13 -13
- package/lib/assets/docs/template//343/203/223/343/202/270/343/203/215/343/202/271/343/202/242/343/203/274/343/202/255/343/203/206/343/202/257/343/203/201/343/203/243.md +379 -379
- package/lib/assets/docs/template//344/274/201/346/245/255/345/210/206/346/236/220.md +573 -573
- package/lib/assets/docs/template//345/256/214/345/205/250/345/275/242/345/274/217/343/201/256/343/203/246/343/203/274/343/202/271/343/202/261/343/203/274/343/202/271.md +69 -68
- package/lib/assets/docs/template//350/246/201/344/273/266/345/256/232/347/276/251.md +669 -669
- package/lib/assets/docs/template//350/250/255/350/250/210.md +173 -173
- package/lib/assets/docs/template//351/226/213/347/231/272/347/222/260/345/242/203/343/202/273/343/203/203/343/203/210/343/202/242/343/203/203/343/203/227/346/211/213/351/240/206/346/233/270.md +688 -688
- package/lib/assets/gulpfile.js +25 -25
- package/lib/assets/mkdocs.yml +136 -135
- package/lib/assets/ops/docker/mkdoc/Dockerfile +19 -19
- package/lib/assets/ops/scripts/journal.js +180 -180
- package/lib/assets/ops/scripts/mkdocs.js +82 -82
- package/lib/assets/ops/scripts/release.js +431 -431
- package/lib/assets/ops/scripts/sonar_local.js +726 -726
- package/lib/assets/ops/scripts/ssh.js +190 -190
- package/lib/assets/ops/scripts/vault.js +299 -299
- package/lib/assets/package-lock.json +1653 -1653
- package/lib/assets/package.json +40 -40
- package/lib/gulpfile.js +37 -37
- package/package.json +41 -41
- package/lib/assets/.claude/agent-memory/xp-programmer/MEMORY.md +0 -6
- package/lib/assets/.claude/agent-memory/xp-programmer/project_cargo_tracker.md +0 -11
- package/lib/assets/.claude/agent-memory/xp-programmer/project_ddd_patterns.md +0 -27
- package/lib/assets/.claude/agent-memory/xp-programmer/project_us07_route_assignment.md +0 -19
|
@@ -1,2044 +1,2044 @@
|
|
|
1
|
-
# 実践データベース設計:販売管理システム 研究 3 - gRPC サービスの実装
|
|
2
|
-
|
|
3
|
-
## はじめに
|
|
4
|
-
|
|
5
|
-
本研究では、API サーバー構成(第13章)やモノリスサービス(研究 1)とは異なるアプローチとして、**gRPC** による販売管理システムを実装します。Protocol Buffers による高効率なバイナリシリアライゼーションと、HTTP/2 によるストリーミング通信を活用し、高性能なマイクロサービス間通信を実現します。
|
|
6
|
-
|
|
7
|
-
研究 1 で構築したヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として gRPC サービス層のみを追加**します。
|
|
8
|
-
|
|
9
|
-
---
|
|
10
|
-
|
|
11
|
-
## 第14章:gRPC サーバーの基礎
|
|
12
|
-
|
|
13
|
-
### 14.1 gRPC とは
|
|
14
|
-
|
|
15
|
-
gRPC は Google が開発したオープンソースの高性能 RPC(Remote Procedure Call)フレームワークです。HTTP/2 プロトコルと Protocol Buffers を基盤とし、マイクロサービス間通信に最適化されています。
|
|
16
|
-
|
|
17
|
-
```plantuml
|
|
18
|
-
@startuml grpc_architecture
|
|
19
|
-
!define RECTANGLE class
|
|
20
|
-
|
|
21
|
-
skinparam backgroundColor #FEFEFE
|
|
22
|
-
|
|
23
|
-
package "gRPC Architecture (販売管理システム)" {
|
|
24
|
-
|
|
25
|
-
package "Client Side" {
|
|
26
|
-
RECTANGLE "gRPC Client\n(Generated Stub)" as client {
|
|
27
|
-
- 単項 RPC
|
|
28
|
-
- サーバーストリーミング
|
|
29
|
-
- クライアントストリーミング
|
|
30
|
-
- 双方向ストリーミング
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
package "Server Side" {
|
|
35
|
-
RECTANGLE "gRPC Server\n(grpc-spring-boot-starter)" as server {
|
|
36
|
-
- ProductService
|
|
37
|
-
- PartnerService
|
|
38
|
-
- OrderService
|
|
39
|
-
- ShipmentService
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
package "Shared" {
|
|
44
|
-
RECTANGLE "Protocol Buffers\n(.proto files)" as proto {
|
|
45
|
-
- product.proto
|
|
46
|
-
- partner.proto
|
|
47
|
-
- order.proto
|
|
48
|
-
- common.proto
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
client --> proto : ".proto からスタブ生成"
|
|
54
|
-
server --> proto : ".proto からスケルトン生成"
|
|
55
|
-
client <--> server : "HTTP/2\n(Binary Protocol)"
|
|
56
|
-
|
|
57
|
-
note bottom of proto
|
|
58
|
-
スキーマ駆動開発
|
|
59
|
-
言語中立なインターフェース定義
|
|
60
|
-
高効率なバイナリシリアライゼーション
|
|
61
|
-
end note
|
|
62
|
-
|
|
63
|
-
@enduml
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
**gRPC の主な特徴:**
|
|
67
|
-
|
|
68
|
-
| 特徴 | 説明 |
|
|
69
|
-
|------|------|
|
|
70
|
-
| HTTP/2 | 多重化、ヘッダー圧縮、バイナリフレーミング |
|
|
71
|
-
| Protocol Buffers | 高速なバイナリシリアライゼーション |
|
|
72
|
-
| 多言語サポート | Java, Go, Python, C#, Node.js 等 |
|
|
73
|
-
| ストリーミング | 4つの RPC パターンをサポート |
|
|
74
|
-
| デッドライン | タイムアウト管理の組み込みサポート |
|
|
75
|
-
| 認証 | TLS/SSL、トークンベース認証のサポート |
|
|
76
|
-
|
|
77
|
-
---
|
|
78
|
-
|
|
79
|
-
### 14.2 REST API / GraphQL との比較
|
|
80
|
-
|
|
81
|
-
```plantuml
|
|
82
|
-
@startuml api_comparison
|
|
83
|
-
skinparam backgroundColor #FEFEFE
|
|
84
|
-
|
|
85
|
-
rectangle "REST API" as rest {
|
|
86
|
-
(HTTP/1.1 or 2)
|
|
87
|
-
(JSON/XML)
|
|
88
|
-
(OpenAPI)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
rectangle "gRPC" as grpc {
|
|
92
|
-
(HTTP/2)
|
|
93
|
-
(Protocol Buffers)
|
|
94
|
-
(.proto)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
rectangle "GraphQL" as graphql {
|
|
98
|
-
(HTTP/1.1 or 2)
|
|
99
|
-
(JSON)
|
|
100
|
-
(.graphqls)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
note bottom of rest
|
|
104
|
-
汎用性が高い
|
|
105
|
-
ブラウザ直接アクセス可能
|
|
106
|
-
人間が読みやすい
|
|
107
|
-
end note
|
|
108
|
-
|
|
109
|
-
note bottom of grpc
|
|
110
|
-
高性能
|
|
111
|
-
マイクロサービス向け
|
|
112
|
-
型安全
|
|
113
|
-
end note
|
|
114
|
-
|
|
115
|
-
note bottom of graphql
|
|
116
|
-
柔軟なクエリ
|
|
117
|
-
フロントエンド向け
|
|
118
|
-
単一エンドポイント
|
|
119
|
-
end note
|
|
120
|
-
|
|
121
|
-
@enduml
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
**詳細比較表:**
|
|
125
|
-
|
|
126
|
-
| 特徴 | REST API | gRPC | GraphQL |
|
|
127
|
-
|------|----------|------|---------|
|
|
128
|
-
| プロトコル | HTTP/1.1 | HTTP/2 | HTTP/1.1 or HTTP/2 |
|
|
129
|
-
| データ形式 | JSON | Protocol Buffers | JSON |
|
|
130
|
-
| スキーマ | OpenAPI (任意) | .proto (必須) | .graphqls (必須) |
|
|
131
|
-
| シリアライゼーション | テキスト | バイナリ | テキスト |
|
|
132
|
-
| パフォーマンス | 中 | 高 | 中 |
|
|
133
|
-
| ストリーミング | WebSocket 別実装 | ネイティブサポート | Subscription |
|
|
134
|
-
| ブラウザ対応 | ◎ | △ (gRPC-Web) | ◎ |
|
|
135
|
-
| 学習コスト | 低 | 中 | 中 |
|
|
136
|
-
| 主な用途 | 汎用 API | マイクロサービス | フロントエンド向け |
|
|
137
|
-
|
|
138
|
-
**gRPC を選択する場面:**
|
|
139
|
-
|
|
140
|
-
1. **マイクロサービス間通信**: 高性能な内部通信が必要な場合
|
|
141
|
-
2. **リアルタイム処理**: ストリーミングによる進捗通知が必要な場合
|
|
142
|
-
3. **多言語環境**: 異なる言語で実装されたサービス間の連携
|
|
143
|
-
4. **低レイテンシ**: バイナリプロトコルによる高速通信が必要な場合
|
|
144
|
-
|
|
145
|
-
---
|
|
146
|
-
|
|
147
|
-
### 14.3 4つの RPC パターン
|
|
148
|
-
|
|
149
|
-
gRPC は 4 つの RPC パターンをサポートします。
|
|
150
|
-
|
|
151
|
-
```plantuml
|
|
152
|
-
@startuml grpc_patterns
|
|
153
|
-
skinparam backgroundColor #FEFEFE
|
|
154
|
-
|
|
155
|
-
rectangle "1. 単項 RPC (Unary)" {
|
|
156
|
-
card "Client" as c1
|
|
157
|
-
card "Server" as s1
|
|
158
|
-
c1 -right-> s1 : "Request"
|
|
159
|
-
s1 -left-> c1 : "Response"
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
rectangle "2. サーバーストリーミング RPC" {
|
|
163
|
-
card "Client" as c2
|
|
164
|
-
card "Server" as s2
|
|
165
|
-
c2 -right-> s2 : "Request"
|
|
166
|
-
s2 -left-> c2 : "Response 1,2,...N"
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
rectangle "3. クライアントストリーミング RPC" {
|
|
170
|
-
card "Client" as c3
|
|
171
|
-
card "Server" as s3
|
|
172
|
-
c3 -right-> s3 : "Request 1,2,...N"
|
|
173
|
-
s3 -left-> c3 : "Response"
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
rectangle "4. 双方向ストリーミング RPC" {
|
|
177
|
-
card "Client" as c4
|
|
178
|
-
card "Server" as s4
|
|
179
|
-
c4 <-right-> s4 : "Request/Response Stream"
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
@enduml
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
**各パターンの用途:**
|
|
186
|
-
|
|
187
|
-
| パターン | 用途例(販売管理システム) |
|
|
188
|
-
|----------|---------------------------|
|
|
189
|
-
| 単項 RPC | 商品情報取得、受注登録 |
|
|
190
|
-
| サーバーストリーミング | 商品一覧取得、出荷進捗通知 |
|
|
191
|
-
| クライアントストリーミング | 受注明細の一括登録 |
|
|
192
|
-
| 双方向ストリーミング | リアルタイム在庫同期 |
|
|
193
|
-
|
|
194
|
-
---
|
|
195
|
-
|
|
196
|
-
### 14.4 gRPC におけるヘキサゴナルアーキテクチャ
|
|
197
|
-
|
|
198
|
-
gRPC を導入しても、既存のヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として gRPC サービス層のみを追加**します。
|
|
199
|
-
|
|
200
|
-
```plantuml
|
|
201
|
-
@startuml hexagonal_grpc
|
|
202
|
-
!define RECTANGLE class
|
|
203
|
-
|
|
204
|
-
package "Hexagonal Architecture (gRPC版)" {
|
|
205
|
-
|
|
206
|
-
RECTANGLE "Application Core\n(Domain + Use Cases)" as core {
|
|
207
|
-
- Product (商品)
|
|
208
|
-
- Partner (取引先)
|
|
209
|
-
- Order (受注)
|
|
210
|
-
- Shipment (出荷)
|
|
211
|
-
- Invoice (請求)
|
|
212
|
-
- ProductUseCase
|
|
213
|
-
- OrderUseCase
|
|
214
|
-
- ShipmentUseCase
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
RECTANGLE "Input Adapters\n(Driving Side)" as input {
|
|
218
|
-
- REST Controller(既存)
|
|
219
|
-
- gRPC Service(新規追加)
|
|
220
|
-
- StreamObserver
|
|
221
|
-
- ServerInterceptor
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
RECTANGLE "Output Adapters\n(Driven Side)" as output {
|
|
225
|
-
- MyBatis Repository
|
|
226
|
-
- Database Access
|
|
227
|
-
- Entity Mapping
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
input --> core : "Input Ports\n(Use Cases)"
|
|
232
|
-
core --> output : "Output Ports\n(Repository Interfaces)"
|
|
233
|
-
|
|
234
|
-
note top of core
|
|
235
|
-
既存のビジネスロジック
|
|
236
|
-
REST API 版と完全に共有
|
|
237
|
-
gRPC 固有のコードは含まない
|
|
238
|
-
end note
|
|
239
|
-
|
|
240
|
-
note left of input
|
|
241
|
-
gRPC サービスを
|
|
242
|
-
Input Adapter として追加
|
|
243
|
-
既存の REST と共存可能
|
|
244
|
-
end note
|
|
245
|
-
|
|
246
|
-
@enduml
|
|
247
|
-
```
|
|
248
|
-
|
|
249
|
-
**gRPC でもヘキサゴナルアーキテクチャを維持する理由:**
|
|
250
|
-
|
|
251
|
-
1. **再利用性**: 既存の UseCase/Repository をそのまま活用
|
|
252
|
-
2. **並行運用**: REST API と gRPC を同時提供可能
|
|
253
|
-
3. **テスト容易性**: ドメインロジックは通信プロトコルに依存しない
|
|
254
|
-
4. **移行容易性**: 段階的に API 形式を追加・変更可能
|
|
255
|
-
|
|
256
|
-
---
|
|
257
|
-
|
|
258
|
-
### 14.5 ディレクトリ構成
|
|
259
|
-
|
|
260
|
-
既存の構成に `infrastructure/grpc/` を追加するだけです。
|
|
261
|
-
|
|
262
|
-
```
|
|
263
|
-
src/main/java/com/example/sales/
|
|
264
|
-
├── domain/ # ドメイン層(API版と共通)
|
|
265
|
-
│ ├── model/
|
|
266
|
-
│ │ ├── product/ # 商品ドメイン
|
|
267
|
-
│ │ ├── partner/ # 取引先ドメイン
|
|
268
|
-
│ │ ├── order/ # 受注ドメイン
|
|
269
|
-
│ │ ├── shipment/ # 出荷ドメイン
|
|
270
|
-
│ │ └── invoice/ # 請求ドメイン
|
|
271
|
-
│ └── exception/
|
|
272
|
-
│
|
|
273
|
-
├── application/ # アプリケーション層(API版と共通)
|
|
274
|
-
│ ├── port/
|
|
275
|
-
│ │ ├── in/ # Input Port(ユースケース)
|
|
276
|
-
│ │ └── out/ # Output Port(リポジトリ)
|
|
277
|
-
│ └── service/
|
|
278
|
-
│
|
|
279
|
-
├── infrastructure/
|
|
280
|
-
│ ├── persistence/ # Output Adapter(DB実装)- 既存
|
|
281
|
-
│ │ ├── mapper/
|
|
282
|
-
│ │ └── repository/
|
|
283
|
-
│ ├── rest/ # Input Adapter(REST実装)- 既存
|
|
284
|
-
│ └── grpc/ # Input Adapter(gRPC実装)- 新規追加
|
|
285
|
-
│ ├── service/ # gRPC サービス実装
|
|
286
|
-
│ ├── interceptor/ # インターセプター
|
|
287
|
-
│ └── converter/ # ドメイン ↔ Proto 変換
|
|
288
|
-
│
|
|
289
|
-
├── config/
|
|
290
|
-
│
|
|
291
|
-
└── src/main/proto/ # Protocol Buffers 定義
|
|
292
|
-
├── common.proto
|
|
293
|
-
├── product.proto
|
|
294
|
-
├── partner.proto
|
|
295
|
-
├── order.proto
|
|
296
|
-
├── shipment.proto
|
|
297
|
-
└── invoice.proto
|
|
298
|
-
```
|
|
299
|
-
|
|
300
|
-
---
|
|
301
|
-
|
|
302
|
-
### 14.6 技術スタックの追加
|
|
303
|
-
|
|
304
|
-
既存の `build.gradle.kts` に gRPC 関連の依存関係を追加します。
|
|
305
|
-
|
|
306
|
-
#### build.gradle.kts(差分)
|
|
307
|
-
|
|
308
|
-
<details>
|
|
309
|
-
<summary>コード例: build.gradle.kts</summary>
|
|
310
|
-
|
|
311
|
-
```kotlin
|
|
312
|
-
import com.google.protobuf.gradle.*
|
|
313
|
-
|
|
314
|
-
plugins {
|
|
315
|
-
// 既存のプラグイン
|
|
316
|
-
id("java")
|
|
317
|
-
id("org.springframework.boot") version "4.0.0"
|
|
318
|
-
id("io.spring.dependency-management") version "1.1.0"
|
|
319
|
-
|
|
320
|
-
// gRPC 関連を追加
|
|
321
|
-
id("com.google.protobuf") version "0.9.4"
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
dependencies {
|
|
325
|
-
// 既存の依存関係(Spring Boot, MyBatis, PostgreSQL等)はそのまま
|
|
326
|
-
|
|
327
|
-
// gRPC 関連を追加
|
|
328
|
-
implementation("net.devh:grpc-spring-boot-starter:3.1.0.RELEASE")
|
|
329
|
-
implementation("io.grpc:grpc-protobuf:1.62.2")
|
|
330
|
-
implementation("io.grpc:grpc-stub:1.62.2")
|
|
331
|
-
implementation("io.grpc:grpc-services:1.62.2") // ヘルスチェック、リフレクション
|
|
332
|
-
|
|
333
|
-
// Protocol Buffers
|
|
334
|
-
implementation("com.google.protobuf:protobuf-java:3.25.3")
|
|
335
|
-
implementation("com.google.protobuf:protobuf-java-util:3.25.3") // JSON 変換
|
|
336
|
-
|
|
337
|
-
// Jakarta Annotation (gRPC generated code で必要)
|
|
338
|
-
compileOnly("jakarta.annotation:jakarta.annotation-api:2.1.1")
|
|
339
|
-
|
|
340
|
-
// Test
|
|
341
|
-
testImplementation("io.grpc:grpc-testing:1.62.2")
|
|
342
|
-
testImplementation("net.devh:grpc-client-spring-boot-starter:3.1.0.RELEASE")
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
protobuf {
|
|
346
|
-
protoc {
|
|
347
|
-
artifact = "com.google.protobuf:protoc:3.25.3"
|
|
348
|
-
}
|
|
349
|
-
plugins {
|
|
350
|
-
create("grpc") {
|
|
351
|
-
artifact = "io.grpc:protoc-gen-grpc-java:1.62.2"
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
generateProtoTasks {
|
|
355
|
-
all().forEach { task ->
|
|
356
|
-
task.plugins {
|
|
357
|
-
create("grpc")
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
// 生成コードのソースパス追加
|
|
364
|
-
sourceSets {
|
|
365
|
-
main {
|
|
366
|
-
java {
|
|
367
|
-
srcDirs(
|
|
368
|
-
"build/generated/source/proto/main/java",
|
|
369
|
-
"build/generated/source/proto/main/grpc"
|
|
370
|
-
)
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
</details>
|
|
377
|
-
|
|
378
|
-
**追加パッケージの説明:**
|
|
379
|
-
|
|
380
|
-
| パッケージ | 用途 |
|
|
381
|
-
|-----------|------|
|
|
382
|
-
| grpc-spring-boot-starter | Spring Boot gRPC 統合 |
|
|
383
|
-
| grpc-protobuf | gRPC + Protocol Buffers 統合 |
|
|
384
|
-
| grpc-stub | gRPC クライアントスタブ |
|
|
385
|
-
| grpc-services | ヘルスチェック、リフレクションサービス |
|
|
386
|
-
| protobuf-java | Protocol Buffers Java ランタイム |
|
|
387
|
-
| protobuf-java-util | JSON ⇔ Protobuf 変換ユーティリティ |
|
|
388
|
-
| grpc-testing | gRPC テストサポート |
|
|
389
|
-
|
|
390
|
-
#### application.yml(差分)
|
|
391
|
-
|
|
392
|
-
<details>
|
|
393
|
-
<summary>コード例: application.yml</summary>
|
|
394
|
-
|
|
395
|
-
```yaml
|
|
396
|
-
# 既存の設定はそのまま
|
|
397
|
-
|
|
398
|
-
# gRPC サーバー設定
|
|
399
|
-
grpc:
|
|
400
|
-
server:
|
|
401
|
-
port: 9090
|
|
402
|
-
reflection-service-enabled: true # gRPC リフレクション有効化
|
|
403
|
-
health-service-enabled: true # ヘルスチェック有効化
|
|
404
|
-
max-inbound-message-size: 4194304 # 4MB
|
|
405
|
-
max-inbound-metadata-size: 8192 # 8KB
|
|
406
|
-
|
|
407
|
-
# ログ設定
|
|
408
|
-
logging:
|
|
409
|
-
level:
|
|
410
|
-
net.devh.boot.grpc: DEBUG
|
|
411
|
-
io.grpc: INFO
|
|
412
|
-
```
|
|
413
|
-
|
|
414
|
-
</details>
|
|
415
|
-
|
|
416
|
-
---
|
|
417
|
-
|
|
418
|
-
### 14.7 Protocol Buffers 定義
|
|
419
|
-
|
|
420
|
-
#### src/main/proto/common.proto
|
|
421
|
-
|
|
422
|
-
<details>
|
|
423
|
-
<summary>コード例: common.proto</summary>
|
|
424
|
-
|
|
425
|
-
```protobuf
|
|
426
|
-
syntax = "proto3";
|
|
427
|
-
|
|
428
|
-
package com.example.sales;
|
|
429
|
-
|
|
430
|
-
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
431
|
-
option java_outer_classname = "CommonProto";
|
|
432
|
-
option java_multiple_files = true;
|
|
433
|
-
|
|
434
|
-
import "google/protobuf/timestamp.proto";
|
|
435
|
-
import "google/protobuf/wrappers.proto";
|
|
436
|
-
|
|
437
|
-
// 共通のページネーション
|
|
438
|
-
message PageRequest {
|
|
439
|
-
int32 page = 1;
|
|
440
|
-
int32 size = 2;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
message PageInfo {
|
|
444
|
-
int32 page = 1;
|
|
445
|
-
int32 size = 2;
|
|
446
|
-
int32 total_elements = 3;
|
|
447
|
-
int32 total_pages = 4;
|
|
448
|
-
bool has_next = 5;
|
|
449
|
-
bool has_previous = 6;
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
// 共通のエラー応答
|
|
453
|
-
message ErrorDetail {
|
|
454
|
-
string field = 1;
|
|
455
|
-
string message = 2;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
message ErrorResponse {
|
|
459
|
-
string code = 1;
|
|
460
|
-
string message = 2;
|
|
461
|
-
repeated ErrorDetail details = 3;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
// 金額型(Decimal 相当)
|
|
465
|
-
message Money {
|
|
466
|
-
int64 units = 1; // 整数部
|
|
467
|
-
int32 nanos = 2; // 小数部(10億分の1単位)
|
|
468
|
-
string currency = 3; // 通貨コード(JPY等)
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// 日付型(Date 相当)
|
|
472
|
-
message Date {
|
|
473
|
-
int32 year = 1;
|
|
474
|
-
int32 month = 2;
|
|
475
|
-
int32 day = 3;
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
// 監査情報
|
|
479
|
-
message AuditInfo {
|
|
480
|
-
google.protobuf.Timestamp created_at = 1;
|
|
481
|
-
string created_by = 2;
|
|
482
|
-
google.protobuf.Timestamp updated_at = 3;
|
|
483
|
-
string updated_by = 4;
|
|
484
|
-
}
|
|
485
|
-
```
|
|
486
|
-
|
|
487
|
-
</details>
|
|
488
|
-
|
|
489
|
-
#### src/main/proto/product.proto
|
|
490
|
-
|
|
491
|
-
<details>
|
|
492
|
-
<summary>コード例: product.proto</summary>
|
|
493
|
-
|
|
494
|
-
```protobuf
|
|
495
|
-
syntax = "proto3";
|
|
496
|
-
|
|
497
|
-
package com.example.sales;
|
|
498
|
-
|
|
499
|
-
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
500
|
-
option java_outer_classname = "ProductProto";
|
|
501
|
-
option java_multiple_files = true;
|
|
502
|
-
|
|
503
|
-
import "common.proto";
|
|
504
|
-
import "google/protobuf/empty.proto";
|
|
505
|
-
|
|
506
|
-
// 商品区分
|
|
507
|
-
enum ProductCategory {
|
|
508
|
-
PRODUCT_CATEGORY_UNSPECIFIED = 0;
|
|
509
|
-
PRODUCT_CATEGORY_GOODS = 1; // 商品
|
|
510
|
-
PRODUCT_CATEGORY_SERVICE = 2; // サービス
|
|
511
|
-
PRODUCT_CATEGORY_SUPPLY = 3; // 消耗品
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
// 税区分
|
|
515
|
-
enum TaxCategory {
|
|
516
|
-
TAX_CATEGORY_UNSPECIFIED = 0;
|
|
517
|
-
TAX_CATEGORY_TAXABLE = 1; // 課税
|
|
518
|
-
TAX_CATEGORY_EXEMPT = 2; // 非課税
|
|
519
|
-
TAX_CATEGORY_REDUCED = 3; // 軽減税率
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
// 商品メッセージ
|
|
523
|
-
message Product {
|
|
524
|
-
string product_code = 1; // 商品コード
|
|
525
|
-
string product_name = 2; // 商品名
|
|
526
|
-
ProductCategory category = 3; // 商品区分
|
|
527
|
-
TaxCategory tax_category = 4; // 税区分
|
|
528
|
-
Money standard_price = 5; // 標準単価
|
|
529
|
-
string product_group_code = 6; // 商品分類コード
|
|
530
|
-
string unit_code = 7; // 単位コード
|
|
531
|
-
bool is_active = 8; // 有効フラグ
|
|
532
|
-
AuditInfo audit = 9; // 監査情報
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
// === リクエスト/レスポンス ===
|
|
536
|
-
|
|
537
|
-
// 商品取得
|
|
538
|
-
message GetProductRequest {
|
|
539
|
-
string product_code = 1;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
message GetProductResponse {
|
|
543
|
-
Product product = 1;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
// 商品一覧取得(サーバーストリーミング)
|
|
547
|
-
message ListProductsRequest {
|
|
548
|
-
PageRequest page = 1;
|
|
549
|
-
ProductCategory category = 2; // フィルタ(オプション)
|
|
550
|
-
string product_group_code = 3; // フィルタ(オプション)
|
|
551
|
-
string keyword = 4; // 検索キーワード
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
// 商品登録
|
|
555
|
-
message CreateProductRequest {
|
|
556
|
-
string product_code = 1;
|
|
557
|
-
string product_name = 2;
|
|
558
|
-
ProductCategory category = 3;
|
|
559
|
-
TaxCategory tax_category = 4;
|
|
560
|
-
Money standard_price = 5;
|
|
561
|
-
string product_group_code = 6;
|
|
562
|
-
string unit_code = 7;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
message CreateProductResponse {
|
|
566
|
-
Product product = 1;
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
// 商品一括登録(クライアントストリーミング)
|
|
570
|
-
message BulkCreateProductRequest {
|
|
571
|
-
CreateProductRequest product = 1;
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
message BulkCreateProductResponse {
|
|
575
|
-
int32 success_count = 1;
|
|
576
|
-
int32 failure_count = 2;
|
|
577
|
-
repeated ErrorDetail errors = 3;
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
// === サービス定義 ===
|
|
581
|
-
|
|
582
|
-
service ProductService {
|
|
583
|
-
// 単項 RPC
|
|
584
|
-
rpc GetProduct(GetProductRequest) returns (GetProductResponse);
|
|
585
|
-
rpc CreateProduct(CreateProductRequest) returns (CreateProductResponse);
|
|
586
|
-
rpc UpdateProduct(UpdateProductRequest) returns (UpdateProductResponse);
|
|
587
|
-
rpc DeleteProduct(DeleteProductRequest) returns (DeleteProductResponse);
|
|
588
|
-
|
|
589
|
-
// サーバーストリーミング RPC(大量データ取得)
|
|
590
|
-
rpc ListProducts(ListProductsRequest) returns (stream Product);
|
|
591
|
-
|
|
592
|
-
// クライアントストリーミング RPC(一括登録)
|
|
593
|
-
rpc BulkCreateProducts(stream BulkCreateProductRequest) returns (BulkCreateProductResponse);
|
|
594
|
-
}
|
|
595
|
-
```
|
|
596
|
-
|
|
597
|
-
</details>
|
|
598
|
-
|
|
599
|
-
---
|
|
600
|
-
|
|
601
|
-
### 14.8 gRPC サービス実装
|
|
602
|
-
|
|
603
|
-
#### GrpcProductService.java
|
|
604
|
-
|
|
605
|
-
<details>
|
|
606
|
-
<summary>コード例: GrpcProductService.java</summary>
|
|
607
|
-
|
|
608
|
-
```java
|
|
609
|
-
package com.example.sales.infrastructure.grpc.service;
|
|
610
|
-
|
|
611
|
-
import com.example.sales.application.port.in.ProductUseCase;
|
|
612
|
-
import com.example.sales.domain.model.product.Product;
|
|
613
|
-
import com.example.sales.domain.model.product.ProductCode;
|
|
614
|
-
import com.example.sales.infrastructure.grpc.converter.ProductConverter;
|
|
615
|
-
import com.example.sales.infrastructure.grpc.proto.*;
|
|
616
|
-
import io.grpc.stub.StreamObserver;
|
|
617
|
-
import net.devh.boot.grpc.server.service.GrpcService;
|
|
618
|
-
import org.slf4j.Logger;
|
|
619
|
-
import org.slf4j.LoggerFactory;
|
|
620
|
-
|
|
621
|
-
import java.util.List;
|
|
622
|
-
import java.util.concurrent.atomic.AtomicInteger;
|
|
623
|
-
|
|
624
|
-
/**
|
|
625
|
-
* 商品 gRPC サービス実装
|
|
626
|
-
*/
|
|
627
|
-
@GrpcService
|
|
628
|
-
public class GrpcProductService extends ProductServiceGrpc.ProductServiceImplBase {
|
|
629
|
-
|
|
630
|
-
private static final Logger log = LoggerFactory.getLogger(GrpcProductService.class);
|
|
631
|
-
|
|
632
|
-
private final ProductUseCase productUseCase;
|
|
633
|
-
private final ProductConverter converter;
|
|
634
|
-
|
|
635
|
-
public GrpcProductService(ProductUseCase productUseCase, ProductConverter converter) {
|
|
636
|
-
this.productUseCase = productUseCase;
|
|
637
|
-
this.converter = converter;
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
/**
|
|
641
|
-
* 単項 RPC: 商品取得
|
|
642
|
-
*/
|
|
643
|
-
@Override
|
|
644
|
-
public void getProduct(GetProductRequest request,
|
|
645
|
-
StreamObserver<GetProductResponse> responseObserver) {
|
|
646
|
-
log.info("getProduct: {}", request.getProductCode());
|
|
647
|
-
|
|
648
|
-
try {
|
|
649
|
-
ProductCode code = new ProductCode(request.getProductCode());
|
|
650
|
-
Product product = productUseCase.findByCode(code)
|
|
651
|
-
.orElseThrow(() -> new ProductNotFoundException(code));
|
|
652
|
-
|
|
653
|
-
GetProductResponse response = GetProductResponse.newBuilder()
|
|
654
|
-
.setProduct(converter.toProto(product))
|
|
655
|
-
.build();
|
|
656
|
-
|
|
657
|
-
responseObserver.onNext(response);
|
|
658
|
-
responseObserver.onCompleted();
|
|
659
|
-
|
|
660
|
-
} catch (ProductNotFoundException e) {
|
|
661
|
-
log.warn("Product not found: {}", request.getProductCode());
|
|
662
|
-
responseObserver.onError(
|
|
663
|
-
io.grpc.Status.NOT_FOUND
|
|
664
|
-
.withDescription("商品が見つかりません: " + request.getProductCode())
|
|
665
|
-
.asRuntimeException()
|
|
666
|
-
);
|
|
667
|
-
} catch (Exception e) {
|
|
668
|
-
log.error("Error getting product", e);
|
|
669
|
-
responseObserver.onError(
|
|
670
|
-
io.grpc.Status.INTERNAL
|
|
671
|
-
.withDescription("内部エラーが発生しました")
|
|
672
|
-
.withCause(e)
|
|
673
|
-
.asRuntimeException()
|
|
674
|
-
);
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
|
|
678
|
-
/**
|
|
679
|
-
* サーバーストリーミング RPC: 商品一覧取得
|
|
680
|
-
*/
|
|
681
|
-
@Override
|
|
682
|
-
public void listProducts(ListProductsRequest request,
|
|
683
|
-
StreamObserver<Product> responseObserver) {
|
|
684
|
-
log.info("listProducts: category={}, keyword={}",
|
|
685
|
-
request.getCategory(), request.getKeyword());
|
|
686
|
-
|
|
687
|
-
try {
|
|
688
|
-
List<Product> products = productUseCase.findAll(
|
|
689
|
-
converter.toCriteria(request)
|
|
690
|
-
);
|
|
691
|
-
|
|
692
|
-
// ストリーミングで商品を1件ずつ送信
|
|
693
|
-
for (Product product : products) {
|
|
694
|
-
responseObserver.onNext(converter.toProto(product));
|
|
695
|
-
}
|
|
696
|
-
|
|
697
|
-
responseObserver.onCompleted();
|
|
698
|
-
log.info("listProducts completed: {} items", products.size());
|
|
699
|
-
|
|
700
|
-
} catch (Exception e) {
|
|
701
|
-
log.error("Error listing products", e);
|
|
702
|
-
responseObserver.onError(
|
|
703
|
-
io.grpc.Status.INTERNAL
|
|
704
|
-
.withDescription("内部エラーが発生しました")
|
|
705
|
-
.asRuntimeException()
|
|
706
|
-
);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
/**
|
|
711
|
-
* クライアントストリーミング RPC: 商品一括登録
|
|
712
|
-
*/
|
|
713
|
-
@Override
|
|
714
|
-
public StreamObserver<BulkCreateProductRequest> bulkCreateProducts(
|
|
715
|
-
StreamObserver<BulkCreateProductResponse> responseObserver) {
|
|
716
|
-
|
|
717
|
-
log.info("bulkCreateProducts: started");
|
|
718
|
-
|
|
719
|
-
AtomicInteger successCount = new AtomicInteger(0);
|
|
720
|
-
AtomicInteger failureCount = new AtomicInteger(0);
|
|
721
|
-
List<ErrorDetail> errors = new java.util.ArrayList<>();
|
|
722
|
-
|
|
723
|
-
return new StreamObserver<BulkCreateProductRequest>() {
|
|
724
|
-
@Override
|
|
725
|
-
public void onNext(BulkCreateProductRequest request) {
|
|
726
|
-
try {
|
|
727
|
-
CreateProductRequest productRequest = request.getProduct();
|
|
728
|
-
Product product = converter.toDomain(productRequest);
|
|
729
|
-
productUseCase.create(product);
|
|
730
|
-
successCount.incrementAndGet();
|
|
731
|
-
log.debug("Created product: {}", productRequest.getProductCode());
|
|
732
|
-
|
|
733
|
-
} catch (Exception e) {
|
|
734
|
-
failureCount.incrementAndGet();
|
|
735
|
-
errors.add(ErrorDetail.newBuilder()
|
|
736
|
-
.setField(request.getProduct().getProductCode())
|
|
737
|
-
.setMessage(e.getMessage())
|
|
738
|
-
.build());
|
|
739
|
-
log.warn("Failed to create product: {}",
|
|
740
|
-
request.getProduct().getProductCode(), e);
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
@Override
|
|
745
|
-
public void onError(Throwable t) {
|
|
746
|
-
log.error("bulkCreateProducts error", t);
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
@Override
|
|
750
|
-
public void onCompleted() {
|
|
751
|
-
BulkCreateProductResponse response = BulkCreateProductResponse.newBuilder()
|
|
752
|
-
.setSuccessCount(successCount.get())
|
|
753
|
-
.setFailureCount(failureCount.get())
|
|
754
|
-
.addAllErrors(errors)
|
|
755
|
-
.build();
|
|
756
|
-
|
|
757
|
-
responseObserver.onNext(response);
|
|
758
|
-
responseObserver.onCompleted();
|
|
759
|
-
|
|
760
|
-
log.info("bulkCreateProducts completed: success={}, failure={}",
|
|
761
|
-
successCount.get(), failureCount.get());
|
|
762
|
-
}
|
|
763
|
-
};
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
```
|
|
767
|
-
|
|
768
|
-
</details>
|
|
769
|
-
|
|
770
|
-
---
|
|
771
|
-
|
|
772
|
-
### 14.9 ProductConverter(ドメイン ⇔ Proto 変換)
|
|
773
|
-
|
|
774
|
-
<details>
|
|
775
|
-
<summary>コード例: ProductConverter.java</summary>
|
|
776
|
-
|
|
777
|
-
```java
|
|
778
|
-
package com.example.sales.infrastructure.grpc.converter;
|
|
779
|
-
|
|
780
|
-
import com.example.sales.domain.model.product.*;
|
|
781
|
-
import com.example.sales.infrastructure.grpc.proto.*;
|
|
782
|
-
import org.springframework.stereotype.Component;
|
|
783
|
-
|
|
784
|
-
import java.math.BigDecimal;
|
|
785
|
-
|
|
786
|
-
/**
|
|
787
|
-
* 商品ドメインモデル ⇔ Protocol Buffers 変換
|
|
788
|
-
*/
|
|
789
|
-
@Component
|
|
790
|
-
public class ProductConverter {
|
|
791
|
-
|
|
792
|
-
/**
|
|
793
|
-
* ドメインモデル → Proto
|
|
794
|
-
*/
|
|
795
|
-
public com.example.sales.infrastructure.grpc.proto.Product toProto(Product domain) {
|
|
796
|
-
return com.example.sales.infrastructure.grpc.proto.Product.newBuilder()
|
|
797
|
-
.setProductCode(domain.getProductCode().getValue())
|
|
798
|
-
.setProductName(domain.getProductName())
|
|
799
|
-
.setCategory(toProtoCategory(domain.getCategory()))
|
|
800
|
-
.setTaxCategory(toProtoTaxCategory(domain.getTaxCategory()))
|
|
801
|
-
.setStandardPrice(toProtoMoney(domain.getStandardPrice()))
|
|
802
|
-
.setProductGroupCode(nullToEmpty(domain.getProductGroupCode()))
|
|
803
|
-
.setUnitCode(nullToEmpty(domain.getUnitCode()))
|
|
804
|
-
.setIsActive(domain.isActive())
|
|
805
|
-
.build();
|
|
806
|
-
}
|
|
807
|
-
|
|
808
|
-
/**
|
|
809
|
-
* CreateProductRequest → ドメインモデル
|
|
810
|
-
*/
|
|
811
|
-
public Product toDomain(CreateProductRequest request) {
|
|
812
|
-
return Product.builder()
|
|
813
|
-
.productCode(new ProductCode(request.getProductCode()))
|
|
814
|
-
.productName(request.getProductName())
|
|
815
|
-
.category(toDomainCategory(request.getCategory()))
|
|
816
|
-
.taxCategory(toDomainTaxCategory(request.getTaxCategory()))
|
|
817
|
-
.standardPrice(fromProtoMoney(request.getStandardPrice()))
|
|
818
|
-
.productGroupCode(emptyToNull(request.getProductGroupCode()))
|
|
819
|
-
.unitCode(emptyToNull(request.getUnitCode()))
|
|
820
|
-
.isActive(true)
|
|
821
|
-
.build();
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
// === 区分変換 ===
|
|
825
|
-
|
|
826
|
-
private ProductCategory toProtoCategory(
|
|
827
|
-
com.example.sales.domain.model.product.ProductCategory domain) {
|
|
828
|
-
return switch (domain) {
|
|
829
|
-
case GOODS -> ProductCategory.PRODUCT_CATEGORY_GOODS;
|
|
830
|
-
case SERVICE -> ProductCategory.PRODUCT_CATEGORY_SERVICE;
|
|
831
|
-
case SUPPLY -> ProductCategory.PRODUCT_CATEGORY_SUPPLY;
|
|
832
|
-
};
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
// === Money 変換 ===
|
|
836
|
-
|
|
837
|
-
private Money toProtoMoney(BigDecimal amount) {
|
|
838
|
-
if (amount == null) {
|
|
839
|
-
return Money.getDefaultInstance();
|
|
840
|
-
}
|
|
841
|
-
long units = amount.longValue();
|
|
842
|
-
int nanos = amount.remainder(BigDecimal.ONE)
|
|
843
|
-
.multiply(BigDecimal.valueOf(1_000_000_000))
|
|
844
|
-
.intValue();
|
|
845
|
-
|
|
846
|
-
return Money.newBuilder()
|
|
847
|
-
.setUnits(units)
|
|
848
|
-
.setNanos(nanos)
|
|
849
|
-
.setCurrency("JPY")
|
|
850
|
-
.build();
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
private BigDecimal fromProtoMoney(Money money) {
|
|
854
|
-
if (money == null || money.equals(Money.getDefaultInstance())) {
|
|
855
|
-
return BigDecimal.ZERO;
|
|
856
|
-
}
|
|
857
|
-
return BigDecimal.valueOf(money.getUnits())
|
|
858
|
-
.add(BigDecimal.valueOf(money.getNanos(), 9));
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
// === ユーティリティ ===
|
|
862
|
-
|
|
863
|
-
private String nullToEmpty(String value) {
|
|
864
|
-
return value == null ? "" : value;
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
private String emptyToNull(String value) {
|
|
868
|
-
return value == null || value.isEmpty() ? null : value;
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
```
|
|
872
|
-
|
|
873
|
-
</details>
|
|
874
|
-
|
|
875
|
-
---
|
|
876
|
-
|
|
877
|
-
### 14.10 gRPC インターセプター
|
|
878
|
-
|
|
879
|
-
#### LoggingInterceptor.java
|
|
880
|
-
|
|
881
|
-
<details>
|
|
882
|
-
<summary>コード例: LoggingInterceptor.java</summary>
|
|
883
|
-
|
|
884
|
-
```java
|
|
885
|
-
package com.example.sales.infrastructure.grpc.interceptor;
|
|
886
|
-
|
|
887
|
-
import io.grpc.*;
|
|
888
|
-
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
|
|
889
|
-
import org.slf4j.Logger;
|
|
890
|
-
import org.slf4j.LoggerFactory;
|
|
891
|
-
import org.slf4j.MDC;
|
|
892
|
-
|
|
893
|
-
import java.util.UUID;
|
|
894
|
-
|
|
895
|
-
/**
|
|
896
|
-
* gRPC ログインターセプター
|
|
897
|
-
*/
|
|
898
|
-
@GrpcGlobalServerInterceptor
|
|
899
|
-
public class LoggingInterceptor implements ServerInterceptor {
|
|
900
|
-
|
|
901
|
-
private static final Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);
|
|
902
|
-
|
|
903
|
-
@Override
|
|
904
|
-
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
|
|
905
|
-
ServerCall<ReqT, RespT> call,
|
|
906
|
-
Metadata headers,
|
|
907
|
-
ServerCallHandler<ReqT, RespT> next) {
|
|
908
|
-
|
|
909
|
-
String requestId = UUID.randomUUID().toString().substring(0, 8);
|
|
910
|
-
String methodName = call.getMethodDescriptor().getFullMethodName();
|
|
911
|
-
long startTime = System.currentTimeMillis();
|
|
912
|
-
|
|
913
|
-
MDC.put("requestId", requestId);
|
|
914
|
-
log.info("gRPC Request started: method={}", methodName);
|
|
915
|
-
|
|
916
|
-
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<>(
|
|
917
|
-
next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<>(call) {
|
|
918
|
-
@Override
|
|
919
|
-
public void close(Status status, Metadata trailers) {
|
|
920
|
-
long duration = System.currentTimeMillis() - startTime;
|
|
921
|
-
log.info("gRPC Request completed: method={}, status={}, duration={}ms",
|
|
922
|
-
methodName, status.getCode(), duration);
|
|
923
|
-
MDC.remove("requestId");
|
|
924
|
-
super.close(status, trailers);
|
|
925
|
-
}
|
|
926
|
-
}, headers)) {};
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
```
|
|
930
|
-
|
|
931
|
-
</details>
|
|
932
|
-
|
|
933
|
-
---
|
|
934
|
-
|
|
935
|
-
### 14.11 grpcurl による動作確認
|
|
936
|
-
|
|
937
|
-
<details>
|
|
938
|
-
<summary>コード例: grpcurl コマンド</summary>
|
|
939
|
-
|
|
940
|
-
```bash
|
|
941
|
-
# サービス一覧取得
|
|
942
|
-
grpcurl -plaintext localhost:9090 list
|
|
943
|
-
|
|
944
|
-
# メソッド一覧取得
|
|
945
|
-
grpcurl -plaintext localhost:9090 list com.example.sales.ProductService
|
|
946
|
-
|
|
947
|
-
# 商品取得(単項 RPC)
|
|
948
|
-
grpcurl -plaintext -d '{"product_code": "PRD001"}' \
|
|
949
|
-
localhost:9090 com.example.sales.ProductService/GetProduct
|
|
950
|
-
|
|
951
|
-
# 商品一覧取得(サーバーストリーミング)
|
|
952
|
-
grpcurl -plaintext -d '{"page": {"page": 0, "size": 10}}' \
|
|
953
|
-
localhost:9090 com.example.sales.ProductService/ListProducts
|
|
954
|
-
|
|
955
|
-
# 商品登録
|
|
956
|
-
grpcurl -plaintext -d '{
|
|
957
|
-
"product_code": "PRD100",
|
|
958
|
-
"product_name": "新商品",
|
|
959
|
-
"category": "PRODUCT_CATEGORY_GOODS",
|
|
960
|
-
"tax_category": "TAX_CATEGORY_TAXABLE",
|
|
961
|
-
"standard_price": {"units": 1000, "currency": "JPY"}
|
|
962
|
-
}' localhost:9090 com.example.sales.ProductService/CreateProduct
|
|
963
|
-
```
|
|
964
|
-
|
|
965
|
-
</details>
|
|
966
|
-
|
|
967
|
-
---
|
|
968
|
-
|
|
969
|
-
## 第15章:マスタ API の実装
|
|
970
|
-
|
|
971
|
-
### 15.1 商品マスタ API の詳細設計
|
|
972
|
-
|
|
973
|
-
第14章で基礎を学んだ gRPC サービスを、商品・取引先・倉庫マスタに対して拡張します。
|
|
974
|
-
|
|
975
|
-
**商品マスタ API の機能一覧:**
|
|
976
|
-
|
|
977
|
-
| メソッド | RPC パターン | 説明 |
|
|
978
|
-
|----------|-------------|------|
|
|
979
|
-
| GetProduct | 単項 | 商品コードで取得 |
|
|
980
|
-
| CreateProduct | 単項 | 商品登録 |
|
|
981
|
-
| UpdateProduct | 単項 | 商品更新 |
|
|
982
|
-
| DeleteProduct | 単項 | 商品削除(論理削除) |
|
|
983
|
-
| ListProducts | サーバーストリーミング | 商品一覧取得 |
|
|
984
|
-
| BulkCreateProducts | クライアントストリーミング | 一括登録 |
|
|
985
|
-
| BulkUpdateProducts | クライアントストリーミング | 一括更新 |
|
|
986
|
-
| CheckProductExists | 単項 | 存在チェック |
|
|
987
|
-
|
|
988
|
-
---
|
|
989
|
-
|
|
990
|
-
### 15.2 取引先マスタ API の実装
|
|
991
|
-
|
|
992
|
-
#### src/main/proto/partner.proto
|
|
993
|
-
|
|
994
|
-
<details>
|
|
995
|
-
<summary>コード例: partner.proto</summary>
|
|
996
|
-
|
|
997
|
-
```protobuf
|
|
998
|
-
syntax = "proto3";
|
|
999
|
-
|
|
1000
|
-
package com.example.sales;
|
|
1001
|
-
|
|
1002
|
-
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
1003
|
-
option java_outer_classname = "PartnerProto";
|
|
1004
|
-
option java_multiple_files = true;
|
|
1005
|
-
|
|
1006
|
-
import "common.proto";
|
|
1007
|
-
|
|
1008
|
-
// 取引先区分
|
|
1009
|
-
enum PartnerType {
|
|
1010
|
-
PARTNER_TYPE_UNSPECIFIED = 0;
|
|
1011
|
-
PARTNER_TYPE_CUSTOMER = 1; // 顧客
|
|
1012
|
-
PARTNER_TYPE_SUPPLIER = 2; // 仕入先
|
|
1013
|
-
PARTNER_TYPE_BOTH = 3; // 顧客兼仕入先
|
|
1014
|
-
}
|
|
1015
|
-
|
|
1016
|
-
// 取引先ステータス
|
|
1017
|
-
enum PartnerStatus {
|
|
1018
|
-
PARTNER_STATUS_UNSPECIFIED = 0;
|
|
1019
|
-
PARTNER_STATUS_ACTIVE = 1; // 取引中
|
|
1020
|
-
PARTNER_STATUS_SUSPENDED = 2; // 取引停止
|
|
1021
|
-
PARTNER_STATUS_CLOSED = 3; // 取引終了
|
|
1022
|
-
}
|
|
1023
|
-
|
|
1024
|
-
// 取引先メッセージ
|
|
1025
|
-
message Partner {
|
|
1026
|
-
string partner_code = 1;
|
|
1027
|
-
string partner_name = 2;
|
|
1028
|
-
string partner_name_kana = 3;
|
|
1029
|
-
PartnerType partner_type = 4;
|
|
1030
|
-
PartnerStatus status = 5;
|
|
1031
|
-
Address address = 6;
|
|
1032
|
-
Contact contact = 7;
|
|
1033
|
-
CustomerInfo customer_info = 8; // 顧客情報(顧客の場合)
|
|
1034
|
-
SupplierInfo supplier_info = 9; // 仕入先情報(仕入先の場合)
|
|
1035
|
-
AuditInfo audit = 10;
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
// 与信チェック
|
|
1039
|
-
message CheckCreditRequest {
|
|
1040
|
-
string partner_code = 1;
|
|
1041
|
-
Money amount = 2; // チェック金額
|
|
1042
|
-
}
|
|
1043
|
-
|
|
1044
|
-
message CheckCreditResponse {
|
|
1045
|
-
bool is_within_limit = 1; // 限度内か
|
|
1046
|
-
Money credit_limit = 2; // 与信限度額
|
|
1047
|
-
Money current_receivable = 3; // 現在売掛金残高
|
|
1048
|
-
Money available_credit = 4; // 利用可能与信額
|
|
1049
|
-
}
|
|
1050
|
-
|
|
1051
|
-
// === サービス定義 ===
|
|
1052
|
-
|
|
1053
|
-
service PartnerService {
|
|
1054
|
-
// 単項 RPC
|
|
1055
|
-
rpc GetPartner(GetPartnerRequest) returns (GetPartnerResponse);
|
|
1056
|
-
rpc CreatePartner(CreatePartnerRequest) returns (CreatePartnerResponse);
|
|
1057
|
-
rpc CheckCredit(CheckCreditRequest) returns (CheckCreditResponse);
|
|
1058
|
-
|
|
1059
|
-
// サーバーストリーミング RPC
|
|
1060
|
-
rpc ListPartners(ListPartnersRequest) returns (stream Partner);
|
|
1061
|
-
}
|
|
1062
|
-
```
|
|
1063
|
-
|
|
1064
|
-
</details>
|
|
1065
|
-
|
|
1066
|
-
---
|
|
1067
|
-
|
|
1068
|
-
### 15.3 テスト実装
|
|
1069
|
-
|
|
1070
|
-
<details>
|
|
1071
|
-
<summary>コード例: GrpcProductServiceTest.java</summary>
|
|
1072
|
-
|
|
1073
|
-
```java
|
|
1074
|
-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
|
1075
|
-
@ActiveProfiles("test")
|
|
1076
|
-
class GrpcProductServiceTest {
|
|
1077
|
-
|
|
1078
|
-
private ManagedChannel channel;
|
|
1079
|
-
private ProductServiceGrpc.ProductServiceBlockingStub blockingStub;
|
|
1080
|
-
private ProductServiceGrpc.ProductServiceStub asyncStub;
|
|
1081
|
-
|
|
1082
|
-
@BeforeEach
|
|
1083
|
-
void setUp() {
|
|
1084
|
-
channel = ManagedChannelBuilder.forAddress("localhost", 9999)
|
|
1085
|
-
.usePlaintext()
|
|
1086
|
-
.build();
|
|
1087
|
-
blockingStub = ProductServiceGrpc.newBlockingStub(channel);
|
|
1088
|
-
asyncStub = ProductServiceGrpc.newStub(channel);
|
|
1089
|
-
}
|
|
1090
|
-
|
|
1091
|
-
@AfterEach
|
|
1092
|
-
void tearDown() {
|
|
1093
|
-
channel.shutdownNow();
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
@Test
|
|
1097
|
-
@DisplayName("商品の CRUD 操作が正常に動作すること")
|
|
1098
|
-
void productCrud() {
|
|
1099
|
-
// 登録
|
|
1100
|
-
CreateProductResponse createResponse = blockingStub.createProduct(
|
|
1101
|
-
CreateProductRequest.newBuilder()
|
|
1102
|
-
.setProductCode("PRD999")
|
|
1103
|
-
.setProductName("テスト商品")
|
|
1104
|
-
.setCategory(ProductCategory.PRODUCT_CATEGORY_GOODS)
|
|
1105
|
-
.setTaxCategory(TaxCategory.TAX_CATEGORY_TAXABLE)
|
|
1106
|
-
.setStandardPrice(Money.newBuilder().setUnits(1000).setCurrency("JPY"))
|
|
1107
|
-
.build()
|
|
1108
|
-
);
|
|
1109
|
-
assertThat(createResponse.getProduct().getProductCode()).isEqualTo("PRD999");
|
|
1110
|
-
|
|
1111
|
-
// 取得
|
|
1112
|
-
GetProductResponse getResponse = blockingStub.getProduct(
|
|
1113
|
-
GetProductRequest.newBuilder()
|
|
1114
|
-
.setProductCode("PRD999")
|
|
1115
|
-
.build()
|
|
1116
|
-
);
|
|
1117
|
-
assertThat(getResponse.getProduct().getProductName()).isEqualTo("テスト商品");
|
|
1118
|
-
}
|
|
1119
|
-
|
|
1120
|
-
@Test
|
|
1121
|
-
@DisplayName("サーバーストリーミングで商品一覧を取得できること")
|
|
1122
|
-
void listProductsStreaming() throws InterruptedException {
|
|
1123
|
-
CountDownLatch latch = new CountDownLatch(1);
|
|
1124
|
-
List<Product> products = new ArrayList<>();
|
|
1125
|
-
|
|
1126
|
-
asyncStub.listProducts(
|
|
1127
|
-
ListProductsRequest.newBuilder()
|
|
1128
|
-
.setPage(PageRequest.newBuilder().setPage(0).setSize(100).build())
|
|
1129
|
-
.build(),
|
|
1130
|
-
new StreamObserver<>() {
|
|
1131
|
-
@Override
|
|
1132
|
-
public void onNext(Product product) {
|
|
1133
|
-
products.add(product);
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
@Override
|
|
1137
|
-
public void onError(Throwable t) {
|
|
1138
|
-
latch.countDown();
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
@Override
|
|
1142
|
-
public void onCompleted() {
|
|
1143
|
-
latch.countDown();
|
|
1144
|
-
}
|
|
1145
|
-
}
|
|
1146
|
-
);
|
|
1147
|
-
|
|
1148
|
-
latch.await(10, TimeUnit.SECONDS);
|
|
1149
|
-
assertThat(products).isNotEmpty();
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
```
|
|
1153
|
-
|
|
1154
|
-
</details>
|
|
1155
|
-
|
|
1156
|
-
---
|
|
1157
|
-
|
|
1158
|
-
## 第16章:トランザクション API の実装
|
|
1159
|
-
|
|
1160
|
-
### 16.1 トランザクション API の設計方針
|
|
1161
|
-
|
|
1162
|
-
#### RPC パターンの使い分け
|
|
1163
|
-
|
|
1164
|
-
```plantuml
|
|
1165
|
-
@startuml transaction_rpc_patterns
|
|
1166
|
-
!define RECTANGLE class
|
|
1167
|
-
|
|
1168
|
-
skinparam backgroundColor #FEFEFE
|
|
1169
|
-
|
|
1170
|
-
package "Transaction API - RPC Patterns" {
|
|
1171
|
-
|
|
1172
|
-
RECTANGLE "単項 RPC" as unary {
|
|
1173
|
-
+ CreateOrder(受注登録)
|
|
1174
|
-
+ GetOrder(受注取得)
|
|
1175
|
-
+ ConfirmOrder(受注確定)
|
|
1176
|
-
+ CancelOrder(受注キャンセル)
|
|
1177
|
-
}
|
|
1178
|
-
|
|
1179
|
-
RECTANGLE "サーバーストリーミング" as server_stream {
|
|
1180
|
-
+ ListOrders(受注一覧)
|
|
1181
|
-
+ WatchOrderStatus(ステータス監視)
|
|
1182
|
-
+ StreamShipmentProgress(出荷進捗)
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
RECTANGLE "クライアントストリーミング" as client_stream {
|
|
1186
|
-
+ BulkAddOrderDetails(明細一括登録)
|
|
1187
|
-
+ BatchShipmentConfirm(出荷一括確定)
|
|
1188
|
-
}
|
|
1189
|
-
|
|
1190
|
-
RECTANGLE "双方向ストリーミング" as bidirectional {
|
|
1191
|
-
+ SyncInventory(在庫同期)
|
|
1192
|
-
+ ProcessInvoices(請求処理)
|
|
1193
|
-
}
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
note bottom of unary
|
|
1197
|
-
個別トランザクション
|
|
1198
|
-
即時応答が必要な処理
|
|
1199
|
-
end note
|
|
1200
|
-
|
|
1201
|
-
note bottom of server_stream
|
|
1202
|
-
リアルタイム通知
|
|
1203
|
-
進捗モニタリング
|
|
1204
|
-
end note
|
|
1205
|
-
|
|
1206
|
-
note bottom of client_stream
|
|
1207
|
-
大量データ送信
|
|
1208
|
-
バッチ処理
|
|
1209
|
-
end note
|
|
1210
|
-
|
|
1211
|
-
note bottom of bidirectional
|
|
1212
|
-
双方向同期
|
|
1213
|
-
対話的処理
|
|
1214
|
-
end note
|
|
1215
|
-
|
|
1216
|
-
@enduml
|
|
1217
|
-
```
|
|
1218
|
-
|
|
1219
|
-
---
|
|
1220
|
-
|
|
1221
|
-
### 16.2 受注 API の Protocol Buffers 定義
|
|
1222
|
-
|
|
1223
|
-
#### src/main/proto/order.proto
|
|
1224
|
-
|
|
1225
|
-
<details>
|
|
1226
|
-
<summary>コード例: order.proto</summary>
|
|
1227
|
-
|
|
1228
|
-
```protobuf
|
|
1229
|
-
syntax = "proto3";
|
|
1230
|
-
|
|
1231
|
-
package com.example.sales;
|
|
1232
|
-
|
|
1233
|
-
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
1234
|
-
option java_outer_classname = "OrderProto";
|
|
1235
|
-
option java_multiple_files = true;
|
|
1236
|
-
|
|
1237
|
-
import "common.proto";
|
|
1238
|
-
import "google/protobuf/timestamp.proto";
|
|
1239
|
-
|
|
1240
|
-
// 受注ステータス
|
|
1241
|
-
enum OrderStatus {
|
|
1242
|
-
ORDER_STATUS_UNSPECIFIED = 0;
|
|
1243
|
-
ORDER_STATUS_DRAFT = 1; // 下書き
|
|
1244
|
-
ORDER_STATUS_CONFIRMED = 2; // 確定
|
|
1245
|
-
ORDER_STATUS_PROCESSING = 3; // 処理中
|
|
1246
|
-
ORDER_STATUS_SHIPPED = 4; // 出荷済
|
|
1247
|
-
ORDER_STATUS_COMPLETED = 5; // 完了
|
|
1248
|
-
ORDER_STATUS_CANCELLED = 6; // キャンセル
|
|
1249
|
-
}
|
|
1250
|
-
|
|
1251
|
-
// 受注ヘッダ
|
|
1252
|
-
message Order {
|
|
1253
|
-
string order_number = 1;
|
|
1254
|
-
string partner_code = 2;
|
|
1255
|
-
string partner_name = 3;
|
|
1256
|
-
Date order_date = 4;
|
|
1257
|
-
Date delivery_date = 5;
|
|
1258
|
-
OrderStatus status = 6;
|
|
1259
|
-
Money total_amount = 7;
|
|
1260
|
-
Money tax_amount = 8;
|
|
1261
|
-
string warehouse_code = 9;
|
|
1262
|
-
string sales_rep_code = 10;
|
|
1263
|
-
string notes = 11;
|
|
1264
|
-
repeated OrderDetail details = 12;
|
|
1265
|
-
AuditInfo audit = 13;
|
|
1266
|
-
}
|
|
1267
|
-
|
|
1268
|
-
// 受注明細
|
|
1269
|
-
message OrderDetail {
|
|
1270
|
-
int32 line_number = 1;
|
|
1271
|
-
string product_code = 2;
|
|
1272
|
-
string product_name = 3;
|
|
1273
|
-
int32 quantity = 4;
|
|
1274
|
-
Money unit_price = 5;
|
|
1275
|
-
Money amount = 6;
|
|
1276
|
-
string tax_category = 7;
|
|
1277
|
-
Date delivery_date = 8;
|
|
1278
|
-
string notes = 9;
|
|
1279
|
-
}
|
|
1280
|
-
|
|
1281
|
-
// 受注ステータス監視(サーバーストリーミング)
|
|
1282
|
-
message WatchOrderStatusRequest {
|
|
1283
|
-
repeated string order_numbers = 1;
|
|
1284
|
-
repeated OrderStatus statuses = 2;
|
|
1285
|
-
}
|
|
1286
|
-
|
|
1287
|
-
message OrderStatusChange {
|
|
1288
|
-
string order_number = 1;
|
|
1289
|
-
OrderStatus previous_status = 2;
|
|
1290
|
-
OrderStatus current_status = 3;
|
|
1291
|
-
google.protobuf.Timestamp changed_at = 4;
|
|
1292
|
-
string changed_by = 5;
|
|
1293
|
-
}
|
|
1294
|
-
|
|
1295
|
-
// === サービス定義 ===
|
|
1296
|
-
|
|
1297
|
-
service OrderService {
|
|
1298
|
-
// 単項 RPC
|
|
1299
|
-
rpc GetOrder(GetOrderRequest) returns (GetOrderResponse);
|
|
1300
|
-
rpc CreateOrder(CreateOrderRequest) returns (CreateOrderResponse);
|
|
1301
|
-
rpc UpdateOrder(UpdateOrderRequest) returns (UpdateOrderResponse);
|
|
1302
|
-
rpc ConfirmOrder(ConfirmOrderRequest) returns (ConfirmOrderResponse);
|
|
1303
|
-
rpc CancelOrder(CancelOrderRequest) returns (CancelOrderResponse);
|
|
1304
|
-
|
|
1305
|
-
// サーバーストリーミング RPC
|
|
1306
|
-
rpc ListOrders(ListOrdersRequest) returns (stream Order);
|
|
1307
|
-
rpc WatchOrderStatus(WatchOrderStatusRequest) returns (stream OrderStatusChange);
|
|
1308
|
-
|
|
1309
|
-
// クライアントストリーミング RPC
|
|
1310
|
-
rpc BulkAddOrderDetails(stream BulkAddOrderDetailRequest) returns (BulkAddOrderDetailResponse);
|
|
1311
|
-
}
|
|
1312
|
-
```
|
|
1313
|
-
|
|
1314
|
-
</details>
|
|
1315
|
-
|
|
1316
|
-
---
|
|
1317
|
-
|
|
1318
|
-
### 16.3 受注サービス実装(ステータス監視)
|
|
1319
|
-
|
|
1320
|
-
<details>
|
|
1321
|
-
<summary>コード例: GrpcOrderService.java</summary>
|
|
1322
|
-
|
|
1323
|
-
```java
|
|
1324
|
-
@GrpcService
|
|
1325
|
-
public class GrpcOrderService extends OrderServiceGrpc.OrderServiceImplBase {
|
|
1326
|
-
|
|
1327
|
-
private static final Logger log = LoggerFactory.getLogger(GrpcOrderService.class);
|
|
1328
|
-
|
|
1329
|
-
private final OrderUseCase orderUseCase;
|
|
1330
|
-
private final CreditUseCase creditUseCase;
|
|
1331
|
-
private final OrderConverter converter;
|
|
1332
|
-
|
|
1333
|
-
// ステータス監視用のオブザーバー管理
|
|
1334
|
-
private final Map<String, Set<StreamObserver<OrderStatusChange>>> statusWatchers =
|
|
1335
|
-
new ConcurrentHashMap<>();
|
|
1336
|
-
|
|
1337
|
-
/**
|
|
1338
|
-
* サーバーストリーミング RPC: 受注ステータス監視
|
|
1339
|
-
*/
|
|
1340
|
-
@Override
|
|
1341
|
-
public void watchOrderStatus(WatchOrderStatusRequest request,
|
|
1342
|
-
StreamObserver<OrderStatusChange> responseObserver) {
|
|
1343
|
-
log.info("watchOrderStatus: orders={}", request.getOrderNumbersList());
|
|
1344
|
-
|
|
1345
|
-
// オブザーバー登録
|
|
1346
|
-
for (String orderNumber : request.getOrderNumbersList()) {
|
|
1347
|
-
statusWatchers
|
|
1348
|
-
.computeIfAbsent(orderNumber, k -> ConcurrentHashMap.newKeySet())
|
|
1349
|
-
.add(responseObserver);
|
|
1350
|
-
}
|
|
1351
|
-
|
|
1352
|
-
// コンテキストキャンセル時にオブザーバー削除
|
|
1353
|
-
Context.current().addListener(context -> {
|
|
1354
|
-
for (String orderNumber : request.getOrderNumbersList()) {
|
|
1355
|
-
Set<StreamObserver<OrderStatusChange>> watchers = statusWatchers.get(orderNumber);
|
|
1356
|
-
if (watchers != null) {
|
|
1357
|
-
watchers.remove(responseObserver);
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
}, Runnable::run);
|
|
1361
|
-
}
|
|
1362
|
-
|
|
1363
|
-
/**
|
|
1364
|
-
* Spring Events でステータス変更を受信し、ウォッチャーに通知
|
|
1365
|
-
*/
|
|
1366
|
-
@EventListener
|
|
1367
|
-
public void onOrderStatusChanged(OrderStatusChangedEvent event) {
|
|
1368
|
-
Set<StreamObserver<OrderStatusChange>> watchers =
|
|
1369
|
-
statusWatchers.get(event.getOrderNumber());
|
|
1370
|
-
|
|
1371
|
-
if (watchers == null || watchers.isEmpty()) {
|
|
1372
|
-
return;
|
|
1373
|
-
}
|
|
1374
|
-
|
|
1375
|
-
OrderStatusChange change = OrderStatusChange.newBuilder()
|
|
1376
|
-
.setOrderNumber(event.getOrderNumber())
|
|
1377
|
-
.setPreviousStatus(converter.toProtoStatus(event.getPreviousStatus()))
|
|
1378
|
-
.setCurrentStatus(converter.toProtoStatus(event.getCurrentStatus()))
|
|
1379
|
-
.setChangedAt(converter.toProtoTimestamp(event.getChangedAt()))
|
|
1380
|
-
.setChangedBy(event.getChangedBy())
|
|
1381
|
-
.build();
|
|
1382
|
-
|
|
1383
|
-
for (StreamObserver<OrderStatusChange> watcher : watchers) {
|
|
1384
|
-
try {
|
|
1385
|
-
watcher.onNext(change);
|
|
1386
|
-
} catch (Exception e) {
|
|
1387
|
-
log.warn("Failed to notify watcher", e);
|
|
1388
|
-
watchers.remove(watcher);
|
|
1389
|
-
}
|
|
1390
|
-
}
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
```
|
|
1394
|
-
|
|
1395
|
-
</details>
|
|
1396
|
-
|
|
1397
|
-
---
|
|
1398
|
-
|
|
1399
|
-
### 16.4 請求サービス実装(双方向ストリーミング)
|
|
1400
|
-
|
|
1401
|
-
<details>
|
|
1402
|
-
<summary>コード例: processInvoices メソッド</summary>
|
|
1403
|
-
|
|
1404
|
-
```java
|
|
1405
|
-
/**
|
|
1406
|
-
* 双方向ストリーミング RPC: 請求処理
|
|
1407
|
-
*/
|
|
1408
|
-
@Override
|
|
1409
|
-
public StreamObserver<ProcessInvoiceRequest> processInvoices(
|
|
1410
|
-
StreamObserver<ProcessInvoiceResponse> responseObserver) {
|
|
1411
|
-
|
|
1412
|
-
log.info("processInvoices: started");
|
|
1413
|
-
|
|
1414
|
-
return new StreamObserver<ProcessInvoiceRequest>() {
|
|
1415
|
-
@Override
|
|
1416
|
-
public void onNext(ProcessInvoiceRequest request) {
|
|
1417
|
-
ProcessInvoiceResponse response;
|
|
1418
|
-
|
|
1419
|
-
try {
|
|
1420
|
-
switch (request.getCommandCase()) {
|
|
1421
|
-
case CREATE:
|
|
1422
|
-
response = handleCreateInvoice(request.getCreate());
|
|
1423
|
-
break;
|
|
1424
|
-
case ISSUE:
|
|
1425
|
-
response = handleIssueInvoice(request.getIssue());
|
|
1426
|
-
break;
|
|
1427
|
-
case RECEIPT:
|
|
1428
|
-
response = handleRecordReceipt(request.getReceipt());
|
|
1429
|
-
break;
|
|
1430
|
-
default:
|
|
1431
|
-
response = ProcessInvoiceResponse.newBuilder()
|
|
1432
|
-
.setSuccess(false)
|
|
1433
|
-
.setMessage("不明なコマンド")
|
|
1434
|
-
.build();
|
|
1435
|
-
}
|
|
1436
|
-
} catch (Exception e) {
|
|
1437
|
-
log.error("processInvoices: error", e);
|
|
1438
|
-
response = ProcessInvoiceResponse.newBuilder()
|
|
1439
|
-
.setSuccess(false)
|
|
1440
|
-
.setMessage("エラー: " + e.getMessage())
|
|
1441
|
-
.build();
|
|
1442
|
-
}
|
|
1443
|
-
|
|
1444
|
-
// 即座に結果を返す
|
|
1445
|
-
responseObserver.onNext(response);
|
|
1446
|
-
}
|
|
1447
|
-
|
|
1448
|
-
@Override
|
|
1449
|
-
public void onError(Throwable t) {
|
|
1450
|
-
log.error("processInvoices: client error", t);
|
|
1451
|
-
}
|
|
1452
|
-
|
|
1453
|
-
@Override
|
|
1454
|
-
public void onCompleted() {
|
|
1455
|
-
log.info("processInvoices: client completed");
|
|
1456
|
-
responseObserver.onCompleted();
|
|
1457
|
-
}
|
|
1458
|
-
};
|
|
1459
|
-
}
|
|
1460
|
-
```
|
|
1461
|
-
|
|
1462
|
-
</details>
|
|
1463
|
-
|
|
1464
|
-
---
|
|
1465
|
-
|
|
1466
|
-
### 16.5 grpcurl による動作確認
|
|
1467
|
-
|
|
1468
|
-
<details>
|
|
1469
|
-
<summary>コード例: grpcurl コマンド</summary>
|
|
1470
|
-
|
|
1471
|
-
```bash
|
|
1472
|
-
# 受注登録
|
|
1473
|
-
grpcurl -plaintext -d '{
|
|
1474
|
-
"partner_code": "CUS001",
|
|
1475
|
-
"order_date": {"year": 2025, "month": 12, "day": 29},
|
|
1476
|
-
"delivery_date": {"year": 2026, "month": 1, "day": 5},
|
|
1477
|
-
"warehouse_code": "WH001",
|
|
1478
|
-
"details": [
|
|
1479
|
-
{"product_code": "PRD001", "quantity": 10},
|
|
1480
|
-
{"product_code": "PRD002", "quantity": 5}
|
|
1481
|
-
]
|
|
1482
|
-
}' localhost:9090 com.example.sales.OrderService/CreateOrder
|
|
1483
|
-
|
|
1484
|
-
# 受注確定
|
|
1485
|
-
grpcurl -plaintext -d '{
|
|
1486
|
-
"order_number": "ORD20251229001"
|
|
1487
|
-
}' localhost:9090 com.example.sales.OrderService/ConfirmOrder
|
|
1488
|
-
|
|
1489
|
-
# 受注ステータス監視(サーバーストリーミング)
|
|
1490
|
-
grpcurl -plaintext -d '{
|
|
1491
|
-
"order_numbers": ["ORD20251229001"]
|
|
1492
|
-
}' localhost:9090 com.example.sales.OrderService/WatchOrderStatus
|
|
1493
|
-
|
|
1494
|
-
# 出荷進捗監視(サーバーストリーミング)
|
|
1495
|
-
grpcurl -plaintext -d '{
|
|
1496
|
-
"shipment_number": "SHP20251230001"
|
|
1497
|
-
}' localhost:9090 com.example.sales.ShipmentService/StreamShipmentProgress
|
|
1498
|
-
```
|
|
1499
|
-
|
|
1500
|
-
</details>
|
|
1501
|
-
|
|
1502
|
-
---
|
|
1503
|
-
|
|
1504
|
-
## 第17章:エラーハンドリングとストリーミング
|
|
1505
|
-
|
|
1506
|
-
### 17.1 gRPC エラーハンドリングの概要
|
|
1507
|
-
|
|
1508
|
-
gRPC では、エラーは **Status** オブジェクトで表現されます。Status には **コード**(StatusCode)と **メッセージ**、オプションで **詳細情報** を含めることができます。
|
|
1509
|
-
|
|
1510
|
-
```plantuml
|
|
1511
|
-
@startuml grpc_error_architecture
|
|
1512
|
-
|
|
1513
|
-
title gRPC エラーハンドリングアーキテクチャ
|
|
1514
|
-
|
|
1515
|
-
package "クライアント" {
|
|
1516
|
-
[クライアント\nアプリケーション] as client
|
|
1517
|
-
[リトライ\nインターセプター] as retry
|
|
1518
|
-
[ヘルスチェック\nクライアント] as health_client
|
|
1519
|
-
}
|
|
1520
|
-
|
|
1521
|
-
package "gRPC チャネル" {
|
|
1522
|
-
[チャネル] as channel
|
|
1523
|
-
[回路ブレーカー] as circuit
|
|
1524
|
-
[デッドライン\n管理] as deadline
|
|
1525
|
-
}
|
|
1526
|
-
|
|
1527
|
-
package "サーバー" {
|
|
1528
|
-
[エラー\nインターセプター] as error_interceptor
|
|
1529
|
-
[gRPC サービス] as service
|
|
1530
|
-
[ヘルスサービス] as health_service
|
|
1531
|
-
}
|
|
1532
|
-
|
|
1533
|
-
client --> retry : "リクエスト"
|
|
1534
|
-
retry --> channel
|
|
1535
|
-
channel --> circuit
|
|
1536
|
-
circuit --> deadline
|
|
1537
|
-
deadline --> error_interceptor
|
|
1538
|
-
error_interceptor --> service
|
|
1539
|
-
|
|
1540
|
-
health_client --> health_service : "ヘルスチェック"
|
|
1541
|
-
|
|
1542
|
-
note right of error_interceptor
|
|
1543
|
-
例外をStatus変換
|
|
1544
|
-
詳細情報追加
|
|
1545
|
-
end note
|
|
1546
|
-
|
|
1547
|
-
note right of circuit
|
|
1548
|
-
連続エラー時に
|
|
1549
|
-
回路をオープン
|
|
1550
|
-
end note
|
|
1551
|
-
|
|
1552
|
-
@enduml
|
|
1553
|
-
```
|
|
1554
|
-
|
|
1555
|
-
---
|
|
1556
|
-
|
|
1557
|
-
### 17.2 gRPC ステータスコード
|
|
1558
|
-
|
|
1559
|
-
<details>
|
|
1560
|
-
<summary>コード例: GrpcStatusCode.java</summary>
|
|
1561
|
-
|
|
1562
|
-
```java
|
|
1563
|
-
/**
|
|
1564
|
-
* gRPC ステータスコードとビジネス例外のマッピング
|
|
1565
|
-
*/
|
|
1566
|
-
public enum GrpcStatusCode {
|
|
1567
|
-
|
|
1568
|
-
// 成功
|
|
1569
|
-
OK(Status.OK, "正常終了"),
|
|
1570
|
-
|
|
1571
|
-
// クライアントエラー
|
|
1572
|
-
INVALID_ARGUMENT(Status.INVALID_ARGUMENT, "不正な引数"),
|
|
1573
|
-
NOT_FOUND(Status.NOT_FOUND, "リソースが見つかりません"),
|
|
1574
|
-
ALREADY_EXISTS(Status.ALREADY_EXISTS, "リソースが既に存在します"),
|
|
1575
|
-
PERMISSION_DENIED(Status.PERMISSION_DENIED, "権限がありません"),
|
|
1576
|
-
FAILED_PRECONDITION(Status.FAILED_PRECONDITION, "前提条件が満たされていません"),
|
|
1577
|
-
|
|
1578
|
-
// サーバーエラー
|
|
1579
|
-
INTERNAL(Status.INTERNAL, "内部エラー"),
|
|
1580
|
-
UNAVAILABLE(Status.UNAVAILABLE, "サービス利用不可"),
|
|
1581
|
-
|
|
1582
|
-
// タイムアウト
|
|
1583
|
-
DEADLINE_EXCEEDED(Status.DEADLINE_EXCEEDED, "タイムアウト"),
|
|
1584
|
-
CANCELLED(Status.CANCELLED, "キャンセルされました"),
|
|
1585
|
-
|
|
1586
|
-
// 認証
|
|
1587
|
-
UNAUTHENTICATED(Status.UNAUTHENTICATED, "認証が必要です");
|
|
1588
|
-
|
|
1589
|
-
/**
|
|
1590
|
-
* リトライ可能なステータスかどうか
|
|
1591
|
-
*/
|
|
1592
|
-
public boolean isRetryable() {
|
|
1593
|
-
return this == UNAVAILABLE ||
|
|
1594
|
-
this == DEADLINE_EXCEEDED ||
|
|
1595
|
-
this == INTERNAL;
|
|
1596
|
-
}
|
|
1597
|
-
}
|
|
1598
|
-
```
|
|
1599
|
-
|
|
1600
|
-
</details>
|
|
1601
|
-
|
|
1602
|
-
---
|
|
1603
|
-
|
|
1604
|
-
### 17.3 カスタム例外クラス
|
|
1605
|
-
|
|
1606
|
-
<details>
|
|
1607
|
-
<summary>コード例: GrpcException.java</summary>
|
|
1608
|
-
|
|
1609
|
-
```java
|
|
1610
|
-
/**
|
|
1611
|
-
* gRPC 用カスタム例外
|
|
1612
|
-
*/
|
|
1613
|
-
public class GrpcException extends RuntimeException {
|
|
1614
|
-
|
|
1615
|
-
private final GrpcStatusCode statusCode;
|
|
1616
|
-
private final Map<String, String> details;
|
|
1617
|
-
private final boolean retryable;
|
|
1618
|
-
private final long retryDelayMillis;
|
|
1619
|
-
|
|
1620
|
-
// 便利なファクトリメソッド
|
|
1621
|
-
public static GrpcException notFound(String resource, String identifier) {
|
|
1622
|
-
return builder(GrpcStatusCode.NOT_FOUND)
|
|
1623
|
-
.message(resource + " が見つかりません: " + identifier)
|
|
1624
|
-
.detail("resource", resource)
|
|
1625
|
-
.detail("identifier", identifier)
|
|
1626
|
-
.build();
|
|
1627
|
-
}
|
|
1628
|
-
|
|
1629
|
-
public static GrpcException invalidArgument(String field, String reason) {
|
|
1630
|
-
return builder(GrpcStatusCode.INVALID_ARGUMENT)
|
|
1631
|
-
.message("不正な引数: " + field + " - " + reason)
|
|
1632
|
-
.detail("field", field)
|
|
1633
|
-
.detail("reason", reason)
|
|
1634
|
-
.build();
|
|
1635
|
-
}
|
|
1636
|
-
|
|
1637
|
-
public static GrpcException failedPrecondition(String condition) {
|
|
1638
|
-
return builder(GrpcStatusCode.FAILED_PRECONDITION)
|
|
1639
|
-
.message("前提条件が満たされていません: " + condition)
|
|
1640
|
-
.detail("condition", condition)
|
|
1641
|
-
.build();
|
|
1642
|
-
}
|
|
1643
|
-
|
|
1644
|
-
public static GrpcException unavailable(String service, long retryDelayMillis) {
|
|
1645
|
-
return builder(GrpcStatusCode.UNAVAILABLE)
|
|
1646
|
-
.message("サービス利用不可: " + service)
|
|
1647
|
-
.retryable(true)
|
|
1648
|
-
.retryDelayMillis(retryDelayMillis)
|
|
1649
|
-
.build();
|
|
1650
|
-
}
|
|
1651
|
-
|
|
1652
|
-
public Status toStatus() {
|
|
1653
|
-
return statusCode.getStatus().withDescription(getMessage());
|
|
1654
|
-
}
|
|
1655
|
-
}
|
|
1656
|
-
```
|
|
1657
|
-
|
|
1658
|
-
</details>
|
|
1659
|
-
|
|
1660
|
-
---
|
|
1661
|
-
|
|
1662
|
-
### 17.4 例外変換インターセプター
|
|
1663
|
-
|
|
1664
|
-
<details>
|
|
1665
|
-
<summary>コード例: ExceptionTranslationInterceptor.java</summary>
|
|
1666
|
-
|
|
1667
|
-
```java
|
|
1668
|
-
/**
|
|
1669
|
-
* 例外を gRPC Status に変換するインターセプター
|
|
1670
|
-
*/
|
|
1671
|
-
@GrpcGlobalServerInterceptor
|
|
1672
|
-
public class ExceptionTranslationInterceptor implements ServerInterceptor {
|
|
1673
|
-
|
|
1674
|
-
private StatusRuntimeException translateException(Exception e) {
|
|
1675
|
-
// GrpcException の場合
|
|
1676
|
-
if (e instanceof GrpcException grpcEx) {
|
|
1677
|
-
return createStatusException(grpcEx);
|
|
1678
|
-
}
|
|
1679
|
-
|
|
1680
|
-
// ドメイン例外の変換
|
|
1681
|
-
if (e instanceof EntityNotFoundException notFound) {
|
|
1682
|
-
return createNotFoundStatus(notFound);
|
|
1683
|
-
}
|
|
1684
|
-
|
|
1685
|
-
if (e instanceof ValidationException validation) {
|
|
1686
|
-
return createValidationStatus(validation);
|
|
1687
|
-
}
|
|
1688
|
-
|
|
1689
|
-
if (e instanceof BusinessException business) {
|
|
1690
|
-
return createBusinessStatus(business);
|
|
1691
|
-
}
|
|
1692
|
-
|
|
1693
|
-
// その他は内部エラー
|
|
1694
|
-
return createInternalStatus(e);
|
|
1695
|
-
}
|
|
1696
|
-
|
|
1697
|
-
private StatusRuntimeException createValidationStatus(ValidationException e) {
|
|
1698
|
-
BadRequest.Builder badRequest = BadRequest.newBuilder();
|
|
1699
|
-
|
|
1700
|
-
e.getViolations().forEach(violation ->
|
|
1701
|
-
badRequest.addFieldViolations(
|
|
1702
|
-
BadRequest.FieldViolation.newBuilder()
|
|
1703
|
-
.setField(violation.field())
|
|
1704
|
-
.setDescription(violation.message())
|
|
1705
|
-
.build()
|
|
1706
|
-
)
|
|
1707
|
-
);
|
|
1708
|
-
|
|
1709
|
-
com.google.rpc.Status status = com.google.rpc.Status.newBuilder()
|
|
1710
|
-
.setCode(Status.INVALID_ARGUMENT.getCode().value())
|
|
1711
|
-
.setMessage("バリデーションエラー")
|
|
1712
|
-
.addDetails(Any.pack(badRequest.build()))
|
|
1713
|
-
.build();
|
|
1714
|
-
|
|
1715
|
-
return StatusProto.toStatusRuntimeException(status);
|
|
1716
|
-
}
|
|
1717
|
-
}
|
|
1718
|
-
```
|
|
1719
|
-
|
|
1720
|
-
</details>
|
|
1721
|
-
|
|
1722
|
-
---
|
|
1723
|
-
|
|
1724
|
-
### 17.5 リトライ設定
|
|
1725
|
-
|
|
1726
|
-
<details>
|
|
1727
|
-
<summary>コード例: GrpcRetryConfig.java</summary>
|
|
1728
|
-
|
|
1729
|
-
```java
|
|
1730
|
-
/**
|
|
1731
|
-
* リトライポリシー設定
|
|
1732
|
-
*/
|
|
1733
|
-
@Configuration
|
|
1734
|
-
public class GrpcRetryConfig {
|
|
1735
|
-
|
|
1736
|
-
@Bean
|
|
1737
|
-
public RetryPolicy retryPolicy() {
|
|
1738
|
-
return RetryPolicy.builder()
|
|
1739
|
-
.maxAttempts(3)
|
|
1740
|
-
.initialBackoff(Duration.ofMillis(500))
|
|
1741
|
-
.maxBackoff(Duration.ofSeconds(10))
|
|
1742
|
-
.backoffMultiplier(2.0)
|
|
1743
|
-
.retryableStatusCodes(Set.of(
|
|
1744
|
-
Status.Code.UNAVAILABLE,
|
|
1745
|
-
Status.Code.DEADLINE_EXCEEDED,
|
|
1746
|
-
Status.Code.INTERNAL
|
|
1747
|
-
))
|
|
1748
|
-
.build();
|
|
1749
|
-
}
|
|
1750
|
-
}
|
|
1751
|
-
```
|
|
1752
|
-
|
|
1753
|
-
</details>
|
|
1754
|
-
|
|
1755
|
-
---
|
|
1756
|
-
|
|
1757
|
-
### 17.6 回路ブレーカー
|
|
1758
|
-
|
|
1759
|
-
<details>
|
|
1760
|
-
<summary>コード例: GrpcCircuitBreaker.java</summary>
|
|
1761
|
-
|
|
1762
|
-
```java
|
|
1763
|
-
/**
|
|
1764
|
-
* gRPC 回路ブレーカー
|
|
1765
|
-
*/
|
|
1766
|
-
public class GrpcCircuitBreaker {
|
|
1767
|
-
|
|
1768
|
-
public enum State {
|
|
1769
|
-
CLOSED, // 正常(リクエスト許可)
|
|
1770
|
-
OPEN, // 遮断(リクエスト拒否)
|
|
1771
|
-
HALF_OPEN // 試行中
|
|
1772
|
-
}
|
|
1773
|
-
|
|
1774
|
-
private final int failureThreshold;
|
|
1775
|
-
private final int successThreshold;
|
|
1776
|
-
private final Duration openStateTimeout;
|
|
1777
|
-
|
|
1778
|
-
private volatile State state = State.CLOSED;
|
|
1779
|
-
private final AtomicInteger failureCount = new AtomicInteger(0);
|
|
1780
|
-
private final AtomicInteger successCount = new AtomicInteger(0);
|
|
1781
|
-
private volatile Instant lastFailureTime;
|
|
1782
|
-
|
|
1783
|
-
public boolean allowRequest() {
|
|
1784
|
-
return switch (state) {
|
|
1785
|
-
case CLOSED -> true;
|
|
1786
|
-
case OPEN -> shouldAttemptReset();
|
|
1787
|
-
case HALF_OPEN -> true;
|
|
1788
|
-
};
|
|
1789
|
-
}
|
|
1790
|
-
|
|
1791
|
-
private boolean shouldAttemptReset() {
|
|
1792
|
-
if (lastFailureTime == null) {
|
|
1793
|
-
return false;
|
|
1794
|
-
}
|
|
1795
|
-
if (Duration.between(lastFailureTime, Instant.now())
|
|
1796
|
-
.compareTo(openStateTimeout) > 0) {
|
|
1797
|
-
state = State.HALF_OPEN;
|
|
1798
|
-
return true;
|
|
1799
|
-
}
|
|
1800
|
-
return false;
|
|
1801
|
-
}
|
|
1802
|
-
|
|
1803
|
-
public void recordSuccess() {
|
|
1804
|
-
if (state == State.HALF_OPEN) {
|
|
1805
|
-
if (successCount.incrementAndGet() >= successThreshold) {
|
|
1806
|
-
reset();
|
|
1807
|
-
}
|
|
1808
|
-
} else {
|
|
1809
|
-
failureCount.set(0);
|
|
1810
|
-
}
|
|
1811
|
-
}
|
|
1812
|
-
|
|
1813
|
-
public void recordFailure() {
|
|
1814
|
-
lastFailureTime = Instant.now();
|
|
1815
|
-
if (state == State.HALF_OPEN) {
|
|
1816
|
-
trip();
|
|
1817
|
-
} else if (failureCount.incrementAndGet() >= failureThreshold) {
|
|
1818
|
-
trip();
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
|
|
1822
|
-
private void trip() {
|
|
1823
|
-
state = State.OPEN;
|
|
1824
|
-
failureCount.set(0);
|
|
1825
|
-
successCount.set(0);
|
|
1826
|
-
}
|
|
1827
|
-
|
|
1828
|
-
private void reset() {
|
|
1829
|
-
state = State.CLOSED;
|
|
1830
|
-
failureCount.set(0);
|
|
1831
|
-
successCount.set(0);
|
|
1832
|
-
}
|
|
1833
|
-
}
|
|
1834
|
-
```
|
|
1835
|
-
|
|
1836
|
-
</details>
|
|
1837
|
-
|
|
1838
|
-
---
|
|
1839
|
-
|
|
1840
|
-
### 17.7 ヘルスチェックサービス
|
|
1841
|
-
|
|
1842
|
-
<details>
|
|
1843
|
-
<summary>コード例: HealthCheckService.java</summary>
|
|
1844
|
-
|
|
1845
|
-
```java
|
|
1846
|
-
/**
|
|
1847
|
-
* gRPC ヘルスチェックサービス
|
|
1848
|
-
*/
|
|
1849
|
-
@GrpcService
|
|
1850
|
-
public class HealthCheckService extends HealthGrpc.HealthImplBase {
|
|
1851
|
-
|
|
1852
|
-
private final Map<String, HealthCheckResponse.ServingStatus> serviceStatuses =
|
|
1853
|
-
new ConcurrentHashMap<>();
|
|
1854
|
-
|
|
1855
|
-
public HealthCheckService() {
|
|
1856
|
-
// デフォルトで全サービスを SERVING に設定
|
|
1857
|
-
setServiceStatus("", HealthCheckResponse.ServingStatus.SERVING);
|
|
1858
|
-
setServiceStatus("com.example.sales.ProductService",
|
|
1859
|
-
HealthCheckResponse.ServingStatus.SERVING);
|
|
1860
|
-
setServiceStatus("com.example.sales.OrderService",
|
|
1861
|
-
HealthCheckResponse.ServingStatus.SERVING);
|
|
1862
|
-
}
|
|
1863
|
-
|
|
1864
|
-
@Override
|
|
1865
|
-
public void check(HealthCheckRequest request,
|
|
1866
|
-
StreamObserver<HealthCheckResponse> responseObserver) {
|
|
1867
|
-
String service = request.getService();
|
|
1868
|
-
HealthCheckResponse.ServingStatus status = serviceStatuses.getOrDefault(
|
|
1869
|
-
service,
|
|
1870
|
-
HealthCheckResponse.ServingStatus.SERVICE_UNKNOWN
|
|
1871
|
-
);
|
|
1872
|
-
|
|
1873
|
-
HealthCheckResponse response = HealthCheckResponse.newBuilder()
|
|
1874
|
-
.setStatus(status)
|
|
1875
|
-
.build();
|
|
1876
|
-
|
|
1877
|
-
responseObserver.onNext(response);
|
|
1878
|
-
responseObserver.onCompleted();
|
|
1879
|
-
}
|
|
1880
|
-
|
|
1881
|
-
@Override
|
|
1882
|
-
public void watch(HealthCheckRequest request,
|
|
1883
|
-
StreamObserver<HealthCheckResponse> responseObserver) {
|
|
1884
|
-
// 継続的なヘルスチェック監視
|
|
1885
|
-
String service = request.getService();
|
|
1886
|
-
|
|
1887
|
-
// 初期状態を送信
|
|
1888
|
-
HealthCheckResponse.ServingStatus status = serviceStatuses.getOrDefault(
|
|
1889
|
-
service,
|
|
1890
|
-
HealthCheckResponse.ServingStatus.SERVICE_UNKNOWN
|
|
1891
|
-
);
|
|
1892
|
-
|
|
1893
|
-
responseObserver.onNext(HealthCheckResponse.newBuilder()
|
|
1894
|
-
.setStatus(status)
|
|
1895
|
-
.build());
|
|
1896
|
-
|
|
1897
|
-
// 状態変更時に通知(実装省略)
|
|
1898
|
-
}
|
|
1899
|
-
}
|
|
1900
|
-
```
|
|
1901
|
-
|
|
1902
|
-
</details>
|
|
1903
|
-
|
|
1904
|
-
---
|
|
1905
|
-
|
|
1906
|
-
### 17.8 フロー制御付きストリームオブザーバー
|
|
1907
|
-
|
|
1908
|
-
<details>
|
|
1909
|
-
<summary>コード例: FlowControlledStreamObserver.java</summary>
|
|
1910
|
-
|
|
1911
|
-
```java
|
|
1912
|
-
/**
|
|
1913
|
-
* フロー制御付きストリームオブザーバー
|
|
1914
|
-
*/
|
|
1915
|
-
public class FlowControlledStreamObserver<T> implements StreamObserver<T> {
|
|
1916
|
-
|
|
1917
|
-
private final CallStreamObserver<T> delegate;
|
|
1918
|
-
private final Queue<T> buffer;
|
|
1919
|
-
private final int maxBufferSize;
|
|
1920
|
-
private final AtomicBoolean completed;
|
|
1921
|
-
|
|
1922
|
-
public FlowControlledStreamObserver(CallStreamObserver<T> delegate, int maxBufferSize) {
|
|
1923
|
-
this.delegate = delegate;
|
|
1924
|
-
this.buffer = new ConcurrentLinkedQueue<>();
|
|
1925
|
-
this.maxBufferSize = maxBufferSize;
|
|
1926
|
-
this.completed = new AtomicBoolean(false);
|
|
1927
|
-
|
|
1928
|
-
// バックプレッシャー設定
|
|
1929
|
-
delegate.setOnReadyHandler(this::drainBuffer);
|
|
1930
|
-
}
|
|
1931
|
-
|
|
1932
|
-
@Override
|
|
1933
|
-
public void onNext(T value) {
|
|
1934
|
-
if (completed.get()) {
|
|
1935
|
-
return;
|
|
1936
|
-
}
|
|
1937
|
-
|
|
1938
|
-
if (delegate.isReady()) {
|
|
1939
|
-
delegate.onNext(value);
|
|
1940
|
-
} else {
|
|
1941
|
-
if (buffer.size() >= maxBufferSize) {
|
|
1942
|
-
return; // バッファ満杯時は破棄
|
|
1943
|
-
}
|
|
1944
|
-
buffer.offer(value);
|
|
1945
|
-
}
|
|
1946
|
-
}
|
|
1947
|
-
|
|
1948
|
-
private void drainBuffer() {
|
|
1949
|
-
while (delegate.isReady() && !buffer.isEmpty()) {
|
|
1950
|
-
T value = buffer.poll();
|
|
1951
|
-
if (value != null) {
|
|
1952
|
-
delegate.onNext(value);
|
|
1953
|
-
}
|
|
1954
|
-
}
|
|
1955
|
-
|
|
1956
|
-
if (completed.get() && buffer.isEmpty()) {
|
|
1957
|
-
delegate.onCompleted();
|
|
1958
|
-
}
|
|
1959
|
-
}
|
|
1960
|
-
}
|
|
1961
|
-
```
|
|
1962
|
-
|
|
1963
|
-
</details>
|
|
1964
|
-
|
|
1965
|
-
---
|
|
1966
|
-
|
|
1967
|
-
## Part 10-D のまとめ
|
|
1968
|
-
|
|
1969
|
-
### 実装した機能一覧
|
|
1970
|
-
|
|
1971
|
-
| 章 | 内容 | 主要技術 |
|
|
1972
|
-
|---|---|---|
|
|
1973
|
-
| **第14章: 基礎** | gRPC サーバーの基礎 | Protocol Buffers, HTTP/2, grpc-spring-boot-starter |
|
|
1974
|
-
| **第15章: マスタ API** | 商品・取引先・倉庫マスタ | 単項 RPC, サーバーストリーミング |
|
|
1975
|
-
| **第16章: トランザクション API** | 受注・出荷・請求処理 | クライアントストリーミング, 双方向ストリーミング |
|
|
1976
|
-
| **第17章: エラーハンドリング** | リトライ, 回路ブレーカー, ヘルスチェック | gRPC Status, インターセプター |
|
|
1977
|
-
|
|
1978
|
-
### 実装した gRPC サービス
|
|
1979
|
-
|
|
1980
|
-
- **ProductService**: 商品マスタの CRUD、一覧ストリーミング、一括登録
|
|
1981
|
-
- **PartnerService**: 取引先マスタの CRUD、一覧ストリーミング、与信チェック
|
|
1982
|
-
- **WarehouseService**: 倉庫マスタの CRUD、在庫ストリーミング
|
|
1983
|
-
- **OrderService**: 受注の CRUD、確定、キャンセル、ステータス監視、明細一括登録
|
|
1984
|
-
- **ShipmentService**: 出荷指示、ピッキング、出荷確定、進捗監視、一括確定
|
|
1985
|
-
- **InvoiceService**: 締処理、発行、入金記録、双方向ストリーミング処理
|
|
1986
|
-
|
|
1987
|
-
### アーキテクチャの特徴
|
|
1988
|
-
|
|
1989
|
-
```
|
|
1990
|
-
┌─────────────────────────────────────┐
|
|
1991
|
-
│ プレゼンテーション層 │
|
|
1992
|
-
│ ┌─────────┐ ┌─────────┐ ┌──────┐ │
|
|
1993
|
-
│ │ REST API │ │ GraphQL │ │ gRPC │ │
|
|
1994
|
-
│ └─────────┘ └─────────┘ └──────┘ │
|
|
1995
|
-
└─────────────────────────────────────┘
|
|
1996
|
-
│
|
|
1997
|
-
┌─────────────────────────────────────┐
|
|
1998
|
-
│ アプリケーション層 │
|
|
1999
|
-
│ ┌──────────────────────────────┐ │
|
|
2000
|
-
│ │ ユースケース / サービス │ │
|
|
2001
|
-
│ └──────────────────────────────┘ │
|
|
2002
|
-
└─────────────────────────────────────┘
|
|
2003
|
-
│
|
|
2004
|
-
┌─────────────────────────────────────┐
|
|
2005
|
-
│ ドメイン層 │
|
|
2006
|
-
│ ┌────────┐ ┌────────┐ ┌──────┐ │
|
|
2007
|
-
│ │ エンティティ │ │ 値オブジェクト │ │ リポジトリ │ │
|
|
2008
|
-
│ └────────┘ └────────┘ └──────┘ │
|
|
2009
|
-
└─────────────────────────────────────┘
|
|
2010
|
-
```
|
|
2011
|
-
|
|
2012
|
-
gRPC は REST API、GraphQL と並ぶプレゼンテーション層のエンドポイントとして機能し、高性能なバイナリ通信と双方向ストリーミングにより、リアルタイム性が求められるシステム間連携に適しています。
|
|
2013
|
-
|
|
2014
|
-
### 技術スタック
|
|
2015
|
-
|
|
2016
|
-
| カテゴリ | 技術 |
|
|
2017
|
-
|---------|------|
|
|
2018
|
-
| **言語** | Java 21 |
|
|
2019
|
-
| **フレームワーク** | Spring Boot 4.0, grpc-spring-boot-starter 3.1 |
|
|
2020
|
-
| **プロトコル** | gRPC 1.62, Protocol Buffers 3.25 |
|
|
2021
|
-
| **ORM** | MyBatis 3.0 |
|
|
2022
|
-
| **データベース** | PostgreSQL 16 |
|
|
2023
|
-
| **テスト** | JUnit 5, grpc-testing, TestContainers |
|
|
2024
|
-
|
|
2025
|
-
### API 形式の比較と選択基準
|
|
2026
|
-
|
|
2027
|
-
| 観点 | REST API | gRPC | GraphQL |
|
|
2028
|
-
|------|----------|------|---------|
|
|
2029
|
-
| **用途** | 汎用 Web API | マイクロサービス間 | フロントエンド向け |
|
|
2030
|
-
| **パフォーマンス** | 中 | 高 | 中 |
|
|
2031
|
-
| **ストリーミング** | WebSocket 別実装 | ネイティブ | Subscription |
|
|
2032
|
-
| **スキーマ** | OpenAPI (任意) | .proto (必須) | .graphqls (必須) |
|
|
2033
|
-
| **ブラウザ対応** | ◎ | △ (gRPC-Web) | ◎ |
|
|
2034
|
-
| **学習コスト** | 低 | 中 | 中 |
|
|
2035
|
-
|
|
2036
|
-
### gRPC を選択する場面
|
|
2037
|
-
|
|
2038
|
-
1. **マイクロサービス間通信**: 高性能な内部通信が必要
|
|
2039
|
-
2. **リアルタイム処理**: ストリーミングによる進捗通知が必要
|
|
2040
|
-
3. **多言語環境**: 異なる言語で実装されたサービス間の連携
|
|
2041
|
-
4. **低レイテンシ**: バイナリプロトコルによる高速通信が必要
|
|
2042
|
-
5. **双方向通信**: 対話的な処理フローが必要
|
|
2043
|
-
|
|
2044
|
-
gRPC サービスは、既存のヘキサゴナルアーキテクチャを活かしながら、Input Adapter として追加することで、REST API と並行運用が可能です。用途に応じて適切な API 形式を選択し、システム全体の効率と柔軟性を高めることができます。
|
|
1
|
+
# 実践データベース設計:販売管理システム 研究 3 - gRPC サービスの実装
|
|
2
|
+
|
|
3
|
+
## はじめに
|
|
4
|
+
|
|
5
|
+
本研究では、API サーバー構成(第13章)やモノリスサービス(研究 1)とは異なるアプローチとして、**gRPC** による販売管理システムを実装します。Protocol Buffers による高効率なバイナリシリアライゼーションと、HTTP/2 によるストリーミング通信を活用し、高性能なマイクロサービス間通信を実現します。
|
|
6
|
+
|
|
7
|
+
研究 1 で構築したヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として gRPC サービス層のみを追加**します。
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 第14章:gRPC サーバーの基礎
|
|
12
|
+
|
|
13
|
+
### 14.1 gRPC とは
|
|
14
|
+
|
|
15
|
+
gRPC は Google が開発したオープンソースの高性能 RPC(Remote Procedure Call)フレームワークです。HTTP/2 プロトコルと Protocol Buffers を基盤とし、マイクロサービス間通信に最適化されています。
|
|
16
|
+
|
|
17
|
+
```plantuml
|
|
18
|
+
@startuml grpc_architecture
|
|
19
|
+
!define RECTANGLE class
|
|
20
|
+
|
|
21
|
+
skinparam backgroundColor #FEFEFE
|
|
22
|
+
|
|
23
|
+
package "gRPC Architecture (販売管理システム)" {
|
|
24
|
+
|
|
25
|
+
package "Client Side" {
|
|
26
|
+
RECTANGLE "gRPC Client\n(Generated Stub)" as client {
|
|
27
|
+
- 単項 RPC
|
|
28
|
+
- サーバーストリーミング
|
|
29
|
+
- クライアントストリーミング
|
|
30
|
+
- 双方向ストリーミング
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
package "Server Side" {
|
|
35
|
+
RECTANGLE "gRPC Server\n(grpc-spring-boot-starter)" as server {
|
|
36
|
+
- ProductService
|
|
37
|
+
- PartnerService
|
|
38
|
+
- OrderService
|
|
39
|
+
- ShipmentService
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
package "Shared" {
|
|
44
|
+
RECTANGLE "Protocol Buffers\n(.proto files)" as proto {
|
|
45
|
+
- product.proto
|
|
46
|
+
- partner.proto
|
|
47
|
+
- order.proto
|
|
48
|
+
- common.proto
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
client --> proto : ".proto からスタブ生成"
|
|
54
|
+
server --> proto : ".proto からスケルトン生成"
|
|
55
|
+
client <--> server : "HTTP/2\n(Binary Protocol)"
|
|
56
|
+
|
|
57
|
+
note bottom of proto
|
|
58
|
+
スキーマ駆動開発
|
|
59
|
+
言語中立なインターフェース定義
|
|
60
|
+
高効率なバイナリシリアライゼーション
|
|
61
|
+
end note
|
|
62
|
+
|
|
63
|
+
@enduml
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
**gRPC の主な特徴:**
|
|
67
|
+
|
|
68
|
+
| 特徴 | 説明 |
|
|
69
|
+
|------|------|
|
|
70
|
+
| HTTP/2 | 多重化、ヘッダー圧縮、バイナリフレーミング |
|
|
71
|
+
| Protocol Buffers | 高速なバイナリシリアライゼーション |
|
|
72
|
+
| 多言語サポート | Java, Go, Python, C#, Node.js 等 |
|
|
73
|
+
| ストリーミング | 4つの RPC パターンをサポート |
|
|
74
|
+
| デッドライン | タイムアウト管理の組み込みサポート |
|
|
75
|
+
| 認証 | TLS/SSL、トークンベース認証のサポート |
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
### 14.2 REST API / GraphQL との比較
|
|
80
|
+
|
|
81
|
+
```plantuml
|
|
82
|
+
@startuml api_comparison
|
|
83
|
+
skinparam backgroundColor #FEFEFE
|
|
84
|
+
|
|
85
|
+
rectangle "REST API" as rest {
|
|
86
|
+
(HTTP/1.1 or 2)
|
|
87
|
+
(JSON/XML)
|
|
88
|
+
(OpenAPI)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
rectangle "gRPC" as grpc {
|
|
92
|
+
(HTTP/2)
|
|
93
|
+
(Protocol Buffers)
|
|
94
|
+
(.proto)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
rectangle "GraphQL" as graphql {
|
|
98
|
+
(HTTP/1.1 or 2)
|
|
99
|
+
(JSON)
|
|
100
|
+
(.graphqls)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
note bottom of rest
|
|
104
|
+
汎用性が高い
|
|
105
|
+
ブラウザ直接アクセス可能
|
|
106
|
+
人間が読みやすい
|
|
107
|
+
end note
|
|
108
|
+
|
|
109
|
+
note bottom of grpc
|
|
110
|
+
高性能
|
|
111
|
+
マイクロサービス向け
|
|
112
|
+
型安全
|
|
113
|
+
end note
|
|
114
|
+
|
|
115
|
+
note bottom of graphql
|
|
116
|
+
柔軟なクエリ
|
|
117
|
+
フロントエンド向け
|
|
118
|
+
単一エンドポイント
|
|
119
|
+
end note
|
|
120
|
+
|
|
121
|
+
@enduml
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**詳細比較表:**
|
|
125
|
+
|
|
126
|
+
| 特徴 | REST API | gRPC | GraphQL |
|
|
127
|
+
|------|----------|------|---------|
|
|
128
|
+
| プロトコル | HTTP/1.1 | HTTP/2 | HTTP/1.1 or HTTP/2 |
|
|
129
|
+
| データ形式 | JSON | Protocol Buffers | JSON |
|
|
130
|
+
| スキーマ | OpenAPI (任意) | .proto (必須) | .graphqls (必須) |
|
|
131
|
+
| シリアライゼーション | テキスト | バイナリ | テキスト |
|
|
132
|
+
| パフォーマンス | 中 | 高 | 中 |
|
|
133
|
+
| ストリーミング | WebSocket 別実装 | ネイティブサポート | Subscription |
|
|
134
|
+
| ブラウザ対応 | ◎ | △ (gRPC-Web) | ◎ |
|
|
135
|
+
| 学習コスト | 低 | 中 | 中 |
|
|
136
|
+
| 主な用途 | 汎用 API | マイクロサービス | フロントエンド向け |
|
|
137
|
+
|
|
138
|
+
**gRPC を選択する場面:**
|
|
139
|
+
|
|
140
|
+
1. **マイクロサービス間通信**: 高性能な内部通信が必要な場合
|
|
141
|
+
2. **リアルタイム処理**: ストリーミングによる進捗通知が必要な場合
|
|
142
|
+
3. **多言語環境**: 異なる言語で実装されたサービス間の連携
|
|
143
|
+
4. **低レイテンシ**: バイナリプロトコルによる高速通信が必要な場合
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
147
|
+
### 14.3 4つの RPC パターン
|
|
148
|
+
|
|
149
|
+
gRPC は 4 つの RPC パターンをサポートします。
|
|
150
|
+
|
|
151
|
+
```plantuml
|
|
152
|
+
@startuml grpc_patterns
|
|
153
|
+
skinparam backgroundColor #FEFEFE
|
|
154
|
+
|
|
155
|
+
rectangle "1. 単項 RPC (Unary)" {
|
|
156
|
+
card "Client" as c1
|
|
157
|
+
card "Server" as s1
|
|
158
|
+
c1 -right-> s1 : "Request"
|
|
159
|
+
s1 -left-> c1 : "Response"
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
rectangle "2. サーバーストリーミング RPC" {
|
|
163
|
+
card "Client" as c2
|
|
164
|
+
card "Server" as s2
|
|
165
|
+
c2 -right-> s2 : "Request"
|
|
166
|
+
s2 -left-> c2 : "Response 1,2,...N"
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
rectangle "3. クライアントストリーミング RPC" {
|
|
170
|
+
card "Client" as c3
|
|
171
|
+
card "Server" as s3
|
|
172
|
+
c3 -right-> s3 : "Request 1,2,...N"
|
|
173
|
+
s3 -left-> c3 : "Response"
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
rectangle "4. 双方向ストリーミング RPC" {
|
|
177
|
+
card "Client" as c4
|
|
178
|
+
card "Server" as s4
|
|
179
|
+
c4 <-right-> s4 : "Request/Response Stream"
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
@enduml
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
**各パターンの用途:**
|
|
186
|
+
|
|
187
|
+
| パターン | 用途例(販売管理システム) |
|
|
188
|
+
|----------|---------------------------|
|
|
189
|
+
| 単項 RPC | 商品情報取得、受注登録 |
|
|
190
|
+
| サーバーストリーミング | 商品一覧取得、出荷進捗通知 |
|
|
191
|
+
| クライアントストリーミング | 受注明細の一括登録 |
|
|
192
|
+
| 双方向ストリーミング | リアルタイム在庫同期 |
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
### 14.4 gRPC におけるヘキサゴナルアーキテクチャ
|
|
197
|
+
|
|
198
|
+
gRPC を導入しても、既存のヘキサゴナルアーキテクチャ(ドメイン層・アプリケーション層)はそのまま共有し、**Input Adapter として gRPC サービス層のみを追加**します。
|
|
199
|
+
|
|
200
|
+
```plantuml
|
|
201
|
+
@startuml hexagonal_grpc
|
|
202
|
+
!define RECTANGLE class
|
|
203
|
+
|
|
204
|
+
package "Hexagonal Architecture (gRPC版)" {
|
|
205
|
+
|
|
206
|
+
RECTANGLE "Application Core\n(Domain + Use Cases)" as core {
|
|
207
|
+
- Product (商品)
|
|
208
|
+
- Partner (取引先)
|
|
209
|
+
- Order (受注)
|
|
210
|
+
- Shipment (出荷)
|
|
211
|
+
- Invoice (請求)
|
|
212
|
+
- ProductUseCase
|
|
213
|
+
- OrderUseCase
|
|
214
|
+
- ShipmentUseCase
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
RECTANGLE "Input Adapters\n(Driving Side)" as input {
|
|
218
|
+
- REST Controller(既存)
|
|
219
|
+
- gRPC Service(新規追加)
|
|
220
|
+
- StreamObserver
|
|
221
|
+
- ServerInterceptor
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
RECTANGLE "Output Adapters\n(Driven Side)" as output {
|
|
225
|
+
- MyBatis Repository
|
|
226
|
+
- Database Access
|
|
227
|
+
- Entity Mapping
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
input --> core : "Input Ports\n(Use Cases)"
|
|
232
|
+
core --> output : "Output Ports\n(Repository Interfaces)"
|
|
233
|
+
|
|
234
|
+
note top of core
|
|
235
|
+
既存のビジネスロジック
|
|
236
|
+
REST API 版と完全に共有
|
|
237
|
+
gRPC 固有のコードは含まない
|
|
238
|
+
end note
|
|
239
|
+
|
|
240
|
+
note left of input
|
|
241
|
+
gRPC サービスを
|
|
242
|
+
Input Adapter として追加
|
|
243
|
+
既存の REST と共存可能
|
|
244
|
+
end note
|
|
245
|
+
|
|
246
|
+
@enduml
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
**gRPC でもヘキサゴナルアーキテクチャを維持する理由:**
|
|
250
|
+
|
|
251
|
+
1. **再利用性**: 既存の UseCase/Repository をそのまま活用
|
|
252
|
+
2. **並行運用**: REST API と gRPC を同時提供可能
|
|
253
|
+
3. **テスト容易性**: ドメインロジックは通信プロトコルに依存しない
|
|
254
|
+
4. **移行容易性**: 段階的に API 形式を追加・変更可能
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
### 14.5 ディレクトリ構成
|
|
259
|
+
|
|
260
|
+
既存の構成に `infrastructure/grpc/` を追加するだけです。
|
|
261
|
+
|
|
262
|
+
```
|
|
263
|
+
src/main/java/com/example/sales/
|
|
264
|
+
├── domain/ # ドメイン層(API版と共通)
|
|
265
|
+
│ ├── model/
|
|
266
|
+
│ │ ├── product/ # 商品ドメイン
|
|
267
|
+
│ │ ├── partner/ # 取引先ドメイン
|
|
268
|
+
│ │ ├── order/ # 受注ドメイン
|
|
269
|
+
│ │ ├── shipment/ # 出荷ドメイン
|
|
270
|
+
│ │ └── invoice/ # 請求ドメイン
|
|
271
|
+
│ └── exception/
|
|
272
|
+
│
|
|
273
|
+
├── application/ # アプリケーション層(API版と共通)
|
|
274
|
+
│ ├── port/
|
|
275
|
+
│ │ ├── in/ # Input Port(ユースケース)
|
|
276
|
+
│ │ └── out/ # Output Port(リポジトリ)
|
|
277
|
+
│ └── service/
|
|
278
|
+
│
|
|
279
|
+
├── infrastructure/
|
|
280
|
+
│ ├── persistence/ # Output Adapter(DB実装)- 既存
|
|
281
|
+
│ │ ├── mapper/
|
|
282
|
+
│ │ └── repository/
|
|
283
|
+
│ ├── rest/ # Input Adapter(REST実装)- 既存
|
|
284
|
+
│ └── grpc/ # Input Adapter(gRPC実装)- 新規追加
|
|
285
|
+
│ ├── service/ # gRPC サービス実装
|
|
286
|
+
│ ├── interceptor/ # インターセプター
|
|
287
|
+
│ └── converter/ # ドメイン ↔ Proto 変換
|
|
288
|
+
│
|
|
289
|
+
├── config/
|
|
290
|
+
│
|
|
291
|
+
└── src/main/proto/ # Protocol Buffers 定義
|
|
292
|
+
├── common.proto
|
|
293
|
+
├── product.proto
|
|
294
|
+
├── partner.proto
|
|
295
|
+
├── order.proto
|
|
296
|
+
├── shipment.proto
|
|
297
|
+
└── invoice.proto
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
### 14.6 技術スタックの追加
|
|
303
|
+
|
|
304
|
+
既存の `build.gradle.kts` に gRPC 関連の依存関係を追加します。
|
|
305
|
+
|
|
306
|
+
#### build.gradle.kts(差分)
|
|
307
|
+
|
|
308
|
+
<details>
|
|
309
|
+
<summary>コード例: build.gradle.kts</summary>
|
|
310
|
+
|
|
311
|
+
```kotlin
|
|
312
|
+
import com.google.protobuf.gradle.*
|
|
313
|
+
|
|
314
|
+
plugins {
|
|
315
|
+
// 既存のプラグイン
|
|
316
|
+
id("java")
|
|
317
|
+
id("org.springframework.boot") version "4.0.0"
|
|
318
|
+
id("io.spring.dependency-management") version "1.1.0"
|
|
319
|
+
|
|
320
|
+
// gRPC 関連を追加
|
|
321
|
+
id("com.google.protobuf") version "0.9.4"
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
dependencies {
|
|
325
|
+
// 既存の依存関係(Spring Boot, MyBatis, PostgreSQL等)はそのまま
|
|
326
|
+
|
|
327
|
+
// gRPC 関連を追加
|
|
328
|
+
implementation("net.devh:grpc-spring-boot-starter:3.1.0.RELEASE")
|
|
329
|
+
implementation("io.grpc:grpc-protobuf:1.62.2")
|
|
330
|
+
implementation("io.grpc:grpc-stub:1.62.2")
|
|
331
|
+
implementation("io.grpc:grpc-services:1.62.2") // ヘルスチェック、リフレクション
|
|
332
|
+
|
|
333
|
+
// Protocol Buffers
|
|
334
|
+
implementation("com.google.protobuf:protobuf-java:3.25.3")
|
|
335
|
+
implementation("com.google.protobuf:protobuf-java-util:3.25.3") // JSON 変換
|
|
336
|
+
|
|
337
|
+
// Jakarta Annotation (gRPC generated code で必要)
|
|
338
|
+
compileOnly("jakarta.annotation:jakarta.annotation-api:2.1.1")
|
|
339
|
+
|
|
340
|
+
// Test
|
|
341
|
+
testImplementation("io.grpc:grpc-testing:1.62.2")
|
|
342
|
+
testImplementation("net.devh:grpc-client-spring-boot-starter:3.1.0.RELEASE")
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
protobuf {
|
|
346
|
+
protoc {
|
|
347
|
+
artifact = "com.google.protobuf:protoc:3.25.3"
|
|
348
|
+
}
|
|
349
|
+
plugins {
|
|
350
|
+
create("grpc") {
|
|
351
|
+
artifact = "io.grpc:protoc-gen-grpc-java:1.62.2"
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
generateProtoTasks {
|
|
355
|
+
all().forEach { task ->
|
|
356
|
+
task.plugins {
|
|
357
|
+
create("grpc")
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
// 生成コードのソースパス追加
|
|
364
|
+
sourceSets {
|
|
365
|
+
main {
|
|
366
|
+
java {
|
|
367
|
+
srcDirs(
|
|
368
|
+
"build/generated/source/proto/main/java",
|
|
369
|
+
"build/generated/source/proto/main/grpc"
|
|
370
|
+
)
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
</details>
|
|
377
|
+
|
|
378
|
+
**追加パッケージの説明:**
|
|
379
|
+
|
|
380
|
+
| パッケージ | 用途 |
|
|
381
|
+
|-----------|------|
|
|
382
|
+
| grpc-spring-boot-starter | Spring Boot gRPC 統合 |
|
|
383
|
+
| grpc-protobuf | gRPC + Protocol Buffers 統合 |
|
|
384
|
+
| grpc-stub | gRPC クライアントスタブ |
|
|
385
|
+
| grpc-services | ヘルスチェック、リフレクションサービス |
|
|
386
|
+
| protobuf-java | Protocol Buffers Java ランタイム |
|
|
387
|
+
| protobuf-java-util | JSON ⇔ Protobuf 変換ユーティリティ |
|
|
388
|
+
| grpc-testing | gRPC テストサポート |
|
|
389
|
+
|
|
390
|
+
#### application.yml(差分)
|
|
391
|
+
|
|
392
|
+
<details>
|
|
393
|
+
<summary>コード例: application.yml</summary>
|
|
394
|
+
|
|
395
|
+
```yaml
|
|
396
|
+
# 既存の設定はそのまま
|
|
397
|
+
|
|
398
|
+
# gRPC サーバー設定
|
|
399
|
+
grpc:
|
|
400
|
+
server:
|
|
401
|
+
port: 9090
|
|
402
|
+
reflection-service-enabled: true # gRPC リフレクション有効化
|
|
403
|
+
health-service-enabled: true # ヘルスチェック有効化
|
|
404
|
+
max-inbound-message-size: 4194304 # 4MB
|
|
405
|
+
max-inbound-metadata-size: 8192 # 8KB
|
|
406
|
+
|
|
407
|
+
# ログ設定
|
|
408
|
+
logging:
|
|
409
|
+
level:
|
|
410
|
+
net.devh.boot.grpc: DEBUG
|
|
411
|
+
io.grpc: INFO
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
</details>
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
### 14.7 Protocol Buffers 定義
|
|
419
|
+
|
|
420
|
+
#### src/main/proto/common.proto
|
|
421
|
+
|
|
422
|
+
<details>
|
|
423
|
+
<summary>コード例: common.proto</summary>
|
|
424
|
+
|
|
425
|
+
```protobuf
|
|
426
|
+
syntax = "proto3";
|
|
427
|
+
|
|
428
|
+
package com.example.sales;
|
|
429
|
+
|
|
430
|
+
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
431
|
+
option java_outer_classname = "CommonProto";
|
|
432
|
+
option java_multiple_files = true;
|
|
433
|
+
|
|
434
|
+
import "google/protobuf/timestamp.proto";
|
|
435
|
+
import "google/protobuf/wrappers.proto";
|
|
436
|
+
|
|
437
|
+
// 共通のページネーション
|
|
438
|
+
message PageRequest {
|
|
439
|
+
int32 page = 1;
|
|
440
|
+
int32 size = 2;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
message PageInfo {
|
|
444
|
+
int32 page = 1;
|
|
445
|
+
int32 size = 2;
|
|
446
|
+
int32 total_elements = 3;
|
|
447
|
+
int32 total_pages = 4;
|
|
448
|
+
bool has_next = 5;
|
|
449
|
+
bool has_previous = 6;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// 共通のエラー応答
|
|
453
|
+
message ErrorDetail {
|
|
454
|
+
string field = 1;
|
|
455
|
+
string message = 2;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
message ErrorResponse {
|
|
459
|
+
string code = 1;
|
|
460
|
+
string message = 2;
|
|
461
|
+
repeated ErrorDetail details = 3;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// 金額型(Decimal 相当)
|
|
465
|
+
message Money {
|
|
466
|
+
int64 units = 1; // 整数部
|
|
467
|
+
int32 nanos = 2; // 小数部(10億分の1単位)
|
|
468
|
+
string currency = 3; // 通貨コード(JPY等)
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
// 日付型(Date 相当)
|
|
472
|
+
message Date {
|
|
473
|
+
int32 year = 1;
|
|
474
|
+
int32 month = 2;
|
|
475
|
+
int32 day = 3;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
// 監査情報
|
|
479
|
+
message AuditInfo {
|
|
480
|
+
google.protobuf.Timestamp created_at = 1;
|
|
481
|
+
string created_by = 2;
|
|
482
|
+
google.protobuf.Timestamp updated_at = 3;
|
|
483
|
+
string updated_by = 4;
|
|
484
|
+
}
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
</details>
|
|
488
|
+
|
|
489
|
+
#### src/main/proto/product.proto
|
|
490
|
+
|
|
491
|
+
<details>
|
|
492
|
+
<summary>コード例: product.proto</summary>
|
|
493
|
+
|
|
494
|
+
```protobuf
|
|
495
|
+
syntax = "proto3";
|
|
496
|
+
|
|
497
|
+
package com.example.sales;
|
|
498
|
+
|
|
499
|
+
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
500
|
+
option java_outer_classname = "ProductProto";
|
|
501
|
+
option java_multiple_files = true;
|
|
502
|
+
|
|
503
|
+
import "common.proto";
|
|
504
|
+
import "google/protobuf/empty.proto";
|
|
505
|
+
|
|
506
|
+
// 商品区分
|
|
507
|
+
enum ProductCategory {
|
|
508
|
+
PRODUCT_CATEGORY_UNSPECIFIED = 0;
|
|
509
|
+
PRODUCT_CATEGORY_GOODS = 1; // 商品
|
|
510
|
+
PRODUCT_CATEGORY_SERVICE = 2; // サービス
|
|
511
|
+
PRODUCT_CATEGORY_SUPPLY = 3; // 消耗品
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// 税区分
|
|
515
|
+
enum TaxCategory {
|
|
516
|
+
TAX_CATEGORY_UNSPECIFIED = 0;
|
|
517
|
+
TAX_CATEGORY_TAXABLE = 1; // 課税
|
|
518
|
+
TAX_CATEGORY_EXEMPT = 2; // 非課税
|
|
519
|
+
TAX_CATEGORY_REDUCED = 3; // 軽減税率
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// 商品メッセージ
|
|
523
|
+
message Product {
|
|
524
|
+
string product_code = 1; // 商品コード
|
|
525
|
+
string product_name = 2; // 商品名
|
|
526
|
+
ProductCategory category = 3; // 商品区分
|
|
527
|
+
TaxCategory tax_category = 4; // 税区分
|
|
528
|
+
Money standard_price = 5; // 標準単価
|
|
529
|
+
string product_group_code = 6; // 商品分類コード
|
|
530
|
+
string unit_code = 7; // 単位コード
|
|
531
|
+
bool is_active = 8; // 有効フラグ
|
|
532
|
+
AuditInfo audit = 9; // 監査情報
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// === リクエスト/レスポンス ===
|
|
536
|
+
|
|
537
|
+
// 商品取得
|
|
538
|
+
message GetProductRequest {
|
|
539
|
+
string product_code = 1;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
message GetProductResponse {
|
|
543
|
+
Product product = 1;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// 商品一覧取得(サーバーストリーミング)
|
|
547
|
+
message ListProductsRequest {
|
|
548
|
+
PageRequest page = 1;
|
|
549
|
+
ProductCategory category = 2; // フィルタ(オプション)
|
|
550
|
+
string product_group_code = 3; // フィルタ(オプション)
|
|
551
|
+
string keyword = 4; // 検索キーワード
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
// 商品登録
|
|
555
|
+
message CreateProductRequest {
|
|
556
|
+
string product_code = 1;
|
|
557
|
+
string product_name = 2;
|
|
558
|
+
ProductCategory category = 3;
|
|
559
|
+
TaxCategory tax_category = 4;
|
|
560
|
+
Money standard_price = 5;
|
|
561
|
+
string product_group_code = 6;
|
|
562
|
+
string unit_code = 7;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
message CreateProductResponse {
|
|
566
|
+
Product product = 1;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// 商品一括登録(クライアントストリーミング)
|
|
570
|
+
message BulkCreateProductRequest {
|
|
571
|
+
CreateProductRequest product = 1;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
message BulkCreateProductResponse {
|
|
575
|
+
int32 success_count = 1;
|
|
576
|
+
int32 failure_count = 2;
|
|
577
|
+
repeated ErrorDetail errors = 3;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// === サービス定義 ===
|
|
581
|
+
|
|
582
|
+
service ProductService {
|
|
583
|
+
// 単項 RPC
|
|
584
|
+
rpc GetProduct(GetProductRequest) returns (GetProductResponse);
|
|
585
|
+
rpc CreateProduct(CreateProductRequest) returns (CreateProductResponse);
|
|
586
|
+
rpc UpdateProduct(UpdateProductRequest) returns (UpdateProductResponse);
|
|
587
|
+
rpc DeleteProduct(DeleteProductRequest) returns (DeleteProductResponse);
|
|
588
|
+
|
|
589
|
+
// サーバーストリーミング RPC(大量データ取得)
|
|
590
|
+
rpc ListProducts(ListProductsRequest) returns (stream Product);
|
|
591
|
+
|
|
592
|
+
// クライアントストリーミング RPC(一括登録)
|
|
593
|
+
rpc BulkCreateProducts(stream BulkCreateProductRequest) returns (BulkCreateProductResponse);
|
|
594
|
+
}
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
</details>
|
|
598
|
+
|
|
599
|
+
---
|
|
600
|
+
|
|
601
|
+
### 14.8 gRPC サービス実装
|
|
602
|
+
|
|
603
|
+
#### GrpcProductService.java
|
|
604
|
+
|
|
605
|
+
<details>
|
|
606
|
+
<summary>コード例: GrpcProductService.java</summary>
|
|
607
|
+
|
|
608
|
+
```java
|
|
609
|
+
package com.example.sales.infrastructure.grpc.service;
|
|
610
|
+
|
|
611
|
+
import com.example.sales.application.port.in.ProductUseCase;
|
|
612
|
+
import com.example.sales.domain.model.product.Product;
|
|
613
|
+
import com.example.sales.domain.model.product.ProductCode;
|
|
614
|
+
import com.example.sales.infrastructure.grpc.converter.ProductConverter;
|
|
615
|
+
import com.example.sales.infrastructure.grpc.proto.*;
|
|
616
|
+
import io.grpc.stub.StreamObserver;
|
|
617
|
+
import net.devh.boot.grpc.server.service.GrpcService;
|
|
618
|
+
import org.slf4j.Logger;
|
|
619
|
+
import org.slf4j.LoggerFactory;
|
|
620
|
+
|
|
621
|
+
import java.util.List;
|
|
622
|
+
import java.util.concurrent.atomic.AtomicInteger;
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* 商品 gRPC サービス実装
|
|
626
|
+
*/
|
|
627
|
+
@GrpcService
|
|
628
|
+
public class GrpcProductService extends ProductServiceGrpc.ProductServiceImplBase {
|
|
629
|
+
|
|
630
|
+
private static final Logger log = LoggerFactory.getLogger(GrpcProductService.class);
|
|
631
|
+
|
|
632
|
+
private final ProductUseCase productUseCase;
|
|
633
|
+
private final ProductConverter converter;
|
|
634
|
+
|
|
635
|
+
public GrpcProductService(ProductUseCase productUseCase, ProductConverter converter) {
|
|
636
|
+
this.productUseCase = productUseCase;
|
|
637
|
+
this.converter = converter;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* 単項 RPC: 商品取得
|
|
642
|
+
*/
|
|
643
|
+
@Override
|
|
644
|
+
public void getProduct(GetProductRequest request,
|
|
645
|
+
StreamObserver<GetProductResponse> responseObserver) {
|
|
646
|
+
log.info("getProduct: {}", request.getProductCode());
|
|
647
|
+
|
|
648
|
+
try {
|
|
649
|
+
ProductCode code = new ProductCode(request.getProductCode());
|
|
650
|
+
Product product = productUseCase.findByCode(code)
|
|
651
|
+
.orElseThrow(() -> new ProductNotFoundException(code));
|
|
652
|
+
|
|
653
|
+
GetProductResponse response = GetProductResponse.newBuilder()
|
|
654
|
+
.setProduct(converter.toProto(product))
|
|
655
|
+
.build();
|
|
656
|
+
|
|
657
|
+
responseObserver.onNext(response);
|
|
658
|
+
responseObserver.onCompleted();
|
|
659
|
+
|
|
660
|
+
} catch (ProductNotFoundException e) {
|
|
661
|
+
log.warn("Product not found: {}", request.getProductCode());
|
|
662
|
+
responseObserver.onError(
|
|
663
|
+
io.grpc.Status.NOT_FOUND
|
|
664
|
+
.withDescription("商品が見つかりません: " + request.getProductCode())
|
|
665
|
+
.asRuntimeException()
|
|
666
|
+
);
|
|
667
|
+
} catch (Exception e) {
|
|
668
|
+
log.error("Error getting product", e);
|
|
669
|
+
responseObserver.onError(
|
|
670
|
+
io.grpc.Status.INTERNAL
|
|
671
|
+
.withDescription("内部エラーが発生しました")
|
|
672
|
+
.withCause(e)
|
|
673
|
+
.asRuntimeException()
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* サーバーストリーミング RPC: 商品一覧取得
|
|
680
|
+
*/
|
|
681
|
+
@Override
|
|
682
|
+
public void listProducts(ListProductsRequest request,
|
|
683
|
+
StreamObserver<Product> responseObserver) {
|
|
684
|
+
log.info("listProducts: category={}, keyword={}",
|
|
685
|
+
request.getCategory(), request.getKeyword());
|
|
686
|
+
|
|
687
|
+
try {
|
|
688
|
+
List<Product> products = productUseCase.findAll(
|
|
689
|
+
converter.toCriteria(request)
|
|
690
|
+
);
|
|
691
|
+
|
|
692
|
+
// ストリーミングで商品を1件ずつ送信
|
|
693
|
+
for (Product product : products) {
|
|
694
|
+
responseObserver.onNext(converter.toProto(product));
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
responseObserver.onCompleted();
|
|
698
|
+
log.info("listProducts completed: {} items", products.size());
|
|
699
|
+
|
|
700
|
+
} catch (Exception e) {
|
|
701
|
+
log.error("Error listing products", e);
|
|
702
|
+
responseObserver.onError(
|
|
703
|
+
io.grpc.Status.INTERNAL
|
|
704
|
+
.withDescription("内部エラーが発生しました")
|
|
705
|
+
.asRuntimeException()
|
|
706
|
+
);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* クライアントストリーミング RPC: 商品一括登録
|
|
712
|
+
*/
|
|
713
|
+
@Override
|
|
714
|
+
public StreamObserver<BulkCreateProductRequest> bulkCreateProducts(
|
|
715
|
+
StreamObserver<BulkCreateProductResponse> responseObserver) {
|
|
716
|
+
|
|
717
|
+
log.info("bulkCreateProducts: started");
|
|
718
|
+
|
|
719
|
+
AtomicInteger successCount = new AtomicInteger(0);
|
|
720
|
+
AtomicInteger failureCount = new AtomicInteger(0);
|
|
721
|
+
List<ErrorDetail> errors = new java.util.ArrayList<>();
|
|
722
|
+
|
|
723
|
+
return new StreamObserver<BulkCreateProductRequest>() {
|
|
724
|
+
@Override
|
|
725
|
+
public void onNext(BulkCreateProductRequest request) {
|
|
726
|
+
try {
|
|
727
|
+
CreateProductRequest productRequest = request.getProduct();
|
|
728
|
+
Product product = converter.toDomain(productRequest);
|
|
729
|
+
productUseCase.create(product);
|
|
730
|
+
successCount.incrementAndGet();
|
|
731
|
+
log.debug("Created product: {}", productRequest.getProductCode());
|
|
732
|
+
|
|
733
|
+
} catch (Exception e) {
|
|
734
|
+
failureCount.incrementAndGet();
|
|
735
|
+
errors.add(ErrorDetail.newBuilder()
|
|
736
|
+
.setField(request.getProduct().getProductCode())
|
|
737
|
+
.setMessage(e.getMessage())
|
|
738
|
+
.build());
|
|
739
|
+
log.warn("Failed to create product: {}",
|
|
740
|
+
request.getProduct().getProductCode(), e);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
@Override
|
|
745
|
+
public void onError(Throwable t) {
|
|
746
|
+
log.error("bulkCreateProducts error", t);
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
@Override
|
|
750
|
+
public void onCompleted() {
|
|
751
|
+
BulkCreateProductResponse response = BulkCreateProductResponse.newBuilder()
|
|
752
|
+
.setSuccessCount(successCount.get())
|
|
753
|
+
.setFailureCount(failureCount.get())
|
|
754
|
+
.addAllErrors(errors)
|
|
755
|
+
.build();
|
|
756
|
+
|
|
757
|
+
responseObserver.onNext(response);
|
|
758
|
+
responseObserver.onCompleted();
|
|
759
|
+
|
|
760
|
+
log.info("bulkCreateProducts completed: success={}, failure={}",
|
|
761
|
+
successCount.get(), failureCount.get());
|
|
762
|
+
}
|
|
763
|
+
};
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
```
|
|
767
|
+
|
|
768
|
+
</details>
|
|
769
|
+
|
|
770
|
+
---
|
|
771
|
+
|
|
772
|
+
### 14.9 ProductConverter(ドメイン ⇔ Proto 変換)
|
|
773
|
+
|
|
774
|
+
<details>
|
|
775
|
+
<summary>コード例: ProductConverter.java</summary>
|
|
776
|
+
|
|
777
|
+
```java
|
|
778
|
+
package com.example.sales.infrastructure.grpc.converter;
|
|
779
|
+
|
|
780
|
+
import com.example.sales.domain.model.product.*;
|
|
781
|
+
import com.example.sales.infrastructure.grpc.proto.*;
|
|
782
|
+
import org.springframework.stereotype.Component;
|
|
783
|
+
|
|
784
|
+
import java.math.BigDecimal;
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* 商品ドメインモデル ⇔ Protocol Buffers 変換
|
|
788
|
+
*/
|
|
789
|
+
@Component
|
|
790
|
+
public class ProductConverter {
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* ドメインモデル → Proto
|
|
794
|
+
*/
|
|
795
|
+
public com.example.sales.infrastructure.grpc.proto.Product toProto(Product domain) {
|
|
796
|
+
return com.example.sales.infrastructure.grpc.proto.Product.newBuilder()
|
|
797
|
+
.setProductCode(domain.getProductCode().getValue())
|
|
798
|
+
.setProductName(domain.getProductName())
|
|
799
|
+
.setCategory(toProtoCategory(domain.getCategory()))
|
|
800
|
+
.setTaxCategory(toProtoTaxCategory(domain.getTaxCategory()))
|
|
801
|
+
.setStandardPrice(toProtoMoney(domain.getStandardPrice()))
|
|
802
|
+
.setProductGroupCode(nullToEmpty(domain.getProductGroupCode()))
|
|
803
|
+
.setUnitCode(nullToEmpty(domain.getUnitCode()))
|
|
804
|
+
.setIsActive(domain.isActive())
|
|
805
|
+
.build();
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
/**
|
|
809
|
+
* CreateProductRequest → ドメインモデル
|
|
810
|
+
*/
|
|
811
|
+
public Product toDomain(CreateProductRequest request) {
|
|
812
|
+
return Product.builder()
|
|
813
|
+
.productCode(new ProductCode(request.getProductCode()))
|
|
814
|
+
.productName(request.getProductName())
|
|
815
|
+
.category(toDomainCategory(request.getCategory()))
|
|
816
|
+
.taxCategory(toDomainTaxCategory(request.getTaxCategory()))
|
|
817
|
+
.standardPrice(fromProtoMoney(request.getStandardPrice()))
|
|
818
|
+
.productGroupCode(emptyToNull(request.getProductGroupCode()))
|
|
819
|
+
.unitCode(emptyToNull(request.getUnitCode()))
|
|
820
|
+
.isActive(true)
|
|
821
|
+
.build();
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
// === 区分変換 ===
|
|
825
|
+
|
|
826
|
+
private ProductCategory toProtoCategory(
|
|
827
|
+
com.example.sales.domain.model.product.ProductCategory domain) {
|
|
828
|
+
return switch (domain) {
|
|
829
|
+
case GOODS -> ProductCategory.PRODUCT_CATEGORY_GOODS;
|
|
830
|
+
case SERVICE -> ProductCategory.PRODUCT_CATEGORY_SERVICE;
|
|
831
|
+
case SUPPLY -> ProductCategory.PRODUCT_CATEGORY_SUPPLY;
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
// === Money 変換 ===
|
|
836
|
+
|
|
837
|
+
private Money toProtoMoney(BigDecimal amount) {
|
|
838
|
+
if (amount == null) {
|
|
839
|
+
return Money.getDefaultInstance();
|
|
840
|
+
}
|
|
841
|
+
long units = amount.longValue();
|
|
842
|
+
int nanos = amount.remainder(BigDecimal.ONE)
|
|
843
|
+
.multiply(BigDecimal.valueOf(1_000_000_000))
|
|
844
|
+
.intValue();
|
|
845
|
+
|
|
846
|
+
return Money.newBuilder()
|
|
847
|
+
.setUnits(units)
|
|
848
|
+
.setNanos(nanos)
|
|
849
|
+
.setCurrency("JPY")
|
|
850
|
+
.build();
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
private BigDecimal fromProtoMoney(Money money) {
|
|
854
|
+
if (money == null || money.equals(Money.getDefaultInstance())) {
|
|
855
|
+
return BigDecimal.ZERO;
|
|
856
|
+
}
|
|
857
|
+
return BigDecimal.valueOf(money.getUnits())
|
|
858
|
+
.add(BigDecimal.valueOf(money.getNanos(), 9));
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// === ユーティリティ ===
|
|
862
|
+
|
|
863
|
+
private String nullToEmpty(String value) {
|
|
864
|
+
return value == null ? "" : value;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
private String emptyToNull(String value) {
|
|
868
|
+
return value == null || value.isEmpty() ? null : value;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
```
|
|
872
|
+
|
|
873
|
+
</details>
|
|
874
|
+
|
|
875
|
+
---
|
|
876
|
+
|
|
877
|
+
### 14.10 gRPC インターセプター
|
|
878
|
+
|
|
879
|
+
#### LoggingInterceptor.java
|
|
880
|
+
|
|
881
|
+
<details>
|
|
882
|
+
<summary>コード例: LoggingInterceptor.java</summary>
|
|
883
|
+
|
|
884
|
+
```java
|
|
885
|
+
package com.example.sales.infrastructure.grpc.interceptor;
|
|
886
|
+
|
|
887
|
+
import io.grpc.*;
|
|
888
|
+
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
|
|
889
|
+
import org.slf4j.Logger;
|
|
890
|
+
import org.slf4j.LoggerFactory;
|
|
891
|
+
import org.slf4j.MDC;
|
|
892
|
+
|
|
893
|
+
import java.util.UUID;
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* gRPC ログインターセプター
|
|
897
|
+
*/
|
|
898
|
+
@GrpcGlobalServerInterceptor
|
|
899
|
+
public class LoggingInterceptor implements ServerInterceptor {
|
|
900
|
+
|
|
901
|
+
private static final Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);
|
|
902
|
+
|
|
903
|
+
@Override
|
|
904
|
+
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
|
|
905
|
+
ServerCall<ReqT, RespT> call,
|
|
906
|
+
Metadata headers,
|
|
907
|
+
ServerCallHandler<ReqT, RespT> next) {
|
|
908
|
+
|
|
909
|
+
String requestId = UUID.randomUUID().toString().substring(0, 8);
|
|
910
|
+
String methodName = call.getMethodDescriptor().getFullMethodName();
|
|
911
|
+
long startTime = System.currentTimeMillis();
|
|
912
|
+
|
|
913
|
+
MDC.put("requestId", requestId);
|
|
914
|
+
log.info("gRPC Request started: method={}", methodName);
|
|
915
|
+
|
|
916
|
+
return new ForwardingServerCallListener.SimpleForwardingServerCallListener<>(
|
|
917
|
+
next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<>(call) {
|
|
918
|
+
@Override
|
|
919
|
+
public void close(Status status, Metadata trailers) {
|
|
920
|
+
long duration = System.currentTimeMillis() - startTime;
|
|
921
|
+
log.info("gRPC Request completed: method={}, status={}, duration={}ms",
|
|
922
|
+
methodName, status.getCode(), duration);
|
|
923
|
+
MDC.remove("requestId");
|
|
924
|
+
super.close(status, trailers);
|
|
925
|
+
}
|
|
926
|
+
}, headers)) {};
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
```
|
|
930
|
+
|
|
931
|
+
</details>
|
|
932
|
+
|
|
933
|
+
---
|
|
934
|
+
|
|
935
|
+
### 14.11 grpcurl による動作確認
|
|
936
|
+
|
|
937
|
+
<details>
|
|
938
|
+
<summary>コード例: grpcurl コマンド</summary>
|
|
939
|
+
|
|
940
|
+
```bash
|
|
941
|
+
# サービス一覧取得
|
|
942
|
+
grpcurl -plaintext localhost:9090 list
|
|
943
|
+
|
|
944
|
+
# メソッド一覧取得
|
|
945
|
+
grpcurl -plaintext localhost:9090 list com.example.sales.ProductService
|
|
946
|
+
|
|
947
|
+
# 商品取得(単項 RPC)
|
|
948
|
+
grpcurl -plaintext -d '{"product_code": "PRD001"}' \
|
|
949
|
+
localhost:9090 com.example.sales.ProductService/GetProduct
|
|
950
|
+
|
|
951
|
+
# 商品一覧取得(サーバーストリーミング)
|
|
952
|
+
grpcurl -plaintext -d '{"page": {"page": 0, "size": 10}}' \
|
|
953
|
+
localhost:9090 com.example.sales.ProductService/ListProducts
|
|
954
|
+
|
|
955
|
+
# 商品登録
|
|
956
|
+
grpcurl -plaintext -d '{
|
|
957
|
+
"product_code": "PRD100",
|
|
958
|
+
"product_name": "新商品",
|
|
959
|
+
"category": "PRODUCT_CATEGORY_GOODS",
|
|
960
|
+
"tax_category": "TAX_CATEGORY_TAXABLE",
|
|
961
|
+
"standard_price": {"units": 1000, "currency": "JPY"}
|
|
962
|
+
}' localhost:9090 com.example.sales.ProductService/CreateProduct
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
</details>
|
|
966
|
+
|
|
967
|
+
---
|
|
968
|
+
|
|
969
|
+
## 第15章:マスタ API の実装
|
|
970
|
+
|
|
971
|
+
### 15.1 商品マスタ API の詳細設計
|
|
972
|
+
|
|
973
|
+
第14章で基礎を学んだ gRPC サービスを、商品・取引先・倉庫マスタに対して拡張します。
|
|
974
|
+
|
|
975
|
+
**商品マスタ API の機能一覧:**
|
|
976
|
+
|
|
977
|
+
| メソッド | RPC パターン | 説明 |
|
|
978
|
+
|----------|-------------|------|
|
|
979
|
+
| GetProduct | 単項 | 商品コードで取得 |
|
|
980
|
+
| CreateProduct | 単項 | 商品登録 |
|
|
981
|
+
| UpdateProduct | 単項 | 商品更新 |
|
|
982
|
+
| DeleteProduct | 単項 | 商品削除(論理削除) |
|
|
983
|
+
| ListProducts | サーバーストリーミング | 商品一覧取得 |
|
|
984
|
+
| BulkCreateProducts | クライアントストリーミング | 一括登録 |
|
|
985
|
+
| BulkUpdateProducts | クライアントストリーミング | 一括更新 |
|
|
986
|
+
| CheckProductExists | 単項 | 存在チェック |
|
|
987
|
+
|
|
988
|
+
---
|
|
989
|
+
|
|
990
|
+
### 15.2 取引先マスタ API の実装
|
|
991
|
+
|
|
992
|
+
#### src/main/proto/partner.proto
|
|
993
|
+
|
|
994
|
+
<details>
|
|
995
|
+
<summary>コード例: partner.proto</summary>
|
|
996
|
+
|
|
997
|
+
```protobuf
|
|
998
|
+
syntax = "proto3";
|
|
999
|
+
|
|
1000
|
+
package com.example.sales;
|
|
1001
|
+
|
|
1002
|
+
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
1003
|
+
option java_outer_classname = "PartnerProto";
|
|
1004
|
+
option java_multiple_files = true;
|
|
1005
|
+
|
|
1006
|
+
import "common.proto";
|
|
1007
|
+
|
|
1008
|
+
// 取引先区分
|
|
1009
|
+
enum PartnerType {
|
|
1010
|
+
PARTNER_TYPE_UNSPECIFIED = 0;
|
|
1011
|
+
PARTNER_TYPE_CUSTOMER = 1; // 顧客
|
|
1012
|
+
PARTNER_TYPE_SUPPLIER = 2; // 仕入先
|
|
1013
|
+
PARTNER_TYPE_BOTH = 3; // 顧客兼仕入先
|
|
1014
|
+
}
|
|
1015
|
+
|
|
1016
|
+
// 取引先ステータス
|
|
1017
|
+
enum PartnerStatus {
|
|
1018
|
+
PARTNER_STATUS_UNSPECIFIED = 0;
|
|
1019
|
+
PARTNER_STATUS_ACTIVE = 1; // 取引中
|
|
1020
|
+
PARTNER_STATUS_SUSPENDED = 2; // 取引停止
|
|
1021
|
+
PARTNER_STATUS_CLOSED = 3; // 取引終了
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// 取引先メッセージ
|
|
1025
|
+
message Partner {
|
|
1026
|
+
string partner_code = 1;
|
|
1027
|
+
string partner_name = 2;
|
|
1028
|
+
string partner_name_kana = 3;
|
|
1029
|
+
PartnerType partner_type = 4;
|
|
1030
|
+
PartnerStatus status = 5;
|
|
1031
|
+
Address address = 6;
|
|
1032
|
+
Contact contact = 7;
|
|
1033
|
+
CustomerInfo customer_info = 8; // 顧客情報(顧客の場合)
|
|
1034
|
+
SupplierInfo supplier_info = 9; // 仕入先情報(仕入先の場合)
|
|
1035
|
+
AuditInfo audit = 10;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
// 与信チェック
|
|
1039
|
+
message CheckCreditRequest {
|
|
1040
|
+
string partner_code = 1;
|
|
1041
|
+
Money amount = 2; // チェック金額
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
message CheckCreditResponse {
|
|
1045
|
+
bool is_within_limit = 1; // 限度内か
|
|
1046
|
+
Money credit_limit = 2; // 与信限度額
|
|
1047
|
+
Money current_receivable = 3; // 現在売掛金残高
|
|
1048
|
+
Money available_credit = 4; // 利用可能与信額
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
// === サービス定義 ===
|
|
1052
|
+
|
|
1053
|
+
service PartnerService {
|
|
1054
|
+
// 単項 RPC
|
|
1055
|
+
rpc GetPartner(GetPartnerRequest) returns (GetPartnerResponse);
|
|
1056
|
+
rpc CreatePartner(CreatePartnerRequest) returns (CreatePartnerResponse);
|
|
1057
|
+
rpc CheckCredit(CheckCreditRequest) returns (CheckCreditResponse);
|
|
1058
|
+
|
|
1059
|
+
// サーバーストリーミング RPC
|
|
1060
|
+
rpc ListPartners(ListPartnersRequest) returns (stream Partner);
|
|
1061
|
+
}
|
|
1062
|
+
```
|
|
1063
|
+
|
|
1064
|
+
</details>
|
|
1065
|
+
|
|
1066
|
+
---
|
|
1067
|
+
|
|
1068
|
+
### 15.3 テスト実装
|
|
1069
|
+
|
|
1070
|
+
<details>
|
|
1071
|
+
<summary>コード例: GrpcProductServiceTest.java</summary>
|
|
1072
|
+
|
|
1073
|
+
```java
|
|
1074
|
+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
|
|
1075
|
+
@ActiveProfiles("test")
|
|
1076
|
+
class GrpcProductServiceTest {
|
|
1077
|
+
|
|
1078
|
+
private ManagedChannel channel;
|
|
1079
|
+
private ProductServiceGrpc.ProductServiceBlockingStub blockingStub;
|
|
1080
|
+
private ProductServiceGrpc.ProductServiceStub asyncStub;
|
|
1081
|
+
|
|
1082
|
+
@BeforeEach
|
|
1083
|
+
void setUp() {
|
|
1084
|
+
channel = ManagedChannelBuilder.forAddress("localhost", 9999)
|
|
1085
|
+
.usePlaintext()
|
|
1086
|
+
.build();
|
|
1087
|
+
blockingStub = ProductServiceGrpc.newBlockingStub(channel);
|
|
1088
|
+
asyncStub = ProductServiceGrpc.newStub(channel);
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
@AfterEach
|
|
1092
|
+
void tearDown() {
|
|
1093
|
+
channel.shutdownNow();
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
@Test
|
|
1097
|
+
@DisplayName("商品の CRUD 操作が正常に動作すること")
|
|
1098
|
+
void productCrud() {
|
|
1099
|
+
// 登録
|
|
1100
|
+
CreateProductResponse createResponse = blockingStub.createProduct(
|
|
1101
|
+
CreateProductRequest.newBuilder()
|
|
1102
|
+
.setProductCode("PRD999")
|
|
1103
|
+
.setProductName("テスト商品")
|
|
1104
|
+
.setCategory(ProductCategory.PRODUCT_CATEGORY_GOODS)
|
|
1105
|
+
.setTaxCategory(TaxCategory.TAX_CATEGORY_TAXABLE)
|
|
1106
|
+
.setStandardPrice(Money.newBuilder().setUnits(1000).setCurrency("JPY"))
|
|
1107
|
+
.build()
|
|
1108
|
+
);
|
|
1109
|
+
assertThat(createResponse.getProduct().getProductCode()).isEqualTo("PRD999");
|
|
1110
|
+
|
|
1111
|
+
// 取得
|
|
1112
|
+
GetProductResponse getResponse = blockingStub.getProduct(
|
|
1113
|
+
GetProductRequest.newBuilder()
|
|
1114
|
+
.setProductCode("PRD999")
|
|
1115
|
+
.build()
|
|
1116
|
+
);
|
|
1117
|
+
assertThat(getResponse.getProduct().getProductName()).isEqualTo("テスト商品");
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
@Test
|
|
1121
|
+
@DisplayName("サーバーストリーミングで商品一覧を取得できること")
|
|
1122
|
+
void listProductsStreaming() throws InterruptedException {
|
|
1123
|
+
CountDownLatch latch = new CountDownLatch(1);
|
|
1124
|
+
List<Product> products = new ArrayList<>();
|
|
1125
|
+
|
|
1126
|
+
asyncStub.listProducts(
|
|
1127
|
+
ListProductsRequest.newBuilder()
|
|
1128
|
+
.setPage(PageRequest.newBuilder().setPage(0).setSize(100).build())
|
|
1129
|
+
.build(),
|
|
1130
|
+
new StreamObserver<>() {
|
|
1131
|
+
@Override
|
|
1132
|
+
public void onNext(Product product) {
|
|
1133
|
+
products.add(product);
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
@Override
|
|
1137
|
+
public void onError(Throwable t) {
|
|
1138
|
+
latch.countDown();
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
@Override
|
|
1142
|
+
public void onCompleted() {
|
|
1143
|
+
latch.countDown();
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
);
|
|
1147
|
+
|
|
1148
|
+
latch.await(10, TimeUnit.SECONDS);
|
|
1149
|
+
assertThat(products).isNotEmpty();
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
```
|
|
1153
|
+
|
|
1154
|
+
</details>
|
|
1155
|
+
|
|
1156
|
+
---
|
|
1157
|
+
|
|
1158
|
+
## 第16章:トランザクション API の実装
|
|
1159
|
+
|
|
1160
|
+
### 16.1 トランザクション API の設計方針
|
|
1161
|
+
|
|
1162
|
+
#### RPC パターンの使い分け
|
|
1163
|
+
|
|
1164
|
+
```plantuml
|
|
1165
|
+
@startuml transaction_rpc_patterns
|
|
1166
|
+
!define RECTANGLE class
|
|
1167
|
+
|
|
1168
|
+
skinparam backgroundColor #FEFEFE
|
|
1169
|
+
|
|
1170
|
+
package "Transaction API - RPC Patterns" {
|
|
1171
|
+
|
|
1172
|
+
RECTANGLE "単項 RPC" as unary {
|
|
1173
|
+
+ CreateOrder(受注登録)
|
|
1174
|
+
+ GetOrder(受注取得)
|
|
1175
|
+
+ ConfirmOrder(受注確定)
|
|
1176
|
+
+ CancelOrder(受注キャンセル)
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
RECTANGLE "サーバーストリーミング" as server_stream {
|
|
1180
|
+
+ ListOrders(受注一覧)
|
|
1181
|
+
+ WatchOrderStatus(ステータス監視)
|
|
1182
|
+
+ StreamShipmentProgress(出荷進捗)
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
RECTANGLE "クライアントストリーミング" as client_stream {
|
|
1186
|
+
+ BulkAddOrderDetails(明細一括登録)
|
|
1187
|
+
+ BatchShipmentConfirm(出荷一括確定)
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
RECTANGLE "双方向ストリーミング" as bidirectional {
|
|
1191
|
+
+ SyncInventory(在庫同期)
|
|
1192
|
+
+ ProcessInvoices(請求処理)
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
note bottom of unary
|
|
1197
|
+
個別トランザクション
|
|
1198
|
+
即時応答が必要な処理
|
|
1199
|
+
end note
|
|
1200
|
+
|
|
1201
|
+
note bottom of server_stream
|
|
1202
|
+
リアルタイム通知
|
|
1203
|
+
進捗モニタリング
|
|
1204
|
+
end note
|
|
1205
|
+
|
|
1206
|
+
note bottom of client_stream
|
|
1207
|
+
大量データ送信
|
|
1208
|
+
バッチ処理
|
|
1209
|
+
end note
|
|
1210
|
+
|
|
1211
|
+
note bottom of bidirectional
|
|
1212
|
+
双方向同期
|
|
1213
|
+
対話的処理
|
|
1214
|
+
end note
|
|
1215
|
+
|
|
1216
|
+
@enduml
|
|
1217
|
+
```
|
|
1218
|
+
|
|
1219
|
+
---
|
|
1220
|
+
|
|
1221
|
+
### 16.2 受注 API の Protocol Buffers 定義
|
|
1222
|
+
|
|
1223
|
+
#### src/main/proto/order.proto
|
|
1224
|
+
|
|
1225
|
+
<details>
|
|
1226
|
+
<summary>コード例: order.proto</summary>
|
|
1227
|
+
|
|
1228
|
+
```protobuf
|
|
1229
|
+
syntax = "proto3";
|
|
1230
|
+
|
|
1231
|
+
package com.example.sales;
|
|
1232
|
+
|
|
1233
|
+
option java_package = "com.example.sales.infrastructure.grpc.proto";
|
|
1234
|
+
option java_outer_classname = "OrderProto";
|
|
1235
|
+
option java_multiple_files = true;
|
|
1236
|
+
|
|
1237
|
+
import "common.proto";
|
|
1238
|
+
import "google/protobuf/timestamp.proto";
|
|
1239
|
+
|
|
1240
|
+
// 受注ステータス
|
|
1241
|
+
enum OrderStatus {
|
|
1242
|
+
ORDER_STATUS_UNSPECIFIED = 0;
|
|
1243
|
+
ORDER_STATUS_DRAFT = 1; // 下書き
|
|
1244
|
+
ORDER_STATUS_CONFIRMED = 2; // 確定
|
|
1245
|
+
ORDER_STATUS_PROCESSING = 3; // 処理中
|
|
1246
|
+
ORDER_STATUS_SHIPPED = 4; // 出荷済
|
|
1247
|
+
ORDER_STATUS_COMPLETED = 5; // 完了
|
|
1248
|
+
ORDER_STATUS_CANCELLED = 6; // キャンセル
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
// 受注ヘッダ
|
|
1252
|
+
message Order {
|
|
1253
|
+
string order_number = 1;
|
|
1254
|
+
string partner_code = 2;
|
|
1255
|
+
string partner_name = 3;
|
|
1256
|
+
Date order_date = 4;
|
|
1257
|
+
Date delivery_date = 5;
|
|
1258
|
+
OrderStatus status = 6;
|
|
1259
|
+
Money total_amount = 7;
|
|
1260
|
+
Money tax_amount = 8;
|
|
1261
|
+
string warehouse_code = 9;
|
|
1262
|
+
string sales_rep_code = 10;
|
|
1263
|
+
string notes = 11;
|
|
1264
|
+
repeated OrderDetail details = 12;
|
|
1265
|
+
AuditInfo audit = 13;
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
// 受注明細
|
|
1269
|
+
message OrderDetail {
|
|
1270
|
+
int32 line_number = 1;
|
|
1271
|
+
string product_code = 2;
|
|
1272
|
+
string product_name = 3;
|
|
1273
|
+
int32 quantity = 4;
|
|
1274
|
+
Money unit_price = 5;
|
|
1275
|
+
Money amount = 6;
|
|
1276
|
+
string tax_category = 7;
|
|
1277
|
+
Date delivery_date = 8;
|
|
1278
|
+
string notes = 9;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
// 受注ステータス監視(サーバーストリーミング)
|
|
1282
|
+
message WatchOrderStatusRequest {
|
|
1283
|
+
repeated string order_numbers = 1;
|
|
1284
|
+
repeated OrderStatus statuses = 2;
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
message OrderStatusChange {
|
|
1288
|
+
string order_number = 1;
|
|
1289
|
+
OrderStatus previous_status = 2;
|
|
1290
|
+
OrderStatus current_status = 3;
|
|
1291
|
+
google.protobuf.Timestamp changed_at = 4;
|
|
1292
|
+
string changed_by = 5;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
// === サービス定義 ===
|
|
1296
|
+
|
|
1297
|
+
service OrderService {
|
|
1298
|
+
// 単項 RPC
|
|
1299
|
+
rpc GetOrder(GetOrderRequest) returns (GetOrderResponse);
|
|
1300
|
+
rpc CreateOrder(CreateOrderRequest) returns (CreateOrderResponse);
|
|
1301
|
+
rpc UpdateOrder(UpdateOrderRequest) returns (UpdateOrderResponse);
|
|
1302
|
+
rpc ConfirmOrder(ConfirmOrderRequest) returns (ConfirmOrderResponse);
|
|
1303
|
+
rpc CancelOrder(CancelOrderRequest) returns (CancelOrderResponse);
|
|
1304
|
+
|
|
1305
|
+
// サーバーストリーミング RPC
|
|
1306
|
+
rpc ListOrders(ListOrdersRequest) returns (stream Order);
|
|
1307
|
+
rpc WatchOrderStatus(WatchOrderStatusRequest) returns (stream OrderStatusChange);
|
|
1308
|
+
|
|
1309
|
+
// クライアントストリーミング RPC
|
|
1310
|
+
rpc BulkAddOrderDetails(stream BulkAddOrderDetailRequest) returns (BulkAddOrderDetailResponse);
|
|
1311
|
+
}
|
|
1312
|
+
```
|
|
1313
|
+
|
|
1314
|
+
</details>
|
|
1315
|
+
|
|
1316
|
+
---
|
|
1317
|
+
|
|
1318
|
+
### 16.3 受注サービス実装(ステータス監視)
|
|
1319
|
+
|
|
1320
|
+
<details>
|
|
1321
|
+
<summary>コード例: GrpcOrderService.java</summary>
|
|
1322
|
+
|
|
1323
|
+
```java
|
|
1324
|
+
@GrpcService
|
|
1325
|
+
public class GrpcOrderService extends OrderServiceGrpc.OrderServiceImplBase {
|
|
1326
|
+
|
|
1327
|
+
private static final Logger log = LoggerFactory.getLogger(GrpcOrderService.class);
|
|
1328
|
+
|
|
1329
|
+
private final OrderUseCase orderUseCase;
|
|
1330
|
+
private final CreditUseCase creditUseCase;
|
|
1331
|
+
private final OrderConverter converter;
|
|
1332
|
+
|
|
1333
|
+
// ステータス監視用のオブザーバー管理
|
|
1334
|
+
private final Map<String, Set<StreamObserver<OrderStatusChange>>> statusWatchers =
|
|
1335
|
+
new ConcurrentHashMap<>();
|
|
1336
|
+
|
|
1337
|
+
/**
|
|
1338
|
+
* サーバーストリーミング RPC: 受注ステータス監視
|
|
1339
|
+
*/
|
|
1340
|
+
@Override
|
|
1341
|
+
public void watchOrderStatus(WatchOrderStatusRequest request,
|
|
1342
|
+
StreamObserver<OrderStatusChange> responseObserver) {
|
|
1343
|
+
log.info("watchOrderStatus: orders={}", request.getOrderNumbersList());
|
|
1344
|
+
|
|
1345
|
+
// オブザーバー登録
|
|
1346
|
+
for (String orderNumber : request.getOrderNumbersList()) {
|
|
1347
|
+
statusWatchers
|
|
1348
|
+
.computeIfAbsent(orderNumber, k -> ConcurrentHashMap.newKeySet())
|
|
1349
|
+
.add(responseObserver);
|
|
1350
|
+
}
|
|
1351
|
+
|
|
1352
|
+
// コンテキストキャンセル時にオブザーバー削除
|
|
1353
|
+
Context.current().addListener(context -> {
|
|
1354
|
+
for (String orderNumber : request.getOrderNumbersList()) {
|
|
1355
|
+
Set<StreamObserver<OrderStatusChange>> watchers = statusWatchers.get(orderNumber);
|
|
1356
|
+
if (watchers != null) {
|
|
1357
|
+
watchers.remove(responseObserver);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
}, Runnable::run);
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
/**
|
|
1364
|
+
* Spring Events でステータス変更を受信し、ウォッチャーに通知
|
|
1365
|
+
*/
|
|
1366
|
+
@EventListener
|
|
1367
|
+
public void onOrderStatusChanged(OrderStatusChangedEvent event) {
|
|
1368
|
+
Set<StreamObserver<OrderStatusChange>> watchers =
|
|
1369
|
+
statusWatchers.get(event.getOrderNumber());
|
|
1370
|
+
|
|
1371
|
+
if (watchers == null || watchers.isEmpty()) {
|
|
1372
|
+
return;
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1375
|
+
OrderStatusChange change = OrderStatusChange.newBuilder()
|
|
1376
|
+
.setOrderNumber(event.getOrderNumber())
|
|
1377
|
+
.setPreviousStatus(converter.toProtoStatus(event.getPreviousStatus()))
|
|
1378
|
+
.setCurrentStatus(converter.toProtoStatus(event.getCurrentStatus()))
|
|
1379
|
+
.setChangedAt(converter.toProtoTimestamp(event.getChangedAt()))
|
|
1380
|
+
.setChangedBy(event.getChangedBy())
|
|
1381
|
+
.build();
|
|
1382
|
+
|
|
1383
|
+
for (StreamObserver<OrderStatusChange> watcher : watchers) {
|
|
1384
|
+
try {
|
|
1385
|
+
watcher.onNext(change);
|
|
1386
|
+
} catch (Exception e) {
|
|
1387
|
+
log.warn("Failed to notify watcher", e);
|
|
1388
|
+
watchers.remove(watcher);
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
```
|
|
1394
|
+
|
|
1395
|
+
</details>
|
|
1396
|
+
|
|
1397
|
+
---
|
|
1398
|
+
|
|
1399
|
+
### 16.4 請求サービス実装(双方向ストリーミング)
|
|
1400
|
+
|
|
1401
|
+
<details>
|
|
1402
|
+
<summary>コード例: processInvoices メソッド</summary>
|
|
1403
|
+
|
|
1404
|
+
```java
|
|
1405
|
+
/**
|
|
1406
|
+
* 双方向ストリーミング RPC: 請求処理
|
|
1407
|
+
*/
|
|
1408
|
+
@Override
|
|
1409
|
+
public StreamObserver<ProcessInvoiceRequest> processInvoices(
|
|
1410
|
+
StreamObserver<ProcessInvoiceResponse> responseObserver) {
|
|
1411
|
+
|
|
1412
|
+
log.info("processInvoices: started");
|
|
1413
|
+
|
|
1414
|
+
return new StreamObserver<ProcessInvoiceRequest>() {
|
|
1415
|
+
@Override
|
|
1416
|
+
public void onNext(ProcessInvoiceRequest request) {
|
|
1417
|
+
ProcessInvoiceResponse response;
|
|
1418
|
+
|
|
1419
|
+
try {
|
|
1420
|
+
switch (request.getCommandCase()) {
|
|
1421
|
+
case CREATE:
|
|
1422
|
+
response = handleCreateInvoice(request.getCreate());
|
|
1423
|
+
break;
|
|
1424
|
+
case ISSUE:
|
|
1425
|
+
response = handleIssueInvoice(request.getIssue());
|
|
1426
|
+
break;
|
|
1427
|
+
case RECEIPT:
|
|
1428
|
+
response = handleRecordReceipt(request.getReceipt());
|
|
1429
|
+
break;
|
|
1430
|
+
default:
|
|
1431
|
+
response = ProcessInvoiceResponse.newBuilder()
|
|
1432
|
+
.setSuccess(false)
|
|
1433
|
+
.setMessage("不明なコマンド")
|
|
1434
|
+
.build();
|
|
1435
|
+
}
|
|
1436
|
+
} catch (Exception e) {
|
|
1437
|
+
log.error("processInvoices: error", e);
|
|
1438
|
+
response = ProcessInvoiceResponse.newBuilder()
|
|
1439
|
+
.setSuccess(false)
|
|
1440
|
+
.setMessage("エラー: " + e.getMessage())
|
|
1441
|
+
.build();
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
// 即座に結果を返す
|
|
1445
|
+
responseObserver.onNext(response);
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
@Override
|
|
1449
|
+
public void onError(Throwable t) {
|
|
1450
|
+
log.error("processInvoices: client error", t);
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
@Override
|
|
1454
|
+
public void onCompleted() {
|
|
1455
|
+
log.info("processInvoices: client completed");
|
|
1456
|
+
responseObserver.onCompleted();
|
|
1457
|
+
}
|
|
1458
|
+
};
|
|
1459
|
+
}
|
|
1460
|
+
```
|
|
1461
|
+
|
|
1462
|
+
</details>
|
|
1463
|
+
|
|
1464
|
+
---
|
|
1465
|
+
|
|
1466
|
+
### 16.5 grpcurl による動作確認
|
|
1467
|
+
|
|
1468
|
+
<details>
|
|
1469
|
+
<summary>コード例: grpcurl コマンド</summary>
|
|
1470
|
+
|
|
1471
|
+
```bash
|
|
1472
|
+
# 受注登録
|
|
1473
|
+
grpcurl -plaintext -d '{
|
|
1474
|
+
"partner_code": "CUS001",
|
|
1475
|
+
"order_date": {"year": 2025, "month": 12, "day": 29},
|
|
1476
|
+
"delivery_date": {"year": 2026, "month": 1, "day": 5},
|
|
1477
|
+
"warehouse_code": "WH001",
|
|
1478
|
+
"details": [
|
|
1479
|
+
{"product_code": "PRD001", "quantity": 10},
|
|
1480
|
+
{"product_code": "PRD002", "quantity": 5}
|
|
1481
|
+
]
|
|
1482
|
+
}' localhost:9090 com.example.sales.OrderService/CreateOrder
|
|
1483
|
+
|
|
1484
|
+
# 受注確定
|
|
1485
|
+
grpcurl -plaintext -d '{
|
|
1486
|
+
"order_number": "ORD20251229001"
|
|
1487
|
+
}' localhost:9090 com.example.sales.OrderService/ConfirmOrder
|
|
1488
|
+
|
|
1489
|
+
# 受注ステータス監視(サーバーストリーミング)
|
|
1490
|
+
grpcurl -plaintext -d '{
|
|
1491
|
+
"order_numbers": ["ORD20251229001"]
|
|
1492
|
+
}' localhost:9090 com.example.sales.OrderService/WatchOrderStatus
|
|
1493
|
+
|
|
1494
|
+
# 出荷進捗監視(サーバーストリーミング)
|
|
1495
|
+
grpcurl -plaintext -d '{
|
|
1496
|
+
"shipment_number": "SHP20251230001"
|
|
1497
|
+
}' localhost:9090 com.example.sales.ShipmentService/StreamShipmentProgress
|
|
1498
|
+
```
|
|
1499
|
+
|
|
1500
|
+
</details>
|
|
1501
|
+
|
|
1502
|
+
---
|
|
1503
|
+
|
|
1504
|
+
## 第17章:エラーハンドリングとストリーミング
|
|
1505
|
+
|
|
1506
|
+
### 17.1 gRPC エラーハンドリングの概要
|
|
1507
|
+
|
|
1508
|
+
gRPC では、エラーは **Status** オブジェクトで表現されます。Status には **コード**(StatusCode)と **メッセージ**、オプションで **詳細情報** を含めることができます。
|
|
1509
|
+
|
|
1510
|
+
```plantuml
|
|
1511
|
+
@startuml grpc_error_architecture
|
|
1512
|
+
|
|
1513
|
+
title gRPC エラーハンドリングアーキテクチャ
|
|
1514
|
+
|
|
1515
|
+
package "クライアント" {
|
|
1516
|
+
[クライアント\nアプリケーション] as client
|
|
1517
|
+
[リトライ\nインターセプター] as retry
|
|
1518
|
+
[ヘルスチェック\nクライアント] as health_client
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
package "gRPC チャネル" {
|
|
1522
|
+
[チャネル] as channel
|
|
1523
|
+
[回路ブレーカー] as circuit
|
|
1524
|
+
[デッドライン\n管理] as deadline
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
package "サーバー" {
|
|
1528
|
+
[エラー\nインターセプター] as error_interceptor
|
|
1529
|
+
[gRPC サービス] as service
|
|
1530
|
+
[ヘルスサービス] as health_service
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
client --> retry : "リクエスト"
|
|
1534
|
+
retry --> channel
|
|
1535
|
+
channel --> circuit
|
|
1536
|
+
circuit --> deadline
|
|
1537
|
+
deadline --> error_interceptor
|
|
1538
|
+
error_interceptor --> service
|
|
1539
|
+
|
|
1540
|
+
health_client --> health_service : "ヘルスチェック"
|
|
1541
|
+
|
|
1542
|
+
note right of error_interceptor
|
|
1543
|
+
例外をStatus変換
|
|
1544
|
+
詳細情報追加
|
|
1545
|
+
end note
|
|
1546
|
+
|
|
1547
|
+
note right of circuit
|
|
1548
|
+
連続エラー時に
|
|
1549
|
+
回路をオープン
|
|
1550
|
+
end note
|
|
1551
|
+
|
|
1552
|
+
@enduml
|
|
1553
|
+
```
|
|
1554
|
+
|
|
1555
|
+
---
|
|
1556
|
+
|
|
1557
|
+
### 17.2 gRPC ステータスコード
|
|
1558
|
+
|
|
1559
|
+
<details>
|
|
1560
|
+
<summary>コード例: GrpcStatusCode.java</summary>
|
|
1561
|
+
|
|
1562
|
+
```java
|
|
1563
|
+
/**
|
|
1564
|
+
* gRPC ステータスコードとビジネス例外のマッピング
|
|
1565
|
+
*/
|
|
1566
|
+
public enum GrpcStatusCode {
|
|
1567
|
+
|
|
1568
|
+
// 成功
|
|
1569
|
+
OK(Status.OK, "正常終了"),
|
|
1570
|
+
|
|
1571
|
+
// クライアントエラー
|
|
1572
|
+
INVALID_ARGUMENT(Status.INVALID_ARGUMENT, "不正な引数"),
|
|
1573
|
+
NOT_FOUND(Status.NOT_FOUND, "リソースが見つかりません"),
|
|
1574
|
+
ALREADY_EXISTS(Status.ALREADY_EXISTS, "リソースが既に存在します"),
|
|
1575
|
+
PERMISSION_DENIED(Status.PERMISSION_DENIED, "権限がありません"),
|
|
1576
|
+
FAILED_PRECONDITION(Status.FAILED_PRECONDITION, "前提条件が満たされていません"),
|
|
1577
|
+
|
|
1578
|
+
// サーバーエラー
|
|
1579
|
+
INTERNAL(Status.INTERNAL, "内部エラー"),
|
|
1580
|
+
UNAVAILABLE(Status.UNAVAILABLE, "サービス利用不可"),
|
|
1581
|
+
|
|
1582
|
+
// タイムアウト
|
|
1583
|
+
DEADLINE_EXCEEDED(Status.DEADLINE_EXCEEDED, "タイムアウト"),
|
|
1584
|
+
CANCELLED(Status.CANCELLED, "キャンセルされました"),
|
|
1585
|
+
|
|
1586
|
+
// 認証
|
|
1587
|
+
UNAUTHENTICATED(Status.UNAUTHENTICATED, "認証が必要です");
|
|
1588
|
+
|
|
1589
|
+
/**
|
|
1590
|
+
* リトライ可能なステータスかどうか
|
|
1591
|
+
*/
|
|
1592
|
+
public boolean isRetryable() {
|
|
1593
|
+
return this == UNAVAILABLE ||
|
|
1594
|
+
this == DEADLINE_EXCEEDED ||
|
|
1595
|
+
this == INTERNAL;
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
```
|
|
1599
|
+
|
|
1600
|
+
</details>
|
|
1601
|
+
|
|
1602
|
+
---
|
|
1603
|
+
|
|
1604
|
+
### 17.3 カスタム例外クラス
|
|
1605
|
+
|
|
1606
|
+
<details>
|
|
1607
|
+
<summary>コード例: GrpcException.java</summary>
|
|
1608
|
+
|
|
1609
|
+
```java
|
|
1610
|
+
/**
|
|
1611
|
+
* gRPC 用カスタム例外
|
|
1612
|
+
*/
|
|
1613
|
+
public class GrpcException extends RuntimeException {
|
|
1614
|
+
|
|
1615
|
+
private final GrpcStatusCode statusCode;
|
|
1616
|
+
private final Map<String, String> details;
|
|
1617
|
+
private final boolean retryable;
|
|
1618
|
+
private final long retryDelayMillis;
|
|
1619
|
+
|
|
1620
|
+
// 便利なファクトリメソッド
|
|
1621
|
+
public static GrpcException notFound(String resource, String identifier) {
|
|
1622
|
+
return builder(GrpcStatusCode.NOT_FOUND)
|
|
1623
|
+
.message(resource + " が見つかりません: " + identifier)
|
|
1624
|
+
.detail("resource", resource)
|
|
1625
|
+
.detail("identifier", identifier)
|
|
1626
|
+
.build();
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
public static GrpcException invalidArgument(String field, String reason) {
|
|
1630
|
+
return builder(GrpcStatusCode.INVALID_ARGUMENT)
|
|
1631
|
+
.message("不正な引数: " + field + " - " + reason)
|
|
1632
|
+
.detail("field", field)
|
|
1633
|
+
.detail("reason", reason)
|
|
1634
|
+
.build();
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
public static GrpcException failedPrecondition(String condition) {
|
|
1638
|
+
return builder(GrpcStatusCode.FAILED_PRECONDITION)
|
|
1639
|
+
.message("前提条件が満たされていません: " + condition)
|
|
1640
|
+
.detail("condition", condition)
|
|
1641
|
+
.build();
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
public static GrpcException unavailable(String service, long retryDelayMillis) {
|
|
1645
|
+
return builder(GrpcStatusCode.UNAVAILABLE)
|
|
1646
|
+
.message("サービス利用不可: " + service)
|
|
1647
|
+
.retryable(true)
|
|
1648
|
+
.retryDelayMillis(retryDelayMillis)
|
|
1649
|
+
.build();
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
public Status toStatus() {
|
|
1653
|
+
return statusCode.getStatus().withDescription(getMessage());
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
```
|
|
1657
|
+
|
|
1658
|
+
</details>
|
|
1659
|
+
|
|
1660
|
+
---
|
|
1661
|
+
|
|
1662
|
+
### 17.4 例外変換インターセプター
|
|
1663
|
+
|
|
1664
|
+
<details>
|
|
1665
|
+
<summary>コード例: ExceptionTranslationInterceptor.java</summary>
|
|
1666
|
+
|
|
1667
|
+
```java
|
|
1668
|
+
/**
|
|
1669
|
+
* 例外を gRPC Status に変換するインターセプター
|
|
1670
|
+
*/
|
|
1671
|
+
@GrpcGlobalServerInterceptor
|
|
1672
|
+
public class ExceptionTranslationInterceptor implements ServerInterceptor {
|
|
1673
|
+
|
|
1674
|
+
private StatusRuntimeException translateException(Exception e) {
|
|
1675
|
+
// GrpcException の場合
|
|
1676
|
+
if (e instanceof GrpcException grpcEx) {
|
|
1677
|
+
return createStatusException(grpcEx);
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
// ドメイン例外の変換
|
|
1681
|
+
if (e instanceof EntityNotFoundException notFound) {
|
|
1682
|
+
return createNotFoundStatus(notFound);
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
if (e instanceof ValidationException validation) {
|
|
1686
|
+
return createValidationStatus(validation);
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
if (e instanceof BusinessException business) {
|
|
1690
|
+
return createBusinessStatus(business);
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
// その他は内部エラー
|
|
1694
|
+
return createInternalStatus(e);
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
private StatusRuntimeException createValidationStatus(ValidationException e) {
|
|
1698
|
+
BadRequest.Builder badRequest = BadRequest.newBuilder();
|
|
1699
|
+
|
|
1700
|
+
e.getViolations().forEach(violation ->
|
|
1701
|
+
badRequest.addFieldViolations(
|
|
1702
|
+
BadRequest.FieldViolation.newBuilder()
|
|
1703
|
+
.setField(violation.field())
|
|
1704
|
+
.setDescription(violation.message())
|
|
1705
|
+
.build()
|
|
1706
|
+
)
|
|
1707
|
+
);
|
|
1708
|
+
|
|
1709
|
+
com.google.rpc.Status status = com.google.rpc.Status.newBuilder()
|
|
1710
|
+
.setCode(Status.INVALID_ARGUMENT.getCode().value())
|
|
1711
|
+
.setMessage("バリデーションエラー")
|
|
1712
|
+
.addDetails(Any.pack(badRequest.build()))
|
|
1713
|
+
.build();
|
|
1714
|
+
|
|
1715
|
+
return StatusProto.toStatusRuntimeException(status);
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
```
|
|
1719
|
+
|
|
1720
|
+
</details>
|
|
1721
|
+
|
|
1722
|
+
---
|
|
1723
|
+
|
|
1724
|
+
### 17.5 リトライ設定
|
|
1725
|
+
|
|
1726
|
+
<details>
|
|
1727
|
+
<summary>コード例: GrpcRetryConfig.java</summary>
|
|
1728
|
+
|
|
1729
|
+
```java
|
|
1730
|
+
/**
|
|
1731
|
+
* リトライポリシー設定
|
|
1732
|
+
*/
|
|
1733
|
+
@Configuration
|
|
1734
|
+
public class GrpcRetryConfig {
|
|
1735
|
+
|
|
1736
|
+
@Bean
|
|
1737
|
+
public RetryPolicy retryPolicy() {
|
|
1738
|
+
return RetryPolicy.builder()
|
|
1739
|
+
.maxAttempts(3)
|
|
1740
|
+
.initialBackoff(Duration.ofMillis(500))
|
|
1741
|
+
.maxBackoff(Duration.ofSeconds(10))
|
|
1742
|
+
.backoffMultiplier(2.0)
|
|
1743
|
+
.retryableStatusCodes(Set.of(
|
|
1744
|
+
Status.Code.UNAVAILABLE,
|
|
1745
|
+
Status.Code.DEADLINE_EXCEEDED,
|
|
1746
|
+
Status.Code.INTERNAL
|
|
1747
|
+
))
|
|
1748
|
+
.build();
|
|
1749
|
+
}
|
|
1750
|
+
}
|
|
1751
|
+
```
|
|
1752
|
+
|
|
1753
|
+
</details>
|
|
1754
|
+
|
|
1755
|
+
---
|
|
1756
|
+
|
|
1757
|
+
### 17.6 回路ブレーカー
|
|
1758
|
+
|
|
1759
|
+
<details>
|
|
1760
|
+
<summary>コード例: GrpcCircuitBreaker.java</summary>
|
|
1761
|
+
|
|
1762
|
+
```java
|
|
1763
|
+
/**
|
|
1764
|
+
* gRPC 回路ブレーカー
|
|
1765
|
+
*/
|
|
1766
|
+
public class GrpcCircuitBreaker {
|
|
1767
|
+
|
|
1768
|
+
public enum State {
|
|
1769
|
+
CLOSED, // 正常(リクエスト許可)
|
|
1770
|
+
OPEN, // 遮断(リクエスト拒否)
|
|
1771
|
+
HALF_OPEN // 試行中
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
private final int failureThreshold;
|
|
1775
|
+
private final int successThreshold;
|
|
1776
|
+
private final Duration openStateTimeout;
|
|
1777
|
+
|
|
1778
|
+
private volatile State state = State.CLOSED;
|
|
1779
|
+
private final AtomicInteger failureCount = new AtomicInteger(0);
|
|
1780
|
+
private final AtomicInteger successCount = new AtomicInteger(0);
|
|
1781
|
+
private volatile Instant lastFailureTime;
|
|
1782
|
+
|
|
1783
|
+
public boolean allowRequest() {
|
|
1784
|
+
return switch (state) {
|
|
1785
|
+
case CLOSED -> true;
|
|
1786
|
+
case OPEN -> shouldAttemptReset();
|
|
1787
|
+
case HALF_OPEN -> true;
|
|
1788
|
+
};
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
private boolean shouldAttemptReset() {
|
|
1792
|
+
if (lastFailureTime == null) {
|
|
1793
|
+
return false;
|
|
1794
|
+
}
|
|
1795
|
+
if (Duration.between(lastFailureTime, Instant.now())
|
|
1796
|
+
.compareTo(openStateTimeout) > 0) {
|
|
1797
|
+
state = State.HALF_OPEN;
|
|
1798
|
+
return true;
|
|
1799
|
+
}
|
|
1800
|
+
return false;
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
public void recordSuccess() {
|
|
1804
|
+
if (state == State.HALF_OPEN) {
|
|
1805
|
+
if (successCount.incrementAndGet() >= successThreshold) {
|
|
1806
|
+
reset();
|
|
1807
|
+
}
|
|
1808
|
+
} else {
|
|
1809
|
+
failureCount.set(0);
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
|
|
1813
|
+
public void recordFailure() {
|
|
1814
|
+
lastFailureTime = Instant.now();
|
|
1815
|
+
if (state == State.HALF_OPEN) {
|
|
1816
|
+
trip();
|
|
1817
|
+
} else if (failureCount.incrementAndGet() >= failureThreshold) {
|
|
1818
|
+
trip();
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
private void trip() {
|
|
1823
|
+
state = State.OPEN;
|
|
1824
|
+
failureCount.set(0);
|
|
1825
|
+
successCount.set(0);
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
private void reset() {
|
|
1829
|
+
state = State.CLOSED;
|
|
1830
|
+
failureCount.set(0);
|
|
1831
|
+
successCount.set(0);
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
```
|
|
1835
|
+
|
|
1836
|
+
</details>
|
|
1837
|
+
|
|
1838
|
+
---
|
|
1839
|
+
|
|
1840
|
+
### 17.7 ヘルスチェックサービス
|
|
1841
|
+
|
|
1842
|
+
<details>
|
|
1843
|
+
<summary>コード例: HealthCheckService.java</summary>
|
|
1844
|
+
|
|
1845
|
+
```java
|
|
1846
|
+
/**
|
|
1847
|
+
* gRPC ヘルスチェックサービス
|
|
1848
|
+
*/
|
|
1849
|
+
@GrpcService
|
|
1850
|
+
public class HealthCheckService extends HealthGrpc.HealthImplBase {
|
|
1851
|
+
|
|
1852
|
+
private final Map<String, HealthCheckResponse.ServingStatus> serviceStatuses =
|
|
1853
|
+
new ConcurrentHashMap<>();
|
|
1854
|
+
|
|
1855
|
+
public HealthCheckService() {
|
|
1856
|
+
// デフォルトで全サービスを SERVING に設定
|
|
1857
|
+
setServiceStatus("", HealthCheckResponse.ServingStatus.SERVING);
|
|
1858
|
+
setServiceStatus("com.example.sales.ProductService",
|
|
1859
|
+
HealthCheckResponse.ServingStatus.SERVING);
|
|
1860
|
+
setServiceStatus("com.example.sales.OrderService",
|
|
1861
|
+
HealthCheckResponse.ServingStatus.SERVING);
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
@Override
|
|
1865
|
+
public void check(HealthCheckRequest request,
|
|
1866
|
+
StreamObserver<HealthCheckResponse> responseObserver) {
|
|
1867
|
+
String service = request.getService();
|
|
1868
|
+
HealthCheckResponse.ServingStatus status = serviceStatuses.getOrDefault(
|
|
1869
|
+
service,
|
|
1870
|
+
HealthCheckResponse.ServingStatus.SERVICE_UNKNOWN
|
|
1871
|
+
);
|
|
1872
|
+
|
|
1873
|
+
HealthCheckResponse response = HealthCheckResponse.newBuilder()
|
|
1874
|
+
.setStatus(status)
|
|
1875
|
+
.build();
|
|
1876
|
+
|
|
1877
|
+
responseObserver.onNext(response);
|
|
1878
|
+
responseObserver.onCompleted();
|
|
1879
|
+
}
|
|
1880
|
+
|
|
1881
|
+
@Override
|
|
1882
|
+
public void watch(HealthCheckRequest request,
|
|
1883
|
+
StreamObserver<HealthCheckResponse> responseObserver) {
|
|
1884
|
+
// 継続的なヘルスチェック監視
|
|
1885
|
+
String service = request.getService();
|
|
1886
|
+
|
|
1887
|
+
// 初期状態を送信
|
|
1888
|
+
HealthCheckResponse.ServingStatus status = serviceStatuses.getOrDefault(
|
|
1889
|
+
service,
|
|
1890
|
+
HealthCheckResponse.ServingStatus.SERVICE_UNKNOWN
|
|
1891
|
+
);
|
|
1892
|
+
|
|
1893
|
+
responseObserver.onNext(HealthCheckResponse.newBuilder()
|
|
1894
|
+
.setStatus(status)
|
|
1895
|
+
.build());
|
|
1896
|
+
|
|
1897
|
+
// 状態変更時に通知(実装省略)
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
```
|
|
1901
|
+
|
|
1902
|
+
</details>
|
|
1903
|
+
|
|
1904
|
+
---
|
|
1905
|
+
|
|
1906
|
+
### 17.8 フロー制御付きストリームオブザーバー
|
|
1907
|
+
|
|
1908
|
+
<details>
|
|
1909
|
+
<summary>コード例: FlowControlledStreamObserver.java</summary>
|
|
1910
|
+
|
|
1911
|
+
```java
|
|
1912
|
+
/**
|
|
1913
|
+
* フロー制御付きストリームオブザーバー
|
|
1914
|
+
*/
|
|
1915
|
+
public class FlowControlledStreamObserver<T> implements StreamObserver<T> {
|
|
1916
|
+
|
|
1917
|
+
private final CallStreamObserver<T> delegate;
|
|
1918
|
+
private final Queue<T> buffer;
|
|
1919
|
+
private final int maxBufferSize;
|
|
1920
|
+
private final AtomicBoolean completed;
|
|
1921
|
+
|
|
1922
|
+
public FlowControlledStreamObserver(CallStreamObserver<T> delegate, int maxBufferSize) {
|
|
1923
|
+
this.delegate = delegate;
|
|
1924
|
+
this.buffer = new ConcurrentLinkedQueue<>();
|
|
1925
|
+
this.maxBufferSize = maxBufferSize;
|
|
1926
|
+
this.completed = new AtomicBoolean(false);
|
|
1927
|
+
|
|
1928
|
+
// バックプレッシャー設定
|
|
1929
|
+
delegate.setOnReadyHandler(this::drainBuffer);
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
@Override
|
|
1933
|
+
public void onNext(T value) {
|
|
1934
|
+
if (completed.get()) {
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
if (delegate.isReady()) {
|
|
1939
|
+
delegate.onNext(value);
|
|
1940
|
+
} else {
|
|
1941
|
+
if (buffer.size() >= maxBufferSize) {
|
|
1942
|
+
return; // バッファ満杯時は破棄
|
|
1943
|
+
}
|
|
1944
|
+
buffer.offer(value);
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
|
|
1948
|
+
private void drainBuffer() {
|
|
1949
|
+
while (delegate.isReady() && !buffer.isEmpty()) {
|
|
1950
|
+
T value = buffer.poll();
|
|
1951
|
+
if (value != null) {
|
|
1952
|
+
delegate.onNext(value);
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
if (completed.get() && buffer.isEmpty()) {
|
|
1957
|
+
delegate.onCompleted();
|
|
1958
|
+
}
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
```
|
|
1962
|
+
|
|
1963
|
+
</details>
|
|
1964
|
+
|
|
1965
|
+
---
|
|
1966
|
+
|
|
1967
|
+
## Part 10-D のまとめ
|
|
1968
|
+
|
|
1969
|
+
### 実装した機能一覧
|
|
1970
|
+
|
|
1971
|
+
| 章 | 内容 | 主要技術 |
|
|
1972
|
+
|---|---|---|
|
|
1973
|
+
| **第14章: 基礎** | gRPC サーバーの基礎 | Protocol Buffers, HTTP/2, grpc-spring-boot-starter |
|
|
1974
|
+
| **第15章: マスタ API** | 商品・取引先・倉庫マスタ | 単項 RPC, サーバーストリーミング |
|
|
1975
|
+
| **第16章: トランザクション API** | 受注・出荷・請求処理 | クライアントストリーミング, 双方向ストリーミング |
|
|
1976
|
+
| **第17章: エラーハンドリング** | リトライ, 回路ブレーカー, ヘルスチェック | gRPC Status, インターセプター |
|
|
1977
|
+
|
|
1978
|
+
### 実装した gRPC サービス
|
|
1979
|
+
|
|
1980
|
+
- **ProductService**: 商品マスタの CRUD、一覧ストリーミング、一括登録
|
|
1981
|
+
- **PartnerService**: 取引先マスタの CRUD、一覧ストリーミング、与信チェック
|
|
1982
|
+
- **WarehouseService**: 倉庫マスタの CRUD、在庫ストリーミング
|
|
1983
|
+
- **OrderService**: 受注の CRUD、確定、キャンセル、ステータス監視、明細一括登録
|
|
1984
|
+
- **ShipmentService**: 出荷指示、ピッキング、出荷確定、進捗監視、一括確定
|
|
1985
|
+
- **InvoiceService**: 締処理、発行、入金記録、双方向ストリーミング処理
|
|
1986
|
+
|
|
1987
|
+
### アーキテクチャの特徴
|
|
1988
|
+
|
|
1989
|
+
```
|
|
1990
|
+
┌─────────────────────────────────────┐
|
|
1991
|
+
│ プレゼンテーション層 │
|
|
1992
|
+
│ ┌─────────┐ ┌─────────┐ ┌──────┐ │
|
|
1993
|
+
│ │ REST API │ │ GraphQL │ │ gRPC │ │
|
|
1994
|
+
│ └─────────┘ └─────────┘ └──────┘ │
|
|
1995
|
+
└─────────────────────────────────────┘
|
|
1996
|
+
│
|
|
1997
|
+
┌─────────────────────────────────────┐
|
|
1998
|
+
│ アプリケーション層 │
|
|
1999
|
+
│ ┌──────────────────────────────┐ │
|
|
2000
|
+
│ │ ユースケース / サービス │ │
|
|
2001
|
+
│ └──────────────────────────────┘ │
|
|
2002
|
+
└─────────────────────────────────────┘
|
|
2003
|
+
│
|
|
2004
|
+
┌─────────────────────────────────────┐
|
|
2005
|
+
│ ドメイン層 │
|
|
2006
|
+
│ ┌────────┐ ┌────────┐ ┌──────┐ │
|
|
2007
|
+
│ │ エンティティ │ │ 値オブジェクト │ │ リポジトリ │ │
|
|
2008
|
+
│ └────────┘ └────────┘ └──────┘ │
|
|
2009
|
+
└─────────────────────────────────────┘
|
|
2010
|
+
```
|
|
2011
|
+
|
|
2012
|
+
gRPC は REST API、GraphQL と並ぶプレゼンテーション層のエンドポイントとして機能し、高性能なバイナリ通信と双方向ストリーミングにより、リアルタイム性が求められるシステム間連携に適しています。
|
|
2013
|
+
|
|
2014
|
+
### 技術スタック
|
|
2015
|
+
|
|
2016
|
+
| カテゴリ | 技術 |
|
|
2017
|
+
|---------|------|
|
|
2018
|
+
| **言語** | Java 21 |
|
|
2019
|
+
| **フレームワーク** | Spring Boot 4.0, grpc-spring-boot-starter 3.1 |
|
|
2020
|
+
| **プロトコル** | gRPC 1.62, Protocol Buffers 3.25 |
|
|
2021
|
+
| **ORM** | MyBatis 3.0 |
|
|
2022
|
+
| **データベース** | PostgreSQL 16 |
|
|
2023
|
+
| **テスト** | JUnit 5, grpc-testing, TestContainers |
|
|
2024
|
+
|
|
2025
|
+
### API 形式の比較と選択基準
|
|
2026
|
+
|
|
2027
|
+
| 観点 | REST API | gRPC | GraphQL |
|
|
2028
|
+
|------|----------|------|---------|
|
|
2029
|
+
| **用途** | 汎用 Web API | マイクロサービス間 | フロントエンド向け |
|
|
2030
|
+
| **パフォーマンス** | 中 | 高 | 中 |
|
|
2031
|
+
| **ストリーミング** | WebSocket 別実装 | ネイティブ | Subscription |
|
|
2032
|
+
| **スキーマ** | OpenAPI (任意) | .proto (必須) | .graphqls (必須) |
|
|
2033
|
+
| **ブラウザ対応** | ◎ | △ (gRPC-Web) | ◎ |
|
|
2034
|
+
| **学習コスト** | 低 | 中 | 中 |
|
|
2035
|
+
|
|
2036
|
+
### gRPC を選択する場面
|
|
2037
|
+
|
|
2038
|
+
1. **マイクロサービス間通信**: 高性能な内部通信が必要
|
|
2039
|
+
2. **リアルタイム処理**: ストリーミングによる進捗通知が必要
|
|
2040
|
+
3. **多言語環境**: 異なる言語で実装されたサービス間の連携
|
|
2041
|
+
4. **低レイテンシ**: バイナリプロトコルによる高速通信が必要
|
|
2042
|
+
5. **双方向通信**: 対話的な処理フローが必要
|
|
2043
|
+
|
|
2044
|
+
gRPC サービスは、既存のヘキサゴナルアーキテクチャを活かしながら、Input Adapter として追加することで、REST API と並行運用が可能です。用途に応じて適切な API 形式を選択し、システム全体の効率と柔軟性を高めることができます。
|