1.spring cloud gateway 搭建
需要特别注意的两点:
spring cloud gateway是 基于SpringBoot2.x、SpringWebFlux和ProjectReactor构建。
Spring Cloud Gateway需要Spring Boot和Spring Webflux提供的Netty运行时。它在传统的Servlet容器中或作为WAR构建时不起作用。
![](https://i-blog.csdnimg.cn/blog_migrate/77345cbf2f4c0b0b505dc921d290638c.png)
1.1 gateway前置知识
a. gateway网关的工作流程如下图:
![](https://i-blog.csdnimg.cn/blog_migrate/f6bbde56c51690a5bcbcc5388a35ad81.png)
客户端向SpringCloudGateway发出请求。如果网关处理程序映射确定请求与路由匹配,则将其发送到网关Web处理程序。此处理程序通过特定于请求的筛选器链运行请求。过滤器被虚线分隔的原因是,过滤器可以在发送代理请求之前和之后运行逻辑。执行所有“预”过滤器逻辑。然后发出代理请求。发出代理请求后,运行“后”过滤器逻辑。
1.2 pom依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
</dependencies>
1.3 yaml 配置
server:
port: 17001
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://localhost:9001/gateway
predicates:
- Path=/gateway/**
- Method=POST
- id: proxy_route
uri: http://localhost:7001
predicates:
- Path=/test/**
1.4 测试
![](https://i-blog.csdnimg.cn/blog_migrate/963403af30603d6183d71353d2d92bf4.png)
2. 集成nacos实现动态配置
2.1 下载nacos,启动部署,参考官网
![](https://i-blog.csdnimg.cn/blog_migrate/0e3c1dbaf63d4694c66df718e5f76e85.png)
![](https://i-blog.csdnimg.cn/blog_migrate/218d26622154497967d036737ac34fe6.jpeg)
关于配置文件的必要知识点
![](https://i-blog.csdnimg.cn/blog_migrate/97504d4678b7cb158a28be9abff6ae12.png)
2.2 pom依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
2.3 新建bootstrap 文件(格式可以是properties、yaml)
spring:
application:
# 应用的名称建议设置,因为涉及到nacos配置文件的命名
name: api-gateway
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
profiles:
active: dev
![](https://i-blog.csdnimg.cn/blog_migrate/c9e267c2e0dd103996431adb88f8b749.png)
2.4 在服务启动类上增加 配置自动刷新注解
![](https://i-blog.csdnimg.cn/blog_migrate/48d73832ee571ac4b66d6a32079843cf.png)
3. 集成sentinel 实现限流熔断
3.1 sentinel介绍;主要实现服务的限流、熔断功能
工作流程详解,自行打开官网了解
3.2 pom依赖
<!-- 引入sentinel进行服务降级熔断 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<!-- gateway网关整合sentinel进行限流降级 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
3.3 yaml配置
spring:
cloud:
sentinel:
eager: true
transport:
port: 8719
dashboard: localhost:8700
scg:
fallback:
mode: response
response-status: 426
response-body: '{"code":1001,"msg":"请求过于频繁,请稍后重试","data":""}'
3.4 注入异常处理类ExceptionHandler和过滤器Filter
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;
import java.util.Collections;
import java.util.List;
@Configuration
public class GatewayConfiguration {
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,
ServerCodecConfigurer serverCodecConfigurer) {
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
// Register the block exception handler for Spring Cloud Gateway.
return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
@Bean(name = {"customer-sentinelGatewayFilter"})
@Order(-1)
public GlobalFilter sentinelGatewayFilter() {
return new SentinelGatewayFilter();
}
}
若低版本未成功,可尝试在启动时增加参数: -Dcsp.sentinel.app.type=1
3.5 下载启动sentinel控制台dashboard
此例子,仅适合学习测试阶段,因为在dashboard设置的所有规则皆存在内存中,并未进行持久化。
用于生产环境,必须实现规则的持久化,持久化请看官网资料
![](https://i-blog.csdnimg.cn/blog_migrate/9af49ef0ab356d63a8edfb71b78ffdd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cafb56210e5e628be6ab82d32aab7a2b.png)
3.6 限流测试
![](https://i-blog.csdnimg.cn/blog_migrate/bd220c402f3522b17216247ae2d57e17.png)
![](https://i-blog.csdnimg.cn/blog_migrate/90ee80424588a93870fc4af19795e332.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4f4c69f7525583e33c030d3adf5ca172.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2acf6674212ec2901c6f3dffa857fba8.png)
![](https://i-blog.csdnimg.cn/blog_migrate/be07ef20dc1d95d10cc8af5d2421e815.png)
3.7 熔断测试
![](https://i-blog.csdnimg.cn/blog_migrate/7a8cbaa8421c1834702db177014bd14e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/92d604272b81b3114d67602646098f95.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ceed5f80b1c6d8e0ec84713e6daa7f51.png)
![](https://i-blog.csdnimg.cn/blog_migrate/37f66ffac22ba90c2d3732e3307a73b3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/65b91b32e4376137353da9dae8fe5ba0.png)
通过jmeter触发一定并发数量的慢请求后,再发起请求即可触发
![](https://i-blog.csdnimg.cn/blog_migrate/accb5921a410e644c1e69873882ec5a0.png)
以上笔记,仅适合入门学习,不可直接在生产环境使用。
需进行sentinel的规则持久化以及相关权限限制、鉴权等,方可应用至生产环境。