[ Spring ] Spring Cloud Gateway 2025 Comprehensive Overview

文章目录

          • [Spring Gateway Architecture](#Spring Gateway Architecture)
          • [Project Level Dependency](#Project Level Dependency)
          • [Service Center](#Service Center)
          • [Service Provider](#Service Provider)
          • [Gateway Service](#Gateway Service)
          • [Launch All Service](#Launch All Service)
Spring Gateway Architecture
  • Service Center : register and find service provider
  • Service Provider : programs that provide actual service
  • Gateway Service : dispatch client requests to service providers

data flow : request => gateway => service center => service provider

Project Level Dependency
kotlin 复制代码
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode = RepositoriesMode.PREFER_SETTINGS
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
}

plugins {
    id("org.jetbrains.kotlin.jvm") version "2.0.21" apply false
    id("org.jetbrains.kotlin.kapt") version "2.0.21" apply false
    id("org.jetbrains.kotlin.plugin.spring") version "2.0.21" apply false
    id("org.springframework.boot") version "3.4.1" apply false
}

include("eureka-server")
include("spring-gateway-service")
include("spring-gateway-provider")
Service Center
kotlin 复制代码
plugins {
    id("org.jetbrains.kotlin.jvm")
    id("org.jetbrains.kotlin.kapt")
    id("org.jetbrains.kotlin.plugin.spring")
    id("org.springframework.boot")
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

dependencies {
    // commons
    api("io.github.hellogoogle2000:kotlin-commons:1.0.19")
    // kotlin
    api("org.jetbrains.kotlin:kotlin-reflect:2.0.21")
    // spring
    api("org.springframework.boot:spring-boot-starter:3.4.1")
    api("org.springframework.boot:spring-boot-starter-web:3.4.1")
    api("org.springframework.boot:spring-boot-devtools:3.4.1")
    // eureka
    api("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server:4.2.0")
}
properties 复制代码
# service
server.port=10001
spring.application.name=eureka-server
# eureka
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:10001/eureka/
kotlin 复制代码
package x.spring.hello

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer

@EnableEurekaServer
@SpringBootApplication
class EurekaServerApplication

fun main(args: Array<String>) {
    runApplication<EurekaServerApplication>(*args)
}
Service Provider
kotlin 复制代码
plugins {
    id("org.jetbrains.kotlin.jvm")
    id("org.jetbrains.kotlin.kapt")
    id("org.jetbrains.kotlin.plugin.spring")
    id("org.springframework.boot")
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

dependencies {
    // commons
    api("io.github.hellogoogle2000:kotlin-commons:1.0.19")
    // kotlin
    api("org.jetbrains.kotlin:kotlin-reflect:2.0.21")
    // spring
    api("org.springframework.boot:spring-boot-starter:3.4.1")
    api("org.springframework.boot:spring-boot-starter-web:3.4.1")
    // eureka
    api("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:4.2.0")
}
properties 复制代码
# service
server.port=10003
spring.application.name=gateway-provider
# eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:10001/eureka/
kotlin 复制代码
package x.spring.hello

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class GatewayProviderApplication

fun main(args: Array<String>) {
    runApplication<GatewayProviderApplication>(*args)
}
kotlin 复制代码
package x.spring.hello.controller

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

@RestController
class ConfigController {

    @GetMapping("/config/{key}")
    fun index(@PathVariable("key") key: String): String {
        return "config service provider on 10003 key=$key"
    }
}
Gateway Service
kotlin 复制代码
plugins {
    id("org.jetbrains.kotlin.jvm")
    id("org.jetbrains.kotlin.kapt")
    id("org.jetbrains.kotlin.plugin.spring")
    id("org.springframework.boot")
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

dependencies {
    val springBootVersion = "3.4.1"
    val springCloudVersion = "4.2.0"
    // commons
    api("io.github.hellogoogle2000:kotlin-commons:1.0.19")
    // kotlin
    api("org.jetbrains.kotlin:kotlin-reflect:2.0.21")
    // spring
    api("org.springframework.boot:spring-boot-starter:$springBootVersion")
    api("org.springframework.boot:spring-boot-devtools:$springBootVersion")
    api("org.springframework.cloud:spring-cloud-starter-bootstrap:$springCloudVersion")
    // spring cloud gateway
    api("org.springframework.boot:spring-boot-starter-webflux:$springBootVersion")
    api("org.springframework.cloud:spring-cloud-starter-gateway:$springCloudVersion")
    api("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:$springCloudVersion")
    api("javax.servlet:javax.servlet-api:4.0.1")
}
properties 复制代码
# service
server.port=10002
spring.application.name=gateway-service
# eureka
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:10001/eureka/
# gateway
spring.cloud.gateway.routes[0].id=config
spring.cloud.gateway.routes[0].uri=lb://gateway-provider
spring.cloud.gateway.routes[0].predicates[0]=Path=/config/**
# http://localhost:10002/config/name => http://localhost:10003/config/name
kotlin 复制代码
package x.spring.hello

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
class GatewayServiceApplication

fun main(args: Array<String>) {
    runApplication<GatewayServiceApplication>(*args)
}
kotlin 复制代码
package x.spring.hello.component

import org.springframework.cloud.gateway.route.RouteLocator
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder
import org.springframework.cloud.gateway.route.builder.filters
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class GatewayRule {

    @Bean
    fun rule(builder: RouteLocatorBuilder): RouteLocator {
        val routes = builder.routes()
        // http://localhost:10002/github/byteflys => https://github.com/byteflys
        routes.route("csdn") { route ->
            route.filters {
                rewritePath("/github/(?<variable>.*)", "/\${variable}")
            }
            route.path("/github/**").uri("https://github.com/")
        }
        return routes.build()
    }
}
Launch All Service
properties 复制代码
http://localhost:10001
http://localhost:10003/config/name
http://localhost:10002/config/name => http://localhost:10003/config/name
http://localhost:10002/github/byteflys => https://github.com/byteflys
相关推荐
我是苏苏6 分钟前
C#高级:常用的扩展方法大全
java·windows·c#
customer089 分钟前
【开源免费】基于SpringBoot+Vue.JS贸易行业crm系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源
_GR1 小时前
Java程序基础⑪Java的异常体系和使用
java·开发语言
CPU NULL1 小时前
新版IDEA创建数据库表
java·数据库·spring boot·sql·学习·mysql·intellij-idea
极客先躯1 小时前
高级java每日一道面试题-2025年01月22日-JVM篇-乐观锁和悲观锁的理解及如何实现,有哪些实现方式?
java·jvm·优化性能·选择合适的锁策略·结合实际案例·乐观锁的实现方式
秋月的私语1 小时前
c#启动程序时使用异步读取输出避免假死
java·前端·c#
花心蝴蝶.2 小时前
Spring IoC & DI
java·后端·spring
Kerwin要坚持日更2 小时前
一文讲解CMS收集器的垃圾收集过程
java·开发语言·jvm
我想学LINUX2 小时前
【2024年华为OD机试】 (C卷,200分)- 机器人走迷宫(JavaScript&Java & Python&C/C++)
java·c语言·javascript·python·华为od·机器人
闲暇部落3 小时前
kotlin内联函数——runCatching
android·开发语言·kotlin