1. Gateway简介
Gateway网关是微服务架构中不可或缺的组件,是微服务架构中的统一入口,它作为所有客户端请求的第一道防线,负责请求的路由、过滤和聚合。
Gateway核心功能
路由(Routing)
- 根据请求路径、Header、参数等将请求路由到不同微服务
过滤(Filtering)
前置过滤器:认证、鉴权、请求改写
后置过滤器:响应改写、添加Header
负载均衡
熔断降级
集成Hystrix或Resilience4j实现熔断机制
限流
- 基于Redis等实现分布式限流
2. 搭建网关服务
2.1 创建网关模块,导依赖
创建模块
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
pom导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud-demo</artifactId>
<groupId>com.itgaohe</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway-service</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!--网关-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos服务发现依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--openfeign依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--负载均衡依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
yml配置文件
server:
port: 10010 # 网关端口
spring:
application:
name: gatewayservice # 服务名称
cloud:
nacos:
server-addr: localhost:8848 # nacos地址
discovery:
username: nacos
password: nacos
gateway:
# 。。。
globalcors: # 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]':
allowedOrigins: # 允许哪些网站的跨域请求
- "http://127.0.0.1:8849"
allowedMethods: # 允许的跨域ajax的请求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期
routes: # 网关路由配置
- id: user-service # 路由id,自定义,只要唯一即可
# uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址
uri: lb://userservice # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/user/** # 这个是按照路径匹配,只要以/user/开头就符合要求
- id: order-service # 路由id,自定义,只要唯一即可
# uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址
uri: lb://orderservice # 路由的目标地址 lb就是负载均衡,后面跟服务名称
predicates: # 路由断言,也就是判断请求是否符合路由规则的条件
- Path=/order/** # 这个是按照路径匹配,只要以/user/开头就符合要求
filters: #路由过滤器
- AddRequestHeader=tou,itgaohe # 添加请求头 : 格式 k,v
default-filters: # 默认过滤项
- AddRequestHeader=tou2,itgaohe22 # 添加请求头
Gateway网关搭建好之后,需要在服务生产者(order-service)设置拦截器,在网关服务的配置文件中配置全局拦截器,携带请求头,通过网关进行请求的话携带请求头,拦截器放行,如果请求不是从网关过来的,则不会携带强求头,拦截器会进行请求拦截
2.2 拦截器
定义网关拦截器
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
/**
* 网关拦截器,用于校验请求是否通过网关访问。
*/
@Component
public class GateInterceptor implements HandlerInterceptor {
/**
* 在处理请求之前进行拦截操作。
*
* @param request HTTP请求对象
* @param response HTTP响应对象
* @param handler 请求处理器
* @return 如果校验通过返回true,否则返回false
* @throws Exception 异常信息
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 设置响应内容类型和字符编码
response.setContentType("html/text;charset=utf8");
// 从请求头中获取"tou"字段的值
String tou = request.getHeader("tou");
// 校验"tou"字段是否为"itgaohe"
if ("itgaohe".equals(tou)) {
// 校验通过,继续后续处理
return true;
} else {
// 校验未通过,设置错误状态码并返回提示信息
response.setStatus(502);
response.getWriter().write("没有通过网关访问");
return false;
}
}
}
在核心配置中进行拦截器配置
import com.itgaohe.order.interceptor.GateInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private GateInterceptor gateInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(gateInterceptor)
.addPathPatterns("/**");
}
}
3.跨域问题
跨域问题的核心表现
当以下任意一项不同时,就会触发跨域限制:
协议不同(http vs https)
域名不同(a.com vs b.com)
端口不同(8080 vs 8081)
Gateway解决跨域问题采用的是CORS方案,只需要在yml配置文件中进行配置就行
# 。。。
globalcors: # 全局的跨域处理
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]':
allowedOrigins: # 允许哪些网站的跨域请求
- "http://127.0.0.1:8849"
allowedMethods: # 允许的跨域ajax的请求方式
- "GET"
- "POST"
- "DELETE"
- "PUT"
- "OPTIONS"
allowedHeaders: "*" # 允许在请求中携带的头信息
allowCredentials: true # 是否允许携带cookie
maxAge: 360000 # 这次跨域检测的有效期