• 学习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
    
  • 相关阅读:
    简易sql拼接工具类(使用StringBuilder实现)
    缓存工具类(使用ConcurrentMap集合实现)
    properties文档读取工具类
    【Codeforces Round #655 (Div. 2)】A-D
    【2020 杭电多校第四场】Go Running 最小点覆盖
    【2020 杭电多校第四场】1002 Blow up the Enemy
    【2020 HDU 多校训练第三场 1007 Tokitsukaze and Rescue】暴力删边&最短路
    【2020杭电多校第二场】Total Eclipse 思维+并查集
    【2020HDU多校】Lead of Wisdom 暴力
    【CF-1371 C-E2】
  • 原文地址:https://www.cnblogs.com/Ywfgu/p/14142553.html
Copyright © 2020-2023  润新知