spring,

Code with Kubernetes Native Spring Boot

Cui Cui Follow Sep 16, 2021 · 2 mins read
Code with Kubernetes Native Spring Boot
Share this

“I am interested in making the simple profound, always searching for that universal moment in the world around us. I draw inspiration for my landscape paintings from many places, it comes from the trees and fields near my home in rural Pennsylvania along the Delaware River as well as my frequent visits over the years to New England.” - Referenced from here

Kubernetes Native Spring Boot Project

Customer component

  • Java

  • Spring Reactive Web
    • @ResponseBody at class level
    • Flux return data type
  • Lombok
    • @Data
    • @RequiredArgsConstructor
  • Spring Data R2DBC
    • ReactiveCrudRepository
  • H2 Database
    • schema.sql
    • data.sql
  • Spring Boot Actuator
    • management.endpoints.web.exposure.include=*
    • management.endpoints.health.probes.enabled=true
    • management.endpoints.health.show-details=always
    • info.message=Hello World
    • git-commit-id-plugin
    • /mectrics Micrometer, Premethus
    • Customize HealthIndicator status message
    • server.shutdown=graceful
    • spring.lifecycle.timeout-per-shutdown-phase=30s
  • Spring Native
    • buildpacks.io
    • paketo.io
    • kpack
    • spring-boot:build-image -Dspring-boot.build-images.imageName=gcr.io/myrepo/myapp:0.0.1 —> then docker push
    • mvn -DskipTests -Pnative clean package
  • Spring feature
    • spring.config.activate.on-cloud-platform=kubernetes properties applying on specific platform only
    • @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) methods running on specific platform only
    • VM option: spring.main.cloud-platform=kubernets telling which platform running
    • spring.config.import=configtree:${HOME}/.evn/config

Order component

  • Kotlin
    • primary constructor. class Order (val id: Int, val customerId: Int)
    • data class. data class Order
    • init block. init {}
    • IntRange. (0..8).forEach {}
    • random Int. (Math.random() * 100).toInt()
    • transforming data. (0..8).map{}, (0..8).flatMap { }
    • ConcurrentHashMap
  • RSocket
    • @MessageMapping(“orders.{customerId}”)
    • @DestinationVariable
    • Return Flux.fromStream()
    • rsc cli, like curl or httpie. rsc tcp://localhost:8181 --route orders.2 --stream

Gateway component

  • Kotlin
    • infix functions. infix fun calling() -> one calling another
    • flatMap vs map. stream of stream vs stream of value
  • Spring Cloud Gateway
  • RSocket
    • RSocket Client. @Bean fun rsocket(rr: RSocketRequester.Builder) = rr.tcp("localhost", 8080)
    • GET Flux. fun orders(customerId : Int): Flux<Order> = this.rsocket.route("orders.{customerId}", customerId).retrieveFlux<Order>()
  • Spring Reactive Web
    • WebClient. @Bean fun http(wc: WebClient.Builder) = wc.build()
    • GET Flux. fun customers(): Flux<Customer> = this.http.get().uri("http://localhost:8080/customers").retrieve().bodyToFlux<Customer>().retry(10).onErrorResume({ Flux.empty<Customer>() }).doOnError {}
    • Flux vs Mono. It’s more like zeroMany vs zeroOne, or list response vs one response.
    • Mono.zip(). Return type is Tuple.
        fun customerOrders() = this.customers()
            .flatMap {
                Mono.zip(
                    Mono.just(it),
                    this.orders(it.id).collectList()
                )
            }
            .map { CustomerOrders(it.t1, it.t2) }
      
  • GraphQL

    ```

Reference

Join Newsletter
Get the latest news right in your inbox. We never spam!
Cui
Written by Cui Follow
Hi, I am Cui, the colourful coder for colourful.codes!