1.1 微服务⽹关简介
微服务⽹关能够实现,路由、负载均衡等多种功能。类似Nginx,反向代理的功能。在微服务架构 中,后端服务往往不直接开放给调⽤端,⽽是通过⼀个API⽹关根据请求的URL,路由到相应的服务。 当添加API⽹关后,在第三⽅调⽤端和服务提供⽅之间就创建了⼀⾯墙,在API⽹关中进⾏权限控制,同 时API⽹关将请求以负载均衡的⽅式发送给后端服务。微服务⽹关架构
1.2Spring Cloud Gateway简介
SpringCloud Gateway 是 Spring Cloud 的⼀个全新项⽬,该项⽬是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的⽹关,它旨在为微服务架构提供⼀种简单有效的统⼀的 API 路由管理⽅式。
SpringCloud Gateway 作为 Spring Cloud ⽣态系统中的⽹关,⽬标是替代 Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新⾼性能版本进⾏集成,仍然还是使⽤的Zuul 2.0之前的⾮Reactor模式的⽼版本。⽽为了提升⽹关的性能,SpringCloud Gateway是基于WebFlux框 架实现的,⽽WebFlux框架底层则使⽤了⾼性能的Reactor模式通信框架Netty。
Spring Cloud Gateway 的⽬标,不仅提供统⼀的路由⽅式,并且基于 Filter 链的⽅式提供了⽹关 基本的功能,例如:安全,监控/指标,和限流。
注意:Spring Cloud Gateway 底层使⽤了⾼性能的通信框架Netty。
2.2 特征
基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
集成 Spring Cloud DiscoveryClient
Predicates 和 Filters 作⽤于特定路由,易于编写的 Predicates 和 Filters
具备⼀些⽹关的⾼级功能:动态路由、限流、路径重写
集成Spring Cloud DiscoveryClient
集成熔断器CircuitBreaker
1.3入门案例
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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kkb</groupId>
<artifactId>05_cloud_gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>05_cloud_gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application,yml
server:
port: 8085 //端口
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: service1
uri: https://blog.csdn.net //地址
predicates:
- Path=/csdn //前缀
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8084/eureka
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class GateWayApplication {
public static void main(String[] args) {
SpringApplication.run(GateWayApplication.class, args);
}
}
效果图