SpringCloud系列之网关(Gateway)应用篇

简介:

SpringCloud系列之网关(Gateway)应用篇

@

目录
前言
项目版本
网关访问
鉴权配置
限流配置
前言
由于项目采用了微服务架构,业务功能都在相应各自的模块中,每个业务模块都是以独立的项目运行着,对外提供各自的服务接口,如没有类似网关之类组件的话,相应的鉴权,限流等功能实现起来不能够进行统一的配置和管理,有了网关后一切都是如此的优雅。刚好新项目中采用了SpringCloud Gateway组件作为网关,就记录下项目中常用的配置吧。

项目版本
spring-boot-version:2.2.5.RELEASE
spring-cloud.version:Hoxton.SR3

网关访问
示例项目还是延续SpringCloud系列原先的示例代码,引入网关仅仅只需新增spring-cloud-gateway项目即可。
核心pom.xml(详细信息查看示例源码,在文章末尾)


org.springframework.cloud

<artifactId>spring-cloud-starter-gateway</artifactId>


bootstrap.yml

server:
port: 9005
spring:
application:

name: springcloud-gateway-service

cloud:

config:
  discovery:
    enabled: true
    service-id: config-server
  profile: dev
  label: master
gateway:
  enabled: true  #开启网关
  discovery:
    locator:
      enabled: true #开启自动路由,以服务id建立路由,服务id默认大写
      lower-case-service-id: true #服务id设置为小写

eureka:
client:

service-url:
  defaultZone: http://localhost:9003/eureka/

ApiGatewayApplication.java

@EnableDiscoveryClient
@SpringBootApplication
public class ApiGatewayApplication {

public static void main(String[] args) {
    SpringApplication.run(ApiGatewayApplication.class, args);
}

}
访问原先spring-cloud-system-server模块对外提供的接口
http://localhost:9004/web/system/getEnvName

通过网关进行访问
http://localhost:9005/system-server/web/system/getEnvName

请求能正常返回,那就说明网关组件已集成进来了,是不是很简单呢,一行配置项就搞定了,便于展现这边采用properties配置方式说明

spring.cloud.gateway.discovery.locator.enabled=true
到此网关的基础配置应用已完成,通过网关访问的请求路径格式如下
http://网关地址:网关端口/各自服务id/各自服务对外提供的URL访问

鉴权配置
这边将spring-cloud-system-server模块引入spring security安全认证组件,上代码。
pom.xml

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>


application.properties

spring.security.user.name=test
spring.security.user.password=123456
服务模块调整完后,重新启动该模块,访问对外请求接口,出现认证登录界面说明配置成功。
http://localhost:9004/web/system/getEnvName

输入上述配置项中配置的用户名和密码后,接口请求返回正常。

请求网关地址,访问服务接口按下回车键时会跳转至服务项目认证页面,如下
http://localhost:9005/system-server/web/system/getEnvName

接下来对网关模块进行相应调整
bootstrap.yml

spring:
application:

name: springcloud-gateway-service

security:

user:
  name: test
  password: 123456

新增安全认证过滤类
SecurityBasicAuthorizationFilter.java

@Component
public class SecurityBasicAuthorizationFilter implements GlobalFilter, Ordered {

@Value("${spring.security.user.name}")
private String username;
@Value("${spring.security.user.password}")
private String password;

public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    String auth = username.concat(":").concat(password);
    String encodedAuth = new sun.misc.BASE64Encoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
    String authHeader = "Basic " +encodedAuth;
    //headers中增加授权信息
    ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate().header("Authorization", authHeader).build();
    ServerWebExchange build = exchange.mutate().request(serverHttpRequest).build();
    return chain.filter(build);
}
/**
 * 优先级
 * 数字越大优先级越低
 * @return
 */
public int getOrder() {
    return -1;
}

}
重启网关项目,重新访问服务地址,返回正常数据。这边说明下在测试时最好新开一个无痕窗口或者清理浏览器缓存后再进行测试,不然因会话缓存会导致安全认证没有生效的假象。
http://localhost:9005/system-server/web/system/getEnvName

