ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

Springy-Store-Microservices API网关精讲:Spring Cloud Gateway路由、负载均衡与聚合健康检查的6个关键实践

Springy-Store-Microservices API网关精讲:Spring Cloud Gateway路由、负载均衡与聚合健康检查的6个关键实践 Springy-Store-Microservices API网关精讲Spring Cloud Gateway路由、负载均衡与聚合健康检查的6个关键实践【免费下载链接】Springy-Store-MicroservicesSpringy Store is a conceptual simple μServices-based project using the latest cutting-edge technologies, to demonstrate how the Store services are created to be a cloud-native and 12-factor app agnostic. Those μServices are developed based on Spring Boot Cloud framework that implements cloud-native intuitive, design patterns, and best practices.项目地址: https://gitcode.com/gh_mirrors/sp/Springy-Store-MicroservicesSpringy Store 是一个基于 Spring Boot 与 Spring Cloud 的微服务商店示例项目它的 edge-server 模块用Spring Cloud Gateway实现了完整的API 网关按路径路由、Eureka 客户端负载均衡、JWT 安全过滤以及把所有核心微服务汇聚成一个端点的聚合健康检查。这篇指南带你逐个拆解这套网关是怎么工作的。上图是 Springy Store 的微服务全景图所有外部流量HTTPS 8443 端口都必须先进入 Edge Server 网关再由网关分发给 store、product、review、recommendation 等下游服务。网关因此是整个系统的唯一大门。一、网关在系统中的位置一条请求的完整旅程当你调用https://localhost:8443/store/api/v1/products/1时请求经历了这样一条链路客户端通过HTTPS访问网关 8443 端口网关使用 PKCS12 证书开启 SSL网关的 Spring Security 过滤器校验 JWT 令牌Spring Cloud Gateway 匹配路由规则把请求转发到 Eureka 中名为store的服务实例store 服务再调用 product、review、recommendation组装成聚合商品视图返回。网关的入口类非常简洁就在 EdgeServer.java它做了一件关键的事声明了一个带LoadBalanced注解的WebClient.Builder这正是后面客户端负载均衡的基础。二、Spring Cloud Gateway 路由读懂 gateway.yml 的 9 条路由网关的路由规则全部集中存放在 gateway.yml 中由 config-server 集中下发见 config-server 源码。每条路由由三部分组成id唯一标识、uri目标地址、predicates匹配条件。项目里最有代表性的几类路由如下2.1 业务路由lb:// 前缀触发负载均衡store-products路径/store/**→lb://storeoauth-server路径/oauth/**→lb://auth-server这里的lb://是重点它告诉网关不要写死 IP 和端口而是到注册中心查询服务名从实例列表中挑一个。这正是 Spring Cloud 2020 系版本用 Ribbon 风格客户端负载均衡的写法配置中spring.cloud.loadbalancer.ribbon.enabled: false表示切换到了新的BlockingLoadBalancerClient实现。2.2 基础设施路由SetPath 过滤器改写路径网关还代理了 Eureka 和 Config Server 的管理接口用SetPath/RewritePath过滤器把外部路径映射为内部真实路径路由 id外部路径内部真实路径使用的过滤器eureka-api/eureka/api/{segment}/eureka/{segment}SetPatheureka-web-start/eureka/web/SetPathconfig-server/config/**去掉/config前缀RewritePath正则这意味着你可以只暴露一个 8443 端口就能安全地访问 Eureka 管理页面/eureka/web和配置中心而不是让每个基础设施组件各自暴露端口。2.3 教学路由用 Host 头做路由演练配置末尾还有三条host_route_200 / 418 / 501路由通过Host断言i.feel.lucky:8080、im.a.teapot:8080把/headerrouting/**打到不同的状态码方便你亲手验证断言 过滤器的组合玩法——这也是 Spring Cloud Gateway 路由、负载均衡与聚合健康检查知识点的最佳实验场。三、客户端负载均衡如何工作当网关决定转发lb://store时内部流程是Eureka 客户端定时5 秒一次租约续约拉取注册表中store的所有健康实例负载均衡器在实例列表中挑选目标多实例部署时轮询/随机请求被转发到被选中的实例。项目提供了三个 docker-compose 编排文件来观察这一行为docker-compose.ymlRabbitMQ 单实例、docker-compose-partitions.yml每个服务 2 个实例 RabbitMQ 分区和 docker-compose-kafka.ymlKafka 分区。多实例模式下多次刷新/store相关接口就能亲眼看到请求在两个实例间被轮转分发。如果本地想跑起来仓库地址为git clone https://gitcode.com/gh_mirrors/sp/Springy-Store-Microservices然后执行docker-compose -p ssm up -d即可。启动后访问 Zipkinhttp://localhost:9411/zipkin/可以看到网关转发、store 聚合调用 product/review/recommendation 的完整调用链——这正是分布式链路追踪配合网关工作的真实画面。四、聚合健康检查一个端点看住全部微服务网关最巧妙的设计之一是 GatewayConfiguration.java 中注册的Core Microservices健康贡献者它为 product、recommendation、review、store、auth-server五个核心服务各建了一个ReactiveHealthIndicator每个指示器通过 WebClient 请求对应服务的/actuator/health成功则返回UP异常则返回DOWN五个指示器被CompositeReactiveHealthContributor组合成一个整体。效果是只有当所有核心微服务和依赖项都健康时网关自身的/actuator/health才返回 UP。运维上只需盯住一个端口curl -k https://localhost:8443/actuator/health -s | jq .components.Core Microservices全部就绪时会得到类似这样的响应{ status: UP, components: { Authorization Server: { status: UP }, Product Service: { status: UP }, Recommendation Service:{ status: UP }, Review Service: { status: UP }, Store Service: { status: UP } } }自动化测试脚本 test-em-all.sh 正是先轮询这个端点确认整体UP之后才开始黑盒测试。五、网关安全OAuth2 资源服务器配置SecurityConfig.java 把网关本身也配置成了一个OAuth2 资源服务器关闭 CSRF启用 WebFlux 响应式安全/headerrouting/**、/actuator/**、/eureka/**、/oauth/**白名单放行其余所有交换业务 API必须携带合法 JWTJWT 的公钥从认证服务获取http://auth-server/.well-known/jwks.json见 gateway.yml 中的jwk-set-uri。也就是说认证在授权服务器、鉴权在网关、校验在服务端的三层安全模型在项目中完整落地。六、总结从这份网关实现你能学到什么实践点本项目对应位置路径路由 lb://负载均衡config/repo/gateway.ymlSetPath/RewritePath过滤器改写同上eureka、config 路由LoadBalancedWebClientEdgeServer.java聚合健康检查GatewayConfiguration.javaJWT 资源服务器安全SecurityConfig.java容器化部署多阶段构建 非 root 用户DockerfileSpringy Store 用一个几百行的网关模块演示了云原生 API 网关应具备的核心能力统一入口、路由规则、服务发现式负载均衡、安全过滤与聚合可观测性。把 edge-server 模块 通读一遍你会对 Spring Cloud Gateway 路由、负载均衡与聚合健康检查这套经典组合形成完整的肌肉记忆 【免费下载链接】Springy-Store-MicroservicesSpringy Store is a conceptual simple μServices-based project using the latest cutting-edge technologies, to demonstrate how the Store services are created to be a cloud-native and 12-factor app agnostic. Those μServices are developed based on Spring Boot Cloud framework that implements cloud-native intuitive, design patterns, and best practices.项目地址: https://gitcode.com/gh_mirrors/sp/Springy-Store-Microservices创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表