SpringCloud(十二)——路由网关组件:Gateway
Gateway组件简介
Gateway提供了一个在springmvc之上构建API网关的库。springcloudgateway旨在提供一种简单而有效的方法来路由到api,并为api提供横切关注点,比如:安全性、监控/度量和弹性。
1.特性
- 基于springboot2.x 和 spring webFlux 和 Reactor 构建 响应式异步非阻塞IO模型
- 动态路由
- 请求过滤
网关 gateway = 断言predicate + 过滤(后置filler)
- 断言:当请求到达网关时,网关前置处理
- 过滤:当请求满足断言的所有条件之后,会向后端服务转发,在向后端服务转发之前会经过一些过滤
Gateway组件使用
1、新建一个子模块springcloud-Gateway-7979
2、导入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--引入Gateway网关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
3、编写主启动类
package com.study.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication_7979 {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication_7979.class,args);
}
}
4、编写application.yaml配置文件
server:
port: 7979
spring:
application:
name: springcloud-Gateway-7979
#Gateway配置
cloud:
gateway:
routes:
- id: dept8001 # 指定路由唯一标识
uri: http://localhost:8001/ # 指定路由服务的地址
predicates:
# 指定路由规则,及访问当前端口localhost:7979/dept/get/的时候会转发到localhost:8001/dept/get/下所有路径
- Path=/dept/get/**
- id: dept8002 # 指定路由唯一标识
uri: http://localhost:8002/ # 指定路由服务的地址
predicates:
# 指定路由规则,及访问当前端口localhost:7979/dept/list的时候会转发到localhost:8002/dept/list
- Path=/dept/list
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: Gateway7979.com
prefer-ip-address: true
5、启动测试
1、启动eureka注册中心集群springcloud-eureka-7001
2、启动服务提供者springcloud-provider-dept-8001和springcloud-provider-dept-8002
3、启动Gateway路由网关springcloud-Gateway-7979
配置路由服务负载均衡
1、编写application.yaml
路由predicate(断言,验证)
网关 gateway = 断言predicate + 过滤(后置filler)
- 断言:当请求到达网关时,网关前置处理
- 过滤:当请求满足断言的所有条件之后,会向后端服务转发,在向后端服务转发之前会经过一些过滤
路由Filter以及自定义filter
一、使用内置的Filter
二、自定义全局Filter
自定义全局filter:所有请求都要经过全局filter之后再转发到后端服务
新建一个全局配置类实现GlobalFilter, Ordered接口
package com.study.springcloud.filters;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* 自定义网关全局filter
*/
@Configuration
public class CustomerGlobalFilter implements GlobalFilter, Ordered {
//类似javaweb doFilter
//exchange : 交换 request response 封装了 request response
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//httprequest对象
ServerHttpRequest request = exchange.getRequest();
//httpresponse对象
ServerHttpResponse response = exchange.getResponse();
System.out.println("经过全局Filter处理.......");
Mono<Void> filter = chain.filter(exchange);//放心filter继续向后执行
System.out.println("响应回来Filter处理......");
return filter;
}
//order 排序 int数字:当又多个filter的时候,用来指定filter执行顺序 默认顺序按照自然数字进行排序 , -1 在所有filter执行之前执行
@Override
public int getOrder() {
return -1;
}
}
通过网关提供web路径查看路由详细规则
1、applicaiton.yaml添加以下配置
management:
endpoints:
web:
exposure:
include: "*"
2、访问http://localhost:7979/actuator/gateway/routes 路径