3三、Spring Cloud網關Gateway

Spring Cloud Gateway是由spring官方基於Spring5.0、Spring Boot2.x、Project Reactor等技術開發的網關,目的是代替原先版本中的Spring Cloud Netfilx Zuul,目前Netfilx已經開源了Zuul2.0,但Spring 沒有考慮集成,而是推出了本身開發的Spring Cloud GateWay。該項目提供了一個構建在Spring Ecosystem之上的API網關,旨在提供一種簡單而有效的途徑來發送API,並向他們提供交叉關注點,例如:安全性,監控、埋點,限流等。(具體能夠查看官網http://spring.io/projects/spring-cloud-gateway)
Spring Cloud Gateway 工做原理圖:
3三、Spring Cloud網關Gateway
一、新建項目sc-gateway,對應的pom.xml文件以下spring

<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-cloud</groupId>
    <artifactId>sc-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sc-gateway</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

    </dependencies>
</project>

能夠看到spring cloud gateway是從spring cloud 2.x後纔有的apache

3三、Spring Cloud網關Gateway
二、新建springboot啓動類安全

package sc.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) {
                SpringApplication.run(GatewayApplication.class, args);
        }

}

三、編寫配置文件application.ymlspringboot

server:
    port: 8600

spring:
    application:
        name: sc-gateway
    cloud:
        gateway:
            routes:
            - id: baidu
                uri: http://www.baidu.com/
                predicates:
                    - Path=/baidu/**
            - id: jianshu
                uri: http://www.jianshu.com/
                predicates:
                    - Path=/jianshu/**

備註:gateway的配置項參考org.springframework.cloud.gateway.config.GatewayProperties類app

Spring Cloud Gateway提供了兩種配置路由規則的方式:
方式1、經過@Bean自定義RouteLocatormaven

@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                            //basic proxy
                            .route("baidu", r -> r.path("/baidu ")
                            .uri("http://www.baidu.com.cn/")
                            ).build(); 
}

方式2、經過屬於文件或者yml文件配置ide

spring:
    cloud:
        gateway:
            routes:
            - id: baidu
                uri: http://www.baidu.com /
                predicates:
                - Path=/ baidu

四、啓動項目,並驗證
3三、Spring Cloud網關Gateway
訪問http://127.0.0.1:8600/jianshu轉發到https://www.jianshu.com/spring-boot

訪問http://127.0.0.1:8600/baidu轉發到https://www.baidu.com/ui

訪問http://127.0.0.1:8600/sina轉發到https://www.sina.com.cn/url

相關文章
相關標籤/搜索