学习springcloud之gateway

是什么
怎么用
为什么用

1.什么springcloud gateway

功能特点

Built on Spring Framework 5, Project Reactor and Spring Boot 2.0 基于spring5,和springboot2.0框架
Able to match routes on any request attribute. 使用请求参数来匹配路由规则
Predicates and filters are specific to routes. 在路由规则上的断言和过滤器
Circuit Breaker integration. 集成了断路器?熔断器?
Spring Cloud DiscoveryClient integration 集成了Spring Cloud DiscoveryClient
Easy to write Predicates and Filters 更方便编写的断言和过滤器
Request Rate Limiting 请求发送速率限制
Path Rewriting 请求路径的转发

2.怎么用

maven配置

<?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.8.RELEASE</version>
<!--		<relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

<!--	<dependencyManagement>-->
<!--		<dependencies>-->
<!--			<dependency>-->
<!--				<groupId>org.springframework.cloud</groupId>-->
<!--				<artifactId>spring-cloud-dependencies</artifactId>-->
<!--				<version>Finchley.RELEASE</version>-->
<!--				<type>pom</type>-->
<!--				<scope>import</scope>-->
<!--			</dependency>-->
<!--		</dependencies>-->
<!--	</dependencyManagement>-->

	<dependencies>
<!--		<dependency>-->
<!--			<groupId>org.springframework.boot</groupId>-->
<!--			<artifactId>spring-boot-starter-actuator</artifactId>-->
<!--		</dependency>-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-gateway</artifactId>
			<version>2.2.6.RELEASE</version>
		</dependency>
<!--		<dependency>-->
<!--			<groupId>org.springframework.cloud</groupId>-->
<!--			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!--		</dependency>-->
<!--		<dependency>-->
<!--			<groupId>org.springframework.cloud</groupId>-->
<!--			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>-->
<!--		</dependency>-->
<!--		<dependency>-->
<!--			<groupId>org.springframework.boot</groupId>-->
<!--			<artifactId>spring-boot-starter-webflux</artifactId>-->
<!--&lt;!&ndash;			<version>2.2.8.RELEASE</version>&ndash;&gt;-->
<!--		</dependency>-->



		<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>knife4j-spring-boot-starter</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>3.0.0</version>
		</dependency>
<!--&lt;!&ndash;		<dependency>&ndash;&gt;-->
<!--&lt;!&ndash;			<groupId>io.springfox</groupId>&ndash;&gt;-->
<!--&lt;!&ndash;			<artifactId>springfox-swagger2</artifactId>&ndash;&gt;-->
<!--&lt;!&ndash;			<version>2.9.2</version>&ndash;&gt;-->
<!--&lt;!&ndash;		</dependency>&ndash;&gt;-->


<!--		<dependency>-->
<!--			<groupId>org.springframework.boot</groupId>-->
<!--			<artifactId>spring-boot-starter-test</artifactId>-->
<!--			<scope>test</scope>-->
<!--		</dependency>-->
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
<!--			<plugin>-->
<!--				<groupId>org.apache.maven.plugins</groupId>-->
<!--				<artifactId>maven-surefire-plugin</artifactId>-->
<!--				<version>2.22.2</version>-->
<!--				<configuration>-->
<!--					<skipTests>true</skipTests>-->
<!--				</configuration>-->
<!--			</plugin>-->
		</plugins>
	</build>

</project>

application.yaml配置内容

参考地址: https://cloud.spring.io/spring-cloud-gateway/reference/html/#gateway-request-predicates-factories

spring:      
  cloud:
    gateway:
      routes:
#        - id: header_route
#          uri: http://localhost:8888
#          predicates:
#            - Header=X-Request-Id, d+
#        - id: after_route
#          uri: http://localhost:8888
#          predicates:
#            - After=2020-12-17T14:46:47.789-07:00[Asia/Shanghai]
        - id: path_route
          uri: http://localhost:8888
          predicates:
            - Path=/get1
原文地址:https://www.cnblogs.com/Ywfgu/p/14142553.html