篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud-gateway 网关异常处理相关的知识,希望对你有一定的参考价值。
1.什么是服务网关
1.说明
2.为什么需要网关
- 1.网关可以实现服务的统一管理
- 2.网关可以解决微服务中通用代码的冗余问题(如权限控制,流量监控,限流等)
3.网关组件在微服务中架构
2.服务网关组件
1.zuul
Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security.
-
原文翻译
-
zuul版本说明
- 目前zuul组件已经从1.0更新到2.0,但是作为springcloud官方不再推荐使用zuul2.0,但是依然支持zuul2.
-
springcloud 官方集成zuul文档
2.gateway
This project provides a library for building an API Gateway on top of Spring MVC. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.
-
原文翻译
-
特性
- 基于springboot2.x 和 spring webFlux 和 Reactor 构建 响应式异步非阻塞IO模型
- 动态路由
- 请求过滤
创建一个新的项目,根据之前写的环境搭建方式
3.开发网关动态路由
- 网关配置有两种方式一种是快捷方式,一种是完全展开方式
1.创建项目引入网关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
完整的pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.md</groupId>
<artifactId>05-gateway8989</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>05-gateway8989</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.编写网关配置
spring:
application:
name: gateway
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
gateway:
routes:
- id: user_route
uri: http://localhost:9999/
predicates:
- Path=/user/**
- id: product_route
uri: http://localhost:9998/
predicates:
- Path=/product/**
server:
port: 8989
3.启动gateway网关项目
4.测试网关路由转发
使用之前写的写的,可以看前面几篇文章
java方式配置路由(了解)
环境还是和上面的一样
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("order_route", r -> r.path("/order/**")
.uri("http://localhost:9997/"))
.build();
}
}
4.查看网关路由规则列表
1.说明
- gateway提供路由访问规则列表的web界面,但是默认是关闭的,如果想要查看服务路由规则可以在配置文件中开启
management:
endpoints:
web:
exposure:
include: "*"
访问路由管理列表地址
3.配置路由服务负载均衡
1.说明
- 现有路由配置方式,都是基于服务地址写死的路由转发,能不能根据服务名称进行路由转发同时实现负载均衡的呢?
2.动态路由以及负载均衡转发配置
spring:
application:
name: gateway
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
gateway:
routes:
- id: user_route
uri: lb://users
predicates:
- Path=/user/**
- id: product_route
uri: lb://products
predicates:
- Path=/product/**
discovery:
locator:
enabled: true
server:
port: 8989
management:
endpoints:
web:
exposure:
include: "*"
此时访问就实现了负载均衡的效果
4.常用路由predicate(断言,验证)
1.Gateway支持多种方式的predicate
After、Before、Between用于商城活动的时候指定日期才能访问
2.使用predicate
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: product_route
uri: lb://products
predicates:
- Path=/product/**
filters:
- AddRequestParameter=id,12
- AddResponseHeader=username,chenyn
5.常用的Filter以及自定义filter
1.作用
- 当我们有很多个服务时,比如下图中的user-service、order-service、product-service等服务,客户端请求各个服务的Api时,每个服务都需要做相同的事情,比如鉴权、限流、日志输出等。
2.使用内置过滤器
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: product_route
uri: lb://products
predicates:
- Path=/product/**
filters:
- AddRequestParameter=id,12
- AddResponseHeader=username,chenyn
3.使用自定义filter
@Configuration
@Slf4j
public class CustomGlobalFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
log.info("进入自定义的filter");
if(exchange.getRequest().getQueryParams().get("username")!=null){
log.info("用户身份信息合法,放行请求继续执行!!!");
return chain.filter(exchange);
}
log.info("非法用户,拒绝访问!!!");
return exchange.getResponse().setComplete();
}
@Override
public int getOrder() {
return -1;
}
}
自定义全局filter