图1
图2
图1
图2
网关 = 路由转发 + 过滤器(编写额外功能)
简单说明一下三个术语:
URI就是一个地址
Predicate过滤器
Filter过滤器
为了保证 Gateway 的高可用性,可以同时启动多个 Gateway 实例进行负载,在 Gateway 的上游使用 Nginx 或者 F5 进行负载转发以达到高可用。
spring:
application:
name: demo-one
@Controller
public class DemoController {
@RequestMapping("/one")
@ResponseBody
public String demo(){
System.out.println("执行one控制器");
return "one";
}
@RequestMapping("/demo/one")
@ResponseBody
public String demo2(){
System.out.println("执行demo-one控制器");
return "demo-one";
}
}
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
org.springframework.cloud
spring-cloud-dependencies
Hoxton.SR3
pom
import
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
server:
port: 9000
spring:
application:
name: gateway-demo
cloud:
gateway:
discovery:
locator:
enabled: true # 开启当前项目服务注册与发现功能
lower-case-service-id: true # 把服务名转换为小写,Eureka中默认都是大写
routes:
- id: demo #自定义唯一标识,只要不重复即可
uri: lb://EUREKA-CLIENT # lb:loadBalance EUREKA-CLIENT 代理项目的名
predicates: Path=/demo/** # 路径规则
filters: StripPrefix=1 # 转发后忽略第一层
新建启动器:
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
第四步:访问GatewayDemo项目。
访问测试:http://localhost:9000/demo/one
页面会显示:
通过结果可以看出,用户虽然访问的GatewayDemo网关项目,但是最后真是访问的是DemoOne项目中/one 控制器
(1)集成nacos前:
(2)集成Nacos:现在在配置文件中写死了转发路径的地址, 前面我们已经分析过地址写死带来的问题, 接下来我们从注册中心获取此地址
(3)测试
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: url-proxy-1 #id:我们自定义的路由 ID,保持唯一
uri: https://blog.csdn.net #uri:目标服务地址
predicates: #predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
- Path=/csdn
#上面这段配置的意思是,配置了一个 id 为 url-proxy-1的URI代理规则,路由的规则为:
#当访问地址http://localhost:8080/csdn/1.jsp时,会路由到上游地址https://blog.csdn.net/1.jsp。
#下面示例中 uri指向了我的CSDN博客,在实际项目使用中可以将 uri 指向对外提供服务的项目地址,统一对外输出接口。
package com.springcloud.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
/**
同理:
下面这段配置的意思是,配置了一个 id 为 url-proxy-1的URI代理规则,路由的规则为:
当访问地址http://localhost:8080/csdn/1.jsp时,会路由到上游地址https://blog.csdn.net/1.jsp。
下面示例中 uri指向了我的CSDN博客,在实际项目使用中可以将 uri 指向对外提供服务的项目地址,统一对外输出接口。
*/
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/csdn")
.uri("https://blog.csdn.net"))
.build();
}
}
在uri的schema协议部分为自定义的lb:类型,表示从微服务注册中心(如zoolkepper、Eureka、nacos、阿波罗)订阅服务,并且进行服务的路由。
注册中心相结合的路由配置方式,与单个URI的路由配置,区别其实很小,仅仅在于URI的schema协议不同。单个URI的地址的schema协议,一般为http或者https协议
server:
port: 8084
spring:
cloud:
gateway:
routes:
- id: seckill-provider-route
uri: lb://seckill-provider
predicates:
- Path=/seckill-provider/**
- id: message-provider-route
uri: lb://message-provider
predicates:
- Path=/message-provider/**
application:
name: cloud-gateway
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8888/eureka/
Spring Cloud Gateway 的功能很强大,我们仅仅通过 Predicates 的设计就可以看出来,前面我们只是使用了 predicates 进行了简单的条件匹配,其实 Spring Cloud Gataway 帮我们内置了很多 Predicates 功能。
Spring Cloud Gateway 是通过 Spring WebFlux 的 HandlerMapping 做为底层支持来匹配到转发路由,Spring Cloud Gateway 内置了很多 Predicates 工厂,这些 Predicates 工厂通过不同的 HTTP 请求参数来匹配,多个 Predicates 工厂可以组合使用。
1)Predicates谓词:当满足条件在进行路由转发。
2)过滤器规则(Filter)
Filter作用:在路由转发到代理服务之前和代理服务返回结果之后额外做的事情。
Filter执行了说明谓词条件通过了。
Spring Cloud Gateway中Filter分类:
内置Filter,都是GatewayFilter实现类
自定义GlobalFilter
自定义FilterFactory【sxt】:可以定义针对于Router的Filter。
请求超时、异常。请求失败堆积于网关。需要快速失败并返回给客户端。不然系统崩溃。
顾名思义,限流就是限制流量,就像你宽带包有 1 个 G 的流量,用完了就没了。通过限流,我们可以很好地控制系统的 QPS,从而达到保护系统的目的。
比如 Web 服务、对外 API,这种类型的服务有以下几种可能导致机器被拖垮:
现在的请求通过经过gateWay网关时,需要在网关统一配置跨域请求,需求:所有请求通过,有如下配置:
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowed-origins: "*"
allowed-headers: "*"
allow-credentials: true
allowed-methods:
- GET
- POST
- DELETE
- PUT
- OPTION
SpringCloud gateway (史上最全) - 疯狂创客圈 - 博客园
全网最全讲解 Spring Cloud Gateway,认真看完这一篇就够了!
一篇就看懂:SpringCloud网关 - GateWay详解
SpringCloud Gateway获取body参数(一篇就够了)
Spring Cloud Gateway解决跨域问题【理解跨域】