限流配置
SpringCloud Gateway自带限流功能,但是基于redis,这边简单演示下,项目中没有使用而是使用了阿里开源的sentinel,后续将介绍下集成sentinel组件。
pom.xml

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>


bootstrap.yml

spring:
cloud:

gateway:
  enabled: true  #开启网关
  discovery:
    locator:
      enabled: true #开启自动路由,以服务id建立路由,服务id默认大写
      lower-case-service-id: true #服务id设置为小写
  routes:
    - id: baidu_route
      uri: https://www.baidu.com/
      predicates:
      - Path=/baidu/**
      filters:
        - name: RequestRateLimiter
          args:
            key-resolver: "#{@apiKeyResolver}"
            redis-rate-limiter.replenishRate: 1 #允许每秒处理多少个请求
            redis-rate-limiter.burstCapacity: 5 #允许在一秒钟内完成的最大请求数

redis:

host: 192.168.28.142
pool: 6379
password: password
database: 1              

RequestRateLimiterConfig.java

@Configuration
public class RequestRateLimiterConfig {

@Bean
@Primary
public KeyResolver apiKeyResolver() {
    //URL限流,超出限流返回429状态
    return exchange -> Mono.just(exchange.getRequest().getPath().toString());
}

}
重新启动网关项目,访问如下请求地址,会请求跳转至百度首页,目前配置项配置为1s内请求数5次,超过5次就会触发限流,返回429状态码,多次刷新就会出现如下页面
http://localhost:9005/baidu/test

通过monitor命令实时查看redis信息

本次网关项目目录结构

同系列文章
1-SpringCloud系列之配置中心(Config)使用说明
2-SpringCloud系列之服务注册发现(Eureka)应用篇
示例源码

原文地址https://www.cnblogs.com/chinaWu/p/12731796.html

相关文章
|
存储 缓存 负载均衡
Gateway 网关坑我! 被这个404 问题折腾了一年?
小富分享了一个困扰团队一年多的 SpringCloud Gateway 路由 404 问题。通过日志追踪和源码分析,发现是网关在 Nacos 配置更新后未能正确清理旧的路由权重缓存,导致负载均衡时仍使用已删除的路由数据。最终通过监听路由刷新事件并手动更新缓存,成功解决了问题。
1441 125
Gateway 网关坑我! 被这个404 问题折腾了一年?
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
机器学习/深度学习 Kubernetes API
【Azure APIM】自建网关(self-host gateway)收集请求的Header和Body内容到日志中的办法
在Azure API Management中,通过配置trace策略可完整记录API请求的Header和Body信息。在Inbound和Outbound策略中分别使用context.Request/Response.Headers和Body.As&lt;string&gt;方法捕获数据,并写入Trace日志,便于排查与审计。
394 8
|
缓存 JSON NoSQL
别再手写过滤器!SpringCloud Gateway 内置30 个,少写 80% 重复代码
小富分享Spring Cloud Gateway内置30+过滤器,涵盖请求、响应、路径、安全等场景,无需重复造轮子。通过配置实现Header处理、限流、重试、熔断等功能,提升网关开发效率,避免代码冗余。
949 1
|
人工智能 运维 安全
F5推出AI网关,赋能企业化解大模型应用风险
F5推出AI网关,赋能企业化解大模型应用风险
557 5
|
缓存 监控 Java
说一说 SpringCloud Gateway 堆外内存溢出排查
我是小假 期待与你的下一次相遇 ~
1770 5
|
安全 虚拟化
Omnissa Secure Email Gateway 2.33 - 电子邮件网关
Omnissa Secure Email Gateway 2.33 - 电子邮件网关
325 0
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
1101 0
|
监控 负载均衡 安全
微服务(五)-服务网关zuul(一)
微服务(五)-服务网关zuul(一)
|
安全 5G 网络性能优化
深入理解5G中的SAEGW:服务网关边界
【10月更文挑战第9天】
882 0