Skip to content

Instantly share code, notes, and snippets.

@fragaLY
Last active July 2, 2021 11:53
Show Gist options
  • Save fragaLY/5d6424012b81c4b3088dcb2cd973a2e6 to your computer and use it in GitHub Desktop.
Save fragaLY/5d6424012b81c4b3088dcb2cd973a2e6 to your computer and use it in GitHub Desktop.
Spring Boot Gateway Setup [ for 'prod' profile - config server in use ]
server:
port: 8082
spring:
application:
name: gateway-service
main:
banner-mode: off
zipkin:
kafka:
topic: zipkin
sender:
type: KAFKA
kafka:
bootstrap-servers: http://localhost:9092, http://localhost:9093, http://localhost:9094
listener:
missing-topics-fatal: false
thymeleaf:
enabled: false
cloud:
config:
enabled: false
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins:
- "http://localhost"
allowedMethods:
- POST
- PUT
- OPTIONS
routes:
- id: notification-service
uri: http://localhost:8083
predicates:
- Path=/api/notifications/**
- Header=X-CSRF-TOKEN, [0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}
- Host=**localhost**
filters:
- name: NotificationPreFilter
- name: NotificationPostFilter
resilience4j.timelimiter:
configs:
default:
timeoutDuration: 1s
cancelRunningFuture: false
instances:
notification-service:
timeoutDuration: 250ms
logging:
level:
org.springframework: info
org.springframework.cloud: info
com.api.gateway.demo: info
---
spring:
main:
banner-mode: off
cloud:
config:
enabled: true
config:
activate:
on-profile: prod
import: configserver:http://config-server-prod:8088?fail-fast=true&max-attempts=3&max-interval=1500&multiplier=1.2&initial-interval=1100
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.util.parseSpaceSeparatedArgs
plugins {
application
id("org.springframework.boot") version "2.5.2"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
kotlin("jvm") version "1.5.20"
kotlin("plugin.spring") version "1.5.20"
id("com.google.cloud.tools.jib") version "3.1.1"
}
springBoot {
buildInfo()
}
group = "by.vk"
version = "0.0.1"
java.sourceCompatibility = JavaVersion.VERSION_11
application {
mainClass.set("com.api.gateway.demo.DemoApplicationKt")
applicationName = "kafka-producer"
}
repositories {
mavenCentral()
}
extra["springCloudVersion"] = "2020.0.3"
configurations.all {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-starter-gateway")
implementation("org.springframework.cloud:spring-cloud-config-client")
implementation("org.springframework.cloud:spring-cloud-starter-sleuth")
implementation("org.springframework.cloud:spring-cloud-sleuth-zipkin")
implementation("org.springframework.cloud:spring-cloud-stream-binder-kafka")
implementation("org.springframework.cloud:spring-cloud-starter-circuitbreaker-reactor-resilience4j")
implementation("io.github.resilience4j:resilience4j-spring-boot2:1.7.0")
testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation("ch.qos.logback.contrib:logback-json-classic:0.1.5")
implementation("ch.qos.logback.contrib:logback-jackson:0.1.5")
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
object DockerProps {
const val BASE_IMAGE = "gcr.io/distroless/java:11"
const val APP_PORT = "8082"
const val DEBUG_PORT = "5082"
const val JMX_PORT = "38082"
}
object JVMProps {
const val XMX = "256m"
const val XMS = "64m"
const val MAX_METASPACE_SIZE = "64m"
const val MAX_DIRECT_MEMORY_SIZE = "128m"
const val HEAPDUMP_PATH = "/opt/tmp/heapdump.bin"
}
jib {
from {
image = DockerProps.BASE_IMAGE
}
container {
jvmFlags = parseSpaceSeparatedArgs("-noverify -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -XX:InitialRAMPercentage=50.0 -XX:+OptimizeStringConcat -XX:+UseStringDeduplication -XX:+ExitOnOutOfMemoryError -XX:+AlwaysActAsServerClassMachine -Xmx${JVMProps.XMX} -Xms${JVMProps.XMS} -XX:MaxMetaspaceSize=${JVMProps.MAX_METASPACE_SIZE} -XX:MaxDirectMemorySize=${JVMProps.MAX_DIRECT_MEMORY_SIZE} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${JVMProps.HEAPDUMP_PATH} -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=${DockerProps.JMX_PORT} -Dcom.sun.management.jmxremote.rmi.port=${DockerProps.JMX_PORT} -Dspring.profiles.active=prod")
ports = listOf(DockerProps.APP_PORT, DockerProps.DEBUG_PORT, DockerProps.JMX_PORT)
labels.set(mapOf("maintainer" to "Vadzim Kavalkou <[email protected]>",
"app-name" to application.applicationName,
"service-version" to version.toString()))
creationTime = "USE_CURRENT_TIMESTAMP"
}
}
@Component
class NotificationPostFilter : AbstractGatewayFilterFactory<NotificationPostFilter.Config>(Config::class.java) {
private val logger = LoggerFactory.getLogger(javaClass)
class Config
override fun apply(config: Config?): GatewayFilter {
return GatewayFilter { exchange: ServerWebExchange?, chain: GatewayFilterChain ->
chain.filter(exchange)
.then(Mono.fromRunnable {
logger.info("[GATEWAY] Post filter")
when (exchange?.response?.statusCode) {
HttpStatus.NO_CONTENT -> exchange.response.headers["post-filter"] =
mutableListOf(UUID.randomUUID().toString())
else -> logger.warn("[GATEWAY] Something went wrong. Response status is [${exchange?.response?.statusCode}]")
}
})
}
}
}
@Component
class NotificationPreFilter : AbstractGatewayFilterFactory<NotificationPreFilter.Config>(Config::class.java) {
private val logger = LoggerFactory.getLogger(javaClass)
class Config
override fun apply(config: Config?) =
GatewayFilter { exchange: ServerWebExchange?, chain: GatewayFilterChain ->
chain.filter(exchange).then(Mono.fromRunnable { logger.info("[GATEWAY] Pre filter") })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment