1.访问官网学习gateway 找到稳定版

Spring Cloud Gateway

2、官网介绍

2.1.5.RELEASE

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. 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.

Glossary

  • Route(规则): Route the basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates and a collection of filters. A route is matched if aggregate predicate is true.

  • Predicate(断言): This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This allows developers to match on anything from the HTTP request, such as headers or parameters.

  • Filter: (过滤器)These are instances Spring Framework GatewayFilter constructed in with a specific factory. Here, requests and responses can be modified before or after sending the downstream request.

特点

Spring Cloud Gateway features:

  • 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.

  • 断言和过滤器在特定的规则上

  • Hystrix Circuit Breaker integration.

  • 整合熔断器

  • Spring Cloud DiscoveryClient integration

  • 与Spring Cloud DiscoveryClient集成

  • Easy to write Predicates and Filters

  • 简单编写的断言和过滤器

  • Request Rate Limiting

  • 访问控制

  • Path Rewriting

  • 路径重写

3.创建一个新maven的项目,来作为整个项目的网关 gulimall-gateway

4.修改pom文件

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <artifactId>gulimall-gateway</artifactId>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
​
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
</project>

5.开启服务注册发现

package com.lcl.gilimail.gateway;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
​
​
//开启服务的注册和发现功能
@EnableDiscoveryClient
@SpringBootApplication
public class GuliMailGateWayApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(GuliMailGateWayApplication.class,args);
    }
} 

6.修改application.properties 配置nacos的地址

spring.application.name=gulimall-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
server.port=88

7.启动nacos,在nacos中添加一个网关工程的namespace

8. 修改bootstrap.properties配置中心中的地址

spring.cloud.nacos.config.server-addr=localhost:8848
spring.cloud.nacos.config.namespace=d3495d51-d78f-4d76-8821-5c6a3b427510

9.在nacos配置中心中创建配置文件

10.(示例代码)在application.yml中新建路由规则

spring:
  cloud:
    gateway:
      routes:
      - id: test_route
        uri: https://www.baidu.com
        predicates:
        - Query=url,baidu
​
      - id: qq_routes
        uri: https://www.qq.com
        predicates:
        - Query=url,qq

11.启动测试

localhost:88?url=qq

网关统一配置

3.1在网关项目中创建网关配置类CorsConfiguration

package com.lcl.gilimall.gateway.config;
​
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
​
@Configuration
public class GuliMallCorsConfiguration {
​
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
​
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //1.配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        //cookie信息
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}

3.2删除renren-fast中的跨域配置