Skip to content

Introduction

Interactive Diagram

새 탭 ↗

Describes how to develop RESTful APIs for building Cloud Native Applications.
This document covers RESTful API development using platforms (Spring Boot, Node.js, Python) as well as how to run them on Kubernetes.

cf) https://docs.microsoft.com/ko-kr/dotnet/architecture/cloud-native/definition cf) https://github.com/cncf/foundation/blob/master/charter.md

FactorDescription
1. CodebaseA single codebase for each microservice, stored in its own repository. It is tracked using version control and can be deployed to multiple environments (Dev, Staging, Production).
2. DependenciesEach service isolates and packages its own dependencies, embracing changes without affecting the entire system.
3. ConfigConfiguration information is applied by the service and managed through configuration management tools external to the code. The same deployment artifact can be propagated/applied per environment (Dev/Staging/Prod) simply by modifying the configuration.
4. Backing ServicesAlso called Backend Services or Backing Services, auxiliary resources (data stores, caches, message brokers) should be exposed via addressable URLs. This lets resources be decoupled from the application and swapped with each other.
5. Build, Release, RunEach release must enforce strict separation across the build, release, and run stages. Each tag must be assigned a unique ID and support rollback. Modern CI/CD systems can help satisfy this principle.
6. ProcessesEach microservice should run in its own process, isolated from other running services. It must be able to operate without storing state for continuous business processing (Stateless).
7. Port BindingEach service must expose its interfaces and functionality by exposing a port.
8. ConcurrencyIt must be possible to scale out according to the Application Process Model.
9. DisposabilityService instances must be disposable, favoring fast startup to increase scaling opportunities and shutting down gracefully to leave the system in a correct state.
10. Dev/Prod parityKeep environments (Dev/Prod) as similar as possible throughout the application lifecycle to minimize cost.
11. LoggingTreat logs generated by the microservice as event streams. In other words, logs should be analyzable.
12. Admin ProcessesRun admin/management tasks as processes. Tasks may include data cleanup and analysis for reports. The tools that run these tasks should be invoked in production but invoked separately from the application.
  • One codebase under version control, deployed many times
  • There is always a one-to-one relationship between the codebase and the app
  • An application’s dependencies must be declared explicitly
  • All dependent libraries must be downloadable from a library repository using a dependency management tool such as Apache Maven or Gradle
    dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('org.springframework.boot:spring-boot-starter-actuator')
    implementation('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    implementation('io.micrometer:micrometer-registry-prometheus')
    compileOnly('org.projectlombok:lombok')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    }
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
    </dependency>
  • Configuration information is stored in the runtime environment
  • Configuration is strictly separated from application code
  • Configuration is everything that can vary between deployments (staging, production, development, etc.), such as DB info, external service credentials, host names, etc.
  • Store configuration in environment variables (env)
PlantUML Diagram

application-prod.properties

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.url=jdbc:mariadb://localhost:3306/employees
spring.datasource.username=root
spring.datasource.password=${DB_PASSWORD}
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver

application-dev.properties

spring.datasource.url=jdbc:h2:mem:employees
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
  • A backing service is treated as a resource attached on demand
  • Backing services include databases, API-based RESTful web services, SMTP servers, FTP servers, etc.
  • Backing services are regarded as resources of the application
  • You should be able to swap the embedded SQL used in the test environment for MySQL in the staging environment
  • Thoroughly separated build and run stages
  • The codebase is transformed into a (non-development) deploy through three stages
    • Build stage : takes the source code, compiles it, and produces a single package
    • Release stage : combines the build with configuration information
    • The release version is fully ready to operate in the runtime environment
    • Identifiers such as semantic versioning are assigned; these versions are used to roll back
  • Run stage : usually called the runtime : selects one of the release versions and runs the application on top of the runtime environment
  • Each process must not interfere with the others. When one terminates, there must be no physical impact on other apps
    (In a WAS, the JVM is shared per Application Context, so interference occurs)
  • Resources used must be usable independently.
    • App-1 : 2 Instances (2core, 4GB Memory)
    • App-2 : 4 Instances (1core, 1GB Memory)
  • A service binds to a port and exposes itself externally
  • It is fully self-contained, including its own web server, so there is no need to add a separate web server to the runtime environment
  • Scale horizontally through the process model
  • The application must be able to scale out processes or threads horizontally and run them in parallel as needed
  • Long-running data processing tasks should be assigned to a thread pool and executed via a thread executor
  • For example, HTTP requests should be handled by servlet threads, while time-consuming tasks should be handled by worker threads
  • Maximize robustness with fast startup and graceful shutdown
  • The application can be stopped at any time during process execution, and when stopped it can perform all tasks that must be handled and then shut down cleanly
  • It should start up in as short a time as possible
  • Keep the development, staging, and production environments as similar as possible
  • Parity that keeps development and production environments as identical as possible should prevent divergence
  • Three differences to keep in mind
    • Time gap : developers must deploy changes to production quickly
    • Personnel gap : the developer who makes the code change should also be able to deploy to production and monitor it
    • Tools gap : the technologies and frameworks used in each runtime environment should be configured identically
  • Treat logs as event streams
  • Write logs to stdout
  • The application should not be involved in storing log files
  • Log aggregation and storage should be handled by the runtime environment, not the application
  • Run admin/maintenance tasks as one-off processes
  • Run them in the same environment as the running processes
  • Admin code must be deployed together with the application code