Geteway
-
Route: The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates, and a collection of filters. A route is matched if the aggregate predicate is true.
-
Predicate: This is a Java 8 Function Predicate. The input type is a Spring Framework
ServerWebExchange
. This lets you match on anything from the HTTP request, such as headers or parameters. -
Filter: These are instances of Spring Framework
GatewayFilter
that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request. -
路线:网关的基本构建块。它由ID,目标URI,谓词集合和过滤器集合定义。如果聚合谓词为true,则匹配路由。
-
谓词:这是Java 8函数谓词。输入类型是Spring Framework
ServerWebExchange
。这使您可以匹配HTTP请求中的所有内容,例如标头或参数。 -
Filter:这些是使用特定工厂构造的Spring Framework
GatewayFilter
实例。在这里,您可以在发送下游请求之前或之后修改请求和响应
Predicate 断言是条件判断,判断路由到什么地方
Filter:过滤
即使请求到达网关,网关利用Predicate断言判断是否符合规则,符合路由到某个位子,途径多个filter
- Netty started on port(s): 88
网络是用netty,效率高
Geteway使用
导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
着之前需要导入 nacos的服务注册发现和配置中心
启动项加上注册发现
@EnableDiscoveryClient
bootstrap.properties 加载配置中心的文件 因为没有别的就默认defalut不设置group分组
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=0cdb6a02-0a01-4659-bab3-7d71372fe688
spring.application.name=gulimall-gateway
application.properties 加载注册中心的文件
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-gateway
server.port=88
application.yml创建 gateway的请求转发地址和断言的判断
spring:
cloud:
gateway:
routes:
- id: baidu_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq
启动项目访问成功