当前位置: 首页 > news >正文

[ Spring ] Spring Cloud Gateway 2025 Comprehensive Overview

文章目录

          • Spring Gateway Architecture
          • Project Level Dependency
          • Service Center
          • Service Provider
          • Gateway 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
pluginManagement {repositories {gradlePluginPortal()google()mavenCentral()}
}dependencyResolutionManagement {repositoriesMode = RepositoriesMode.PREFER_SETTINGSrepositories {gradlePluginPortal()google()mavenCentral()}
}buildscript {repositories {gradlePluginPortal()google()mavenCentral()}
}plugins {id("org.jetbrains.kotlin.jvm") version "2.0.21" apply falseid("org.jetbrains.kotlin.kapt") version "2.0.21" apply falseid("org.jetbrains.kotlin.plugin.spring") version "2.0.21" apply falseid("org.springframework.boot") version "3.4.1" apply false
}include("eureka-server")
include("spring-gateway-service")
include("spring-gateway-provider")
Service Center
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 {// commonsapi("io.github.hellogoogle2000:kotlin-commons:1.0.19")// kotlinapi("org.jetbrains.kotlin:kotlin-reflect:2.0.21")// springapi("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")// eurekaapi("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server:4.2.0")
}
# 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/
package x.spring.helloimport org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer@EnableEurekaServer
@SpringBootApplication
class EurekaServerApplicationfun main(args: Array<String>) {runApplication<EurekaServerApplication>(*args)
}
Service Provider
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 {// commonsapi("io.github.hellogoogle2000:kotlin-commons:1.0.19")// kotlinapi("org.jetbrains.kotlin:kotlin-reflect:2.0.21")// springapi("org.springframework.boot:spring-boot-starter:3.4.1")api("org.springframework.boot:spring-boot-starter-web:3.4.1")// eurekaapi("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:4.2.0")
}
# 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/
package x.spring.helloimport org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication@SpringBootApplication
class GatewayProviderApplicationfun main(args: Array<String>) {runApplication<GatewayProviderApplication>(*args)
}
package x.spring.hello.controllerimport 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
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"// commonsapi("io.github.hellogoogle2000:kotlin-commons:1.0.19")// kotlinapi("org.jetbrains.kotlin:kotlin-reflect:2.0.21")// springapi("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 gatewayapi("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")
}
# 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
package x.spring.helloimport org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication@SpringBootApplication
class GatewayServiceApplicationfun main(args: Array<String>) {runApplication<GatewayServiceApplication>(*args)
}
package x.spring.hello.componentimport 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 {@Beanfun rule(builder: RouteLocatorBuilder): RouteLocator {val routes = builder.routes()// http://localhost:10002/github/byteflys => https://github.com/byteflysroutes.route("csdn") { route ->route.filters {rewritePath("/github/(?<variable>.*)", "/\${variable}")}route.path("/github/**").uri("https://github.com/")}return routes.build()}
}
Launch All Service
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
http://www.lryc.cn/news/526263.html

相关文章:

  • 【项目初始化】自定义异常处理
  • Windows10安装MySQL找不到MSVCR120.dll和MSVCP120.dll问题解决
  • 【嵌入式】总结——Linux驱动开发(三)
  • 计算机图形学:实验三 光照与阴影
  • 「 机器人 」扑翼飞行器混合控制策略缺点浅谈
  • 蓝桥杯算法日常|c\c++常用竞赛函数总结备用
  • 每日十题八股-2025年1月24日
  • tomcat的accept-count、max-connections、max-threads三个参数的含义
  • 【无标题】mysql python 连接
  • linux naive代理设置
  • [STM32 - 野火] - - - 固件库学习笔记 - - -十一.电源管理系统
  • DBO优化最近邻分类预测matlab
  • 【深入理解FFMPEG】命令行阅读笔记
  • 图形化数据报文转换映射工具
  • 智能体0门槛开发
  • ssh密钥登录GitHub时一直提示“Error: Permission denied (publickey)”
  • mapbox加载geojson,鼠标移入改变颜色,设置样式以及vue中的使用
  • 考研机试题:打印日期
  • OpenHarmonyOS 3.2 编译生成的hap和app文件的名称如何配置追加版本号?
  • 【openwrt】openwrt odhcpd配置介绍
  • 基于神经网络的视频编码NNVC(1):帧内预测
  • Android开发,待办事项提醒App的设计与实现
  • 豆瓣Top250电影的数据采集与可视化分析(scrapy+mysql+matplotlib)
  • MySQL索引——让查询飞起来
  • Springboot集成Elasticsearch8.0(ES)版本,采用JAVA Client方式进行连接和实现CRUD操作
  • 【Linux】APT 密钥管理迁移指南:有效解决 apt-key 弃用警告
  • 洛谷P1143 进制转换
  • 99.12 金融难点通俗解释:毛利率
  • HUMANITY’S LAST EXAM (HLE) 综述:人工智能领域的“最终考试”
  • C++从入门到实战(二)C++命名空间